SlideShare a Scribd company logo
1 of 14
Download to read offline
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 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) {
} 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 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
*/
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());
}
}

More Related Content

Similar to Creat Shape classes from scratch DETAILS You will create 3 shape cla.pdf

import java.util.Random;ASSIGNMENT #2 MATRIX ARITHMETIC Cla.pdf
import java.util.Random;ASSIGNMENT #2 MATRIX ARITHMETIC Cla.pdfimport java.util.Random;ASSIGNMENT #2 MATRIX ARITHMETIC Cla.pdf
import java.util.Random;ASSIGNMENT #2 MATRIX ARITHMETIC Cla.pdfaquacareser
 
Write a program that reads a graph from a file and determines whether.docx
 Write a program that reads a graph from a file and determines whether.docx Write a program that reads a graph from a file and determines whether.docx
Write a program that reads a graph from a file and determines whether.docxajoy21
 
java question Fill the add statement areaProject is to wo.pdf
java question Fill the add statement areaProject is to wo.pdfjava question Fill the add statement areaProject is to wo.pdf
java question Fill the add statement areaProject is to wo.pdfdbrienmhompsonkath75
 
Dotnet unit 4
Dotnet unit 4Dotnet unit 4
Dotnet unit 4007laksh
 
Labprogram.javaLinkedList.javaimport java.util.NoSuchElementEx.pdf
Labprogram.javaLinkedList.javaimport java.util.NoSuchElementEx.pdfLabprogram.javaLinkedList.javaimport java.util.NoSuchElementEx.pdf
Labprogram.javaLinkedList.javaimport java.util.NoSuchElementEx.pdffreddysarabia1
 
Inheritance - Creating a Multilevel Hierarchy In this lab- you will s.pdf
Inheritance - Creating a Multilevel Hierarchy  In this lab- you will s.pdfInheritance - Creating a Multilevel Hierarchy  In this lab- you will s.pdf
Inheritance - Creating a Multilevel Hierarchy In this lab- you will s.pdfvishalateen
 
Inheritance - Creating a Multilevel Hierarchy In this lab- you will s.pdf
Inheritance - Creating a Multilevel Hierarchy  In this lab- you will s.pdfInheritance - Creating a Multilevel Hierarchy  In this lab- you will s.pdf
Inheritance - Creating a Multilevel Hierarchy In this lab- you will s.pdfEvanpZjSandersony
 
