Creat Shape classes from scratch DETAILS You will create 3 s

Creat Shape classes from scratch

DETAILS You will create 3 shape classes (Circle, Rectangle, Triangle) that all inherit from a single abstract class called AbstractShape which implements Shape (also created by you). You are also responsible for creating the driver class \"Assignment7.java\" (program that tests your classes and described on page 3) which does the following reads input data from a file instantiates various objects of the three shapes based on the input data stores each in a LinkedList outputs this list to an output file sorts a \"copy\" of this LinkedList of objects outputs the sorted version of the list to the output file outputs the original list to the output file This driver program also needs to \"ignore errors in the input file that breach the specified input format as described in the Assianment7,java details (see page 3 1. Shape.java This is an interface that has 2 abstract methods, and passes the responsibility of implementing the compareTo method to the class that implements Shape (you may note, nomally Comparable is \"implemented\" by a class. However, an interface cannot implement because interfaces can only contain abstract methods. That said, an interface can only extend other interfaces and the responsibility of actually \"implementing\" the abstract method(s) of the super class interface is passed on to the sub-classes) public interface Shape extends Comparable public double calculateAreal) Il This abstract method is implemented at the concrete level public Shape copyShape); Il also implemented at the concrete level 2. AbstractShape.java public abstract class AbstractShape implements Shape This class should contain an instance field to store the name of each obiect. The constructor which sets this field should receive the name and a number to be concatenated to the name and then stored in the name field Recall, when the super class has a parameterized constructor, the sub-classes will need to call it AND the sub- classes will need to also provide a constructor without parameters This abstract class will implement the compareTo method passed on from the Shape interface and will pass on the responsibility of implementing calculateArea to the extending sub-classes (compare To will use the calculateArea method when comparing 2 Shape objects). Along with compare To, one more concrete method should be included. The following will be used by the sub-classes\' toString method: public String getName) II Simply returns the name field data

Solution

in7.txt

4.4
2.5 3
8.1 3.0 5.0
2.5 3 4
2.5
tuesday
-7
1.0
3 three
3 -9
3 5
1.0

Assignment7.java

import java.io.*;
import java.util.*;


public class Assignment7 {
  
