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 myList = new Arra.
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Creat Shape classes from scratch DETAILS You will create 3 shape cla.pdf
1. 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. 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 myList = new ArrayList();
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) {
3. } 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;
4. }
private static List getSortedList(List myList) {
// List myList = new LinkedList( );
List newList = new ArrayList();
for (Shape element : myList) {
Shape s = element.copyShape();
newList.add(s);
}
Collections.sort(newList);
return newList;
}
}
Shape.java
public interface Shape extends Comparable {
/**
*
* @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
*/
5. 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;
}
6. /**
* @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);
}
7. /**
*
* @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
*/
8. 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;
}
9. /**
*
* @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.");