Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

All of the sub-parts need to be answered correctly. You must: Comment and Format and Must make sure about indenting. If you don't follow these

All of the sub-parts need to be answered correctly. You must: Comment and Format and Must make sure about indenting. If you don't follow these steps, it will be flagged for review. MUST BE IN JAVA FORMAT. IT IS A MUST. Please also check the output very carefully.

1 (a) Write a java class definition for a rectangle object. The object should be capable of setting its length and width and computing its area. Use this to create two rectangle objects of dimensions 10 X 30 and 20 X 25, respectively. Print their areas.

Here is the Java class file (Rectangle.java). Compile it.

public class Rectangle{

private int length;

private int width;

public Rectangle(){

}

public void setLength(int l){

length = l;

}

public void setWidth(int w){

width = w;

}

public int getLength(){

return length;

}

public int getWidth(){

return width;

}

public int findArea(){

return length*width;

}

}

Now you can write a tester program called RectangleDemo.java to create and use Rectangle objects. Compile and run it.

Warning! If you cut and paste this code, it may result in errors (for example, the double quotes may not be compiled properly).

public class RectangleDemo{

public static void main(String[] args){

Rectangle rect1, rect2;

rect1 = new Rectangle();

rect2 = new Rectangle();

rect1.setLength(10);

rect1.setWidth(30);

rect2.setLength(20);

rect2.setWidth(25);

System.out.println("Area of the first rectangle: " +

rect1.findArea());

System.out.println("Area of the second rectangle: " +

rect2.findArea());

}

}

1 (b) Now make the following changes:

In the Rectangle class, use the keyword this in the setLength and setWidth methods

In the Rectangle class, add another constructor that accepts the length and the width and sets the attributes.

In the Rectangle class, add a toString method to print the length and width of the rectangle like this:

Length: 10 Width: 30

Modify the RectangleDemo program to read user given length and width from the keyboard, create the Rectangle object and print its dimensions (using the toString method) and the area (using the findArea method):

Enter length and width: 45 60

Length: 45 Width: 60

Area: 2700

All of the sub-parts need to be answered correctly. You must: Comment and Format and Must make sure about indenting. If you don't follow these steps, it will be flagged for review. MUST BE IN JAVA FORMAT. IT IS A MUST. Please also check the output very carefully.

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

4. How is culture a contested site?

Answered: 1 week ago