Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Creating a Programmer-Defined Class in Java: In this lab, you will create a programmer-defined class and then use it in a Java program. The program

Creating a Programmer-Defined Class in Java:

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 lengthof rectangle1 to 10.0 and the width to 5.0. Set the length of rectangle2 to 7.0 and the width to 3.0.

Print the value of rectangle1s perimeter and area, and then print the value of rectangle2s perimeter and area.

Execute the program.

task:

Correct output for Rectangle 2

0 out of 1 checks passed. Review the results below for more details.

Checks

Test CaseIncomplete

Correct output for Rectangle 2

Input

 

Output

Rectangle 1's perimeter is: 40.0 Rectangle 1's area is: 50.0 ------------------------------------------ Rectangle 2's perimeter is: 28.0 Rectangle 2's area is: 21.0 

Results

Rectangle 2's perimeter is: 20

Rectangle 2's area is: 21

5.00 out of 10.00

Correct output for Rectangle 1

0 out of 1 checks passed. Review the results below for more details.

Checks

Test CaseIncomplete

Correct output for Rectangle 1

Input

 

Output

Rectangle 1's perimeter is: 40.0 Rectangle 1's area is: 50.0 ------------------------------------------ Rectangle 2's perimeter is: 28.0 Rectangle 2's area is: 21.0 

Results

Rectangle 1's perimeter is: 30

Rectangle 1's area is: 50

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.

Rectangle rectangle1 = new Rectangle();

Rectangle rectangle2 = new Rectangle();

// Set the length of rectangle1 to 10.0 here.

rectangle1.setLength(10.0);

// Set the width of rectangle1 to 5.0 here.

rectangle1.setWidth(5.0);

// Print the area and perimeter of rectangle1 here.

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

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

// Set the length of rectangle2 to 7.0 here.

rectangle2.setLength(7.0);

// Set the width of rectangle2 to 3.0 here.

rectangle2.setWidth(3.0);

// Print the area and perimeter of rectangle2 here.

System.out.println("------------------------------------------");

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

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

System.exit(0);

}

}

Rectangle.java:

//Rectangle.java

class Rectangle

{

private double length; // Length of this rectangle

private double width; // Width of this rectangle

//Write set methods here.

public void setLength(double len)

{

length = len;

}

//Write set methods here.

public void setWidth(double wid)

{

width = wid;

}

//Write get methods here.

public double getLength()

{

return length;

}

//Write get methods here.

public double getWidth()

{

return width;

}

//Write the calculatePerimeter() and

public double calculatePerimeter()

{

return (2 * (length + length));

}

//calculateArea() methods here.

public double calculateArea()

{

return(length * width);

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

50 Tips And Tricks For MongoDB Developers Get The Most Out Of Your Database

Authors: Kristina Chodorow

1st Edition

1449304613, 978-1449304614

More Books

Students also viewed these Databases questions