Restore
This commit is contained in:
10
Labs/Lab6/Lab6_2230026071/Question5/.classpath
Normal file
10
Labs/Lab6/Lab6_2230026071/Question5/.classpath
Normal file
@@ -0,0 +1,10 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<classpath>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER">
|
||||
<attributes>
|
||||
<attribute name="module" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry kind="src" path="src"/>
|
||||
<classpathentry kind="output" path="bin"/>
|
||||
</classpath>
|
||||
17
Labs/Lab6/Lab6_2230026071/Question5/.project
Normal file
17
Labs/Lab6/Lab6_2230026071/Question5/.project
Normal file
@@ -0,0 +1,17 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<projectDescription>
|
||||
<name>Question5</name>
|
||||
<comment></comment>
|
||||
<projects>
|
||||
</projects>
|
||||
<buildSpec>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.jdt.core.javabuilder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
</buildSpec>
|
||||
<natures>
|
||||
<nature>org.eclipse.jdt.core.javanature</nature>
|
||||
</natures>
|
||||
</projectDescription>
|
||||
@@ -0,0 +1,2 @@
|
||||
eclipse.preferences.version=1
|
||||
encoding/<project>=UTF-8
|
||||
BIN
Labs/Lab6/Lab6_2230026071/Question5/bin/Circle.class
Normal file
BIN
Labs/Lab6/Lab6_2230026071/Question5/bin/Circle.class
Normal file
Binary file not shown.
BIN
Labs/Lab6/Lab6_2230026071/Question5/bin/Dot.class
Normal file
BIN
Labs/Lab6/Lab6_2230026071/Question5/bin/Dot.class
Normal file
Binary file not shown.
BIN
Labs/Lab6/Lab6_2230026071/Question5/bin/ManyShapes.class
Normal file
BIN
Labs/Lab6/Lab6_2230026071/Question5/bin/ManyShapes.class
Normal file
Binary file not shown.
BIN
Labs/Lab6/Lab6_2230026071/Question5/bin/Rectangle.class
Normal file
BIN
Labs/Lab6/Lab6_2230026071/Question5/bin/Rectangle.class
Normal file
Binary file not shown.
BIN
Labs/Lab6/Lab6_2230026071/Question5/bin/Shape.class
Normal file
BIN
Labs/Lab6/Lab6_2230026071/Question5/bin/Shape.class
Normal file
Binary file not shown.
BIN
Labs/Lab6/Lab6_2230026071/Question5/bin/Square.class
Normal file
BIN
Labs/Lab6/Lab6_2230026071/Question5/bin/Square.class
Normal file
Binary file not shown.
BIN
Labs/Lab6/Lab6_2230026071/Question5/bin/Start.class
Normal file
BIN
Labs/Lab6/Lab6_2230026071/Question5/bin/Start.class
Normal file
Binary file not shown.
26
Labs/Lab6/Lab6_2230026071/Question5/src/Circle.java
Normal file
26
Labs/Lab6/Lab6_2230026071/Question5/src/Circle.java
Normal file
@@ -0,0 +1,26 @@
|
||||
|
||||
public class Circle extends Shape{
|
||||
//instance variable
|
||||
private double radius;
|
||||
|
||||
//constructor
|
||||
public Circle(double x, double y, double radius) {
|
||||
super(x, y);
|
||||
this.radius = radius;
|
||||
}
|
||||
|
||||
//method
|
||||
public double area() {
|
||||
return Math.PI * this.radius * this.radius;
|
||||
}
|
||||
|
||||
//test
|
||||
public static void testCircle() {
|
||||
Circle c = new Circle(1.2, 3.4, 4.0);
|
||||
// getX and getY are inherited from Shape.
|
||||
// area comes from Circle itself.
|
||||
System.out.println(c.getX() == 1.2);
|
||||
System.out.println(c.getY() == 3.4);
|
||||
System.out.println(c.area() == Math.PI * 16.0);
|
||||
}
|
||||
}
|
||||
20
Labs/Lab6/Lab6_2230026071/Question5/src/Dot.java
Normal file
20
Labs/Lab6/Lab6_2230026071/Question5/src/Dot.java
Normal file
@@ -0,0 +1,20 @@
|
||||
|
||||
public class Dot extends Shape {
|
||||
//constructor
|
||||
public Dot(double x, double y) {
|
||||
super(x, y);
|
||||
}
|
||||
|
||||
//method
|
||||
public double area() {return 0.0;}
|
||||
|
||||
//test
|
||||
public static void testDot() {
|
||||
Dot d = new Dot(1.2, 3.4);
|
||||
// getX and getY are inherited from Shape.
|
||||
// area comes from Dot itself.
|
||||
System.out.println(d.getX() == 1.2);
|
||||
System.out.println(d.getY() == 3.4);
|
||||
System.out.println(d.area() == 0.0);
|
||||
}
|
||||
}
|
||||
34
Labs/Lab6/Lab6_2230026071/Question5/src/ManyShapes.java
Normal file
34
Labs/Lab6/Lab6_2230026071/Question5/src/ManyShapes.java
Normal file
@@ -0,0 +1,34 @@
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class ManyShapes {
|
||||
//instance variables
|
||||
private ArrayList allshapes;
|
||||
|
||||
//constructor
|
||||
public ManyShapes() {
|
||||
this.allshapes = new ArrayList();
|
||||
}
|
||||
|
||||
//method
|
||||
public void addShape(Shape s) {
|
||||
allshapes.add(s);
|
||||
}
|
||||
|
||||
public void listAllShapes() {
|
||||
for(int i = 0; i < allshapes.size(); ++i) {
|
||||
Shape o = (Shape) allshapes.get(i);
|
||||
System.out.println("Shape has area " + o.area());
|
||||
}
|
||||
}
|
||||
|
||||
//test
|
||||
public static void testManyShapes() {
|
||||
ManyShapes m = new ManyShapes();
|
||||
m.addShape(new Circle(1.2, 3.4, 4.0)); // Upcast from Circle to Shape.
|
||||
m.addShape(new Dot(1.2, 3.4)); // Upcast from Dot to Shape.
|
||||
m.addShape(new Rectangle(1.2, 3.4, 4.0, 5.0)); // Upcast from Rectangle to Shape.
|
||||
m.addShape(new Shape(1.2, 3.4));
|
||||
m.addShape(new Square(1.2, 3.4, 5.0)); // Upcast from Square to Shape.
|
||||
m.listAllShapes();
|
||||
}
|
||||
}
|
||||
28
Labs/Lab6/Lab6_2230026071/Question5/src/Rectangle.java
Normal file
28
Labs/Lab6/Lab6_2230026071/Question5/src/Rectangle.java
Normal file
@@ -0,0 +1,28 @@
|
||||
|
||||
public class Rectangle extends Shape {
|
||||
//instance variables
|
||||
private double width;
|
||||
private double length;
|
||||
|
||||
//constructor
|
||||
public Rectangle(double x, double y, double width, double length) {
|
||||
super(x, y);
|
||||
this.width = width;
|
||||
this.length = length;
|
||||
}
|
||||
|
||||
//method
|
||||
public double area() {
|
||||
return this.width * this.length;
|
||||
}
|
||||
|
||||
//test
|
||||
public static void testRectangle() {
|
||||
Rectangle r = new Rectangle(1.2, 3.4, 4.0, 5.0);
|
||||
// getX and getY are inherited from Shape.
|
||||
// area comes from Rectangle itself.
|
||||
System.out.println(r.getX() == 1.2);
|
||||
System.out.println(r.getY() == 3.4);
|
||||
System.out.println(r.area() == 20.0);
|
||||
}
|
||||
}
|
||||
31
Labs/Lab6/Lab6_2230026071/Question5/src/Shape.java
Normal file
31
Labs/Lab6/Lab6_2230026071/Question5/src/Shape.java
Normal file
@@ -0,0 +1,31 @@
|
||||
|
||||
public class Shape {
|
||||
//instance variables
|
||||
private double x;
|
||||
private double y;
|
||||
|
||||
//constructor
|
||||
public Shape(double x, double y) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
}
|
||||
|
||||
//methods
|
||||
public double getX() {return this.x;}
|
||||
|
||||
public double getY() {return this.y;}
|
||||
|
||||
public double area() {
|
||||
System.out.println("An unknown shape has an unknown area!");
|
||||
return -1.0;
|
||||
}
|
||||
|
||||
//test
|
||||
public static void testShape() {
|
||||
Shape s = new Shape(1.2, 3.4);
|
||||
System.out.println(s.getX() == 1.2);
|
||||
System.out.println(s.getY() == 3.4);
|
||||
System.out.println(s.area() == -1.0); // Will print an error message too.
|
||||
}
|
||||
|
||||
}
|
||||
20
Labs/Lab6/Lab6_2230026071/Question5/src/Square.java
Normal file
20
Labs/Lab6/Lab6_2230026071/Question5/src/Square.java
Normal file
@@ -0,0 +1,20 @@
|
||||
|
||||
public class Square extends Rectangle {
|
||||
//constructor
|
||||
public Square(double x, double y, double size) {
|
||||
super(x, y, size, size);
|
||||
}
|
||||
|
||||
//Square dose not need to have its own area() method
|
||||
|
||||
//test
|
||||
public static void testSquare() {
|
||||
Square s = new Square(1.2, 3.4, 5.0);
|
||||
// getX and getY are inherited from Shape.
|
||||
// area is inherited from Rectangle.
|
||||
System.out.println(s.getX() == 1.2);
|
||||
System.out.println(s.getY() == 3.4);
|
||||
System.out.println(s.area() == 25.0);
|
||||
}
|
||||
|
||||
}
|
||||
13
Labs/Lab6/Lab6_2230026071/Question5/src/Start.java
Normal file
13
Labs/Lab6/Lab6_2230026071/Question5/src/Start.java
Normal file
@@ -0,0 +1,13 @@
|
||||
|
||||
public class Start {
|
||||
|
||||
public static void main(String[] args) {
|
||||
Shape.testShape();
|
||||
Circle.testCircle();
|
||||
Dot.testDot();
|
||||
Rectangle.testRectangle();
|
||||
Square.testSquare();
|
||||
ManyShapes.testManyShapes();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user