Question

In: Computer Science

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 and throw an IllegalTriangleException object if a triangle is created with sides that violate the rule • 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. Write a test program that prompts the user to enter three sides of the triangle, a color, and a Boolean value to indicate whether the triangle is filled. The program should create a Triangle object with these sides and set the color and filled properties using the input. The program should display the area, perimeter, color, and true or false to indicate whether it is filled or not.

Solutions

Expert Solution

CODE IN JAVA:

IllegalTriangleException.java file:


public class IllegalTriangleException extends Exception{

   public IllegalTriangleException(String message) {
       super(message);
   }
  
}

GeometricObject.java file:


public abstract class GeometricObject {
   private String color ;
   private boolean filled ;
   public GeometricObject() {
       this.color = "white" ; // default
       this.filled = false ;
   }
   public GeometricObject(String color, boolean filled) {
       this.color = color;
       this.filled = filled;
   }
   public String getColor() {
       return color;
   }
   public void setColor(String color) {
       this.color = color;
   }
   public boolean isFilled() {
       return filled;
   }
   public void setFilled(boolean filled) {
       this.filled = filled;
   }
  
  
}

Triangle.java file:


public class Triangle extends GeometricObject{
   private double side1 ;
   private double side2 ;
   private double side3 ;
  
   public Triangle() {
       super();
       this.side1 = 1.0 ;
       this.side2 = 1.0 ;
       this.side3 = 1.0 ;
   }

   public Triangle(String color, boolean filled, double side1, double side2, double side3) throws IllegalTriangleException {
       super(color, filled);
       this.side1 = side1;
       this.side2 = side2;
       this.side3 = side3;
       if((side1 + side2 ) < side3)
           throw new IllegalTriangleException("the sum of any two sides should be greater than the other side");
       else if((side1 + side3) < side2)
           throw new IllegalTriangleException("the sum of any two sides should be greater than the other side");
       else if((side2 + side3) < side1)
           throw new IllegalTriangleException("the sum of any two sides should be greater than the other side");
   }

   public double getSide1() {
       return side1;
   }

   public double getSide2() {
       return side2;
   }

   public double getSide3() {
       return side3;
   }
   public double getPerimeter() {
       return this.side1 + this.side2 + this.side3 ;
   }
   public double getArea() {
       double s = (this.side1 + this.side2 + this.side3) / 2 ;
       return Math.sqrt(s * (s - this.side1) * (s - this.side2 ) * (s - this.side3)) ;
   }

  
   public String toString() {
       return "Triangle [side1=" + side1 + ", side2=" + side2 + ", side3=" + side3 + "]";
   }
  
  
}

TriangleDemo.java file:


public class Triangle extends GeometricObject{
   private double side1 ;
   private double side2 ;
   private double side3 ;
  
   public Triangle() {
       super();
       this.side1 = 1.0 ;
       this.side2 = 1.0 ;
       this.side3 = 1.0 ;
   }

   public Triangle(String color, boolean filled, double side1, double side2, double side3) throws IllegalTriangleException {
       super(color, filled);
       this.side1 = side1;
       this.side2 = side2;
       this.side3 = side3;
       if((side1 + side2 ) < side3)
           throw new IllegalTriangleException("the sum of any two sides should be greater than the other side");
       else if((side1 + side3) < side2)
           throw new IllegalTriangleException("the sum of any two sides should be greater than the other side");
       else if((side2 + side3) < side1)
           throw new IllegalTriangleException("the sum of any two sides should be greater than the other side");
   }

   public double getSide1() {
       return side1;
   }

   public double getSide2() {
       return side2;
   }

   public double getSide3() {
       return side3;
   }
   public double getPerimeter() {
       return this.side1 + this.side2 + this.side3 ;
   }
   public double getArea() {
       double s = (this.side1 + this.side2 + this.side3) / 2 ;
       return Math.sqrt(s * (s - this.side1) * (s - this.side2 ) * (s - this.side3)) ;
   }

  
   public String toString() {
       return "Triangle [side1=" + side1 + ", side2=" + side2 + ", side3=" + side3 + "]";
   }
  
  
}

OUTPUT:


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 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...
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....
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...
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...
Design a class named Fan to represent a fan. The class contains: ■ Three constants named...
Design a class named Fan to represent a fan. The class contains: ■ Three constants named SLOW, MEDIUM, and FAST with the values 1, 2, and 3 to denote the fan speed. ■ A private int data field named speed that specifies the speed of the fan (the default is SLOW). ■ A private boolean data field named on that specifies whether the fan is on (the default is false). ■ A private double data field named radius that specifies...
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...
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...
Java - Design a class named Account that contains: A private String data field named accountNumber...
Java - Design a class named Account that contains: A private String data field named accountNumber for the account (default AC000). A private double data field named balance for the account (default 0). A private double data field named annualIntRate that stores the current interest rate (default 0). Assume all accounts have the same interest rate. A private Date data field named dateCreated that stores the date when the account was created. A no-arg constructor that creates a default account....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT