Question

In: Computer Science

In Java Design a Triangle class (Triangle.java) that extends GeometricObject. Draw the UML diagram for both...

In Java

Design a Triangle class (Triangle.java) that extends GeometricObject. Draw the UML diagram for both classes and implement Triangle. The Triangle class contains: ▪ Three double data fields named side1, side2, and side3 with default values 1.0 to denote three sides of the triangle. ▪ A no-arg constructor that creates a default triangle. ▪ A constructor that creates a triangle with the specified side1, side2, side3, color, and filled arguments. ▪ The accessor methods for all three data fields. ▪ A method named getArea() that returns the area of this triangle. ▪ A method named getPerimeter() that returns the perimeter of this triangle. ▪ A method named toString() that returns a string description for the triangle. return "Triangle: side1 = " + side1 + " side2 = " + side2 + " side3 = " + side3; The formulas to compute the area of a triangle are as follows: ? = (????1 + ????2 + ????3) 2 ???? = √?(? − ????1)(? − ????2)(? − ????3) The implementation for the Triangle’s toString() method is as follows: return "Triangle:\n" + super.toString() + "\nTriangle: side1 = " + side1 + " side2 = " + side2 + " side3 = " + side3; Download the attached project: HomeworkCh11.zip. Complete the HomeworkCh11.java test program as follows 1. Prompt the user to enter a. Each of the three sides of a triangle, b. the Triangle’s color, and c. whether the triangle is filled. 2. The program should create a Triangle object with these sides and set the color and filled properties using the input. 3. The program should display the area, perimeter, color, and true or false to indicate whether it is filled or not. 4.

Create an ArrayList and add at least one of each of the following objects to it: Triangle, String, Rectangle, Date, Circle. The list should contain at least seven objects.

The implementation for instantiating the ArrayList and adding objects to it is as follows: ArrayListlist = new ArrayList(); list.add(new Circle(1.5, "green", true)); list.add(new Date()); 5. Create a loop to display all the elements in the list by invoking its toString method. Use the instanceof operator to invoke the correct getArea() and getPerimeter() methods to display additional information about each shape object.

Solutions

Expert Solution



public class Triangle extends GeometricObject {

    // Three Double fields of type double. You can use Double class or primitive type double.
    double side1 = 1.0; // Default value is 1.0 so gave it while initializing. There are other places too where you can do it like init blocks and constructors but this is simple and easy way.
    double side2 = 1.0;
    double side3 = 1.0;

    String color;
    boolean isFilled;


    // No Arg Constructor
    public Triangle() {
    }

    public Triangle(double side1, double side2, double side3, String color, boolean isFilled) {
        this.side1 = side1;
        this.side2 = side2;
        this.side3 = side3;
        this.color = color;
        this.isFilled = isFilled;
    }


    // Returns Area of a Triangle
    public double getArea(){
        double s = side1 + side2 + side3;

        double side1Diff =  s - side1;
        double side2Diff =  s - side2;
        double side3Diff =  s - side3;

        return (Math.sqrt(s * side1Diff * side2Diff * side3Diff))/2;
    }

    // Returns perimeter of this triangle
    public double getPerimeter(){
        return side1+side2+side3;
    }

    @Override
    public String toString() {
        return "Triangle: side1 = " + side1 + " side2 = " + side2 + " side3 = " + side3;
    }

    // Accessor methods for three data fields.
    public double getSide1() {
        return side1;
    }

    public void setSide1(double side1) {
        this.side1 = side1;
    }

    public double getSide2() {
        return side2;
    }

    public void setSide2(double side2) {
        this.side2 = side2;
    }

    public double getSide3() {
        return side3;
    }

    public void setSide3(double side3) {
        this.side3 = side3;
    }

    public String getColor() {
        return this.color;
    }

    public boolean isFilled() {
        return this.isFilled;
    }
}

import java.util.Scanner;

public class TriangleTest {

    public static void main(String[] args) {

        Scanner sc =  new Scanner(System.in); // Scanner Object to take input from user.

        System.out.println("Enter First Side of a Triangle = " );
        double side1 = sc.nextDouble();

        System.out.println("Enter Second Side of a Triangle = " );
        double side2 = sc.nextDouble();

        System.out.println("Enter Third Side of a Triangle = " );
        double side3 = sc.nextDouble();

        System.out.println("Enter Color of a Triangle = " );
        String color = sc.next();

        System.out.println("Is Triangle Filled  true/false");
        boolean isFilled = sc.nextBoolean();

        // Create triangle object with the information provided by user.
        Triangle triangle = new Triangle(side1,side2,side3,color,isFilled);

        System.out.println("Area = " + triangle.getArea());
        System.out.println("Perimeter = " + triangle.getPerimeter());
        System.out.println("Color = " + triangle.getColor());
        System.out.println("Is Filled = " + triangle.isFilled());

    }
}

