Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

You are given the following UML class diagram for a class called Square: Square - side: double {side >= 0} - lowerLeft: Point2D + Square()

You are given the following UML class diagram for a class called Square:

Square

- side: double {side >= 0}

- lowerLeft: Point2D

+ Square()

+ Square(initialSide: double, initialLowerLeft: Point2D)

+ Square(otherSquare: Square)

+ getSide(): double

+ setSide(newSide: double): void

+ getLowerLeft(): Point2D

+ getArea(): double

+ getCircumference(): double

+ equals(otherSquare: Square): boolean

+ toString(): String

Notes:

Point2D is a class representing a single point in 2D space (see code below),

lowerLeft represents Cartesian coordinates of lower left corner of your square,

non-default non-parameterized constructor should set the side to 0 and lowerLeft to (0, 0),

in non-default parameterized constructor:

if lower left corner Point2D reference is null, assume the coordinates are (0, 0),

when initialSide is invalid, set it to 0,

Square(otherSquare: Square) is so called copy constructor. It is a special constructor creates (in this case initializes attributes) an object (a new Square object in this case) using another object of the same Java class (existing Square type object otherSquare). Think: creating a physical duplicate (distinct object) of your IIT ID with the same information (by copying data from one to the other). There is a couple things to consider:

when otherSquare is a null reference, fall back on the non-default non-parameterized constructor behavior,

note that one of class Square attributes/fields is going to be an object. Copying it does NOT mean simply copying its reference (shallow copy). It means creating another object of the same type with same attribute values (deep copy),

equals(otherSquare: Square) is an object comparison method. Using the == operator when applied to object references will tell you whether both references are referring to the same object. If you want to check if two distinct objects of the same type are the same data/information-wise, you will need to use equals() method (see how it is used for String objects) and compare each individual attribute/field value (that includes object comparison if one of the attributes/fields is an object). If all match, two compared objects are the same. If otherSquare is a null reference, return false,

toString() should display a similar message to:

Square: lower left corner: (2, 3) | side: 2.0 | area: 4.0 | circumference: 8.0

Point2D class

public class Point2D {

private final double x;

private final double y;

public Point2D(){

this.x = 0.0d;

this.y = 0.0d;

}

public Point2D(double initialX, double initialY){

this.x = initialX;

this.y = initialY;

}

public Point2D(Point2D otherPoint){

if (otherPoint != null){

this.x = otherPoint.getX();

this.y = otherPoint.getY();

} else {

this.x = 0.0d;

this.y = 0.0d;

}

}

public double getX(){

return this.x;

}

public double getY(){

return this.y;

}

public boolean equals(Point2D otherPoint){

if (otherPoint != null){

if (this.x == otherPoint.getX() && this.y == otherPoint.getY()){

return true;

} else {

return false;

}

} else {

return false;

}

}

public String toString(){

return "(" + this.x + ", " + this.y + ")";

}

}

Your task is to implement this class in Java. You can test your class using the following app code:

SquareApp class

public class SquareApp {

public static void main(String[] args) {

Square s1 = new Square();

Square s2 = new Square(2.0, new Point2D(1.0, 1.0));

Square s3 = null;

Square s4 = new Square(s2);

Square s5 = new Square(3.0, new Point2D(3.0, 2.0));

Square s6 = s2;

Square s7 = new Square(s3);

System.out.println("s1: " + s1);

System.out.println("s2: " + s2);

System.out.println("s3: " + s3);

System.out.println("s4: " + s4);

System.out.println("s5: " + s5);

System.out.println("s6: " + s6);

System.out.println("s7: " + s7);

System.out.println();

System.out.println("s1 refers to the same object as s2: " + (s1 == s2));

System.out.println("s2 refers to the same object as s3: " + (s2 == s3));

System.out.println("s4 refers to the same object as s2: " + (s4 == s2));

System.out.println("s6 refers to the same object as s2: " + (s6 == s2));

System.out.println("s7 refers to the same object as s1: " + (s7 == s1));

System.out.println();

System.out.println("object referred to by s1 represents the same square as object referred to by s2: " + (s1.equals(s2)));

System.out.println("object referred to by s2 represents the same square as object referred to by s3: " + (s2.equals(s3)));

System.out.println("object referred to by s4 represents the same square as object referred to by s2: " + (s4.equals(s2)));

System.out.println("object referred to by s6 represents the same square as object referred to by s2: " + (s6.equals(s2)));

System.out.println("object referred to by s7 represents the same square as object referred to by s1: " + (s7.equals(s1)));

}

}

If you coded everything correctly, the output of this app should be as below:

SquareApp class output

s1: Square: lower left corner (0.0, 0.0) | side : 0.0 | area : 0.0 | circumference: 0.0

s2: Square: lower left corner (1.0, 1.0) | side : 2.0 | area : 4.0 | circumference: 8.0

s3: null

s4: Square: lower left corner (1.0, 1.0) | side : 2.0 | area : 4.0 | circumference: 8.0

s5: Square: lower left corner (3.0, 2.0) | side : 3.0 | area : 9.0 | circumference: 12.0

s6: Square: lower left corner (1.0, 1.0) | side : 2.0 | area : 4.0 | circumference: 8.0

s7: Square: lower left corner (0.0, 0.0) | side : 0.0 | area : 0.0 | circumference: 0.0

s1 refers to the same object as s2: false

s2 refers to the same object as s3: false

s4 refers to the same object as s2: false

s6 refers to the same object as s2: true

s7 refers to the same object as s1: false

object referred to by s1 represents the same square as object referred to by s2: false

object referred to by s2 represents the same square as object referred to by s3: false

object referred to by s4 represents the same square as object referred to by s2: true

object referred to by s6 represents the same square as object referred to by s2: true

object referred to by s7 represents the same square as object referred to by s1: true

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions