Hello, I\'m stuck on an assignment. Here is the question. Design a class named R
ID: 3629803 • Letter: H
Question
Hello, I'm stuck on an assignment.Here is the question. Design a class named Rectangle to represent a rectangle. The class contains:
*Two double data fields named with and height that specify the width and height of the rectangle. The default values are 1 for width and height.
*A no-arg constructor that creates a default rectangle
*A method named get Area() that returns the area of this rectangle
*A method named getPerimeter() that returns the perimeter.
Write a test program that creates two Rectangle objects - one with width 4 and height 40 and the order with width 3.5 and height 35.9 Display the width, height, area, and perimeter of each rectangle in this order.
So far I have
class Rectangle
{
double perimeter = 1.0
Rectangle()
{
Rectangle(double newPerimeter)
{
perimeter= newPerimeter;
double getPerimeter()
{
return width * height
}
}
As you can see the program is incomplete. How do I fit in two Rectangle objects and where does my Area method go. I'm not sure how to organize the code. Thanks in advance for helping.
Explanation / Answer
please rate - thanks
public class rectangleTest
{public static void main(String[] args)
{Rectangle a=new Rectangle(4,40);
Rectangle b=new Rectangle(3.5,35.9);
System.out.println("Rectangle a, width="+a.getWidth()+" height="+a.getWidth());
System.out.println("Area="+a.getArea()+" perimeter="+a.getPerimeter());
System.out.println("Rectangle b, width="+b.getWidth()+" height="+b.getWidth());
System.out.println("Area="+b.getArea()+" perimeter="+b.getPerimeter());
}
}
----------------------------------
public class Rectangle
{private double width=0;
private double height=0;
public Rectangle(double w,double h )
{width=w;
height=h;
}
public void setWidth(double w)
{width=w;
}
public double getWidth()
{return width;
}
public void setHeight(double h)
{height=h;
}
public double getHeight()
{return height;
}
public double getPerimeter()
{return 2*(height+width);
}
public double getArea()
{return height*width;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.