Write a Java class called Rectangle that represents a rectangular two-dimensiona
ID: 3878752 • Letter: W
Question
Write a Java class called Rectangle that represents a rectangular two-dimensional region. Your Rectangle objects should have following methods: public void sctFields(int newx, int newy, int newwidth, int newbeight) Set the values for the fields in the Rectangle objects whose top-left comer is specified by the given x and y coordinates and by the width and height. public int getHeight0 the Rectangle's height. public int getWidth0 Returns the Rectangle's width. public int getx0 Returns the Rectangle's x-coordinate public int getYO Returns the Rectangle's y-coordinate public String toString) Retums a String representation of this Rectangle, such as "Rectangle [x-2, y-13, height-14, width-s1" Write a client program called RectangleClient that creates objects of the Rectangle class called rectl and rect2. Assign values to the fields of these objects. Print out these Rectangle objects using System.out.printin0 method. MacBook Air 20 F8 19 F10Explanation / Answer
RectangleClient.java:
package com.java.demo;
public class RectangleClient {
public static void main(String[] args) {
Rectangle rect1 = new Rectangle(2, 13, 14, 5);
Rectangle rect2 = new Rectangle(12, 23, 24, 15);
System.out.println(rect1);
System.out.println(rect2);
}
Rectangle.java:
package com.java.demo;
public class Rectangle {
private int x;
private int y;
private int height;
private int width;
public Rectangle(int x, int y, int height, int width) {
this.x = x;
this.y = y;
this.height = height;
this.width = width;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public int getHeight() {
return height;
}
public int getWidth() {
return width;
}
@Override
public String toString() {
return "Rectangle [x=" + x + ", y=" + y + ", height=" + height + ", width=" + width + "]";
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.