Question

JAVA Modify the following code to: // Create a class called box. // Create three rectangles....

JAVA

Modify the following code to:

// Create a class called box.
// Create three rectangles.
// Your working constructor is going to take length, width, and height.
// Create an overload constructor that will take the length and width and a height of 1
// You will call this and pass it the length and the width
// and the third parameter would be 1.
// Then create a default constructor which sets everything to 1.

HERE IS THE CODE THAT I HAVE:

// Rectangle.java

public class Rectangle
{
private int length;
private int width;
  
public Rectangle(int length, int width)
{
this.setRectangle(length, width);
}
public Rectangle()
{
this(1,1);
}
public void setRectangle(int length, int width)
{
this.length = length;
this.width = width;
}
public int area()
{
return this.length * this.width;
}
}

// MAIN.java

package console;

import java.awt.Rectangle;
import java.util.Scanner;

public class Console
{
public static void main (String[] args)
{
Scanner in = new Scanner(System.in);
  
Rectangle r1 = new Rectangle(4,3);
Rectangle r2 = new Rectangle(6,5);
  
System.out.println("The area of r1 is " + r1.area());
System.out.println("The area of r2 is " + r2.area());
  
  
}
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Rectangle.java

public class Rectangle {
private int length, width, height;
  
public Rectangle()
{
this.length = 1;
this.width = 1;
this.height = 1;
}
  
public Rectangle(int length, int width)
{
this.length = length;
this.width = width;
this.height = 1;
}

public int getLength() {
return length;
}

public int getWidth() {
return width;
}

public int getHeight() {
return height;
}
  
public int area()
{
return (getLength() * getWidth() * getHeight());
}
  
@Override
public String toString()
{
return ("Length: " + getLength() + " units, Width: " + getWidth()
+ " units, Height: " + getHeight() + " unit, Area: " + area() + " sq. units");
}
}

Box.java (Main class)

import java.util.Scanner;

public class Box {
  
public static void main(String[]args)
{
Scanner sc = new Scanner(System.in);
  
System.out.println("RECTANGLE 1:\n------------");
System.out.print("Enter length: ");
int len1 = Integer.parseInt(sc.nextLine().trim());
System.out.print("Enter width: ");
int wid1 = Integer.parseInt(sc.nextLine().trim());
Rectangle rect1 = new Rectangle(len1, wid1);
  
System.out.println("\nRECTANGLE 2:\n------------");
System.out.print("Enter length: ");
int len2 = Integer.parseInt(sc.nextLine().trim());
System.out.print("Enter width: ");
int wid2 = Integer.parseInt(sc.nextLine().trim());
Rectangle rect2 = new Rectangle(len2, wid2);
  
System.out.println("\nRECTANGLE 3:\n------------");
System.out.print("Enter length: ");
int len3 = Integer.parseInt(sc.nextLine().trim());
System.out.print("Enter width: ");
int wid3 = Integer.parseInt(sc.nextLine().trim());
Rectangle rect3 = new Rectangle(len3, wid3);
  
System.out.println("\nRECTANGLE 1:\n" + rect1.toString() + "\n\nRECTANGLE 2:\n" + rect2.toString()
+ "\n\nRECTANGLE 3:\n" + rect3.toString() + "\n");
}
}

****************************************************************** SCREENSHOT *********************************************************

Add a comment
Know the answer?
Add Answer to:
JAVA Modify the following code to: // Create a class called box. // Create three rectangles....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT