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

  1. Make sure the class file namedRectangle.javais open.
  2. In theRectangleclass, create two private attributes namedlengthandwidth. Both length and width should be data typedouble.
  3. Write publicsetmethods to set the values forlengthandwidth.
  4. Write publicgetmethods to retrieve the values forlengthandwidth.
  5. Write a publiccalculateArea()method and a publiccalculatePerimeter()method to calculate and return the area of the rectangle and the perimeter of the rectangle.
  6. Open the file namedMyRectangleClassProgram.java.
  7. In theMyRectangleClassProgramclass, create twoRectangleobjects namedrectangle1andrectangle2.
  8. Set thelengthofrectangle1to10.0and thewidthto5.0. Set the length ofrectangle2to7.0and thewidthto3.0.
  9. Print the value ofrectangle1's perimeter and area, and then print the value ofrectangle2's perimeter and area.
  10. 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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!