For this question you must write a java class called Rectangle and a client clas
ID: 3748533 • 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 yi 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 dimension is 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 changesExplanation / Answer
Rectangle.java
public class Rectangle {
//Declaring instance variables
private int x;
private int y;
private int width;
private int height;
//Parameterized constructor
public Rectangle(int x, int y, int width, int height) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}
//getters and setters
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
//This method will calculate the area of the rectangle
public int area() {
return width * height;
}
//This method will change the dimensions of the rectangle
public void changeSize(int newWidth, int newHeight) {
this.width = newWidth;
this.height = newHeight;
}
//toString method is used to display the contents of an object inside it
@Override
public String toString() {
return "Coordinate is (" + x + "," + y + ") and dimension is " + width + "x" + height + "";
}
}
_______________
RectangleClient.java
public class RectangleClient {
public static void main(String[] args) {
//Creating an Rectangle class object
Rectangle r1 = new Rectangle(5, 12, 4, 8);
System.out.println(r1);
System.out.println("Area is " + r1.area());
r1.changeSize(3, 10);
System.out.println(r1);
System.out.println("Area after the size in changed is " + r1.area());
}
}
_______________
Output:
Coordinate is (5,12) and dimension is 4x8
Area is 32
Coordinate is (5,12) and dimension is 3x10
Area after the size in changed is 30
______Could you plz rate me well.Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.