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...
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 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...
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 –...
Using Java, write a program named MyAngles that will prompt the user for the measure of...
Using Java, write a program named MyAngles that will prompt the user for the measure of the three sides of a triangle and then reports the measurement of each interior angle of the triangle and the area of the triangle.
In java: -Create a class named Animal
In java: -Create a class named Animal
Write a C++ program (The Account Class) Design a class named Account that contains (keep the...
Write a C++ program (The Account Class) Design a class named Account that contains (keep the data fields private): a) An int data field named id for the account. b) A double data field named balance for the account. c) A double data field named annualInterestRate that stores the current interest rate. d) A no-arg constructor that creates a default account with id 0, balance 0, and annualInterestRate 0. e) The accessor and mutator functions for id, balance, and annualInterestRate....
Develop a java program with a class named friend with data members like name, phno and...
Develop a java program with a class named friend with data members like name, phno and hobby. Use Parameterized constructor to create two friend objects and invoke checkhobby method which takes only one parameter, to find whether they have same hobby or different hobbies
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT