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

This is what I have so far. public static void main(String[] args) { Scanner inp

ID: 3728228 • Letter: T

Question

This is what I have so far.

public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
double x = input.nextDouble();
double y = input.nextDouble();
Double rectangle = new Rectangle(x,y);
System.out.println("The area of a rectangle with width " + Rectangle.getwidth+ "and height "+ Rectangle.getheight+ "is " + Rectangle.getArea);

}
  
public class Rectangle
{
double width = 1.0;
double height = 1.0;
Rectangle()
{
}
  
Rectangle(double newWidth, double newHeight)
{
width = newWidth;
height = newHeight;
}
  
double getArea()
{
return width * height;
}
  
double getPerimeter()
{
return (width * height) * 2;
}
}

Question:

Modification read in two doubles width then height, use width and height to create the rectangle object, then use rectangle toString to output. The method toString in rectangle will override java.lang.Object.toString it will return a line with these two strings....

Explanation / Answer

Rectangle.java

public class Rectangle {

// Declaring instance variables

private double width;

private double height;

// Parameterized constructor

public Rectangle(double width, double height) {

super();

this.width = width;

this.height = height;

}

// getters and setters

public double getWidth() {

return width;

}

public void setWidth(double width) {

this.width = width;

}

public double getHeight() {

return height;

}

public void setHeight(double height) {

this.height = height;

}

// This method will calculate the area of the rectangle

public double calArea() {

return width * height;

}

// This method will calculate the perimter of the rectangle

public double calPerimeter() {

return 2 * (width + height);

}

// toString method is used to display the contents of an object inside it

@Override

public String toString() {

return "The area of a rectangle with width " + getWidth()

+ "and height " + getHeight() + " is " + calArea() + " "

+ "The perimeter is " + calPerimeter()

+ " with interior color white";

}

}

___________________

Test.java

import java.util.Scanner;

public class Test {

public static void main(String[] args) {

/*

* Creating an Scanner class object which is used to get the inputs

* entered by the user

*/

Scanner input = new Scanner(System.in);

//Getting the input entered by the user

double x = input.nextDouble();

double y = input.nextDouble();

//Creating an Instance of Rectangle class object

Rectangle rect = new Rectangle(x, y);

System.out.println(rect.toString());

}

}

____________________

Output:

23.2 55.2
The area of a rectangle with width 23.2and height 55.2 is 1280.64
The perimeter is 156.8 with interior color white

________________

Output#2:

23.222 5.3215
The area of a rectangle with width 23.222and height 5.3215 is 123.57587300000002
The perimeter is 57.087 with interior color white

__________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