In: Computer Science
In Euclidean geometry, a rhombus is a simple quadrilateral whose four sides all have the same length. If lengths of each of the sides are a and the distance between the parallel sides (known as height) is h, then the area of the rhombus is defined as a × h. Suppose that you already have a class named Rhombus that can hold a and h. The class has a method to compute the area as well. The class is as follows.
public class Rhombus {
private double a, h;
Rhombus nextRhombus;
Rhombus(){
h=1; a=h;
}
Rhombus(double aa, double hh){
a=aa; h=hh;
}
public double geta(){
return a;
}
public double geth(){
return h;
}
public double getArea(){
return a*h;
}
public boolean isSquare(){
return a==h;
}
}
Notice that Rhombus nextRhombus; in the Rhombus class can be used to construct a linked list.
(15 points) Now, you have another class named RhombusProcessor that has Rhombus objects in an array in the main method. Write a method named getSquares in the RhombusProcessor class to copy only the Rhombus objects that represent squares. The getSquares method must return these square shaped Rhombus objects using a linked list in the same oder they appear in the original list. Notice that you will only return the head of the linked list. The program is partially written; you just need to write the getSquares method in the space provided within the RhombusProcessor class. The expected header is public static Rhombus getSquares(Rhombus[] r).
public class RhombusProcessor {
public static Rhombus getSquares(Rhombus[] r){
Rhombus.java
public class Rhombus {
private double a, h;
Rhombus nextRhombus;
Rhombus(){
h=1; a=h;
}
Rhombus(double aa, double hh){
a=aa; h=hh;
}
public void setNextRhombus(Rhombus nextRhombus) {
this.nextRhombus = nextRhombus;
}
public double geta(){
return a;
}
public double geth(){
return h;
}
public double getArea(){
return a*h;
}
public boolean isSquare(){
return a==h;
}
}
Driver class
public class CdDriver {
public static void main(String a[]){
Rhombus rh=new Rhombus(5.3, 4.1);
System.out.println("Area of rhombus: "+rh.getArea());
System.out.println("Is it a square? "+rh.isSquare());
System.out.println("------------------------------------");
Rhombus rh1=new Rhombus(5.3, 5.3);
System.out.println("Area of rhombus: "+rh1.getArea());
System.out.println("Is it a square? "+rh1.isSquare());
//create linked list by attaching rh1 with rh
rh.setNextRhombus(rh1);
}
}