Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Need to be answered correctly. You must: Comment and Format and Must make sure a

ID: 3877608 • Letter: N

Question

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. Must not provide the answer on a page. Use the Java compiler. Please provide the code in such a way so that it can be copied and checked in the Java compiler. Must provide the coding as Rectangle.java and RectangleDemo.java (Public class must be in Rectangle and another in RectangleDemo)

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. Must not provide the answer on a page. Use the Java compiler. Please provide the code in such a way so that it can be copied and checked in the Java compiler. Must provide the coding as Rectangle.java and RectangleDemo.java (Public class must be in Rectangle and another in RectangleDemo)

Exercise 1: This exercise reviews the basic concepts of object-oriented programming and uses the Rectangle class. Here is the Java class file (Rectangle.java). Compile it. public class Rectangle private int length; private int width; public Rectangle0 public void setLength(int I)i length = 1; public void setWidth(int w){ width w; public int getlength0) return length; public int getWidth0i return width; public int findArea0i return length*width; 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 Create 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 finda method) Enter length and width: 45 60 Length: 45Width: 60 Area: 2700

Explanation / Answer

Rectangle.java

public class Rectangle {

//Declaring instance variables
private int width;
private int length;

//Parameterized constructor
public Rectangle(int width, int length) {
this.width = width;
this.length = length;
}


//getters and setters
public int getWidth() {
return width;
}


public void setWidth(int width) {
this.width = width;
}


public int getLength() {
return length;
}


public void setLength(int length) {
this.length = length;
}

//Method which calculates the area of the triangle
public int findArea() {
return width * length;
}


@Override
public String toString() {
return "Length :" + getLength() + " " + "Width:" + getWidth();
}

}

___________________

RectangleDemo.java

import java.util.Scanner;

class RectangleDemo {
public static void main(String[] args) {
// Declaring variables
int width, length;

/*
* Creating an Scanner class object which is used to get the inputs
* entered by the user
*/
Scanner sc = new Scanner(System.in);

// Getting the input entered by the user
System.out.print("Enter the Length and Width :");
width = sc.nextInt();
length = sc.nextInt();

// CReating an Rectangle class object by passing user entered inputs as
// arguments
Rectangle r = new Rectangle(width, length);

// Displaying the output

System.out.println(r.toString());
System.out.println("Area:" + r.findArea());


}
}

_________________

Output:

Enter the Length and Width :45 60

Length :60 Width:45

Area:2700

_______________Could you plz rate me well.Thank You

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