In: Computer Science
Objectives: use Scite
Problem : The Rectangle class (Filename: TestRectangle.java)
Design a class named Rectangle to represent a rectangle.
The class contains:
Two double data fields named width and height that specify the width and height of the rectangle. The default values are 1 for both width and height.
A no-arg constructor that creates a default rectangle.
A constructor that creates a rectangle with the specified width and height.
A method named getArea() that returns the area of this rectangle.
A method named getPerimeter() that returns the perimeter.
Write a test program that creates two Rectangle objects. For the first object use the no-arg constructor. For the second object use the second constructor and set the width to 4 and height to 40. Display the width, height, area, and perimeter of each rectangle in this order.
The 2 java files are given below within their respective tables. You are requested to create 2 java files namely Rectangle.java and TestRectangle.java within your project and copy the codes given below in their respective tables and paste into the created files accordingly. Finally, run the driver file TestRectangle.java.
Rectangle.java |
|
TestRectangle.java |
|
Console Output
NOTE: All the java codes are same for any ide, hence you can use the codes to run in any of your desired Programming Environment / IDE. Just follow the IDE specific guidelines on how to create a Java Project and classes (graphically) and to run it. It's not possible to upload project files here, its not supported on this portal. We can help you with codes and pictures only. Kindly, comment your doubts within the comments section and please give a thumbs up if you liked the solution. Thanks.