Circle.javaimport java.text.DecimalFormat;public class Circle {.pdf
Circle.javaimport java.text.DecimalFormat;public class Circle {.pdfCircle.javaimport java.text.DecimalFormat;public class Circle {.pdf
Circle.javaimport java.text.DecimalFormat;public class Circle {.pdfANJALIENTERPRISES1
 
operating system linux,ubuntu,Mac Geometri.pdf
operating system linux,ubuntu,Mac Geometri.pdfoperating system linux,ubuntu,Mac Geometri.pdf
operating system linux,ubuntu,Mac Geometri.pdfaquadreammail
 
Using Array Approach, Linked List approach, and Delete Byte Approach.pdf
Using Array Approach, Linked List approach, and Delete Byte Approach.pdfUsing Array Approach, Linked List approach, and Delete Byte Approach.pdf
Using Array Approach, Linked List approach, and Delete Byte Approach.pdffms12345
 
Apache Flink Training: DataStream API Part 2 Advanced
Apache Flink Training: DataStream API Part 2 Advanced Apache Flink Training: DataStream API Part 2 Advanced
Apache Flink Training: DataStream API Part 2 Advanced Flink Forward
 
OrderTest.javapublic class OrderTest {       Get an arra.pdf
OrderTest.javapublic class OrderTest {         Get an arra.pdfOrderTest.javapublic class OrderTest {         Get an arra.pdf
OrderTest.javapublic class OrderTest {       Get an arra.pdfakkhan101
 
LECTURE 2 MORE TYPES, METHODS, CONDITIONALS.pdf
LECTURE 2 MORE TYPES, METHODS, CONDITIONALS.pdfLECTURE 2 MORE TYPES, METHODS, CONDITIONALS.pdf
LECTURE 2 MORE TYPES, METHODS, CONDITIONALS.pdfShashikantSathe3
 
Java căn bản - Chapter4
Java căn bản - Chapter4Java căn bản - Chapter4
Java căn bản - Chapter4Vince Vo
 
Chapter 4 - Defining Your Own Classes - Part I
Chapter 4 - Defining Your Own Classes - Part IChapter 4 - Defining Your Own Classes - Part I
Chapter 4 - Defining Your Own Classes - Part IEduardo Bergavera
 
Sorted number list implementation with linked listsStep 1 Inspec.pdf
 Sorted number list implementation with linked listsStep 1 Inspec.pdf Sorted number list implementation with linked listsStep 1 Inspec.pdf
Sorted number list implementation with linked listsStep 1 Inspec.pdfalmaniaeyewear
 

Similar to Creat Shape classes from scratch DETAILS You will create 3 shape cla.pdf (20)

import java.util.Random;ASSIGNMENT #2 MATRIX ARITHMETIC Cla.pdf
import java.util.Random;ASSIGNMENT #2 MATRIX ARITHMETIC Cla.pdfimport java.util.Random;ASSIGNMENT #2 MATRIX ARITHMETIC Cla.pdf
import java.util.Random;ASSIGNMENT #2 MATRIX ARITHMETIC Cla.pdf
 
Write a program that reads a graph from a file and determines whether.docx
 Write a program that reads a graph from a file and determines whether.docx Write a program that reads a graph from a file and determines whether.docx
Write a program that reads a graph from a file and determines whether.docx
 
Java execise
Java execiseJava execise
Java execise
 
java question Fill the add statement areaProject is to wo.pdf
java question Fill the add statement areaProject is to wo.pdfjava question Fill the add statement areaProject is to wo.pdf
java question Fill the add statement areaProject is to wo.pdf
 
Dotnet unit 4
Dotnet unit 4Dotnet unit 4
Dotnet unit 4
 
Labprogram.javaLinkedList.javaimport java.util.NoSuchElementEx.pdf
Labprogram.javaLinkedList.javaimport java.util.NoSuchElementEx.pdfLabprogram.javaLinkedList.javaimport java.util.NoSuchElementEx.pdf
Labprogram.javaLinkedList.javaimport java.util.NoSuchElementEx.pdf
 
Inheritance - Creating a Multilevel Hierarchy In this lab- you will s.pdf
Inheritance - Creating a Multilevel Hierarchy  In this lab- you will s.pdfInheritance - Creating a Multilevel Hierarchy  In this lab- you will s.pdf
Inheritance - Creating a Multilevel Hierarchy In this lab- you will s.pdf
 
Inheritance - Creating a Multilevel Hierarchy In this lab- you will s.pdf
Inheritance - Creating a Multilevel Hierarchy  In this lab- you will s.pdfInheritance - Creating a Multilevel Hierarchy  In this lab- you will s.pdf
Inheritance - Creating a Multilevel Hierarchy In this lab- you will s.pdf
 
Circle.javaimport java.text.DecimalFormat;public class Circle {.pdf
Circle.javaimport java.text.DecimalFormat;public class Circle {.pdfCircle.javaimport java.text.DecimalFormat;public class Circle {.pdf
Circle.javaimport java.text.DecimalFormat;public class Circle {.pdf
 
operating system linux,ubuntu,Mac Geometri.pdf
operating system linux,ubuntu,Mac Geometri.pdfoperating system linux,ubuntu,Mac Geometri.pdf
operating system linux,ubuntu,Mac Geometri.pdf
 
Using Array Approach, Linked List approach, and Delete Byte Approach.pdf
Using Array Approach, Linked List approach, and Delete Byte Approach.pdfUsing Array Approach, Linked List approach, and Delete Byte Approach.pdf
Using Array Approach, Linked List approach, and Delete Byte Approach.pdf
 
Creating Interface- Practice Program 6.docx
Creating Interface- Practice Program 6.docxCreating Interface- Practice Program 6.docx
Creating Interface- Practice Program 6.docx
 
Apache Flink Training: DataStream API Part 2 Advanced
Apache Flink Training: DataStream API Part 2 Advanced Apache Flink Training: DataStream API Part 2 Advanced
Apache Flink Training: DataStream API Part 2 Advanced
 
Java Class Design
Java Class DesignJava Class Design
Java Class Design
 
Collections
CollectionsCollections
Collections
 
OrderTest.javapublic class OrderTest {       Get an arra.pdf
OrderTest.javapublic class OrderTest {         Get an arra.pdfOrderTest.javapublic class OrderTest {         Get an arra.pdf
OrderTest.javapublic class OrderTest {       Get an arra.pdf
 
LECTURE 2 MORE TYPES, METHODS, CONDITIONALS.pdf
LECTURE 2 MORE TYPES, METHODS, CONDITIONALS.pdfLECTURE 2 MORE TYPES, METHODS, CONDITIONALS.pdf
LECTURE 2 MORE TYPES, METHODS, CONDITIONALS.pdf
 
Java căn bản - Chapter4
Java căn bản - Chapter4Java căn bản - Chapter4
Java căn bản - Chapter4
 
Chapter 4 - Defining Your Own Classes - Part I
Chapter 4 - Defining Your Own Classes - Part IChapter 4 - Defining Your Own Classes - Part I
Chapter 4 - Defining Your Own Classes - Part I
 
Sorted number list implementation with linked listsStep 1 Inspec.pdf
 Sorted number list implementation with linked listsStep 1 Inspec.pdf Sorted number list implementation with linked listsStep 1 Inspec.pdf
Sorted number list implementation with linked listsStep 1 Inspec.pdf
 

More from aromanets

Multiple sclerosis (like many CNS disorders) is more descriptive.pdf
Multiple sclerosis (like many CNS disorders) is more descriptive.pdfMultiple sclerosis (like many CNS disorders) is more descriptive.pdf
Multiple sclerosis (like many CNS disorders) is more descriptive.pdfaromanets
 
In your own words, define the following terms. Concentrate on structu.pdf
In your own words, define the following terms. Concentrate on structu.pdfIn your own words, define the following terms. Concentrate on structu.pdf
In your own words, define the following terms. Concentrate on structu.pdfaromanets
 
Is R with the cofinite topo1o separable Prove your answer. Solut.pdf
Is R with the cofinite topo1o separable Prove your answer. Solut.pdfIs R with the cofinite topo1o separable Prove your answer. Solut.pdf
Is R with the cofinite topo1o separable Prove your answer. Solut.pdfaromanets
 
Identify the impacts of dementia.Is it preventable in our future a.pdf
Identify the impacts of dementia.Is it preventable in our future a.pdfIdentify the impacts of dementia.Is it preventable in our future a.pdf
Identify the impacts of dementia.Is it preventable in our future a.pdfaromanets
 
Heisenberg Uncertainty question...Why does my professor write delt.pdf
Heisenberg Uncertainty question...Why does my professor write delt.pdfHeisenberg Uncertainty question...Why does my professor write delt.pdf
Heisenberg Uncertainty question...Why does my professor write delt.pdfaromanets
 
How did the Spanish crown initially fulfill its need for African sla.pdf
How did the Spanish crown initially fulfill its need for African sla.pdfHow did the Spanish crown initially fulfill its need for African sla.pdf
How did the Spanish crown initially fulfill its need for African sla.pdfaromanets
 
Four different fluorescent dyes are used in automated DNA sequencing.pdf
Four different fluorescent dyes are used in automated DNA sequencing.pdfFour different fluorescent dyes are used in automated DNA sequencing.pdf
Four different fluorescent dyes are used in automated DNA sequencing.pdfaromanets
 
For the investment situation below. Identify the annual Interest rate.pdf
For the investment situation below. Identify the annual Interest rate.pdfFor the investment situation below. Identify the annual Interest rate.pdf
For the investment situation below. Identify the annual Interest rate.pdfaromanets
 
For a given class, the studen records are stored in a file. The reco.pdf
For a given class, the studen records are stored in a file. The reco.pdfFor a given class, the studen records are stored in a file. The reco.pdf
For a given class, the studen records are stored in a file. The reco.pdfaromanets
 
Elementary school children spent significantly more time reading i.pdf
Elementary school children spent significantly more time reading i.pdfElementary school children spent significantly more time reading i.pdf
Elementary school children spent significantly more time reading i.pdfaromanets
 
Describe the tools and technology used to support IT project managem.pdf
Describe the tools and technology used to support IT project managem.pdfDescribe the tools and technology used to support IT project managem.pdf
Describe the tools and technology used to support IT project managem.pdfaromanets
 
Compare the following bacterial species, bacterial strain, and bact.pdf
Compare the following bacterial species, bacterial strain, and bact.pdfCompare the following bacterial species, bacterial strain, and bact.pdf
Compare the following bacterial species, bacterial strain, and bact.pdfaromanets
 
Annual estimates of the population in a Hawaiian county from 1999 (t .pdf
Annual estimates of the population in a Hawaiian county from 1999 (t .pdfAnnual estimates of the population in a Hawaiian county from 1999 (t .pdf
Annual estimates of the population in a Hawaiian county from 1999 (t .pdfaromanets
 
Acid rain negatively affects plants byA. reducing the photosynthet.pdf
Acid rain negatively affects plants byA. reducing the photosynthet.pdfAcid rain negatively affects plants byA. reducing the photosynthet.pdf
Acid rain negatively affects plants byA. reducing the photosynthet.pdfaromanets
 
A random variable X is exponentially distributed with a mean of 0.14.pdf
A random variable X is exponentially distributed with a mean of 0.14.pdfA random variable X is exponentially distributed with a mean of 0.14.pdf
A random variable X is exponentially distributed with a mean of 0.14.pdfaromanets
 
A protein, called pRb (protein for retinoblastoma) is synthesized by.pdf
A protein, called pRb (protein for retinoblastoma) is synthesized by.pdfA protein, called pRb (protein for retinoblastoma) is synthesized by.pdf
A protein, called pRb (protein for retinoblastoma) is synthesized by.pdfaromanets
 
What kind of bond would form between NH_4 and Cl^- to make the salt a.pdf
What kind of bond would form between NH_4 and Cl^- to make the salt a.pdfWhat kind of bond would form between NH_4 and Cl^- to make the salt a.pdf
What kind of bond would form between NH_4 and Cl^- to make the salt a.pdfaromanets
 
Which muscle provides a guide to the position of the radial artery at.pdf
Which muscle provides a guide to the position of the radial artery at.pdfWhich muscle provides a guide to the position of the radial artery at.pdf
Which muscle provides a guide to the position of the radial artery at.pdfaromanets
 
Which of the following is a true statement regarding lysosomesWhi.pdf
Which of the following is a true statement regarding lysosomesWhi.pdfWhich of the following is a true statement regarding lysosomesWhi.pdf
Which of the following is a true statement regarding lysosomesWhi.pdfaromanets
 
When a signal arrives at the cell, it gets transduced. What is meant.pdf
When a signal arrives at the cell, it gets transduced. What is meant.pdfWhen a signal arrives at the cell, it gets transduced. What is meant.pdf
When a signal arrives at the cell, it gets transduced. What is meant.pdfaromanets
 

More from aromanets (20)

Multiple sclerosis (like many CNS disorders) is more descriptive.pdf
Multiple sclerosis (like many CNS disorders) is more descriptive.pdfMultiple sclerosis (like many CNS disorders) is more descriptive.pdf
Multiple sclerosis (like many CNS disorders) is more descriptive.pdf
 
In your own words, define the following terms. Concentrate on structu.pdf
In your own words, define the following terms. Concentrate on structu.pdfIn your own words, define the following terms. Concentrate on structu.pdf
In your own words, define the following terms. Concentrate on structu.pdf
 
Is R with the cofinite topo1o separable Prove your answer. Solut.pdf
Is R with the cofinite topo1o separable Prove your answer. Solut.pdfIs R with the cofinite topo1o separable Prove your answer. Solut.pdf
Is R with the cofinite topo1o separable Prove your answer. Solut.pdf
 
Identify the impacts of dementia.Is it preventable in our future a.pdf
Identify the impacts of dementia.Is it preventable in our future a.pdfIdentify the impacts of dementia.Is it preventable in our future a.pdf
Identify the impacts of dementia.Is it preventable in our future a.pdf
 
Heisenberg Uncertainty question...Why does my professor write delt.pdf
Heisenberg Uncertainty question...Why does my professor write delt.pdfHeisenberg Uncertainty question...Why does my professor write delt.pdf
Heisenberg Uncertainty question...Why does my professor write delt.pdf
 
How did the Spanish crown initially fulfill its need for African sla.pdf
How did the Spanish crown initially fulfill its need for African sla.pdfHow did the Spanish crown initially fulfill its need for African sla.pdf
How did the Spanish crown initially fulfill its need for African sla.pdf
 
Four different fluorescent dyes are used in automated DNA sequencing.pdf
Four different fluorescent dyes are used in automated DNA sequencing.pdfFour different fluorescent dyes are used in automated DNA sequencing.pdf
Four different fluorescent dyes are used in automated DNA sequencing.pdf
 
For the investment situation below. Identify the annual Interest rate.pdf
For the investment situation below. Identify the annual Interest rate.pdfFor the investment situation below. Identify the annual Interest rate.pdf
For the investment situation below. Identify the annual Interest rate.pdf
 
For a given class, the studen records are stored in a file. The reco.pdf
For a given class, the studen records are stored in a file. The reco.pdfFor a given class, the studen records are stored in a file. The reco.pdf
For a given class, the studen records are stored in a file. The reco.pdf
 
Elementary school children spent significantly more time reading i.pdf
Elementary school children spent significantly more time reading i.pdfElementary school children spent significantly more time reading i.pdf
Elementary school children spent significantly more time reading i.pdf
 
Describe the tools and technology used to support IT project managem.pdf
Describe the tools and technology used to support IT project managem.pdfDescribe the tools and technology used to support IT project managem.pdf
Describe the tools and technology used to support IT project managem.pdf
 
Compare the following bacterial species, bacterial strain, and bact.pdf
Compare the following bacterial species, bacterial strain, and bact.pdfCompare the following bacterial species, bacterial strain, and bact.pdf
Compare the following bacterial species, bacterial strain, and bact.pdf
 
Annual estimates of the population in a Hawaiian county from 1999 (t .pdf
Annual estimates of the population in a Hawaiian county from 1999 (t .pdfAnnual estimates of the population in a Hawaiian county from 1999 (t .pdf
Annual estimates of the population in a Hawaiian county from 1999 (t .pdf
 
Acid rain negatively affects plants byA. reducing the photosynthet.pdf
Acid rain negatively affects plants byA. reducing the photosynthet.pdfAcid rain negatively affects plants byA. reducing the photosynthet.pdf
Acid rain negatively affects plants byA. reducing the photosynthet.pdf
 
A random variable X is exponentially distributed with a mean of 0.14.pdf
A random variable X is exponentially distributed with a mean of 0.14.pdfA random variable X is exponentially distributed with a mean of 0.14.pdf
A random variable X is exponentially distributed with a mean of 0.14.pdf
 
A protein, called pRb (protein for retinoblastoma) is synthesized by.pdf
A protein, called pRb (protein for retinoblastoma) is synthesized by.pdfA protein, called pRb (protein for retinoblastoma) is synthesized by.pdf
A protein, called pRb (protein for retinoblastoma) is synthesized by.pdf
 
What kind of bond would form between NH_4 and Cl^- to make the salt a.pdf
What kind of bond would form between NH_4 and Cl^- to make the salt a.pdfWhat kind of bond would form between NH_4 and Cl^- to make the salt a.pdf
What kind of bond would form between NH_4 and Cl^- to make the salt a.pdf
 
Which muscle provides a guide to the position of the radial artery at.pdf
Which muscle provides a guide to the position of the radial artery at.pdfWhich muscle provides a guide to the position of the radial artery at.pdf
Which muscle provides a guide to the position of the radial artery at.pdf
 
Which of the following is a true statement regarding lysosomesWhi.pdf
Which of the following is a true statement regarding lysosomesWhi.pdfWhich of the following is a true statement regarding lysosomesWhi.pdf
Which of the following is a true statement regarding lysosomesWhi.pdf
 
When a signal arrives at the cell, it gets transduced. What is meant.pdf
When a signal arrives at the cell, it gets transduced. What is meant.pdfWhen a signal arrives at the cell, it gets transduced. What is meant.pdf
When a signal arrives at the cell, it gets transduced. What is meant.pdf
 

Recently uploaded

Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementmkooblal
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxRaymartEstabillo3
 
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxHistory Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxsocialsciencegdgrohi
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxAvyJaneVismanos
 
MARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupMARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupJonathanParaisoCruz
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfadityarao40181
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceSamikshaHamane
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
Capitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitolTechU
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfMahmoud M. Sallam
 

Recently uploaded (20)

ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of management
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
 
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxHistory Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptx
 
MARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupMARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized Group
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdf
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in Pharmacovigilance
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
Capitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptx
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdf
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
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.");
  • 10. // 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
  • 11. */ 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() {
  • 12. 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 */
  • 13. 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");
  • 14. return getName() + " [SideA: " + fmt.format(mySideA) + ", SideB:" + fmt.format(mySideB) + ", SideC:" + fmt.format(mySideC) + "] Area: " + fmt.format(calculateArea()); } }