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

Class Rectangle Create an object that stores information about a rectangle, leng

ID: 3594794 • Letter: C

Question

Class Rectangle Create an object that stores information about a rectangle, length and width. Includes a method that increases length by a number. Includes a method that increases width by a number. Includes another method that increases the length and width (both) by a certain percentage if the user answer is “yes” otherwise don’t increase the length and width. Class RectangleDemo Create one object Increase the length by 2. Increase the width by 1. Printout the object Ask the user if he wants to increase the length and width (both) of the rectangle. Then ask him, by how much percentage. Then printout the new length, width, area, and perimeter

Explanation / Answer

RectangleDemo.java

import java.util.Scanner;

public class RectangleDemo {

public static void main(String a[]) {

Rectangle r = new Rectangle(3,4);

r.increaseLength(2);

r.increaseWidth(1);

System.out.println(r);

Scanner scan = new Scanner(System.in);

System.out.println("Do you want to increase the length and width(yes or no) : ");

String choice = scan.next();

if(choice.equalsIgnoreCase("yes")) {

System.out.println("Enter the percentage too increase: ");

double percentage = scan.nextDouble();

r.increaseLengthAndWidth(percentage);

System.out.println(r);

}

}

}

Rectangle.java

public class Rectangle {

private double length , width;

public Rectangle(double length, double width) {

this.length = length;

this.width = width;

}

public void increaseLength(double l) {

this.length+=l;

}

public void increaseWidth(double w) {

this.width+=w;

}

public void increaseLengthAndWidth(double percentage) {

this.length+=(length * percentage)/100;

this.width+=(width * percentage)/100;

}

public String toString() {

return "Rectangle [length=" + length + ", width=" + width + "]";

}

}

Output:

Rectangle [length=5.0, width=5.0]
Do you want to increase the length and width(yes or no) :
yes
Enter the percentage too increase:
30
Rectangle [length=6.5, width=6.5]