Showing posts with label Object Oriented. Show all posts
Showing posts with label Object Oriented. Show all posts

Wednesday, July 27, 2011

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();
}
}

Date Tree


package com.evs.objava33.class7;


public class DateTree extends Tree {

private int age;
private String breed;

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

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

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

public long time() {
return 10;
}

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

Tree


package com.evs.objava33.class7;

import com.evs.objava33.class8.FirstInterface;

public abstract class Tree implements FirstInterface {
String type;

public Tree() {
type = "Unknown";
}

public Tree(String type) {
this.type = type;
}

public String getType() {
return type;
}

public void setType(String type) {
this.type = type;
}

public String toString() {
return "Tree [type=" + type + "]";
}

public abstract long secndTime();
}

Monday, July 11, 2011

Rectangle

package com.evs.objava33.class6;

public class Rectangle {

Line width, height;

public Rectangle(final Point start1, final Point start2, final Point end1,
final Point end2) {
width = new Line(start1, end1);
height = new Line(start2, end2);
}

public Rectangle(final Line width, final Line height) {
this.width = width;
this.height = height;
}

public Rectangle(final Rectangle rec) {
this.width = rec.width;
this.height = rec.height;
}

public double area() {
return width.length() * height.length();
}

}

Point

package com.evs.objava33.class6;

public class Point {

double x;
double y;

Point(double x, double y) {
this.x = x;
this.y = y;
}

// return_type method_name ( parameter_list ) ;
double getX() {
return x;
}

void setX(double x) {
this.x = x;
}

double getY() {
return y;
}

void setY(double y) {
this.y = y;
}

public double distance(Point p1, Point p2) {
return Math.sqrt((p2.getY() - p1.getY()) * (p2.getY() - p1.getY())
+ (p2.getX() - p1.getX()) * (p2.getX() - p1.getX()));
}

public double distance(Point p) {
return distance(this, p);
}

public String toString() {
return "Point [x=" + x + ", y=" + y + "]";
}

public static void main(String[] args) {
Point point = new Point(3, 4);
System.out.println(point);
}
}

Line

package com.evs.objava33.class6;

public class Line {

// double x1, y1, x2, y2;
Point start, end;

public Line(double x1, double y1, double x2, double y2) {
start = new Point(x1, y1);
end = new Point(x2, y2);
}

public Line(final Point start, final Point end) {
this.start = start;
this.end = end;
}

public Line(final Line line) {
this.start = line.start;
// this.start = new Point(line.start.getX(), line.start.getY());
this.end = line.end;
}

public double length() {
return start.distance(end);
}

public String toString() {
return "Line [start=" + start + ", end=" + end + "]";
}

}