Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

For this question you must write a java class called Rectangle and a client clas

ID: 3663807 • Letter: F

Question

For this question you must write a java class called Rectangle and a client class called RectangleClient. The partial Rectangle class is given below. (For this assignment, you will have to submit 2 .java files: one for the Rectangle class and the other one for the RectangleClient class and 2 .class files associated with these .java files. So in total you will be submitting 4 files for part b of 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 {(5,12), 4x8}

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 Rectangel 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.

Write the client class RectangleClient that creates an object of class Rectangle and initializes its x coordinate, y coordinates, width and height to 5, 12, 4, and 8 respectively. Print out the x- coordinate, y-coordinate, width, height and area of this rectangle (use toString method to print the area). Then change the size of this rectangle to width = 3 and height = 10 and then print out the area again.

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

x-coordinate : 5

y-coordinate : 12

Width : 4

Height : 8

Area : 32

Area after the size is changed : 30

Explanation / Answer

class Rectangle
{
private int x;
private int y;
private int width;
private int height;
Rectangle(int x, int y, int w, int h)
{
this.x = x;
this.y = y;
this.width = w;
this.height = h;
}
public int getX()
{
return x;
}
public int getY()
{
return y;
}
public int getWidth()
{
return width;
}
public int getHeight()
{
return height;
}
public String toString()
{
return ("{("+x+","+width+"x"+height);
}
public int area()
{
return (width*height);
}
public void changeSize(int nh, int nw)
{
width = nw;
height = nh;
}
}
class RectangleClient
{
public static void main(String args[])
{
Rectangle r = new Rectangle(5, 12,4,8);
System.out.println("x-coordinate : "+r.getX());
System.out.println("y-coordinate : "+r.getY());
System.out.println("Width : "+r.getWidth());
System.out.println("Height : "+r.getHeight());
System.out.println("Area : "+r.area());
r.changeSize(5,6);
System.out.println("Area after the size is changed : "+r.area());
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote