Question

In: Computer Science

Write a java program that has a class named Octagon that extends the class Circ and...

Write a java program that has a class named Octagon that extends the class Circ and implements Comparable (compare the object's area) and Cloneable interfaces.

Assume that all the 8 sides of the octagon are of equal size. Your class Octagon, therefore, must represent an octagon inscribed into a circle of a given radius (inherited from Circle) and not introduce any new class variables.

Provide constructors for clas Octagon with no parameters and with 1 parameter radius. Create a method getSide() for computing Octagon's side. Its area then can be computed according to the formula: area = (2 + 4/sqrt(2)) * side * side. Write a test program that creates an array of shapes consisting of several instances of Circ, Rect, and Octagon and displays their area and perimeter. Create a new Octagon object by using the clone() method and compare the two objects using the compareTo() method. Include in your test program the method that returns the largest object in an array of objects. The method header is public static Object max(Object[] a) All the objects are assumed to be the instances of the Comparable interface. Apply the method max to your array of shapes to find and display the maximum one. Therefore, your project should include the following java classes: Circ, Rect, Shape, Octagon, and ShapeMaster (the main class containing the main() method). Redesign the first two classes to implement the Comparable and Cloneable interfaces.

Solutions

Expert Solution

Shape.java

package shape;

public abstract class Shape{
  
    public abstract double getArea();   
   public abstract double getPerimeter();
   public static void main(String args[])
   {
   }
}

Circ.java

package shape;
import shape.*;

public class Circ extends Shape{
   //data fields
        protected double r;
  
   //Constructor
   Circ(){}
   Circ(double r)
   {
       this.r = r;
   }   
   //Getter and Setter methods
   public void setRadius(double r)
   {
       this.r = r;
   }
   public double getRadius()
   {
       return r;
   }
        public double getArea()
   {
       return (3.14 * r *r);
   }   
   public double getPerimeter()
   {
       return (2 * 3.14 * r);
   }   
   public static void main(String args[])
   {
   }
}

Rect.java

package shape;
import shape.*;

public class Rect extends Shape{
   //data fields
        private double length;
   private double breadth;
       
   //Getter and Setter methods
   public void setLength(double l)
   {
       length = l;
   }
   public double getLength()
   {
       return length;
   }
   public void setBreadth(double b)
   {
       breadth= b;
   }
   public double getBreadth()
   {
       return breadth;
   }
        public double getArea()
   {
       return (length * breadth);
   }  
   public double getPerimeter()
   {
       return (2 * (length + breadth));
   }
}

Octagon.java

package shape;
import shape.*;


public class Octagon extends Circ implements Comparable, Cloneable
     {
        Octagon()
        {  
        }
        Octagon(double r)
        {
            super();
        }       
        public double getSide()
        {
       return Math.sqrt((4*r*r) / 4+(2*1.141));
        }
        @Override
        public double getArea()
        {          
       return (2 + (4 / (Math.sqrt(2))) * this.getSide()*this.getSide());
        }
        @Override
        public double getPerimeter()
        {
       return (this.getSide() * 8);
            }
        @Override
        public int compareTo(Shape S1)
        {
       if(this.getArea() > S1.getArea())
           return -1;
       else if(this.getArea() > S1.getArea())
           return 1;
              else
           return 0;
        }
        @Override
        public Octagon clone() throws CloneNotSupportedException{
           return ((Octagon)super.clone());
        }
    }

OctagonTester

package shape;
import shape.*;

public class OctagonTester {
public static void main(String args[]){

   Circ C1 = new Circ(4);
   Octagon O2 = new Octagon(){
   O2.clone();
   };
  
   System.out.println("Area of Circle C1: "+C1.getArea()+" "+"Perimeter of Circle C1"+C1.getPerimeter());  
   System.out.println("Side of Octagon O2: "+O2.getSide()+" "+"Area of Octagon O2"+O2.getArea());

   }
}


Related Solutions

In java Create a class named CellPhoneCase that extends your Case class – it inherits all...
In java Create a class named CellPhoneCase that extends your Case class – it inherits all the fields and methods from the Case class. It should also contain:  One field for a CellPhone object. Be sure to put in the appropriate access modifier. (private, public)  A constructor with 3 parameters – owner’s name, Case color, the phone number of the cell phone that will be in the Case. This constructor MUST call the super class’s constructor. Then set...
write program in java Create a class named PersonalDetails with the fields name and address. The...
write program in java Create a class named PersonalDetails with the fields name and address. The class should have a parameterized constructor and get method for each field.  Create a class named Student with the fields ID, PersonalDetails object, major and GPA. The class should have a parameterized constructor and get method for each field. Create an application/class named StudentApp that declare Student object. Prompts (GUI input) the user for student details including ID, name, address, major and GPA....
In java design and code a class named comparableTriangle that extends Triangle and implements Comparable. Implement...
In java design and code a class named comparableTriangle that extends Triangle and implements Comparable. Implement the compareTo method to compare the triangles on the basis of similarity. Draw the UML diagram for your classes. Write a Driver class (class which instantiates the comparableTriangle objects) to determine if two instances of ComparableTriangle objects are similar (sample output below). It should prompt the user to enter the 3 sides of each triangle and then display whether or not the are similar...
**Java** - Creating from scratch. Original code hasn't been created yet. Design a class named Octagon...
**Java** - Creating from scratch. Original code hasn't been created yet. Design a class named Octagon that extends GeometricObject class and implements the Comparable and Cloneable interface. Assume that all eight sides of the octagon are of equal size. The area can be computed using following formula: Write a test program that creates an Octagon object with side values 5 and display its area and perimeter. Create a new object using clone method and compare the two objects using compareTo...
JAVA (Tree height) Define a new class named BSTWithHeight that extends BST with the following method:...
JAVA (Tree height) Define a new class named BSTWithHeight that extends BST with the following method: /** Return the height of this binary tree */ public int height() Class Name: Exercise25_01
Java program Create a public method named saveData for a class named Signal that will hold...
Java program Create a public method named saveData for a class named Signal that will hold digitized acceleration data. Signal has the following field declarations private     double timeStep;               // time between each data point     int numberOfPoints;          // number of data samples in array     double [] acceleration = new double [1000];          // acceleration data     double [] velocity = new double [1000];        // calculated velocity data     double [] displacement = new double [1000];        // calculated disp...
Write a java program using the information given Design a class named Pet, which should have...
Write a java program using the information given Design a class named Pet, which should have the following fields (i.e. instance variables):  name - The name field holds the name of a pet (a String type)  type - The type field holds the type of animal that a pet is (a String type). Example values are “Dog”, “Cat”, and “Bird”.  age - The age field holds the pet’s age (an int type) Include accessor methods (i.e. get...
Lab to be performed in Java. Lab: 1.) Write a class named TestScores. The class constructor...
Lab to be performed in Java. Lab: 1.) Write a class named TestScores. The class constructor should accept an array of test scores as its argument. The class should have a method that returns the average of the test scores. If any test score in the array is negative or greater than 100, the class should throw an IllegalArgumentException. Write a driver class to test that demonstrates that an exception happens for these scenarios 2.) Write a class named InvalidTestScore...
write on eclipse java Write a program named lab5 that will play a game of Blackjack...
write on eclipse java Write a program named lab5 that will play a game of Blackjack between the user and the computer. Create a second class named Card with the following: Define the following private instance variables: cardValue (int) & cardSuite (String) Write a constructor with no parameters that will Set cardValue to a random number between 1 and 13 Generate a second random number between 0 and 3 and assign cardSuite a string based on its value (0 –...
How to write a Java program that has a base class with methods and attributes, subclasses...
How to write a Java program that has a base class with methods and attributes, subclasses with methods and attributes. Please use any example you'd like.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT