Assignment 3 Direction: Submit the typed source code. Parallelogram For this ass
ID: 3607738 • Letter: A
Question
Assignment 3 Direction: Submit the typed source code. Parallelogram For this assignment, you will create a Parallelogram class. A parallelogram is a four-sided shapes whose opposite sides are parallel and opposites angles are equal (hence opposite sides are equal). The four (4) types of parallelograms and their properties are listed below. Regular Rhombus Square Rectangle o Adjacent sides are not equal o Adjacent sides are o No right angles Adjacent sides are equal-Adjacent sides are not equal o All right angles equal o No right angles o All right angles The Parallelogram class should have Public double field named length. Public double field named width. Public double field named angle. Public string field named type. O Public default constructor that assigns 1 to both length and width, 90 to angle, and "square" to type. Public overloaded constructor that takes two 2) double paruneters. The parameters should be assigned to length and width. Furthermore, angle should be assigned 90. Depending on the values of length and width, chExplanation / Answer
/* package whatever; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Parallelogram{
public double length;
public double width;
public double angle;
public String type;
Parallelogram(){
length = 1;
width = 1;
angle = 90;
type = "square";
}
Parallelogram(double l, double w){
length = l;
width = w;
angle = 90;
if(l==w){
type = "square";
}else{
type = "rectangle";
}
}
Parallelogram(double l, double w, double a){
length = l;
width = w;
angle = a;
if(l==w && a == 90){
type = "square";
}else if(l!=w && a == 90){
type = "rectangle";
}else if(l==w && a != 90){
type = "rhombus";
}else if(l!=w && a != 90){
type = "regular";
}
}
public double perimeter(){
return 2*(length+width);
}
public String ToString(){
return type+"("+ length + "," + width + "," + angle + ")" ;
}
public double Area(){
if(type.equals("square")){
return length*length;
}else if(type.equals("rectangle")){
return length*width;
}else if(type.equals("rhombus")){
return (length*length*Math.sqrt(2+2*Math.cos(Math.toRadians(angle)))*Math.sqrt(2-2*Math.cos(Math.toRadians(angle))))/2;
}
return -1;
}
}
class Test{
public static void main(String[] args){
Parallelogram p1 = new Parallelogram();
Parallelogram p2 = new Parallelogram(5,8);
Parallelogram p3 = new Parallelogram(6,12,45);
Parallelogram p4 = new Parallelogram(10,10,60);
System.out.println(p1.ToString() + " Perimeter: " + p1.perimeter());
System.out.println(p2.ToString() + " Perimeter: " + p2.perimeter());
System.out.println(p3.ToString() + " Perimeter: " + p3.perimeter());
System.out.println(p4.ToString() + " Perimeter: " + p4.perimeter());
}
}