QuestionQuestion

Transcribed TextTranscribed Text

Project 5 Inheritance . Files to be submitted:  A class diagram – created either by Violet UML editor or ArgoUML or StarUML  Java Source code o Shape, o Rectangle, RectangleDriver, o Triangle, TriangleDriver, o Circle, CircleDriver, o InvalidTriangleException o Helper class, o and a driver  Supporting file o A text file containing data that can be used to form shapes. Write a program to model a generic shape and a list of specific shapes such as a rectangle, a triangle, and a circle. Test each class with its own driver. Create a helper class with helper methods that the driver program will use for testing all classes. It is required to meet all the requirements that are included below. Part I Shape: An abstract class A Shape object will have one field: Fields Description name The name of this shape. The Shape class will also have the following methods. Method Description Default constructor Creates a Shape instance with a default name referred as this Shape object. The second constructor Creates a Shape instance with a specific name referred as this Shape object. getName Retrieves the name of this shape. setName Changes the name of this shape. area Returns the area of this shape. Note: this is an abstract method that will be implemented in the sub classes. Shape Rectangle Circle Triangle 6 equals Compares this shape with some other object. This method overrides Java equals method. toString Represents this shape as a string literal. This method overrides Java toString method. This class will be tested where its sub classes are tested. Part II Rectangle: A sub class of Shape. Note: all instance variables and public methods of a super class are inherited by its sub classes as if they were written in the sub classes. A Rectangle object will have the following fields: Fields Description length The length of this rectangle. width The width of this rectangle. The Rectangle class will have the following methods: Method Description Default constructor Creates a default rectangle instance. By default, a rectangle has the following values for its fields: name: “Rectangle” length: 1.0 width: 1.0 The second constructor Creates a rectangle instance with a specific name, specific length, and a specific width. For example, a rectangle can be called “Small rectangle” of size 12.3 x 23.7. This constructor should have three parameters: name, length, and width so that we can create a rectangle by passing three arguments into it in order. For example, new Rectangle (“Small rectangle”, 12.3,23.7). getLength Retrieves the length of this rectangle. setLength Changes the length of this rectangle. getWidth Retrieves the width of this rectangle. setWidth Changes the width of this rectangle. area Returns the area of this rectangle. Note: this method is originally written in the super class, and is implemented to return the area of this rectangle in this class. equals Compares this rectangle with some other object. This method overrides Java equals method. toString Represents this rectangle as a string literal. This method overrides Java toString method. RectangleDriver: A class to test Rectangle class. /** * Test the design of Rectangle class only. */ public class RectangleDriver{ 7 /** * Tests all methods in Rectangle design. * @param args A reference to a string array containing command-line * arguments */ public static void main(String[] args){ //Declare all variables here. //Create at least two Rectangle objects. //Retrieve the name of first Rectangle, store it in memory, and print it. //Retrieve the length of first Rectangle, store it in memory, and print it. //Retrieve the width of first Rectangle, store it in memory, and print it. //Change the name of the first rectangle. //Retrieve the name of the first rectangle, store it in memory, and print it. //Change the length of the first rectangle. //Retrieve the length of the first rectangle, store it in memory, and print it. //Change the width of the first rectangle. //Retrieve the width of the first rectangle, store it in memory, and print it. //Calculate the area of the first rectangle, stored in in memory, and print it. //Print first rectangle. //Print other rectangle objects. //Compare the first rectangle with itself, print the results. //Compare the first rectangle with another rectangle, print the results. //Compare the first rectangle with a String object, print the results. } } Part III Circle: A sub class of Shape. Note: all instance variables and public methods of a super class are inherited by its sub classes as if they were written in the sub classes. A Circle object will have the following fields: Fields Description radius The radius of this circle. The Circle class will also have the following methods: Method Description Default constructor Creates a default circle instance. By default, a circle has the following values for its fields: name: “Circle” radius: 1.0 The second constructor Creates a circle instance with a specific name, specific length, and a specific width. For example, a rectangle can be called “Big circle” of radius of 23.7. This constructor should have three parameters: name and radius so that we can create a circle by passing two arguments into it in order. For example, new Circle (“Circle”,23.7). getRadius Retrieves the radius of this circle. 8 setRadius Changes the radius of this circle. area Returns the area of this circle. Note: this method is originally written in the super class, and is implemented to return the area of this circle in this class. equals Compares this circle with some other object. This method overrides Java equals method. toString Represents this circle as a string literal. This method overrides Java toString method. CircleDriver: A class to test Circle class. /** * Test the design of Circle class only. */ public class CircleDriver{ /** * Tests all methods in Rectangle design. * @param args A reference to a string array containing command-line * arguments */ public static void main(String[] args){ //Declare all variables here. //Create at least two Circle objects. //Retrieve the name of first circle, store it in memory, and print it. //Retrieve the radius of first circle, store it in memory, and print it. //Change the name of the first circle. //Retrieve the name of the first circle, store it in memory, and print it. //Change the radius of the first circle. //Retrieve the radius of the first circle, store it in memory, and print it. //Calculate the area of the first circle, stored in in memory, and print it. //Print first circle. //Print other circle objects. //Compare the first circle with itself, print the results. //Compare the first circle with another circle, print the results. //Compare the first circle with a String object, print the results. } } Part VI Triangle: A sub class of Shape. Note: all instance variables and public methods of a super class are inherited by its sub classes as if they were written in the sub classes. A Triangle object will have the following fields: Fields Description sideOne The side one of this triangle. sideTwo The side two of this triangle. sideThree The side three of this triangle. 9 The Triangle class will also have the following methods: Method Description Default constructor Creates a default triangle instance. By default, a circle has the following values for its fields: name: “Triangle” sideOne: 1.0 sideTwo: 1.0 sideThree: 1.0 The second constructor Creates a triangle instance with a specific name and specific sides. For example, a triangle can be called “Right triangle” of sides 3.0, 4.0, and 5.0. This constructor should have four parameters: name and sides so that we can create a circle by passing four arguments into it in order. For example, new Triangle (“Right Triangle”,3.0, 4.0, 5.0). In a triangle, the sum of any two sides is greater than the third side. Class Triangle must adhere to this rule. If a triangle is created with sides that violate the rule, an InvalidTriangleException object with a proper message should be thrown. For example, 4.0, 8.0, and 2.0 can’t form a triangle. * Information on InvalidTriangleException is provided later. getSideOne Retrieves the side one of this triangle. setSideOne Changes the side one of this triangle. We cannot change side one of this triangle unless the new side one, this sideTwo, and this sideThree will form a triangle. If this happens, an InvalidTriangleException* object with a proper message should be thrown. For example, if a triangle has three sides 5.0, 6.0, and 7.0, we want to change sideOne to 1.0. we can’t change since 1.0, 6.0, and 7.0 wouldn’t form a triangle. * Information on InvalidTriangleException is provided later. getSideTwo Retrieves the side two of this triangle. setSideTwo Changes the side two of this triangle. We cannot change side one of this triangle unless the new side two, this sideOne, and this sideThree will form a triangle. If this happens, an InvalidTriangleException object with a proper message should be thrown. * Information on InvalidTriangleException is provided later. getSideThree Retrieves the side three of this triangle. setSideThree Changes the side three of this triangle. We cannot change side three of this triangle unless the new side three, this sideOne, and this sideTwo will form a triangle. If this happens, an InvalidTriangleException object with a proper message should be thrown. * Information on InvalidTriangleException is provided later. area Returns the area of this circle. Note: this method is originally written in the super class, and is implemented to return the area of this circle in this class. 10 Heron's formula states that the area of a triangle whose sides have lengths a, b, and c is A= √s(s − a)(s − b)(s − c), where s is the semi perimeter of the triangle; that is, s = (a+b+c) 2 equals Compares this circle with some other object. This method overrides Java equals method. toString Represents this circle as a string literal. This method overrides Java toString method. InvalidTriangleException: Create class InvalidTriangelException as a sub class of java.lang.Exception class. It has a constructor that constructs a new exception with a specified message public class InvalidTriangleException extends Exception { public InvalidTriangleException(String message) { // Call super constructor with the parameter message. } } Now it is time to add exception execution flow in Triangle class. In the second constructor, and setters, include the codes that may throw exceptions in a try block, and then add exception handlers. Always add a generic exception handler at the end to catch any exceptions. An exception can be caught and handled where it occurs. An exception can be propagated to be handled in next method in calling hierarchy. In this project, catch and handle InvalidTriangleException and other exceptions where they occur in Triangle class. The following shows one example that handles InvalidTriangleException where it occurs. If three sides don’t construct a valid triangle, an InvalidTriangelException object is thrown, and it is caught in the first exception handler. public Triangle(String name, int sideOne, int sideTwo, int sideThree){ try{ if an valid triangle can be formed with sideOne, sideTwo, and sideThree Set up the name of this triangle. Form this triangle. else Throw an InvalidTriangelException object using a throw statement. This object is caught and handled by corresponding exception handler. }catch (InvalidTriangelException ex){ Print stack trace. // Handle InvalidTriangelException. }catch (Exception ex){ Print stack trace. // handle other exceptions. }finally{ Cleanup code if any. } } Exceptions thrown in other classes can be caught or propagated. TriangleDriver: A class to test Triangle class. /** * Test the design of Triangle class only. 11 */ public class TriangleDriver{ /** * Tests all methods in Triangle design. * @param args A reference to a string array containing command-line * arguments */ public static void main(String[] args){ //Declare all variables here. //Create at least two valid Triangle objects. //Retrieve the name of first triangle, store it in memory, and print it. //Retrieve side one of first triangle, store it in memory, and print it. //Retrieve side two of first triangle, store it in memory, and print it. //Retrieve side three of first triangle, store it in memory, and print it. //Change the name of the first triangle. //Retrieve the name of the first triangle, store it in memory, and print it. //Change side one of the first triangle so that a valid triangle will be formed //with the new side one. //Retrieve side one of the first triangle, store it in memory, and print it. //Change side one of the first triangle again so that an invalid triangle would //be formed and an exception be thrown. //Change side two of the first triangle so that a valid triangle will be formed //with the new side two. //Retrieve side two of the first triangle, store it in memory, and print it. //Change side two of the first triangle again so that an invalid triangle would //be formed and an exception be thrown. //Change side three of the first triangle so that a valid triangle will be formed //with the new side three. //Retrieve side three of the first triangle, store it in memory, and print it. //Change side three of the first triangle again so that an invalid triangle would //be formed and an exception be thrown. //Create a triangle using a name and three sides that would form an invalid //triangle, and run the program. The program should be terminated here. //Print first triangle. //Print other triangle objects. //Compare the first triangle with itself, print the results. //Compare the first triangle with another triangle, print the results. //Compare the first triangle with a String object, print the results. } } Part V: Helper class: Create a helper class with three helper methods to test the design of shape package. start method: A static method This method is called in main in ShapeDriver . It calls method create that creates/returns a list of shapes. It also calls method display that displays the list of shapes in order. public static void start() throws FileNotFoundException{ 12 Scanner input = new Scanner(System.in); String fileName; ArrayList<Shape> shapes; System.out.println("Enter a file name: "); fileName = input.next(); shapes = create(fileName); // calls create that returns a list of shapes. display(shapes); // calls display to print the list of shapes in order. input.close(); } create method: a static method This method has a parameter – a file name such as “shape.txt”, and returns an array list of shapes (rectangles, triangles, and circles). This method reads shape data from a file, and create shapes. The data set for each shape consists of the type of the shape, the name of the shape and sizes of the shape. When an invalid triangle is made (the second triangle stored in the file), the program should be terminated. You must add more data into this file to test more shapes. display method: a static method This method is designed with a ArrayList<Shape> parameter, and displays each shape along with the area of each shape. An array list of shape can be passed into this method, and are printed. ShapeDriver class: This class has main method only. Method main should be very small. In this case, it calls the helper method start in ShapeHelper to start the entire execution.

Solution PreviewSolution Preview

These solutions may offer step-by-step problem-solving explanations or good writing examples that include modern styles of formatting and construction of bibliographies out of text citations and references. Students may use these solutions for personal skill-building and practice. Unethical use is strictly forbidden.

/**
* Test the design of Triangle class only.
*/
public class TriangleDriver{
    /**
    * Tests all methods in Triangle design.
    * @param args A reference to a string array containing command-line
    * arguments
    */
    public static void main(String[] args) {
       //Declare all variables here.
       Triangle triangle1;
       Triangle triangle2;
       Triangle triangle3;
       String name1;
       double sideOne1;
       double sideTwo1;
       double sideThree1;
       double area1;
       //Create at least two valid Triangle objects.
       triangle1 = new Triangle();
       try {
            triangle1 = new Triangle("Triangle 1", 7.1, 5.9, 6.5);
       } catch (InvalidTriangleException e) {
            System.err.println(e.getMessage());
       }
       triangle2 = new Triangle();
       try {
            triangle2 = new Triangle("Triangle 2", 3.0, 4.0, 5.0);
       } catch (InvalidTriangleException e) {
            System.err.println(e.getMessage());
       }
      
       //Retrieve the name of first triangle, store it in memory, and print it.
       name1 = triangle1.getName();
       System.out.println("Name of first Triangle: " + name1);
       //Retrieve side one of first triangle, store it in memory, and print it.
       sideOne1 = triangle1.getSideOne();
       System.out.println("Side One of first Triangle: " + sideOne1);
       //Retrieve side two of first triangle, store it in memory, and print it.
       sideTwo1 = triangle1.getSideTwo();
       System.out.println("Side Two of first Triangle: " + sideTwo1);
       //Retrieve side three of first triangle, store it in memory, and print it.
       sideThree1 = triangle1.getSideThree();
       System.out.println("Side Three of first Triangle: " + sideThree1);
      
       //Change the name of the first triangle.
       triangle1.setName("Triangle 1 Modified");
       //Retrieve the name of the first triangle, store it in memory, and print it.
       name1 = triangle1.getName();
       System.out.println("Name of first Triangle: " + name1);
      
       //Change side one of the first triangle so that a valid triangle will be formed
       //with the new side one.
       try {
            triangle1.setSideOne(6.1);
       } catch (InvalidTriangleException e) {
            System.err.println(e.getMessage());
       }
       //Retrieve side one of the first triangle, store it in memory, and print it.
       sideOne1 = triangle1.getSideOne();
       System.out.println("Side One of first Triangle: " + sideOne1);
      
       //Change side one of the first triangle again so that an invalid triangle would
       //be formed and an exception be thrown.
       try {
            triangle1.setSideOne(19.9);
       } catch (InvalidTriangleException e) {
            System.err.println(e.getMessage());
       }
      
       //Change side two of the first triangle so that a valid triangle will be formed
       //with the new side two.
       try {
            triangle1.setSideTwo(5.4);
       } catch (InvalidTriangleException e) {
            System.err.println(e.getMessage());
       }...

By purchasing this solution you'll be able to access the following files:
Solution.zip and Solution.png.

$70.00
for this solution

or FREE if you
register a new account!

PayPal, G Pay, ApplePay, Amazon Pay, and all major credit cards accepted.

Find A Tutor

View available Java Programming Tutors

Get College Homework Help.

Are you sure you don't want to upload any files?

Fast tutor response requires as much info as possible.

Decision:
Upload a file
Continue without uploading

SUBMIT YOUR HOMEWORK
We couldn't find that subject.
Please select the best match from the list below.

We'll send you an email right away. If it's not in your inbox, check your spam folder.

  • 1
  • 2
  • 3
Live Chats