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: 3814079 • 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 lengthand 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 lengthof rectangle1 to 10.0and the width to 5.0. Set the length of ectangle2 to 7.0 and the widthto 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

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.

  

}

_________________________

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

This is the complete rectangle program:

myrectangleclassprogram.java

import java.util.Scanner;

public class MYRECTANGLECLASSPROGRAM {

   public static void main(String args[])
   {
       Scanner sc = new Scanner(System.in);
       double length1,width1,length2,width2;
       System.out.println("Enter length of first rectanle");
       length1=sc.nextDouble();
       System.out.println("Enter width of first rectanle");
       width1=sc.nextDouble();
       System.out.println("Enter length of second rectanle");
       length2=sc.nextDouble();
       System.out.println("Enter width of second rectanle");
       width2=sc.nextDouble();
      
       RECTANGLE rec1 = new RECTANGLE(length1,width1);
       RECTANGLE rec2 = new RECTANGLE(length2,width2);
      
       System.out.println("Area of first rectanle= "+rec1.calculateArea());
       System.out.println("perimeter of first rectanle= "+rec1.calculatePerimeter());
       System.out.println("Area of second rectanle= "+rec2.calculateArea());
       System.out.println("perimeter of second rectanle= "+rec2.calculatePerimeter());
            
   }
}

Recatngle.java


public class RECTANGLE {

   private double length;
   private double width;
  
   RECTANGLE(double length, double width)
{
this.length = length;
this.width = width;
}


   public double getLength() {
       return length;
   }
   public void setLength(double length) {
       this.length = length;
   }
   public double getWidth() {
       return width;
   }
   public void setWidth(double width) {
       this.width = width;
   }
  
   public double calculateArea()
   {
       return length*width;
   }
  
   public double calculatePerimeter()
   {
       return 2 * (length + width);
   }
}

In this program, we create getter and setter methods of length and width and create a calculateArea method for calculating area using (length*width) formula and create a calculatePerimeter method for calculating perimeter using(2*(length+width)) formula.

Then in tester class, we create 2 rectangle class object and pass the both length and width value in this and using rectangle class object we get area and perimeter of a rectangle.

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