Question

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

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.

  

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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.

Add a comment
Know the answer?
Add Answer to:
Creating a Programmer-Defined Class in Java Summary In this lab, you will create a programmer-defined class...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Creating a Class in C++ Summary In this lab, you create a programmer-defined class and then...

    Creating a Class in C++ Summary In this lab, you create a programmer-defined class and then use it in a C++ program. The program should create two Rectangle objects and find their area and perimeter. Instructions Ensure the class file named Rectangle.cpp is open in your editor. In the Rectangle class, create two private attributes named length and width. Bothlength and width should be data type double. Write public set methods to set the values for lengthand width. Write public...

  • JAVA design a class named Rectangle to represent a rectangle. The class contains: A method named getPerimeter() that returns the perimeter. Two double data fields named width and height that specify...

    JAVA design a class named Rectangle to represent a rectangle. The class contains: A method named getPerimeter() that returns the perimeter. 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. design...

  • ****Here is the assignment **** Purpose: To write an Object-Oriented application that creates a Java class...

    ****Here is the assignment **** Purpose: To write an Object-Oriented application that creates a Java class with several instance variables, a constructor to initialize the instance variables, several methods to access and update the instance variables’ values, along with other methods to perform calculations. Also, write a test class that instantiates the first class and tests the class’s constructor and methods. Details: Create a class called Rectangle containing the following: Two instance variables, An instance variable of type double used...

  • Creating a class Rectangle with attributes length and width.

    Create a class Rectangle with attributes length and width, each of which defaults to 1. Provide methods that calculate the rectangle's perimeter and area. It hasset and get methods for both lengthand width. Theset methodsshould verify that lengthand width are each floating-point numbers larger than 0.0 and less than 20.0. Write a program totest class Rectangle.Program should be written in Java .I need some with this problem so urgently----test is due tomorrow and will be similar tothe above question. I...

  • Consider the Rectangle2 java class definition below. Write the definition of an equals) method that checks...

    Consider the Rectangle2 java class definition below. Write the definition of an equals) method that checks if two Rectangle objects have the same dimensions Write a Java program to test the equals method public class Rectangle2 K private int width, length; public Rectangle2(int w, int 1) { setWidth(w); setLength(1); System.out.println("Inside parameterized!!!"); } public void setWidth(int w) { width = w;} public void setLength(int i) { length = 1;} int getWidth() { return width; } int getLength() { return length; }...

  • JAVA Modify the code to create a class called box, wherein you find the area. I...

    JAVA Modify the code to create a class called box, wherein you find the area. I have the code in rectangle and needs to change it to box. Here is my code. Rectangle.java public class Rectangle { private int length; private int width;       public void setRectangle(int length, int width) { this.length = length; this.width = width; } public int area() { return this.length * this.width; } } // CONSOLE / MAIN import java.awt.Rectangle; import java.util.Scanner; public class Console...

  • (NEED FULL CODE PLEASE)write a java program class to calculate and display the areas of 10 rectan...

    (NEED FULL CODE PLEASE)write a java program class to calculate and display the areas of 10 rectangles using arrays. * input validation sizes must be between 0-50 inches output to be displayed on the screen and send to an output file. *the output must appear in the following format. rectangle length width area 1    2.0    3.0    6.0 2 - - - - 10 display the rectangle with largest area. rectangle1 area= display the rectangle with smallest area. rectangle2 area=

  • Need solution in c++ that works on both Microsoft Visual 2017 and Xcode: Create a class...

    Need solution in c++ that works on both Microsoft Visual 2017 and Xcode: Create a class Rectangle with attributes length and width, each of which defaults to 1. Provide methods that calculate the rectangle's perimeter and area. It has set and get methods for both length and width. The set methods should verify that length and width are each floating-point number larger than 0.0 and less than 20.0. Write a program to test class Rectangle

  • What if you had to write a program that would keep track of a list of...

    What if you had to write a program that would keep track of a list of rectangles? This might be for a house painter to use in calculating square footage of walls that need paint, or for an advertising agency to keep track of the space available on billboards. The first step would be to define a class where one object represents one rectangle's length and width. Once we have class Rectangle, then we can make as many objects of...

  • Please write in Java Language Write an abstract class Shape with an attribute for the name...

    Please write in Java Language Write an abstract class Shape with an attribute for the name of the shape (you'll use this later to identify a particular shape). Include a constructor to set the name of the shape and an abstract calculateArea method that has no parameters and returns the area of the shape. Optionally, include a constructor that returns the name of the shape and its area (by calling the calculateArea method). Write a Rectangle class that inherits from...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT