Question

In: Computer Science

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 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.

For the formula to compute the area of a triangle, see Exercise 5.19. The toString() method is implemented as follows:

return "Triangle: side1 = " + side1 + " side2 = " + side2 +   " side3 = " + side3;

Coding: (Copy and Paste Source Code here)

public class TheTriangleClass {

public static void main(String[] args) {

    Triangle triangle = new Triangle(1, 1.5, 1);

    triangle.setColor("yellow");

    triangle.setFilled(true);

    System.out.println(triangle);

    System.out.println("The area is " + triangle.getArea());

    System.out.println("The perimeter is " + triangle.getPerimeter());

    System.out.println(triangle);

}

}

class Triangle extends GeometricObject {

// Implement it (See below for a complete example, the example is for Rectangle)

}

abstract class GeometricObject {
private String color = "white";
private boolean filled;
private java.util.Date dateCreated;

/** Construct a default geometric object */
protected GeometricObject() {
dateCreated = new java.util.Date();
}

/** Construct a geometric object with color and filled value */
protected GeometricObject(String color, boolean filled)
{
dateCreated = new java.util.Date();
this.color = color;
this.filled = filled;
}

/** Return color */
public String getColor()
{
return color;
}

/** Set a new color */
public void setColor(String color)
{
this.color = color;
}

/** Return filled. Since filled is boolean,
29 * the get method is named isFilled */
public boolean isFilled()
{
return filled;
}

/** Set a new filled */
public void setFilled(boolean filled)
{
this.filled = filled;
}

/** Get dateCreated */
public java.util.Date getDateCreated()
{
return dateCreated;
}

@Override
public String toString()
{
return "created on " + dateCreated + "\ncolor: " + color + " and filled: " + filled;
}

/** Abstract method getArea */
public abstract double getArea();

/** Abstract method getPerimeter */
public abstract double getPerimeter();
}


Solutions

Expert Solution

Program code:

Output:

UML diagram that involves the classes Triangle and GeometricObject:

Code to copy:

GeometricObject.java

public class GeometricObject {
   private String color;
   private boolean filled;

   public GeometricObject() {
       color = "white";
       filled = true;
   }
   public GeometricObject(String color, boolean filled) {
       this.color = color;
       this.filled = filled;
   }

   /**Getter method for color*/

   public String getColor() {
       return color;
   }

   /**Setter method for color*/
   public void setColor(String color) {
       this.color = color;
   }

   /**Getter method for filled. Since filled is boolean,
   * so, the get method name is isFilled*/
  
   public boolean isFilled() {
       return filled;
   }

   /**Setter method for filled*/
   public void setFilled(boolean filled) {
       this.filled = filled;
   }

   /** Return a string representation of this object*/
   public String toString() {
       return "Color: " + color + " and filled: " + filled;
   }
}

Triangle.java

    public class Triangle extends GeometricObject {
  
       /*declaration of three double data fields named
       * side1, side2, and side3 with default values 1.0*/
       double side1 = 1.0;
       double side2 = 1.0;
       double side3 = 1.0;
  
       /*A no-arg constructor that creates a default triangle.*/
       public Triangle() {
          super();
           side1 = 0.0;
           side2 = 0.0;
           side3 = 0.0;
       }
  
        /*A constructor that creates a rectangle with the specified sides.*/
      public Triangle(double a, double b, double c, String color, boolean filled) {
           super(color, filled);

           side1 = a;
           side2 = b;
           side3 = c;
       }
  
       /*getter method for side1*/
       public double getSide1() {
           return side1;
       }
  
       /*setter method for side1*/
       public void setSide1(double side1) {
           this.side1 = side1;
       }
      
       /*getter method for side2*/
       public double getSide2() {
           return side2;
       }
  
       /*Setter method for side2*/
       public void setSide2(double side2) {
           this.side2 = side2;
       }
  
       /*getter method for side3*/
       public double getSide3() {
           return side3;
       }
  
       /*setter method for side3*/
       public void setSide3(double side3) {
           this.side3 = side3;
       }

  
       public void show() {
           System.out.println(side1+","+side2+","+side3+",");
       }
  
       /* method to calculate area of triangle */
       public double getArea(){
           double s=(side1+side2+side3)/2;
            double area=s*(s-side1)*(s-side2)*(s-side3);
            return area;

  
       }
  
       /* method to calculate perimeter of triangle */
       public double getPerimeter(){
           return (side1 + side2 + side3);
       }
  
       /*toString method that returns string representing sides of triangle*/
       public String toString(){
          
           return "Triangle: side1 = " + side1 + " side2 = " + side2 +
                   " side3 = " + side3 +" "+
           super.toString();

       }
   }
  

Test program Asg1.java


   /*test program that creates a Triangle object with
   * sides 1, 1.5, 1, sets color yellow and filled true*/
   public class Asg1 {
       public static void main(String[] args) {
           //create an object of Triangle class
           Triangle my = new Triangle(1,1.5,1,"yellow", true);
           //Call method getArea() to get the area of Triangle
           System.out.println("Area of Triangle " +my.getArea());
           //Call method getPerimeter() to get the perimeter of Triangle
           System.out.println("Perimeter of triangle " +my.getPerimeter());
           //call getter method getColor()
           System.out.println("color of the Triangle is " +my.getColor());
           //call getter method isFilled()
           System.out.println("is triangle filled " +my.isFilled());

       }
   }
Note:

Please give me a positive rating thank you:)


Related Solutions

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...
IN JAVA PLEASE, USE COMMENTS Following the example of Circle class, design a class named Rectangle...
IN JAVA PLEASE, USE COMMENTS Following the example of Circle class, design a class named Rectangle to represent a rectangle. The class contains: • Two double data fields named width and height that specify the width and height of the rectangle. The default values are 1 for both width and height. • A no-arg constructor that creates a default rectangle. • A constructor that creates a rectangle with specified width and height • A method name getWidth() return the value...
In Java, design a class named MyInteger. The class contains: An int data field named value...
In Java, design a class named MyInteger. The class contains: An int data field named value that stores the int value represented by this object. A constructor that creates a MyInteger object for the specified int A get method that returns the int Methods isEven(), isOdd(), and isPrime() that return true if the value is even, odd, or prime, respectively. Static methods isEven(int), isOdd(int), and isPrime(int) that return true if the specified value is even, odd, or prime, respectively. Static...
*In Java please! EXCEPTION HANDLING Concept Summary: 1. Exception   handling   2. Class   design Description Write   a  ...
*In Java please! EXCEPTION HANDLING Concept Summary: 1. Exception   handling   2. Class   design Description Write   a   program   that   creates   an   exception   class   called   ManyCharactersException,   designed   to   be   thrown   when   a   string   is   discovered   that   has   too   many   characters   in   it. Consider   a   program   that   takes   in   the   last   names   of   users.   The   driver   class   of   the   program reads the   names   (strings) from   the   user   until   the   user   enters   "DONE".   If   a   name is   entered   that   has   more   than   20   characters,  ...
Create a custom Exception named IllegalTriangleSideException. Create a class named Triangle. The Triangle class should contain...
Create a custom Exception named IllegalTriangleSideException. Create a class named Triangle. The Triangle class should contain 3 double variables containing the length of each of the triangles three sides. Create a constructor with three parameters to initialize the three sides of the triangle. Add an additional method named checkSides with method header - *boolean checkSides() throws IllegalTriangleSideException *. Write code so that checkSides makes sure that the three sides of the triangle meet the proper criteria for a triangle. It...
Create a custom Exception named IllegalTriangleSideException. Create a class named Triangle. The Triangle class should contain...
Create a custom Exception named IllegalTriangleSideException. Create a class named Triangle. The Triangle class should contain 3 double variables containing the length of each of the triangles three sides. Create a constructor with three parameters to initialize the three sides of the triangle. Add an additional method named checkSides with method header - *boolean checkSides() throws IllegalTriangleSideException *. Write code so that checkSides makes sure that the three sides of the triangle meet the proper criteria for a triangle. It...
Put In Java Programming The TicketMachine class: Design a class named TicketMachine that contains: • A...
Put In Java Programming The TicketMachine class: Design a class named TicketMachine that contains: • A double data field named price (for the price of a ticket from this machine). • A double data field named balance (for the amount of money entered by a customer). • A double data field named total (for total amount of money collected by the machine). • A constructor that creates a TicketMachine with all the three fields initialized to some values. • A...
PUT IN JAVA PROGRAMMING The Stock class: Design a class named Stock that contains: • A...
PUT IN JAVA PROGRAMMING The Stock class: Design a class named Stock that contains: • A string data field named symbol1 for the stock’s symbol. • A string data field named name for the stock’s name. • A double data field named previousClosingPrice that stores the stock price for the previous day. • A double data field named currentPrice that stores the stock price for the current time. • A constructor that creates a stock with the specified symbol and...
Term Project C++ Pet Class Problem Specification: Design a class named Pet, which should have the...
Term Project C++ Pet Class Problem Specification: Design a class named Pet, which should have the following fields: • name: The name field holds the name of a pet. • type: The type field holds the type of animal that a pet is. Example values are “Dog”, “Cat”, and “Bird”. • age: The age field holds the pet’s age. The Pet class should also have the following methods: • setName: The setName method stores a value in the name field....
PUT IN JAVA PROGRAMMING The StockB class: Design a class named StockB that contains the following...
PUT IN JAVA PROGRAMMING The StockB class: Design a class named StockB that contains the following properties: • Name of company • Number of shares owned • Value of each share • Total value of all shares and the following operations: • Acquire stock in a company • Buy more shares of the same stock • Sell stock • Update the per-share value of a stock • Display information about the holdings • The StockB class should have the proper...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT