Thursday, August 4, 2011

Try Streams


package com.evs.objava33.class10;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStreamReader;

public class TryStream {

// BufferredInputStream = Memory Buffer
// DataINputStream = Primitive Data Types Read / Write
// CheckedInputStreamjj = Checksum maintenance
// CiperInputSteam = ENcypt- decyrtion
// DigestInputStream = One way encryption
// InflatorINputStream = Compression (91-97%), Image (4-9%), Video (1-3%).
// LineNUmberINputStream = Read Data and keep track of line#
// ProgressMonitorInputStream = Keep track of progress
// PushBackInputStream = Send it to designated resource

public static void main(String[] args) {
// BufferedInputStream in = new BufferedInputStream(System.in);
// try {
// byte[] b = new byte[100];
// int readBytes = in.read(b);
// String str = new String(b, 0, readBytes);
// System.out.println(str);
// } catch (IOException e) {
// e.printStackTrace();
// }

BufferedReader reader = new BufferedReader(new InputStreamReader(
System.in));
try {
char[] b = new char[100];
int readBytes = reader.read(b);
String str = new String(b, 0, readBytes);
System.out.println(str);
} catch (IOException e) {
e.printStackTrace();
}

}
}

MyExceptionClass


package com.evs.objava33.class9;

public class MyExceptionClass extends Exception {

public MyExceptionClass() {
// TODO Auto-generated constructor stub
}

public MyExceptionClass(String message) {
super(message);
// TODO Auto-generated constructor stub
}

public MyExceptionClass(Throwable cause) {
super(cause);
// TODO Auto-generated constructor stub
}

public MyExceptionClass(String message, Throwable cause) {
super(message, cause);
// TODO Auto-generated constructor stub
}

}

Test Exception


package com.evs.objava33.class9;

public class TestException {

private static int[] intArray = { 1, 2, 3, 4, 5, 6 };

public static void main(String[] args) {
try {
// Catchable Code
// System.out.println(3 / 0);
System.out.println(getValue(3, 0.0));
System.out.println(getArrayValue(7));
} catch (Exception e) {
System.out.println("Exception occur: Please contact ");
e.printStackTrace();
} finally {
// Clean up
}
}

public static int getValue(final Integer i, final Double j)
throws Exception {
return (int) (i / j);
}

private static int getArrayValue(int i) throws MyExceptionClass {
try {
return intArray[i];
} catch (Exception e) {
e.printStackTrace();
throw new MyExceptionClass(e);
}
}

}

Wednesday, July 27, 2011

Assignment - Week 4

Create an object oriented hierarchy & solution to this analysis problem of three entities

- TV Remote
- VCD Remote
- Satellite Remote

Functionalities,
- Volume / Mute
- Program / Channels / Tuning
- Play / Stop / Resume / Pause / Record

Required (Minimum two from each point) concepts in solution.
- Inheritance / Abstraction / Polymorphism
- Interface / Abstract / Inner Class / Anonymous Class

Objective: Solution is not important but design of this solution is much more important in this. A simple diagram or code skeleton would be good enough as a solution to this assignment.

Second Interface


package com.evs.objava33.class8;

public interface SecondInterface {
public long secndTime();
}

First Interface


package com.evs.objava33.class8;

public interface FirstInterface extends SecondInterface {
public long time();
public long secndTime();
}

Palm Tree


package com.evs.objava33.class7;

import com.evs.objava33.class8.FirstInterface;

public class PalmTree extends Tree {
private int age;
private String breed;

private PalmTree(int age) {
super("DateTree");
this.age = age;
}

public PalmTree(int age, String breed) {
this(age);
this.breed = breed;
}

public String toString() {
return "PalmTree [age=" + age + ", breed=" + breed + "]";
}

public static void main(String[] args) {
PalmTree tree1 = new PalmTree(10, "Small");
DateTree tree2 = new DateTree(40, "tall");

// System.out.println(tree1);
// System.out.println(tree2);

Tree tree3 = new PalmTree(30, "Short");
System.out.println(tree3.getType());
Tree t5 = new PalmTree(23, "Dummy");
PalmTree t4 = (PalmTree) tree3;
// if (t5 instanceof PalmTree) {
if (t5.getClass() == PalmTree.class) {
t4 = (PalmTree) t5;
}

Tree[] trees = { tree1, tree2, tree3, t4, t5 };
FirstInterface[] ints = { tree1, tree2 };

for (FirstInterface t : ints) {
System.out.println(t.secndTime());
}

FirstInterface in = new FirstInterface() {
public long time() {
return 0;
}

public long secndTime() {
return 0;
}
};
System.out.println(in);
InnerFirstInterface ifi = tree1.new InnerFirstInterface();
System.out.println(ifi);
AnotherClass ac = new AnotherClass();

// Scope = Public, Private, Protected, Default
// Access = Default, Static

// Class Variable Method Constructor
// Public Y Y Y Y
// Private Y Y Y Y
// Protected Y Y Y Y
// Default Y Y Y Y

}

private interface AnotherInterface {
public void testing();
}

static class AnotherClass {
public int testing;
}

private class InnerFirstInterface implements FirstInterface {
public long time() {
return 0;
}

public long secndTime() {
return 0;
}
}

public long time() {
return System.currentTimeMillis();
}

public long secndTime() {
return time();
}
}