Exercise three

abstract class

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package abstractclass;

public class Circle extends Shape{
double radius;
Circle(){
super();
this.radius = 0;
}
Circle(double r, String c, boolean f){
super(c, f);
this.radius = r;
}
Circle(double r){
super();
this.radius = r;
}
public double getRadius() {
return this.radius;
}

@Override
public double getArea() {
return Math.PI * radius * radius;
}

@Override
public double getPerimeter() {
return Math.PI * 2 * radius;
}

public String toString() {
return "A Circle with radius = " + radius + ", which is a subclass of " + super.toString();
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package abstractclass;

public class Rectangle extends Shape{
double width;
double length;

Rectangle(){
super();
this.width = 0;
this.length = 0;
}
Rectangle(double w, double l, String c, boolean f){
super(c, f);
this.width = w;
this.length = l;
}
Rectangle(double w, double l){
super();
this.width = w;
this.length = l;
}
public double getWidth() {
return this.width;
}

public double getLength() {
return this.length;
}

@Override
public double getArea() {
return width * length;
}

@Override
public double getPerimeter() {
return 2*(width + length);
}

public String toString() {
return "A Rectangle with width = "+width+" and length = "+length+", which is a subclass of "+ super.toString();
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package abstractclass;

public abstract class Shape {
protected String color;
protected boolean filled;

Shape(){
this.color = "red";
this.filled = true;
}

Shape(String c, boolean f){
this.color = c;
this.filled = f;
}

public String getColor() {
return color;
}

public boolean isFilled() {
return filled;
}

public String toString() {
if(filled == true)
return "A Shape with color of " + color + " and filled";
else
return "A Shape with color of " + color + " and NOT filled";
}

public abstract double getArea();

public abstract double getPerimeter();
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package abstractclass;

public class Square extends Rectangle{
public Square(double side) {
super(side, side); // Call superclass Rectangle(double, double)
}

public double getSide() {
return super.getWidth();
}

public String toString() {
return "A Square with side = "+super.getLength()+", which is a subclass of "+super.toString();
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package abstractclass;

public class Main {
public static void main(String[] args) {

java.text.DecimalFormat df =new java.text.DecimalFormat("#.00");
Shape s1 = new Circle(5.5, "green", false); // Upcast Circle to Shape
System.out.println(s1); // which version?
double Area1 = s1.getArea();
double Perimeter1 = s1.getPerimeter();
System.out.println(df.format(Area1)); // which version?
System.out.println(df.format(Perimeter1));
System.out.println(s1.getColor());
System.out.println(s1.isFilled());
//Shape.test

Circle c1 = (Circle)s1; // Downcast back to Circle
System.out.println(c1);
double Area2 = c1.getArea();
double Perimeter2 = c1.getPerimeter();
System.out.println(df.format(Area2));
System.out.println(df.format(Perimeter2));
System.out.println(c1.getColor());
System.out.println(c1.isFilled());
System.out.println(c1.getRadius());

// Shape s2 = new Shape(); // Why wrong? : Abstract class cannot be instantiated

//Circle.test

Shape s3 = new Rectangle(1.0, 2.0, "green", false); // Upcast
System.out.println(s3);
double Area3 = s3.getArea();
double Perimeter3 = s3.getPerimeter();
System.out.println(df.format(Area3));
System.out.println(df.format(Perimeter3));
System.out.println(s3.getColor());


Rectangle r1 = (Rectangle)s3; // downcast
System.out.println(r1);
double Area4 = r1.getArea();
System.out.println(df.format(Area4));
System.out.println(r1.getColor());
System.out.println(r1.getLength());
//Rectangle.test

Shape s4 = new Square(6.6); // Upcast
System.out.println(s4);
double Area5 = s4.getArea();
System.out.println(df.format(Area5));
System.out.println(s4.getColor());

Rectangle r2 = (Rectangle)s4;
System.out.println(r2);
double Area6 = r2.getArea();
System.out.println(df.format(Area6));
System.out.println(r2.getColor());
System.out.println(r2.getLength());

Square sq1 = (Square)r2;
System.out.println(sq1);
double Area7 = sq1.getArea();
System.out.println(df.format(Area7));
System.out.println(sq1.getColor());
System.out.println(sq1.getSide());
System.out.println(sq1.getLength());
//Square.test

}


}

exception

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package exception;

class CircleWithException {
double radius;
static int numberOfObjects = 0;

CircleWithException(){
radius = 0;
numberOfObjects++;
}

CircleWithException(double r){
if(r >= 0)
{
radius = r;
numberOfObjects++;
}
else
throw new IllegalArgumentException("Radius cannot be negative");
}

public double getRadius() {
return radius;
}

public void setRadius(double r) {
radius = r;
}

public static int getNumberOfObjects() {
return numberOfObjects;
}

public double getArea() {
return Math.PI * radius * radius;
}

}


public class Main1 {
public static void main(String[] args) {
try {
CircleWithException c1 = new CircleWithException(5);
CircleWithException c2 = new CircleWithException(-5);
CircleWithException c3 = new CircleWithException(0);
}
catch (IllegalArgumentException ex) {
System.out.println(ex);
}
System.out.println("Number of objects created: " + CircleWithException.getNumberOfObjects() );
}
}

interface

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package Interface;

public class MovablePoint implements Movable {
// instance variables
int x, y, xSpeed, ySpeed; // package access

// Constructor
public MovablePoint(int x, int y, int xSpeed, int ySpeed) {
this.x = x;
this.y = y;
this.xSpeed = xSpeed;
this.ySpeed = ySpeed;
}

// Implement abstract methods declared in the interface Movable
@Override
public void moveUp() {
y += ySpeed;
}

@Override
public void moveDown() {
y -= ySpeed;
}

@Override
public void moveLeft() {
x -= xSpeed;
}

@Override
public void moveRight() {
x += xSpeed;
}

public String toString() {
return "MovablePoint (" + x + ", " + y + ") with xSpeed = " + xSpeed + " and ySpeed = " + ySpeed;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package Interface;

public class MovableCircle implements Movable { // saved as "MovableCircle.java"
// instance variables
private MovablePoint center; // can use center.x, center.y directly
// because they are package accessible
private int radius;

// Constructor
public MovableCircle(int x, int y, int xSpeed, int ySpeed, int radius) {
// Call the MovablePoint's constructor to allocate the center instance.
center = new MovablePoint(x, y, xSpeed, ySpeed);
this.radius = radius;
}

// Implement abstract methods declared in the interface Movable
@Override
public void moveUp() {
center.y += center.ySpeed;
}

@Override
public void moveDown() {
center.y -= center.ySpeed;
}

@Override
public void moveLeft() {
center.x -= center.xSpeed;
}

@Override
public void moveRight() {
center.x += center.xSpeed;
}

public String toString() {
return "MovableCircle at point " + center.toString() + " with radius = " + radius;
}
}
1
2
3
4
5
6
7
8
package Interface;

public interface Movable {
public void moveUp();
public void moveDown();
public void moveLeft();
public void moveRight();
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package Interface;

public class Main{
public static void main(String[] args) {
Movable m1 = new MovablePoint(5, 6, 10, 15); // upcast
System.out.println(m1);
m1.moveLeft();
System.out.println(m1);
m1.moveUp();
System.out.println(m1);

Movable m2 = new MovableCircle(1, 2, 3, 4, 20); // upcast
System.out.println(m2);
m2.moveRight();
System.out.println(m2);
m2.moveDown();
System.out.println(m2);
}
}

superclass

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package superclass;

public class Circle extends Shape{
double radius;
Circle(){
super();
this.radius = 0;
}
Circle(double r, String c, boolean f){
super(c, f);
this.radius = r;
}
Circle(double r){
super();
this.radius = r;
}
public double getRadius() {
return this.radius;
}

public double getArea() {
return Math.PI * radius * radius;
}

public double getPerimeter() {
return Math.PI * 2 * radius;
}

public String toString() {
return "A Circle with radius = " + radius + ", which is a subclass of " + super.toString();
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package superclass;

public class Rectangle extends Shape{
double width;
double length;

Rectangle(){
super();
this.width = 0;
this.length = 0;
}
Rectangle(double w, double l, String c, boolean f){
super(c, f);
this.width = w;
this.length = l;
}
Rectangle(double w, double l){
super();
this.width = w;
this.length = l;
}
public double getWidth() {
return this.width;
}

public double getLength() {
return this.length;
}


public double getArea() {
return width * length;
}

public double getPerimeter() {
return 2*(width + length);
}

public String toString() {
return "A Rectangle with width = "+width+" and length = "+length+", which is a subclass of "+ super.toString();
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package superclass;

public class Shape {
String color;
boolean filled;

Shape(){
this.color = "red";
this.filled = true;
}

Shape(String c, boolean f){
this.color = c;
this.filled = f;
}

public String getColor() {
return color;
}

public boolean isFilled() {
return filled;
}

public String toString() {
if(filled == true)
return "A Shape with color of " + color + " and filled";
else
return "A Shape with color of " + color + " and NOT filled";
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package superclass;

public class Square extends Rectangle{
public Square(double side) {
super(side, side); // Call superclass Rectangle(double, double)
}

public double getSide() {
return super.getWidth();
}

public String toString() {
return "A Square with side = "+super.getLength()+", which is a subclass of "+super.toString();
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package superclass;

public class Main {
public static void main(String[] args) {

// Upcast Circle to Shape
Shape s1 = new Circle(5.5, "green", false);
System.out.println(s1);
System.out.println(s1.getColor());
System.out.println(s1.isFilled());

// Downcast back to Circle
Circle c1 = (Circle)s1;
System.out.println(c1);
System.out.println(c1.getColor());
System.out.println(c1.isFilled());

// Upcast
Shape s2 = new Rectangle(1.0, 2.0, "green", false);
System.out.println(s2);
System.out.println(s2.getColor());

//Downcast
Rectangle r1 = (Rectangle)s2;
System.out.println(r1);
System.out.println(r1.getArea());
System.out.println(r1.getColor());
System.out.println(r1.getLength());

// Upcast
Shape s3 = new Square(6.6);
System.out.println(s3);
System.out.println(s3.getColor());


Rectangle r2 = (Rectangle)s3;
System.out.println(r2);
java.text.DecimalFormat df =new java.text.DecimalFormat("#.00");
double area1 = r2.getArea();
System.out.println(df.format(area1));
System.out.println(r2.getColor());
System.out.println(r2.getLength());


// Downcast Rectangle r2 to Square
Square sq1 = (Square)r2;
System.out.println(sq1);
area1 = sq1.getArea();
System.out.println(df.format(area1));
System.out.println(sq1.getColor());
System.out.println(sq1.getSide());
System.out.println(sq1.getLength());
}


}
Donate? comment?