Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Creating a Programmer-Defined Class in Java Summary In this lab, you will create

ID: 3805738 • Letter: C

Question

Creating a Programmer-Defined Class in Java

Summary

In this lab, you will create a programmer-defined class and then use it in a Java program. The program should create two Rectangle objects and find their area and perimeter.

Instructions

Make sure the class file named Rectangle.java is open.

In the Rectangle class, create two private attributes named length and width. Both length and width should be data type double.

Write public set methods to set the values for length and width.

Write public get methods to retrieve the values for length and width.

Write a public calculateArea() method and a public calculatePerimeter() method to calculate and return the area of the rectangle and the perimeter of the rectangle.

Open the file named MyRectangleClassProgram.java.

In the MyRectangleClassProgram class, create two Rectangle objects named rectangle1 and rectangle2.

Set the length of rectangle1 to 10.0 and the width to 5.0. Set the length of ectangle2 to 7.0 and the width to 3.0.

Print the value of rectangle1’s perimeter and area, and then print the value of rectangle2’s perimeter and area.

Execute the program.

MyRectangleClassProgram.java

// This program uses the programmer-defined Rectangle class.

public class MyRectangleClassProgram

{

public static void main(String args[])

{

// Create rectangle1 and rectangle2 objects here.

  

// Set the length of rectangle1 to 10.0 here.

  

// Set the width of rectangle1 to 5.0 here.

  

// Print the area and perimeter of rectangle1 here.

  

// Set the length of rectangle2 to 7.0 here.

  

// Set the width of rectangle2 to 3.0 here.

// Print the area and perimeter of rectangle2 here.

  

System.exit(0);

}

}

MyRectangleClassProgramsST.java

// This program uses the programmer-defined Rectangle class.

public class MyRectangleClassProgram

{

public static void main(String args[])

{

  

Rectangle rectangle1 = new Rectangle();

Rectangle rectangle2 = new Rectangle();

rectangle1.setLength(10.0);

rectangle1.setWidth(5.0);

System.out.println("Rectangle 1's perimeter is: " + rectangle1.calculatePerimeter());

System.out.println("Rectangle 1's area is: " + rectangle1.calculateArea());

rectangle2.setLength(7.0);

rectangle2.setWidth(3.0);

  

System.exit(0);

}

}

Rectangle.java

class Rectangle

{

// Length of this rectangle.

// Width of this rectangle.

// Write set methods here.

// Write get methods here.

// Write the calculatePerimeter() and

// calculateArea() methods here.

}

Explanation / Answer

Here is the working solution-

//Rectangle.java
public class Rectangle {
  
   private double length;
   private double width;
  
   public Rectangle(){
       // default constructor
   }
  
   /**
   * @return the length
   */
   public double getLength() {
       return length;
   }
   /**
   * @param length the length to set
   */
   public void setLength(double length) {
       this.length = length;
   }
   /**
   * @return the width
   */
   public double getWidth() {
       return width;
   }
   /**
   * @param width the width to set
   */
   public void setWidth(double width) {
       this.width = width;
   }
  
   /**method to calculate area of rectangle
   *
   * @return
   */
   public double calculateArea(){
       return length*width;
   }
     
   /**method to calculate perimeter of rectangle
   *
   * @return
   */
   public double calculatePerimeter()
   {
   return (2 * (length + width));
   }
  
}

//

// MyRectangleClassProgram.java (Driver class to test the program)
public class MyRectangleClassProgram {
  
   public static void main(String args[])
   {
  
   Rectangle rectangle1 = new Rectangle(); //creating rectangle objects
   Rectangle rectangle2 = new Rectangle();
     
   rectangle1.setLength(10.0);   // setting length of rectangle1 object
   rectangle1.setWidth(5.0);       // setting width of rectangle1 object
  
   //displaying rectangle 1's perimeter
   System.out.println("Rectangle 1's perimeter is: " + rectangle1.calculatePerimeter());
   //displaying rectangle 1's area
   System.out.println("Rectangle 1's area is: " + rectangle1.calculateArea());
     
   rectangle2.setLength(7.0);   // setting length of rectangle2 object
   rectangle2.setWidth(3.0);       // setting length of rectangle2 object
  
   //displaying rectangle 1's perimeter
   System.out.println("Rectangle 1's perimeter is: " + rectangle2.calculatePerimeter());
   //displaying rectangle 1's area
   System.out.println("Rectangle 2's area is: " + rectangle2.calculateArea());
     
     
   }

}

//output of the program-

Rectangle 1's perimeter is: 30.0
Rectangle 1's area is: 50.0
Rectangle 1's perimeter is: 20.0
Rectangle 2's area is: 21.0

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote