Question:
Modify the Rectangle and Box classes that are below, so that they implement the Comparable interface. The compareTo() method shall compare rectangles and boxes by areas and volumes.
In main(), create an ArrayList of rectangles and an ArrayList of boxes. Randomly generate ten rectangles and ten boxes adding to the two ArrayLists. Use Collections.sort() to sort the two ArrayLists, respectively. Print the ArrayLists before and after being sorted.
Three classes that need to be edited are below: Main class, Rectangle class, and Box class.
Main Class:
import java.util.Random;
public class Main{
public static void main(String[] args){
// make an array of 100 boxes of random size
Box[] boxes = new Box[100];
Random randomNum = new Random();
int max = 100;
for (int i = 0; i < 100; i++){
int l = randomNum.nextInt(max);
int w = randomNum.nextInt(max);
int h = randomNum.nextInt(max);
boxes[i] = new Box(l, w, h);
}
int max_volume = 0;
Box hasMaxVolume = null;
for (Box box : boxes){
if (max_volume < box.getVolume()){
max_volume = box.getVolume();
hasMaxVolume = box;
}
}
// find the one of biggest volume and print the info of this box
System.out.println();
System.out.println("The box with the biggest volume has the volume of: " + hasMaxVolume.getVolume() + "in^3");
System.out.println();
hasMaxVolume.print();
System.out.println();
}
}
Rectangle class:
public class Rectangle {
private int l;
private int w;
public Rectangle(int l, int w){
super();
this.l = l;
this.w = w;
}
public Rectangle(int l){
super();
this.l = l;
this.w = l;
}
public Rectangle(){
super();
}
public int getL(){
return l;
}
public void setL(int l){
this.l = l;
}
public int getW(){
return w;
}
public void setW(int w){
this.w = w;
}
// method to calculate area
public int getArea(){
return l * w;
}
// method to print l and w
public void print(){
System.out.print("\"Info of this box is\" " + "Width:" + w + "in" + " Length:" + l + "in");
}
}
Box class:
public class Box extends Rectangle {
private int h;
public Box(int l, int w, int h){
super(l, w);
this.h = h;
}
public Box(){
super();
}
public Box(int l, int w){
super(l, w);
}
public Box(int l) {
super(l);
}
// method to calculate volume
public int getVolume() {
return getArea() * h;
}
public int getH() {
return h;
}
public void setH(int h) {
this.h = h;
}
//method to print height
public void print() {
super.print();
System.out.print(" Height:" + h + "in");
}
}
import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;
public class Main {
public static void main(String[] args)
{
// make an array of 100 boxes of random size
ArrayList<Box>
boxes = new ArrayList<>();
ArrayList<Rectangle>
rectangles = new ArrayList<>();
Random randomNum =
new Random();
int max =
100;
for (int i = 0; i
< 100; i++) {
int
l = randomNum.nextInt(max);
int
w = randomNum.nextInt(max);
int
h = randomNum.nextInt(max);
boxes.add(new
Box(l, w, h));
l
= randomNum.nextInt(max);
w
= randomNum.nextInt(max);
rectangles.add(new
Rectangle(l, w));
}
System.out.println("Boxes
before sorting:");
for(Box b: boxes)
{
System.out.println(b);
}
System.out.println("\nBoxes
After sorting:");
Collections.sort(boxes);
for(Box b: boxes)
{
System.out.println(b);
}
System.out.println("Rectangles
before sorting:");
for(Rectangle b:
rectangles) {
System.out.println(b);
}
System.out.println("\nRectangles
After sorting:");
Collections.sort(boxes);
for(Rectangle b:
rectangles) {
System.out.println(b);
}
}
}
class Rectangle implements Comparable<Rectangle> {
private int l;
private int w;
public Rectangle(int l, int w) {
super();
this.l = l;
this.w = w;
}
public Rectangle(int l) {
super();
this.l = l;
this.w = l;
}
public Rectangle() {
super();
}
public int getL() {
return l;
}
public void setL(int l) {
this.l = l;
}
public int getW() {
return w;
}
public void setW(int w) {
this.w = w;
}
// method to calculate area
public int getArea() {
return l * w;
}
// method to print l and w
public String toString() {
return "\"Info of
this box is\" " + "Width:" + w + "in" + " Length:" + l +
"in";
}
@Override
public int compareTo(Rectangle o) {
return getArea() -
o.getArea();
}
}
class Box extends Rectangle {
private int h;
public Box(int l, int w, int h) {
super(l, w);
this.h = h;
}
public Box() {
super();
}
public Box(int l, int w) {
super(l, w);
}
public Box(int l) {
super(l);
}
// method to calculate volume
public int getVolume() {
return getArea() *
h;
}
public int getH() {
return h;
}
public void setH(int h) {
this.h = h;
}
public String toString() {
return
super.toString() + " Height:" + h + "in";
}
@Override
public int compareTo(Rectangle o) {
if (o instanceof
Box) {
return
getVolume() - ((Box) o).getVolume();
}
return
super.compareTo(o);
}
}
**************************************************
Thanks for your question. We try our best to help you with detailed
answers, But in any case, if you need any modification or have a
query/issue with respect to above answer, Please ask that in the
comment section. We will surely try to address your query ASAP and
resolve the issue.
Please consider providing a thumbs up to this question if it helps you. by Doing that, You will help other students, who are facing similar issue.
Question: Modify the Rectangle and Box classes that are below, so that they implement the Comparable...
I need help with a java error Question: Consider a graphics system that has classes for various figures—say, rectangles, boxes, triangles, circles, and so on. For example, a rectangle might have data members’ height, width, and center point, while a box and circle might have only a center point and an edge length or radius, respectively. In a well-designed system, these would be derived from a common class, Figure. You are to implement such a system. The class Figure is...
I need help with a java error Question: Consider a graphics system that has classes for various figures—say, rectangles, boxes, triangles, circles, and so on. For example, a rectangle might have data members’ height, width, and center point, while a box and circle might have only a center point and an edge length or radius, respectively. In a well-designed system, these would be derived from a common class, Figure. You are to implement such a system. The class Figure is...
My assignment is to create a rectangle class in java. •Create a MyRectangleClass •There are two private instance variables, length l and width w with double data type. •There is a constructor with two parameter •There are 4 public methods which are circumference, area, getWidth, and getLength. •circumference method will return circumference to the caller.(the formula for circumference is 2 *l + 2*w ) •Area method Return the area to the caller(:l*w) •getLength method will return length to the caller....
Why are obj1 and obj2 printing the same areas? I'm trying to learn about Comparable interface. Couldn't figure it out. the compare to method should print 0, 1, or -1 import java.util.*; public class RectangleMain { public static void main(String [] args) throws CloneNotSupportedException { /*ComparableRectangleAlsoCloneable obj1 = new ComparableRectangleAlsoCloneable(4, 5); ComparableRectangleAlsoCloneable obj2 = (ComparableRectangleAlsoCloneable)obj1.clone(); System.out.println(obj1.toString()); System.out.println(obj1 == obj2); //false System.out.println(obj2.toString());*/ Scanner...
JAVA Modify the code to create a class called box, wherein you find the area. I have the code in rectangle and needs to change it to box. Here is my code. Rectangle.java public class Rectangle { private int length; private int width; public void setRectangle(int length, int width) { this.length = length; this.width = width; } public int area() { return this.length * this.width; } } // CONSOLE / MAIN import java.awt.Rectangle; import java.util.Scanner; public class Console...
In Java Create a testing class that does the following to the given codes below: To demonstrate polymorphism do the following: Create an arraylist to hold 4 base class objects Populate the arraylist with one object of each data type Code a loop that will process each element of the arraylist Call the first ‘common functionality’ method Call the second ‘common functionality’ method Call the third ‘common functionality’ method Verify that each of these method calls produces unique results Call...
Create a Java Project in Eclipse with the following: Include the Rectangle class supplied below. Override the toString method for Rectangle. Override the equals method for Rectangle. Implement the comparable Interface for Rectangle (Compare by area) Rectangle class: public class Rectangle { private double length; private double width; public Rectangle(double l, double w) { length = l; width = w; } public double getLength() { ...
Consider the Rectangle2 java class definition below. Write the definition of an equals) method that checks if two Rectangle objects have the same dimensions Write a Java program to test the equals method public class Rectangle2 K private int width, length; public Rectangle2(int w, int 1) { setWidth(w); setLength(1); System.out.println("Inside parameterized!!!"); } public void setWidth(int w) { width = w;} public void setLength(int i) { length = 1;} int getWidth() { return width; } int getLength() { return length; }...
package rectangle; public class Rectangle { private int height; private int width; public Rectangle(int aHeight, int aWidth) { super(); height = aHeight; width = aWidth; } public int getHeight() { return height; } public int getWidth() { return width; } public void setHeight(int aHeight) { height = aHeight; } public void setWidth(int aWidth) { width = aWidth; } public int...
I have to create a java graphics program which will draw 10 rectangles and 10 ellipses on the screen. These shapes are to be stored in an arrayList. I have to do this using 6 different class files. I am not sure whether I successfully created an ellipse in the Oval Class & also need help completing the print Class. I need someone to check whether my Oval Class is correct (it successfully creates an ellipse with x, y, width,...