I am a beginner Java programmer. I have just finished up an assignment and would appreciate some advice and/or constructive criticism on my program. For context, I have the assignment details, followed by the full code below:
The Shape interface is as follows:
public interface Shape { public double area(); public double perimeter(); public double getXPos(); public double getYPos(); public void move(double xLoc, double yLoc); public String toString(); }
An AbstractShape has the following attributes:
- xPos
- yPos
An AbstractShape has the following behaviour:
- move(x,y)
- getXPos
- getYPos
- toString
A Circle adds the following attributes:
- radius
A Circle adds the following behaviour:
- area
- perimeter
- equals(Circle)
- clone
- updated toString
A Rectangle adds the following attributes:
- height
- width
A Rectangle adds the following behaviour:
- area
- perimeter
- equals(Rectangle)
- clone
- updated toString
Also include constructors in your classes.
Write a test harness which generates 50 random Shapes, stores them in an array, then prints them out.
Main Class: RandomShapes
public class RandomShapes {
public static void main(String[] args) {
// Creating the random variables
double randX, randY, randRadius, randWidth, randHeight;
// Array to store AbstractShapes
AbstractShape[] shapes = new AbstractShape[50];
for (int i = 0; i < shapes.length; i++) {
// Random number between 1-2 to decide rectangle, or circle
int circleOrRect = (int) (Math.random() * 2 + 1);
// Creating 6 random numbers
randX = (int) ((Math.random() * 100) + 1);
randY = (int) ((Math.random() * 100) + 1);
randRadius = (int) ((Math.random() * 100) + 1);
randWidth = (int) ((Math.random() * 100) + 1);
randHeight = (int) ((Math.random() * 100) + 1);
// If random number is 1, create/add a circle to array, else, add rectangle
if (circleOrRect == 1) {
// Create new circle with random (x, y)
shapes[i] = new Circle(randX, randY);
// Cast AbstractShape to Circle and set random radius
((Circle) shapes[i]).setRadius(randRadius);
// Print out the object's info
System.out.println("\n#" + (i+1) + shapes[i].toString());
} else {
// Create new rectangle with random (x, y)
shapes[i] = new Rectangle(randX, randY);
// Cast AbstractShape to Rectangle and set random height/width
((Rectangle) shapes[i]).setHeight(randHeight);
((Rectangle) shapes[i]).setWidth(randWidth);
// Print out the object's info
System.out.println("\n#" + (i+1) + shapes[i].toString());
}
}
}
}
Interface: Shape
public interface Shape {
// All the abstract methods
public double area();
public double perimeter();
public double getXPos();
public double getYPos();
public void move(double xLoc, double yLoc);
public String toString();
}
AbstractClass: AbstractShapes
abstract public class AbstractShape implements Shape {
// X and Y positions
protected double xpos;
protected double ypos;
// Constructor for an AbstractShape
public AbstractShape(double x, double y) {
xpos = x;
ypos = y;
}
// To move a shape
public void move(double xLoc, double yLoc) {
xpos = xLoc;
ypos = yLoc;
}
// Get X position of a shape
public double getXPos() {
return xpos;
}
// Get Y position of a shape
public double getYPos() {
return ypos;
}
//Generic toString method
public String toString() {
String message = "xPos: " + xpos
+ "\nyPos: " + ypos;
return message;
}
}
Sub-Class: Circle
public class Circle extends AbstractShape {
// Radius variable
private double radius;
// Constructor is the same as AbstractShape
public Circle(double xpos, double ypos) {
super(xpos, ypos);
}
// Set radius value
public void setRadius(double r) {
radius = r;
}
// Get radius value
public double getRadius() {
return radius;
}
// Calculate area
public double area() {
double area = Math.PI * Math.pow(radius, 2);
return area;
}
// Calculate perimeter (circumference)
public double perimeter() {
double perimeter = 2 * Math.PI * radius;
return perimeter;
}
// Clone the object
public Circle clone() {
Circle theClone;
theClone = new Circle(this.xpos, this.ypos);
theClone.setRadius(this.radius);
return theClone;
}
// Check to see if 2 objects are the same
public boolean equals(Circle c) {
return c.radius == this.radius
&& c.getXPos() == this.xpos
&& c.getYPos() == this.ypos;
}
// Specific toString() method
public String toString() {
String message = "\n[CIRCLE]"
+ "\nPosition: (" + xpos + ", " + ypos
+ ")\nRadius: " + radius
+ "\nArea: " + this.area()
+ "\nCircumference: " + this.perimeter();
return message;
}
}
Sub-Class: Rectangle
public class Rectangle extends AbstractShape {
// Height and width variables
private double height;
private double width;
// Constructor for rectangle (same as super)
public Rectangle(double xpos, double ypos) {
super(xpos, ypos);
}
// Set height value
public void setHeight(double h) {
height = h;
}
// Get the height value
public double getHeight() {
return height;
}
// Set the width value
public void setWidth(double w) {
width = w;
}
// Get the width value
public double getWidth(double w) {
return width;
}
// Calculate the area
public double area() {
double area = height * width;
return area;
}
// Calculate the perimeter
public double perimeter() {
double perimeter = (2 * width) + (2 * height);
return perimeter;
}
// Clone the object
public Rectangle clone() {
Rectangle theClone;
theClone = new Rectangle(this.xpos, this.ypos);
theClone.setHeight(this.height);
theClone.setWidth(this.width);
return theClone;
}
// Check to see if 2 objects are the same
public boolean equals(Rectangle r) {
return r.height == this.height
&& r.width == this.width
&& r.getXPos() == this.xpos
&& r.getYPos() == this.ypos;
}
// Specific toString() method
public String toString() {
String message = "\n[RECTANGLE]"
+ "\nPosition: (" + xpos + ", " + ypos
+ ")\nHeight: " + height
+ "\nWidth: " + width
+ "\nArea: " + this.area()
+ "\nPerimeter: " + this.perimeter();
return message;
}
}