Question: In this lab, you will create a programmer-defined class and then use it in a Java program. The program should create twoRectangleobjects and find their
In this lab, you will create a programmer-defined class and then use it in a Java program. The program should create twoRectangleobjects and find their area and perimeter.
Instructions
- Make sure the class file namedRectangle.javais open.
- In theRectangleclass, create two private attributes namedlengthandwidth. Both length and width should be data typedouble.
- Write publicsetmethods to set the values forlengthandwidth.
- Write publicgetmethods to retrieve the values forlengthandwidth.
- Write a publiccalculateArea()method and a publiccalculatePerimeter()method to calculate and return the area of the rectangle and the perimeter of the rectangle.
- Open the file namedMyRectangleClassProgram.java.
- In theMyRectangleClassProgramclass, create twoRectangleobjects namedrectangle1andrectangle2.
- Set thelengthofrectangle1to10.0and thewidthto5.0. Set the length ofrectangle2to7.0and thewidthto3.0.
- Print the value ofrectangle1's perimeter and area, and then print the value ofrectangle2's perimeter and area.
- Execute the program.
MyRectangleClassProgram.java
//Thisprogramusestheprogrammer-definedRectangleclass. publicclassMyRectangleClassProgram { publicstaticvoidmain(Stringargs[]) { //Createrectangle1andrectangle2objectshere. //Setthelengthofrectangle1to10.0here. //Setthewidthofrectangle1to5.0here. //Printtheareaandperimeterofrectangle1here. //Setthelengthofrectangle2to7.0here. //Setthewidthofrectangle2to3.0here. //Printtheareaandperimeterofrectangle2here. System.exit(0); } }
Rectangle.java
classRectangle { //Lengthofthisrectangle. //Widthofthisrectangle. //Writesetmethodshere. //Writegetmethodshere. //WritethecalculatePerimeter()and //calculateArea()methodshere. }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
