Question
Start with a working Point class. Refer to the Point program created for Lab #1 a. Make Point an abstract class 2. Write a new
Start with a working Point class. Refer to the Point program created for Lab #1 a. Make Point an abstract class 2. Write a new Circle class, a subclass of the Point class. a. Add a radius variable to the new class. b. Add constructors i. a default Circle()where radius = 15 ii. a 1 argument constructor Circle(radius) iii. use the Super() default constructor for the x, y values (This is the center point of the circle) c. Calculates the area of a Circle object: Area= 3.15 * radius * radius d. returns the Area of a Circle object e. Do not store the area in a member variable. f. Override the toString method found in class Point to display (and label) all of the characteristics (instance variables) of a Circle object. Do not forget to include the original point instance variables by using the super.toString(). 3. Write a new Cylinder class, a subclass of the Circle class. a. Add height as a new variable in this class b. Add constructors i. a default Cylinder() where height = 15 ii. a 1 argument constructor Circle(height) iii. use the Super() default constructor for the x, y, radius values c. Add get ( ) and set ( ) methods as needed. d. Override the area method inherited from the Circle class to calculate the outside face area of a Cylinder object. i. Calculates: Area= 2 * 3.14 * radius * radius + 2 * 3.14 * radius * height ii. returns the area of a Cylinder object iii. Do not store the area in a member variable.Lab 3 - Advanced Java Inheritance, Polymorphism, Abstract Types Rev. 1/28/2023 3:27 PM e. Add a method called volume that: i. Calculates the volume of a Cylinder object: Volume = 3.14 * radius * radius * height. ii. returns the volume of a Cylinder object iii. Do not store the volume in a member variable. f. Override the toString method inherited from class Circle class to display (and label) all of the characteristics (instance variables) of a Cylinder object. Do not forget to include the original point and radius instance variables by using the super.toString(). 4. Write an executable client class called ShapesABSTester a separate program - that uses the Point, Rectangle, Box, Circle, Cylinder classes. a. Print your name, Lab number, and date as the first three lines of output. Hard-code the date...do not use the system date. b. Create an array of the Point class Point[] myShapes = new Point[4]; c. Populate the array with a new Rectangle(10,20) in myShapes[0]; d. Populate the array with a new Box(30) in myShapes[1]; e. Populate the array with a new Circle(40) in myShapes[2]; f. Populate the array with a new Cylinder(50) in myShapes[3]; g. Using a for loop examine each item in the array with the instanceOf method to determine if the object is a cylinder, circle, box, or rectangle i. Print out the type of object and the objects toString() ii. Only type an object once iii. If an object is a cylinder then do not call it a circle as well iv. If an object is a box then do not call it a rectangle as well
my code thus far:
public class Point { private int x; private int y;
public Point() { x = 0; y = 0; }
public Point(int x, int y) { setX(x); setY(y); } public void setX(int x) { this.x = x; }
public void setY(int y) { this.y = y; } public int getX() { return x; } public int getY() { return y; } public String toString() { return "x=" + x +" y=" + y; }
}
BOX:
public class Box extends Rectangle { private int height; public Box() { super(); height = 15; } public Box(int height) { super(); setHeight(height); }
public void setHeight(int height) { this.height = height; } public int getHeight() { return height; } public int area() { int area = (super.getLength() * super.getWidth() * 2) + (super.getWidth() * getHeight() * 2) + (super.getLength() * getHeight() * 2); return area; } public int volume() { int volume = super.getLength() * super.getWidth() * getHeight(); return volume; }
public String toString() { return "height = " + getHeight() + " " + super.toString(); }
}
RECTANGLE:
public class Rectangle extends Point { private int width; private int length; public Rectangle() { super(); width = 15; length = 15; } public Rectangle(int width, int length) { super(); setWidth(width); setLength(length); }
public Rectangle(int x, int y, int width, int length) { super(x, y); setWidth(width); setLength(length); }
public void setWidth(int width) { this.width = width; }
public void setLength(int length) { this.length = length; } public int getWidth() { return width; } public int getLength() { return length; } public int area() { int area = width * length; return area; }
public String toString() { return "width = " + width +" length = " + length + " at location " + super.toString(); }
}
shapetester:
public class ShapesTester {
public static void main(String[] args) {
System.out.println("Sharon Hoover"); System.out.println("Lab 2"); System.out.println("1/26/2018"); System.out.println(); Point point1 = new Point(); Point point2 = new Point(5, 19);
Rectangle rectangle1 = new Rectangle(); Rectangle rectangle2 = new Rectangle(24, 30);
Box box1 = new Box(); Box box2 = new Box(60);
System.out.printf("The area of rectangle 1 = %d square units%n", rectangle1.area()); System.out.printf("The area of rectangle 2 = %d square units%n", rectangle2.area()); System.out.println("***********************************************************");
System.out.printf("The area of box 1 = %d square units%n", box1.area()); System.out.printf("The volume of box 1 = %d cube units%n", box1.volume()); System.out.println("***********************************************************");
System.out.printf("The area of box 2 = %d square units%n", box2.area()); System.out.printf("The volume of box 2 = %d cube units%n", box2.volume());
System.out.println("***********************************************************");
System.out.println("Changing box 2 width to 21"); box2.setWidth(21);
System.out.printf("The area of box 2 = %d square units%n", box2.area()); System.out.printf("The volume of box 2 = %d cube units%n", box2.volume()); System.out.println("***********************************************************");
System.out.printf("Printing the instance variables for each of the objects%n%n"); System.out.printf("Point1 %s%n%n", point1); System.out.printf("Point2 %s%n%n", point2); System.out.printf("Rectangle1 %s%n%n", rectangle1); System.out.printf("Rectangle2 %s%n%n", rectangle2); System.out.printf("Box1 %s%n%n", box1); System.out.printf("Box2 %s%n%n", box2);
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started