Question

In: Computer Science

in java language. There are three classes for the project, and two inner classes defined in...

in java language.

There are three classes for the project, and two inner classes defined in the Cuboid class.

Create the following classes:

•   Shape. It’s an abstract class with two abstract methods, area and perimeter.
•   Rectangle. It’s a concrete class that extends Shape. Implement area() and perimeter(). Implement the compareTo(object) method to sort rectangles by area in ascending order.
•   Cuboid. It’s a concrete class that extends Rectangle. A cuboid is a 3D rectangle. The shape has a third dimension, Depth. Override the area() method to compute the surface area of the object and implement new method volume(). The perimeter method is invalid in this context. The best way to handle this is to throw an exception called “UnsupportedOperationException”. If perimeter() is called, throw this exception.
•   SortByArea and SortByVolume are two classes defined as attributes of the Cuboid class. If you want to sort, the easiest way is to implement the compareto() method. If you want to sort more than one way, you must implement multiple comparators. Implement these methods so they are sorting by area in ascending order and volume in ascending order.


Complete the following unit tests:
•   Rectangle
o   Test Construction
o   Test getter / setter methods
o   Test area()
o   Test perimeter
o   Test compareTo(Object), ensuring rectangles are being sorted correctly (2x2 sorts < 4x6, for example)
•   Cuboid
o   Test Construction
o   Test getter / setter methods.
o   Test area()
o   Test perimeter
o   Test SortByArea
o   Test SortByVolume

Solutions

Expert Solution

Shape.java:

public abstract class Shape {
    abstract void area();
    abstract void perimeter();
}

Rectangle.java:

public class Rectangle extends Shape implements Comparable<Rectangle>{
   private float length;
    private float width;
    private float area;
    private float perimeter;

    public Rectangle(){
        this.length = 0;
        this.width = 0;
    }

    public Rectangle(float length, float width){
        this.length = length;
        this.width = width;
    }

    public float getLength() {
        return length;
    }

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

    public float getWidth() {
        return width;
    }

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

    public float getArea() {
        return area;
    }

    public void setArea(float area) {
        this.area = area;
    }

    public float getPerimeter() {
        return perimeter;
    }

    public void setPerimeter(float perimeter) {
        this.perimeter = perimeter;
    }

    @Override
    void area() {
       this.area = this.length * this.width;
    }

    @Override
    void perimeter() {
        this.perimeter = 2 * (this.length + this.width);
    }

    @Override
    public int compareTo(Rectangle rectangle) {
        return (int) (this.area - rectangle.area);
    }
}

Cuboid.java:

import java.util.Comparator;
import java.util.List;

public class Cuboid extends Rectangle{
    private float length;
    private float width;
    private float depth; // depth is nothing but height of the cuboid.

    private float surfaceArea;
    private float volume;

    @Override
    public float getLength() {
        return length;
    }

    @Override
    public void setLength(float length) {
        this.length = length;
    }

    @Override
    public float getWidth() {
        return width;
    }

    @Override
    public void setWidth(float width) {
        this.width = width;
    }

    public float getDepth() {
        return depth;
    }

    public void setDepth(float depth) {
        this.depth = depth;
    }

    public float getSurfaceArea() {
        return surfaceArea;
    }

    public void setSurfaceArea(float surfaceArea) {
        this.surfaceArea = surfaceArea;
    }

    public float getVolume() {
        return volume;
    }

    public void setVolume(float volume) {
        this.volume = volume;
    }

    public Cuboid(){
        super();
        this.length = 0;
        this.width = 0;
        this.depth = 0;
    }

    public Cuboid(float length, float width, float depth) {
        this.length = length;
        this.width = width;
        this.depth = depth;
    }

    @Override
    void area() {
        this.surfaceArea = this.length * this.width * this.depth;
    }

    @Override
    void perimeter() {
        throw new UnsupportedOperationException("Perimeter is not in context of cuboid");
    }

    void volume(){
        this.volume = (2 * length * width + 2 * width * depth + 2 * length * depth);
    }

    @Override
    public String toString() {
        return "Cuboid{" +
                "length=" + length +
                ", width=" + width +
                ", depth=" + depth +
                ", surfaceArea=" + surfaceArea +
                ", volume=" + volume +
                '}';
    }
}
SortByArea.java:
import java.util.Comparator;

public class SortByArea implements Comparator<Cuboid> {

    @Override
    public int compare(Cuboid o1, Cuboid o2) {
        return (int) (o1.getSurfaceArea() - o2.getSurfaceArea());
    }
}
SortByVolume.java:
import java.util.Comparator;

public class SortByVolume implements Comparator<Cuboid> {

    @Override
    public int compare(Cuboid o1, Cuboid o2) {
        return (int) (o1.getVolume() - o2.getVolume());
    }
}
RectangleTest.java:
import org.junit.Test;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;


public class RectangleTest {
    @Test
    public void testRectangleContruction(){
        Rectangle rectangle = new Rectangle(2,2);
        assertEquals(2, rectangle.getWidth(),0);
        assertEquals(2, rectangle.getLength(),0);

    }

    @Test
    public void testGettersAndSetters(){
        Rectangle rectangle = new Rectangle();
        rectangle.setLength(2);
        rectangle.setWidth(4);

        assertEquals(2,rectangle.getLength(),0);
        assertEquals(4,rectangle.getWidth(),0);
    }

    @Test
    public void testArea(){
        Rectangle rectangle = new Rectangle(2,2);
        rectangle.area();
        assertEquals(4,rectangle.getArea(),0);
    }

    @Test
    public void testPerimeter(){
        Rectangle rectangle = new Rectangle(2,2);
        rectangle.perimeter();
        assertEquals(8,rectangle.getPerimeter(),0);
    }

    @Test
    public void testCompareTo(){
        Rectangle rectangle1 = new Rectangle(4,6);
        rectangle1.area();
        Rectangle rectangle2 = new Rectangle(2,2);
        rectangle2.area();

        List<Rectangle> rectangles = new ArrayList<>();
        rectangles.add(rectangle1);
        rectangles.add(rectangle2);

        Collections.sort(rectangles);

        assertThat(rectangles.get(0).getLength(), is(2.0F));
        assertThat(rectangles.get(0).getWidth(), is(2.0F));

        assertThat(rectangles.get(1).getLength(), is(4.0F));
        assertThat(rectangles.get(1).getWidth(), is(6.0F));

    }

}
CuboidTest.java:
import org.junit.Test;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import static org.junit.Assert.assertThat;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertTrue;

public class CuboidTest {
    @Test
    public void testConstructor(){
        Cuboid cuboid1 = new Cuboid(1,4,3);
        cuboid1.volume();
        assertThat(cuboid1.getLength(),is(1.0F));
        assertThat(cuboid1.getWidth(),is(4.0F));
        assertThat(cuboid1.getDepth(),is(3.0F));
    }

    @Test
    public void testGettersAndSetters(){
        Cuboid cuboid = new Cuboid();
        cuboid.setLength(1.0F);
        cuboid.setWidth(4.0F);
        cuboid.setDepth(3.0F);

        assertThat(cuboid.getLength(),is(1.0F));
        assertThat(cuboid.getWidth(),is(4.0F));
        assertThat(cuboid.getDepth(),is(3.0F));
    }

    @Test
    public void testArea(){
        Cuboid cuboid  = new Cuboid(1,4,3);
        cuboid.area();

        assertThat(cuboid.getSurfaceArea(),is(12.0F));
    }

    @Test
    public void testPerimeter(){
        Cuboid cuboid  = new Cuboid(1,4,3);
        try{
            cuboid.perimeter();
        } catch (UnsupportedOperationException ex){
            String expectedMessage = "Perimeter is not in context of cuboid";
            String actualMessage = ex.getMessage();

            assertTrue(actualMessage.contains(expectedMessage));
        }

    }

    @Test
    public void testSortByArea(){
        List<Cuboid> cuboids = new ArrayList<>();
        Cuboid cuboid1 = new Cuboid(1,4,3);
        cuboid1.volume();
        cuboid1.area();

        Cuboid cuboid2 = new Cuboid(1,3,3);
        cuboid2.volume();
        cuboid2.area();

        cuboids.add(cuboid1);
        cuboids.add(cuboid2);


        Collections.sort(cuboids,new SortByArea());

        assertThat(cuboids.get(0).getLength(),is(1.0F));
        assertThat(cuboids.get(0).getWidth(),is(3.0F));
        assertThat(cuboids.get(0).getDepth(),is(3.0F));

    }

    @Test
    public void testSortByVolume(){
        List<Cuboid> cuboids = new ArrayList<>();
        Cuboid cuboid1 = new Cuboid(1,4,3);
        cuboid1.volume();
        cuboid1.area();

        Cuboid cuboid2 = new Cuboid(1,3,3);
        cuboid2.volume();
        cuboid2.area();

        cuboids.add(cuboid1);
        cuboids.add(cuboid2);


        Collections.sort(cuboids,new SortByVolume());

        assertThat(cuboids.get(0).getLength(),is(1.0F));
        assertThat(cuboids.get(0).getWidth(),is(3.0F));
        assertThat(cuboids.get(0).getDepth(),is(3.0F));

    }
}

Runner.java:

This is extra class not per your requirement just to illustrate things how they are working.

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

public class Runner {
    public static void main(String args[]){
        List<Cuboid> cuboids = new ArrayList<>();
        Cuboid cuboid1 = new Cuboid(1,4,3);
        cuboid1.volume();
        cuboid1.area();

        Cuboid cuboid2 = new Cuboid(1,3,3);
        cuboid2.volume();
        cuboid2.area();

        Cuboid cuboid3 = new Cuboid(1,2,3);
        cuboid3.volume();
        cuboid3.area();
        cuboids.add(cuboid1);
        cuboids.add(cuboid2);
        cuboids.add(cuboid3);

        System.out.println("List of cuboids before sorting");
        for (Cuboid cuboid : cuboids){
            System.out.println(cuboid);
        }

        Collections.sort(cuboids,new SortByVolume());

        System.out.println("List of cuboids after sorting");
        for (Cuboid cuboid : cuboids){
            System.out.println(cuboid);
        }
    }
}

Output of Runner:

Please go trhought the code its self explanatory with the names


Related Solutions

JAVA LANGUAGE Required Tasks: In Eclipse, create a Java Project as CS-176L-Assign5. Create 3 Java Classes...
JAVA LANGUAGE Required Tasks: In Eclipse, create a Java Project as CS-176L-Assign5. Create 3 Java Classes each in its own file Student.java, StudentList.java, and Assign5Test.java. Copy your code from Assignment 4 into the Student.java and StudentList.java Classes. Assign5Test.java should contain the main method. Modify StudentList.java to use an ArrayList instead of an array. You can find the basics of ArrayList here: https://www.w3schools.com/java/java_arraylist.asp In StudentList.java, create two new public methods: The addStudent method should have one parameter of type Student and...
JAVA LANGUAGE Required Tasks: In Eclipse, create a Java Project as CS-176L-Assign4. Create 3 Java Classes...
JAVA LANGUAGE Required Tasks: In Eclipse, create a Java Project as CS-176L-Assign4. Create 3 Java Classes each in its own file Student.java, StudentList.java, and Assign4Test.java. Copy your code from Assignment 3 into the Student.java and StudentList.java. Assign4Test.java should contain the main method. In Student.java, add a new private variable of type Integer called graduationYear. In Student.java, create a new public setter method setGraduationYear to set the graduationYear. The method will have one parameter of type Integer. The method will set...
JAVA Practice Exam 2 The project practice_exam_2 contains three (3) classes: Testing – This is the...
JAVA Practice Exam 2 The project practice_exam_2 contains three (3) classes: Testing – This is the file you will use to execute your program and test your cases. Each section refers to one or more specific array(s). From here, you can run the whole program or one section at a time. TestCases – This file contains all test cases. NO NEED FOR YOU TO PAY ATTENTION TO IT. YourCode – This is where you will be writing your code. Implement...
Java language This is your first homework on objects and classes, and the basics of object-oriented...
Java language This is your first homework on objects and classes, and the basics of object-oriented programming. For each exercise, your task is to first write the class that defines the given object. Compile it and check it for syntax errors. Then write the “demo class” with the main program that creates and uses the object. a)You are planning to purchase a new motor boat for cool cruises on Porter’s Lake this summer, but you want to simulate it before...
THE QUESTION IS OF JAVA LANGUAGE. ANSWER IS REQUIRED IN THREE PARTS (THREE JAVA FILES). PLEASE...
THE QUESTION IS OF JAVA LANGUAGE. ANSWER IS REQUIRED IN THREE PARTS (THREE JAVA FILES). PLEASE DIFFERENTIATE FILES SO I CAN UNDERSTAND BETTER. NOTE - Submission in parts. Parts required - Dog Class Code, Dog Manager Class Code and the main code. Please differentiate all three in the answer. This Assignment is designed to take you through the process of creating basic classes, aggregation and manipulating arrays of objects. Scenario: A dog shelter would like a simple system to keep...
true or false give reason language in java 1.A static inner class can access the instance...
true or false give reason language in java 1.A static inner class can access the instance variables and methods of its outer non-static class 2.executeQuery from statement may return more than one resultset object 3.Connection is java.sql interface that establishes a session with a specific database 4.Writable is an interface in Hadoop that acts as a wrapper class to almost all the primitive data type 5.Text is the wrapper class of string in Hadoop
Java Language Only Use Java FX In this project we will build a BMI calculator. BMI...
Java Language Only Use Java FX In this project we will build a BMI calculator. BMI is calculated using the following formulas: Measurement Units Formula and Calculation Kilograms and meters (or centimetres) Formula: weight (kg) / [height (m)]2 The formula for BMI is weight in kilograms divided by height in meters squared. If height has been measured in centimetres, divide by 100 to convert this to meters. Pounds and inches Formula: 703 x weight (lbs) / [height (in)]2 When using...
Objective: Using Java coding language, complete each "TO-DO" section for the following classes. Shape code (for...
Objective: Using Java coding language, complete each "TO-DO" section for the following classes. Shape code (for reference, nothing to complete here): import java.awt.Color; import java.awt.Graphics; import java.awt.Rectangle; import java.io.RandomAccessFile; public abstract class Shape extends Rectangle {    public int id;    public Color color;    public int xSpeed, ySpeed;       public Shape(int id, int x, int y, int width, int height, Color color, int xSpeed, int ySpeed) {        super(x,y,width,height);        this.id = id;        this.color...
Required Tasks: In Eclipse, create a Java Project as CS-176L-Assign5. Create 3 Java Classes each in...
Required Tasks: In Eclipse, create a Java Project as CS-176L-Assign5. Create 3 Java Classes each in its own file Student.java, StudentList.java, and Assign5Test.java. Copy your code from Assignment 4 into the Student.java and StudentList.java Classes. Assign5Test.java should contain the main method. Modify StudentList.java to use an ArrayList instead of an array. You can find the basics of ArrayList here: https://www.w3schools.com/java/java_arraylist.asp In StudentList.java, create two new public methods: The addStudent method should have one parameter of type Student and should add...
Writing Classes I Write a Java program containing two classes: Dog and a driver class Kennel....
Writing Classes I Write a Java program containing two classes: Dog and a driver class Kennel. A dog consists of the following information: • An integer age. • A string name. If the given name contains non-alphabetic characters, initialize to Wolfy. • A string bark representing the vocalization the dog makes when they ‘speak’. • A boolean representing hair length; true indicates short hair. • A float weight representing the dog’s weight (in pounds). • An enumeration representing the type...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT