I need to write a rectangle class that cotains these methods below and also work
ID: 665909 • Letter: I
Question
I need to write a rectangle class that cotains these methods below and also works with a tester I will be providing below
Data : double height, width
Constructors:
One no-arg constructor that constructs a rectangle 1X1
One one-arg constructor that takes one double argument, and constructs a square that size
One two-arg constructor that takes two double arguments (height and width, in that order) , and constructs a rectangle that size
One one-arg constructor that takes a Rectangle as an argument, and constructs as new Rectangle of identical size.
Other Methods:
getters and setters for width and height using standard conventions.
a ".equals()" method that accepts another Rectangle as argument, and returns TRUE if the other rectangle has an area within 1% of the instance rectangle.
A ".rotate()" method that reverses height and width of the instance rectangle. (void)
A .getArea()" method that returns the area of the rectangle as a double.
A ".getDiagonal()" method that returns the diagonal of the rectangleas a double.
A ".copy()" method that returns a Rectangle of identical size.
A ".scale(double theScale)" method that accepts a single double argument, and scales the instance rectangle by that amount. For example, a 4X3 rectangle with .scale(2.0) applied to it should become 8X6 in size. Should be void.
A ".doubleIt()" method which should double the area of the rectangle, without changing its aspect ratio (the ratio between the height and width). Should be void.
You don't need to write it, but a .toString() method can make your testing program easier to write.
OPTIONAL :
A ".display(x,y,color)" (x and y should be double, but will need to be cast to int to do the actual delay. color should be a Color object) method that displays the rectangle at the location x,y with the specified color. The upper left corner of the rectangle should appear at pixel (x,y) in standard Java coordinates.
TESTER:
MY CODE THAT I KEEP GETTING AN ERRORS WHEN COMPILING
public class Rectangle {
private double width;
private double height;
private double x=0;
private double y=0;
public Rectangle(){
height=1;
width=1;
}
public Rectangle(double n){
width=n;
height=n;
}
public Rectangle(Rectangle q)
{
width=q.width;
height=q.height;
}
public Rectangle(double h,double w){
width=w;
height=h;
}
public double getHeight(){
return height;
}
public double getWidth(){
return width;
}
public void setHeight(double x)
{
height=x;
}
public void setWidth(double x)
{
width=x;
}
public double getArea(){
return width*height;
}
public double getDiagonal(){
return Math.sqrt(width*width+height*height);
}
public void rotate(){
double var =width;
width=height;
height=var;
}
public void doubleIt(){
double m=Math.sqrt(2.0);
width=width*m;
height=height*m;
}
public boolean equals(Rectangle r){
if (r.getHeight()==height && r.getWidth()==width) return true;
return false;
}
public Rectangle copy(){
Rectangle r= new Rectangle(height, width);
return r;
}
public void scale(double theScale){
width=width*theScale;
height=height*theScale;
}
}
Explanation / Answer
Sample programm
#include<iostream>
using namespace std;
int main()
{
int width,height,area,perimeter;
cout<<"Enter Width of Rectangle = ";
cin>>width;
cout<<"Enter Height of Rectangle = ";
cin>>height;
area=height*width;
cout<<"Area of Rectangle ="<<area<<endl;
perimeter=2*(height+width);
cout<<" Perimeter of rectangle are = "<<perimeter<<endl;
return 0;
}
PROGRAMMING