Last part of the question seems related to some other information not provided above.

UML is attached.


Related Solutions

draw a uml diagram for a class
draw a uml diagram for a class
using java Design a class named Triangle that extends GeometricObject. The class contains: • Three double...
using java Design a class named Triangle that extends GeometricObject. The class contains: • Three double data fields named side1, side2, and side3 to denote three sides of a triangle. • A no-arg constructor that creates a default triangle with default values 1.0. • A constructor that creates a triangle with the specified side1, side2, and side3. In a triangle, the sum of any two sides is greater than the other side. The Triangle class must adhere to this rule...
In java design and code a class named comparableTriangle that extends Triangle and implements Comparable. Implement...
In java design and code a class named comparableTriangle that extends Triangle and implements Comparable. Implement the compareTo method to compare the triangles on the basis of similarity. Draw the UML diagram for your classes. Write a Driver class (class which instantiates the comparableTriangle objects) to determine if two instances of ComparableTriangle objects are similar (sample output below). It should prompt the user to enter the 3 sides of each triangle and then display whether or not the are similar...
Draw a UML diagram for the classes. Code for UML: // Date.java public class Date {...
Draw a UML diagram for the classes. Code for UML: // Date.java public class Date {       public int month;    public int day;    public int year;    public Date(int month, int day, int year) {    this.month = month;    this.day = day;    this.year = year;    }       public Date() {    this.month = 0;    this.day = 0;    this.year = 0;    } } //end of Date.java // Name.java public class Name...
create the UML Diagram to model a Movie/TV viewing site. Draw a complete UML class diagram...
create the UML Diagram to model a Movie/TV viewing site. Draw a complete UML class diagram which shows: Classes (Only the ones listed in bold below) Attributes in classes (remember to indicate privacy level, and type) No need to write methods Relationships between classes (has is, is a, ...) Use a program like Lucid Cart and then upload your diagram. Each movie has: name, description, length, list of actors, list of prequals and sequals Each TV Show has: name, description,...
Draw a UML diagram that describes a class that will be used to describe a product...
Draw a UML diagram that describes a class that will be used to describe a product for sale on Glamazon.com. The product has a name, a description, a price, ratings by many customers (1 to 5 stars), and a group of customer comments. New products have no ratings or comments by customers, but do have a name, description and price. The price can be changed and more customer ratings and comments can be added. A global average rating of all...
in java please Project 2: The Triangle Class Problem Description: Design a class named Triangle that...
in java please Project 2: The Triangle Class Problem Description: Design a class named Triangle that extends GeometricObject. The class contains: • Three double data fields named side1, side2, and side3 with default values 1.0 to denote three sides of the triangle. • A no-arg constructor that creates a default triangle. • A constructor that creates a triangle with the specified side1, side2, and side3. • The accessor methods for all three data fields. • A method named getArea() that...
Complete the following class UML design class diagram by filling in all the sections based on...
Complete the following class UML design class diagram by filling in all the sections based on the information given below.         The class name is Boat, and it is a concrete entity class. All three attributes are private strings with initial null values. The attribute boat identifier has the property of “key.” The other attributes are the manufacturer of the boat and the model of the boat. Provide at least two methods for this class. Class Name: Attribute Names: Method...
Java Write a Payroll class as demonstrated in the following UML diagram. Member variables of the...
Java Write a Payroll class as demonstrated in the following UML diagram. Member variables of the class are: NUM_EMPLOYEES: a constant integer variable to hold the number of employees. employeeId. An array of integers to hold employee identification numbers. hours. An array of integers to hold the number of hours worked by each employee payRate. An array of doubles to hold each employee’s hourly pay rate wages. An array of seven doubles to hold each employee’s gross wages The class...
Design the Class Diagram (UML) for a banking system with its clients’ accounts and an ATM...
Design the Class Diagram (UML) for a banking system with its clients’ accounts and an ATM machine. Each client account holds the user’s name (String of Full Name), account number (int) and balance (int). All client account details should be hidden from other classes but the system should provide tools/methods to access, change, and display (toString for account) each of these details. The ATM machine holds the available money inside it (double) and the maximum money it can hold (double)....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT