Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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 lengthand 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 MyRectangleClassProgramclass, create two Rectangleobjects named rectangle1and 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.

// 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); } }

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

More Books

Students also viewed these Databases questions

Question

3. What strategies might you use?

Answered: 1 week ago

Question

3. Write a policy statement to address these issues.

Answered: 1 week ago