Data Structures I need a full detailed description how you would solve the probl
ID: 3742674 • Letter: D
Question
Data Structures
I need a full detailed description how you would solve the problem in JAVADOC!!!!! The code below works so I need a detailed description for it in JAVADOC format.
Polygon.java.
package shapes;
public interface Polygon {
double area();
double perimeter();
}
Triangle.java
package shapes;
public abstract class Triangle implements Polygon {
double x,y,z;
@Override
public abstract double area();
@Override
public abstract double perimeter();
}
class Isosceles extends Triangle{
Isosceles(double x,double y){
this.x = this.y = x;
this.z = y;
}
@Override
public double area() {
double altitude = Math.sqrt(x*y - z*z/4);
return altitude*this.z/2;
}
@Override
public double perimeter() {
return this.x+this.y+this.z;
}
@Override
public String toString() {
return "Isosceles [common side ="+this.x+" other side ="+this.z+"]";
}
}
class EquilateralTriangle extends Triangle{
EquilateralTriangle(double x){
this.x = this.y = this.z=x;
}
@Override
public double area() {
return Math.sqrt(3)*this.x*this.x/4;
}
@Override
public double perimeter() {
return this.x+this.y+this.z;
}
@Override
public String toString() {
return "EquilateralTriangle [side ="+this.x+"]";
}
}
Quadrilateral.java
package shapes;
public class Quadrilateral implements Polygon {
double x,y;
@Override
public double area() {
return x*y;
}
@Override
public double perimeter() {
return x+y;
}
}
class Rectangle extends Quadrilateral{
Rectangle(double x,double y){
this.x = x;
this.y = y;
}
@Override
public String toString() {
return "Rectangle [a="+this.x+" b = "+this.y+"]";
}
}
class Square extends Quadrilateral{
Square(double x){
this.x = this.y =x;
}
@Override
public String toString() {
return "Square [side="+this.x+"]";
}
}
Pentagon.java
package shapes;
public class Pentagon implements Polygon {
double size;
double noOfSide = 5;
Pentagon(double sizeIn){
size = sizeIn;
}
@Override
public double area() {
double angle = Math.toRadians(180/noOfSide);
return size*size* noOfSide/(4*Math.tan(angle));
}
@Override
public double perimeter() {
return noOfSide*size;
}
@Override
public String toString() {
return "Pentagon [size="+size+"]";
}
}
Hexagon.java
package shapes;
public class Hexagon implements Polygon {
double size;
double noOfSide = 6;
Hexagon(double sizeIn){
size = sizeIn;
}
@Override
public double area() {
double angle = Math.toRadians(180/noOfSide);
return size*size* noOfSide/(4*Math.tan(angle));
}
@Override
public double perimeter() {
return noOfSide*size;
}
@Override
public String toString() {
return "Hexagon [size="+size+"]";
}
}
Octagon.java
package shapes;
public class Octagon implements Polygon {
double size;
double noOfSide = 8;
Octagon(double sizeIn){
size = sizeIn;
}
@Override
public double area() {
double angle = Math.toRadians(180/noOfSide);
return size*size* noOfSide/(4*Math.tan(angle));
}
@Override
public double perimeter() {
return noOfSide*size;
}
@Override
public String toString() {
return "Octagon [size="+size+"]";
}
}
Main.java
package shapes;
public class Main {
public static void main(String[] args) {
Isosceles isosceles = new Isosceles(20, 10);
System.out.println(isosceles);
System.out.println("Area : "+isosceles.area());
System.out.println("Perimeter : "+isosceles.perimeter());
System.out.println(" ");
EquilateralTriangle equilateralTriangle = new EquilateralTriangle(20);
System.out.println(equilateralTriangle);
System.out.println("Area : "+equilateralTriangle.area());
System.out.println("Perimeter : "+equilateralTriangle.perimeter());
System.out.println(" ");
Rectangle rectangle = new Rectangle(20,10);
System.out.println(rectangle);
System.out.println("Area : "+rectangle.area());
System.out.println("Perimeter : "+rectangle.perimeter());
System.out.println(" ");
Square square = new Square(20);
System.out.println(square);
System.out.println("Area : "+square.area());
System.out.println("Perimeter : "+square.perimeter());
System.out.println(" ");
Pentagon pentagon = new Pentagon(20);
System.out.println(pentagon);
System.out.println("Area : "+pentagon.area());
System.out.println("Perimeter : "+pentagon.perimeter());
System.out.println(" ");
Hexagon hexagon = new Hexagon(6);
System.out.println(hexagon);
System.out.println("Area : "+hexagon.area());
System.out.println("Perimeter : "+hexagon.perimeter());
System.out.println(" ");
Octagon octagon = new Octagon(6);
System.out.println(octagon);
System.out.println("Area : "+octagon.area());
System.out.println("Perimeter : "+octagon.perimeter());
System.out.println(" ");
}
}
Define a Polygon interface that has methods area) and perimeter). Then im- plement classes for Triangle, Quadrilateral, Pentagon, Hexagon, and Octagon, which implement this interface, with the obvious meanings for the area) and perimeter() methods. Also implement classes, Isosceles Triangle, Equilateral- Triangle, Rectangle, and Square, which have the appropriate inheritance rela- tionships. Finally, write a simple user interface, which allows users to create polygons of the various types, input their geometric dimensions, and then out- put their area and perimeter. For extra effort, allow users to input polygons by specifying their vertex coordinates and be able to test if two such polygons are Similar. 1. Algorithm/Documentation: detailed description how you would solve the problem in javadoc format. 2. Implementation: The actual code that implementing your solution. The implementation should follow your documentation precisely The documentation should be embedded into the implementation (java files)Explanation / Answer
//------------------------------------Polygon.java------------------------------------
package shapes;
/** Polygon interface will allow us to perform Runtime Polymorphism based on the
* arguements.
*/
public interface Polygon {
// area() method declaration to calculate the area of given polygon.
double area();
// perimeter() method declaration to calculate the perimeter of given polygon.
double perimeter();
}
//------------------------------------Polygon.java------------------------------------
//------------------------------------Triangle.java------------------------------------
package shapes;
/** Creating Triangle Class as an Abstract Class so that it cannot be instantiated
* rather allowing its subclasses to create their objects and hence achieving
* Polymorphism. It should implement Polygon interface to facilate the properties
* of a general polygon.
*/
public abstract class Triangle implements Polygon {
// Declaring three variables to indicate three sides of any triangle.
double x,y,z;
/** Overriding the declaration of base class method "area" so that its subclass can use
* this method property. These methods are declared abstract so that their definition in
* their subclasses are a must to implement.
*/
@Override
public abstract double area();
/** Overriding the declaration of base class method "perimeter" so that its subclass can use
* this method property. These methods are declared abstract so that their definition in
* their subclasses are a must to implement.
*/
@Override
public abstract double perimeter();
}
/** Creating Isosceles as a subclass of Triangle class to make the use of all Triangle
* properties variables & methods. It should extend its base class Triangle.
*/
class Isosceles extends Triangle{
/**
* This is the parameterized constructor to create an instance of this class.
* @param x This is the first paramter indicating the two equal sides of an Isosceles triangle.
* @param y This is the second parameter indicating the third unequal side of an Isosceles triangle.
*/
Isosceles(double x,double y){
// For this, two sides must be equal, so assigning x & y as first parameter.
this.x = this.y = x;
// this is the third unequal side, so assigning z as second parameter.
this.z = y;
}
/**
* This method is used to calculate the area.
* @return double This returns the calculated area.
*/
@Override
public double area() {
// First calculate the altitude (height) of triangle by below formula.
double altitude = Math.sqrt(x*y - z*z/4);
// then return the area which is "half * base * height"
return altitude*this.z/2;
}
/**
* This method is used to calculate the perimeter.
* @return double This returns the calculated perimeter.
*/
@Override
public double perimeter() {
// Simply return the perimeter which is equal to sum of all sides of a triangle.
return this.x+this.y+this.z;
}
/**
* This method is used to print the string.
* @return string This returns the display message output string.
*/
@Override
public String toString() {
return "Isosceles [common side ="+this.x+" other side ="+this.z+"]";
}
}
/** Creating EquilateralTriangle as a subclass of Triangle class to make the use of all Triangle
* properties variables & methods. It should extend its base class Triangle.
*/
class EquilateralTriangle extends Triangle{
/**
* This is the parameterized constructor to create an instance of this class.
* @param x This is the first paramter indicating the three equal sides of an Equilateral Triangle.
*/
EquilateralTriangle(double x){
// For this, all three sides must be equal, so assigning x, y, z as x parameter.
this.x = this.y = this.z=x;
}
/**
* This method is used to calculate the area.
* @return double This returns the calculated area.
*/
@Override
public double area() {
// simply return the are of EquilateralTriangle using below formula
return Math.sqrt(3)*this.x*this.x/4;
}
/**
* This method is used to calculate the perimeter.
* @return double This returns the calculated perimeter.
*/
@Override
public double perimeter() {
// Simply return the perimeter which is equal to sum of all sides of a triangle.
return this.x+this.y+this.z;
}
/**
* This method is used to print the string.
* @return string This returns the display message output string.
*/
@Override
public String toString() {
return "EquilateralTriangle [side ="+this.x+"]";
}
}
//------------------------------------Triangle.java------------------------------------
//------------------------------------Quadrilateral.java------------------------------------
package shapes;
/** Creating Quadrilateral Class. It should implement Polygon interface to facilate the properties
* of a general polygon.
*/
public class Quadrilateral implements Polygon {
// Theese reperesents the length & breadth of a Quadrilateral.
double x,y;
/**
* This method is used to calculate the area.
* @return double This returns the calculated area.
*/
@Override
public double area() {
// simply return the area which is given by length * breadth.
return x*y;
}
/**
* This method is used to calculate the perimeter.
* @return double This returns the calculated perimeter.
*/
@Override
public double perimeter() {
// simply return the perimeter which is given by below formula.
return 2*(x+y);
}
}
/** Creating Rectangle as a subclass of Quadrilateral class to make the use of all Quadrilateral
* properties variables & methods. It should extend its base class Quadrilateral.
*/
class Rectangle extends Quadrilateral{
/**
* This is the parameterized constructor to create an instance of this class.
* @param x This is the first paramter indicating the length of a rectangle.
* @param y This is the second paramter indicating the breadth of a rectangle.
*/
Rectangle(double x,double y){
// assigning base class variables as length & breadth
this.x = x;
this.y = y;
}
// Here if we call area & perimeter on Rectangle class, the super class methods will be
// called which serves same functionality.
/**
* This method is used to print the string.
* @return string This returns the display message output string.
*/
@Override
public String toString() {
return "Rectangle [a="+this.x+" b = "+this.y+"]";
}
}
/** Creating Square as a subclass of Quadrilateral class to make the use of all Quadrilateral
* properties variables & methods. It should extend its base class Quadrilateral.
*/
class Square extends Quadrilateral{
/**
* This is the parameterized constructor to create an instance of this class.
* @param x This is the paramter indicating the side of a square.
*/
Square(double x){
// assigning both base class variables to same equal side length parameter.
this.x = this.y =x;
}
// Here if we call area & perimeter on Square class, the super class methods will be
// called which serves same functionality.
/**
* This method is used to print the string.
* @return string This returns the display message output string.
*/
@Override
public String toString() {
return "Square [side="+this.x+"]";
}
}
//------------------------------------Quadrilateral.java------------------------------------
//------------------------------------Pentagon.java------------------------------------
package shapes;
/** Creating Pentagon Class. It should implement Polygon interface to facilate the properties
* of a general polygon.
*/
public class Pentagon implements Polygon {
// This indicates the size of a pentagon.
double size;
// This indicates the number of sides of a pentagon.
double noOfSide = 5;
/**
* This is the parameterized constructor to create an instance of this class.
* @param sizeIn This is the paramter indicating the size of a pentagon.
*/
Pentagon(double sizeIn){
size = sizeIn;
}
/**
* This method is used to calculate the area.
* @return double This returns the calculated area.
*/
@Override
public double area() {
// first calculate the angle by given below formula.
double angle = Math.toRadians(180/noOfSide);
// then return the area by given below formula.
return size*size* noOfSide/(4*Math.tan(angle));
}
/**
* This method is used to calculate the perimeter.
* @return double This returns the calculated perimeter.
*/
@Override
public double perimeter() {
// simply return the perimeter which is given by number of sides * size of each side.
return noOfSide*size;
}
/**
* This method is used to print the string.
* @return string This returns the display message output string.
*/
@Override
public String toString() {
return "Pentagon [size="+size+"]";
}
}
//------------------------------------Pentagon.java------------------------------------
//------------------------------------Hexagon.java------------------------------------
package shapes;
/** Creating Hexagon Class. It should implement Polygon interface to facilate the properties
* of a general polygon.
*/
public class Hexagon implements Polygon {
// This indicates the size of a pentagon.
double size;
// This indicates the number of sides of a hexagon.
double noOfSide = 6;
/**
* This is the parameterized constructor to create an instance of this class.
* @param sizeIn This is the paramter indicating the size of a hexagon.
*/
Hexagon(double sizeIn){
size = sizeIn;
}
/**
* This method is used to calculate the area.
* @return double This returns the calculated area.
*/
@Override
public double area() {
// first calculate the angle by given below formula.
double angle = Math.toRadians(180/noOfSide);
// then return the area by given below formula.
return size*size* noOfSide/(4*Math.tan(angle));
}
/**
* This method is used to calculate the perimeter.
* @return double This returns the calculated perimeter.
*/
@Override
public double perimeter() {
// simply return the perimeter which is given by number of sides * size of each side.
return noOfSide*size;
}
/**
* This method is used to print the string.
* @return string This returns the display message output string.
*/
@Override
public String toString() {
return "Hexagon [size="+size+"]";
}
}
//------------------------------------Hexagon.java------------------------------------
//------------------------------------Octagon.java------------------------------------
package shapes;
/** Creating Octagon Class. It should implement Polygon interface to facilate the properties
* of a general polygon.
*/
public class Octagon implements Polygon {
// This indicates the size of a pentagon.
double size;
// This indicates the number of sides of a Octagon.
double noOfSide = 6;
/**
* This is the parameterized constructor to create an instance of this class.
* @param sizeIn This is the paramter indicating the size of a Octagon.
*/
Octagon(double sizeIn){
size = sizeIn;
}
/**
* This method is used to calculate the area.
* @return double This returns the calculated area.
*/
@Override
public double area() {
// first calculate the angle by given below formula.
double angle = Math.toRadians(180/noOfSide);
// then return the area by given below formula.
return size*size* noOfSide/(4*Math.tan(angle));
}
/**
* This method is used to calculate the perimeter.
* @return double This returns the calculated perimeter.
*/
@Override
public double perimeter() {
// simply return the perimeter which is given by number of sides * size of each side.
return noOfSide*size;
}
/**
* This method is used to print the string.
* @return string This returns the display message output string.
*/
@Override
public String toString() {
return "Octagon [size="+size+"]";
}
}
//------------------------------------Octagon.java------------------------------------
//------------------------------------Main.java-----------------------------------------
package shapes;
public class Main {
public static void main(String[] args) {
// creating an object of Isosceles class.
Isosceles isosceles = new Isosceles(20, 10);
// printing direct object will call the toString method which will return the string to display.
System.out.println(isosceles);
// calling area method
System.out.println("Area : "+isosceles.area());
// calling perimeter method
System.out.println("Perimeter : "+isosceles.perimeter());
System.out.println(" ");
// Similarly for all, we are testing each class methods by creating its objects & calling certain methods on them.
EquilateralTriangle equilateralTriangle = new EquilateralTriangle(20);
System.out.println(equilateralTriangle);
System.out.println("Area : "+equilateralTriangle.area());
System.out.println("Perimeter : "+equilateralTriangle.perimeter());
System.out.println(" ");
Rectangle rectangle = new Rectangle(20,10);
System.out.println(rectangle);
System.out.println("Area : "+rectangle.area());
System.out.println("Perimeter : "+rectangle.perimeter());
System.out.println(" ");
Square square = new Square(20);
System.out.println(square);
System.out.println("Area : "+square.area());
System.out.println("Perimeter : "+square.perimeter());
System.out.println(" ");
Pentagon pentagon = new Pentagon(20);
System.out.println(pentagon);
System.out.println("Area : "+pentagon.area());
System.out.println("Perimeter : "+pentagon.perimeter());
System.out.println(" ");
Hexagon hexagon = new Hexagon(6);
System.out.println(hexagon);
System.out.println("Area : "+hexagon.area());
System.out.println("Perimeter : "+hexagon.perimeter());
System.out.println(" ");
Octagon octagon = new Octagon(6);
System.out.println(octagon);
System.out.println("Area : "+octagon.area());
System.out.println("Perimeter : "+octagon.perimeter());
System.out.println(" ");
}
}
//------------------------------------Main.java-----------------------------------------
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.