Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

For this question you must write java classes called Square and Rectangle, as well as a client class called ShapeClient. The partial Rectangle class is

For this question you must write java classes called Square and Rectangle, as well as a client class called ShapeClient. The partial Rectangle class is given below. (For this assignment, you will have to submit 3 .java files: one for the Rectangle class, one for the Shape class, and the other one for the ShapeClient class and 3 .class files associated with these .java files. So in total you will be submitting 6 files for this assignment.

// A Rectangle stores an (x, y) coordinate of its top/left corner, a width and height.

public class Rectangle {

private int x;

private int y;

private int width;

private int height;

// constructs a new Rectangle with the given x,y, width, and height public Rectangle(int x, int y, int w, int h)

// returns the fields' values public int getX()

public int getY()

public int getWidth()

public int getHeight()

// returns a string such as Coordinate is (5,12) and dimensionis 4x8 where 4 is width and 8 is height.

public String toString()

... }

Write an instance method called area that will be placed inside the Rectangle class. The method returns the area of the rectangle.

Write another instance method called changeSize that will be placed inside the Rectangle class. This method changes the height and width of the rectangle. The method accepts newWidth and newHeight as parameters and changes the value of width and height to this new width and height.

Create a class Square that extends Rectangle. Make sure that width and height for square object will be equal (hint create a new constructor, that takes only one value for width and height, as well as coordinates of the object. Use super(params) to call the parent constructor).

Write the client class RectangleClient that creates an object of class Rectangle and an object of class Square and initializes their x and y coordinates, widths and heights to arbitrary values. Print out the x- coordinate, y-coordinate, width, height (using toString method) and area of the objects. Then change the sizes of the objects to arbitrary values and print out the x and y coordinates, widths, heights and areas again.

When you execute the RectangleClient file you result should look like:

Coordinate is (5,12) and dimension is 6x8

Area is 48

Coordinate is (7,15) and dimension is 5x5

Area is 25

Coordinate is (5,12) and dimension is 3x4

Area after the size is changed is 12

Coordinate is (7,15) and dimension is 3x3

Area after the size is changed is 9

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_2

Step: 3

blur-text-image_3

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