Upload Tutorial 5
This commit is contained in:
parent
9d4f4352a3
commit
1435fb21b9
|
@ -0,0 +1,5 @@
|
|||
public class ClassA {
|
||||
public void print(int x, String y) {
|
||||
System.out.println(x + " " + y);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,5 @@
|
|||
public class ClassB extends ClassA {
|
||||
public void print(int x) {
|
||||
System.out.println(x);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,5 @@
|
|||
public class ClassC extends ClassA {
|
||||
public void print(String x, String y) {
|
||||
System.out.println(x + " " + y);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,5 @@
|
|||
public class ClassD extends ClassC {
|
||||
public void print(String a, String b) {
|
||||
System.out.println(a + " " + b);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,5 @@
|
|||
public class ClassE extends ClassC {
|
||||
public void print(String x) {
|
||||
System.out.println(x);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,5 @@
|
|||
public class ClassF extends ClassE {
|
||||
public void print(int x) {
|
||||
System.out.println(x);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,5 @@
|
|||
public class ClassG extends ClassF {
|
||||
public void print(String x) {
|
||||
System.out.println(x);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,42 @@
|
|||
public abstract class Polygon {
|
||||
//public class Polygon {
|
||||
public enum KindofPolygon {
|
||||
POLY_PLAIN,
|
||||
POLY_RECT,
|
||||
POLY_TRIANG
|
||||
};
|
||||
protected String name;
|
||||
protected float width;
|
||||
protected float height;
|
||||
protected KindofPolygon polytype;
|
||||
|
||||
public Polygon(String theName, float theWidth, float theHeight) {
|
||||
name = theName;
|
||||
width = theWidth;
|
||||
height = theHeight;
|
||||
polytype = KindofPolygon.POLY_PLAIN;
|
||||
}
|
||||
|
||||
public KindofPolygon getPolytype() {
|
||||
return polytype;
|
||||
}
|
||||
|
||||
public void setPolytype(KindofPolygon value) {
|
||||
polytype = value;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
// public float calArea() {
|
||||
// return 0;
|
||||
// }
|
||||
|
||||
public void printWidthHeight( ) {
|
||||
System.out.println("Width = " + width + " Height = " + height);
|
||||
}
|
||||
|
||||
public abstract float calArea();
|
||||
|
||||
}
|
|
@ -0,0 +1,10 @@
|
|||
public class Rectangle extends Polygon {
|
||||
public Rectangle(String theName, float theWidth, float theHeight) {
|
||||
super(theName, theWidth, theHeight) ;
|
||||
this.polytype = KindofPolygon.POLY_RECT;
|
||||
}
|
||||
|
||||
public float calArea() {
|
||||
return width * height;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,68 @@
|
|||
public class TestPolygon {
|
||||
public static void printArea(Rectangle rect) {
|
||||
float area = rect.calArea();
|
||||
System.out.println("The area of the " + rect.getPolytype() + " is " + area);
|
||||
}
|
||||
|
||||
public static void printArea(Triangle tri) {
|
||||
float area = tri.calArea();
|
||||
System.out.println("The area of the " + tri.getPolytype() + " is " + area);
|
||||
}
|
||||
|
||||
public static void main(String[] args ) {
|
||||
|
||||
Rectangle rect = new Rectangle("Rectangle", 3.0f, 4.0f);
|
||||
printArea(rect); // static binding
|
||||
rect.printWidthHeight();
|
||||
|
||||
Triangle triangle= new Triangle("Triangle", 3.0f, 4.0f);
|
||||
printArea(triangle); // static binding
|
||||
triangle.printWidthHeight();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public static void printArea(Polygon poly) {
|
||||
float area = poly.calArea( );
|
||||
System.out.println("The area of the " + poly.getPolytype() + " is " + area);
|
||||
}
|
||||
|
||||
public static void main2(String[] args ) {
|
||||
|
||||
Rectangle rect = new Rectangle("Rectangle", 3.0f, 4.0f);
|
||||
printArea(rect);
|
||||
rect.printWidthHeight();
|
||||
|
||||
Triangle triangle= new Triangle("Triangle", 3.0f, 4.0f);
|
||||
printArea(triangle);
|
||||
triangle.printWidthHeight();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,10 @@
|
|||
public class Triangle extends Polygon {
|
||||
public Triangle (String theName, float theWidth, float theHeight) {
|
||||
super(theName, theWidth, theHeight) ;
|
||||
this.polytype = KindofPolygon.POLY_TRIANG;
|
||||
}
|
||||
|
||||
public float calArea() {
|
||||
return 0.5f * width * height;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue