In: Computer Science
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
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