   /**
   * This is the test driver class that will include main.
   * This program MUST read a file named in7.txt and
   * generate an output file named out7.txt. The in7.txt
   * file must be created by you based on formatting
   * described shortly.
   *
   * @param theArgs
   */
   public static void main(String[] theArgs) {

       List<Shape> myList = new ArrayList<Shape>();
       try {
           Scanner inputFile = new Scanner(new File(\"in7.txt\"));
           while (inputFile.hasNextLine()) {
               String line = inputFile.nextLine();

               try {
                   Shape element = getShape(line);

                   if (element != null)
                       myList.add(element);

               } catch (NumberFormatException ex) {
               } catch (IllegalArgumentException ex) {
                   System.err.println(ex.getMessage());
               }
           }
           inputFile.close();

           PrintStream outputFile = new PrintStream(
                   new File(\"out7.txt\"));

           outputFile.println(\"\ Original List[unsorted]:\");

           for (Shape element : myList)
               outputFile.println(element.toString());

           outputFile.println(\"\ Copied List[sorted]:\");

           for (Shape element : getSortedList(myList))
               outputFile.println(element.toString());

           outputFile.println(\"\ Original List[unsorted]:\");

           for (Shape element : myList)
               outputFile.println(element.toString());

           outputFile.close();
       } catch (IOException ex) {
       }
   }

   private static Shape getShape(String line)
           throws NumberFormatException, IllegalArgumentException {
       String[] data = line.split(\" \");
       double[] values = new double[data.length];

       for (int i = 0; i < data.length; i++) {
           values[i] = Double.parseDouble(data[i]);
       }

       switch (values.length) {
       case 1:
           return new Circle(values[0]);
       case 2:
           return new Rectangle(values[0], values[1]);
       case 3:
           return new Triangle(values[0], values[1], values[2]);
       }
       return null;
   }

   private static List<Shape> getSortedList(List<Shape> myList) {
       // List<Shape> myList = new LinkedList<Shape>( );
       List<Shape> newList = new ArrayList<Shape>();
       for (Shape element : myList) {
           Shape s = element.copyShape();
           newList.add(s);
       }
       Collections.sort(newList);
       return newList;
   }
}

Shape.java

public interface Shape extends Comparable<Shape> {

   /**
   *
   * @return Calculated Area
   */
   public double calculateArea(); // This abstract method
   // is implemented at the
   // concrete level.

   /**
   *
   * @return Copied shape
   *
   */
   public Shape copyShape(); // also implemented at the
   // concrete level.
}

Circle.java

import java.text.DecimalFormat;


public class Circle extends AbstractShape {
   /**
   * field myRedius
   */
   private double myRadius;
   /**
   * int myID
   */
   private static int myID = 1;

   /**
   * calls this(1.0);
   */
   public Circle() {
       this(1.0);
   }

   /**
   *
   * @param theRadius
   */
   public Circle(final double theRadius) {
       super(\"Circle\" + myID);
       if (theRadius <= 0)
           throw new IllegalArgumentException(
                   \"ERROR! Negative or 0 value can\'t be applied to a circle radius\");

       setRadius(theRadius);
       myID++;
   }

   /**
   *
   * @param theRadius
   */

   public void setRadius(final double theRadius) {
       myRadius = theRadius;
   }

   /**
   * @return calculated area
   */
   public double calculateArea() {
       return myRadius * myRadius * Math.PI;
   }

   /**
   * @return newC
   */
   public final Shape copyShape() {
       Circle newC = new Circle();
       newC.myRadius = myRadius;
       return newC;
   }

   /**
   * @return toString
   */
   public String toString() {
       DecimalFormat fmt = new DecimalFormat(\"0.00\");
       return getName() + \" [Radius: \" + fmt.format(myRadius)
       + \"] Area: \" + fmt.format(calculateArea());
   }
}


Circle.java

import java.text.DecimalFormat;


public class Circle extends AbstractShape {
   /**
   * field myRedius
   */
   private double myRadius;
   /**
   * int myID
   */
   private static int myID = 1;

   /**
   * calls this(1.0);
   */
   public Circle() {
       this(1.0);
   }

   /**
   *
   * @param theRadius
   */
   public Circle(final double theRadius) {
       super(\"Circle\" + myID);
       if (theRadius <= 0)
           throw new IllegalArgumentException(
                   \"ERROR! Negative or 0 value can\'t be applied to a circle radius\");

       setRadius(theRadius);
       myID++;
   }

   /**
   *
   * @param theRadius
   */

   public void setRadius(final double theRadius) {
       myRadius = theRadius;
   }

   /**
   * @return calculated area
   */
   public double calculateArea() {
       return myRadius * myRadius * Math.PI;
   }

   /**
   * @return newC
   */
   public final Shape copyShape() {
       Circle newC = new Circle();
       newC.myRadius = myRadius;
       return newC;
   }

   /**
   * @return toString
   */
   public String toString() {
       DecimalFormat fmt = new DecimalFormat(\"0.00\");
       return getName() + \" [Radius: \" + fmt.format(myRadius)
       + \"] Area: \" + fmt.format(calculateArea());
   }
}


AbstractShape.java


public abstract class AbstractShape implements Shape {
   /**
   * private string myName
   */
   private String myName;

   /**
   *
   * @param theName
   */
   public AbstractShape(String theName) {
       this.myName = theName;
   }

   /**
   * @param from
   *            return 0
   */
   public int compareTo(Shape from) {
       double thisArea = calculateArea();
       double fromArea = from.calculateArea();

       if (thisArea > fromArea) {
           return 1;
       }

       else if (thisArea < fromArea) {
           return -1;
       } else
           return 0;
   }

   /**
   *
   * @return returns the name field data
   */
   public String getName() {
       return myName;
   }
}


Rectangle.java

import java.text.DecimalFormat;

public class Rectangle extends AbstractShape {

   /**
      * double field myLenght
      */
   private double myLength;
   /**
      * double field myWidth
      */
   private double myWidth;
   /**
      * static int field shared by all Rectangle objects
      */
   private static int myID = 1;

   // Beginning methods.
   /**
      * calls this(1.0, 1.0)
      */
   public Rectangle() {
       this(1.0, 1.0);
   }

   public Rectangle(final double theLength, final double theWidth) {
       super(\"Rectangle\" + myID);

       if (theLength <= 0 || theWidth <= 0)
           throw new IllegalArgumentException(
                   \"ERROR! Negative or 0 value(s) can\'t be applied to a rectangle.\");
       // myID--;

       setLength(theLength);
       setWidth(theWidth);
       myID++;
   }

   /**
      *
      * @param theLength
      */
   public void setLength(final double theLength) {
       myLength = theLength;
   }

   /**
      *
      * @param theWidth
      */
   public void setWidth(final double theWidth) {
       myWidth = theWidth;
   }

   /**
      * @return calculated area
      */
   public double calculateArea() {
       return myLength * myWidth;
   }

   /**
      * @return newR
      */
   public final Shape copyShape() {
       Rectangle newR = new Rectangle();
       newR.myLength = myLength;
       newR.myWidth = myWidth;
       return newR;
   }

   /**
      * @return toString
      */
   public String toString() {
       DecimalFormat fmt = new DecimalFormat(\"0.00\");
       return getName() + \" [Length: \" + fmt.format(myLength) + \", \"
       + \"Width:\" + fmt.format(myWidth) + \"] Area: \"
       + fmt.format(calculateArea());
   }
}


Triangle.java


import java.text.DecimalFormat;


public class Triangle extends AbstractShape implements Shape {

   /**
   * SideA
   */
   private double mySideA;
   /**
   * SideB
   */
   private double mySideB;
   /**
   * SideC
   */
   private double mySideC;
   /**
   * Private ID
   */
   private static int myID = 1;
   // calls this(1.0, 1.0, 1.0);

   /**
   * Calls 1.0
   */
   public Triangle() {
       this(1.0, 1.0, 1.0);
   }

   /**
   *
   * @param theSideA
   * @param theSideB
   * @param theSideC
   */
   // calls super with \"Triangle\" and myID incremented
   public Triangle(final double theSideA, final double theSideB,
           final double theSideC) {
       super(\"Triangle\" + myID);

       if (theSideA <= 0 || theSideB <= 0 || theSideC <= 0)
           throw new IllegalArgumentException(
                   \"ERROR! Negative or 0 value can\'t be applied to a triangle.\");

       if (theSideA >= theSideB + theSideC
               || theSideB >= theSideA + theSideC
               || theSideC >= theSideA + theSideB)
           throw new IllegalArgumentException(
                   \"ERROR! Not a Triangle. Longest side too long.\");

       setSideA(theSideA);
       setSideB(theSideB);
       setSideC(theSideC);
       myID++;
   }

   /**
   *
   * @param theSideA
   */

   public void setSideA(final double theSideA) {
       mySideA = theSideA;
   }

   /**
   *
   * @param theSideB
   */
   public void setSideB(final double theSideB) {
       mySideB = theSideB;
   }

   /**
   *
   * @param theSideC
   */

   public void setSideC(final double theSideC) {
       mySideC = theSideC;
   }

   /**
   * @return Calculated area
   */
   public double calculateArea() {
       double p = (mySideA + mySideB + mySideC) / 2;
       return Math.sqrt(
               p * (p - mySideA) * (p - mySideB) * (p - mySideC));
   }

   // Returns a reference to a new Triangle with the same
   // field
   // values as the implied parameter (defensive copy).
   /**
   * @return newT
   */
   public final Shape copyShape() {
       Triangle newT = new Triangle();
       newT.mySideA = mySideA;
       newT.mySideB = mySideB;
       newT.mySideC = mySideC;
       return newT;
   }

   /**
   * @return toString
   */

   public String toString() {
       DecimalFormat fmt = new DecimalFormat(\"0.00\");
       return getName() + \" [SideA: \" + fmt.format(mySideA)
       + \", SideB:\" + fmt.format(mySideB) + \", SideC:\"
       + fmt.format(mySideC) + \"] Area: \"
       + fmt.format(calculateArea());
   }
}

Creat Shape classes from scratch DETAILS You will create 3 shape classes (Circle, Rectangle, Triangle) that all inherit from a single abstract class called Abst
Creat Shape classes from scratch DETAILS You will create 3 shape classes (Circle, Rectangle, Triangle) that all inherit from a single abstract class called Abst
Creat Shape classes from scratch DETAILS You will create 3 shape classes (Circle, Rectangle, Triangle) that all inherit from a single abstract class called Abst
Creat Shape classes from scratch DETAILS You will create 3 shape classes (Circle, Rectangle, Triangle) that all inherit from a single abstract class called Abst
Creat Shape classes from scratch DETAILS You will create 3 shape classes (Circle, Rectangle, Triangle) that all inherit from a single abstract class called Abst
Creat Shape classes from scratch DETAILS You will create 3 shape classes (Circle, Rectangle, Triangle) that all inherit from a single abstract class called Abst
Creat Shape classes from scratch DETAILS You will create 3 shape classes (Circle, Rectangle, Triangle) that all inherit from a single abstract class called Abst
Creat Shape classes from scratch DETAILS You will create 3 shape classes (Circle, Rectangle, Triangle) that all inherit from a single abstract class called Abst
Creat Shape classes from scratch DETAILS You will create 3 shape classes (Circle, Rectangle, Triangle) that all inherit from a single abstract class called Abst

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site