Question

In: Computer Science

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 will return true if and only if the sum of side1+ side2 is greater than side3 AND the sum side2+side3 is greater than side1 AND the sum of side1+ side3 is greater than side2. If any of those three conditions is not met, the method will create and throw an IllegalTriangleSideException. Add a main method to create and check two to three different triangles.

Solutions

Expert Solution

class IllegalTriangleSideException extends RuntimeException{
   public IllegalTriangleSideException(String m) {
       super(m);
   }
}
public class Triangle {
   private double side1;
   private double side2;
   private double side3;
   public Triangle(double aSide1, double aSide2, double aSide3) {
       super();
       side1 = aSide1;
       side2 = aSide2;
       side3 = aSide3;
   }
   boolean checkSides() throws IllegalTriangleSideException{
       if ((side1 + side2) > side3)
               throw new IllegalTriangleSideException("Invalid Sides");
       else
           return true;
   }
}

NOTE : PLEASE COMMENT BELOW IF YOU HAVE CONCERNS.

I AM HERE TO HELP YOUIF YOU LIKE MY ANSWER PLEASE RATE AND HELP ME IT IS VERY IMP FOR ME


Related Solutions

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 class named GameCharacter to define an object as follows: The class should contain class...
Create a class named GameCharacter to define an object as follows: The class should contain class variables for charName (a string to store the character's name), charType (a string to store the character's type), charHealth (an int to store the character's health rating), and charScore (an int to store the character's current score). Provide a constructor with parameters for the name, type, health and score and include code to assign the received values to the four class variables. Provide a...
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...
Create a java program that: - Has a class that defines an exception -Have that exception...
Create a java program that: - Has a class that defines an exception -Have that exception throw(n) in one method, and be caught and handled in another one. -Has a program that always continues even if incorrect data is entered by the user -has a minimum of 2 classes in it
Create a file named StudentArrayList.java,within the file create a class named StudentArrayList. This class is meant...
Create a file named StudentArrayList.java,within the file create a class named StudentArrayList. This class is meant to mimic the ArrayList data structure. It will hold an ordered list of items. This list should have a variable size, meaning an arbitrary number of items may be added to the list. Most importantly this class should implement the interface SimpleArrayList provided. Feel free to add as many other functions and methods as needed to your class to accomplish this task. In other...
In java: -Create a class named Animal
In java: -Create a class named Animal
Create a Class to contain a customer order Create attributes of that class to store Company...
Create a Class to contain a customer order Create attributes of that class to store Company Name, Address and Sales Tax. Create a public property for each of these attributes. Create a class constructor without parameters that initializes the attributes to default values. Create a class constructor with parameters that initializes the attributes to the passed in parameter values. Create a behavior of that class to generate a welcome message that includes the company name. Create a Class to contain...
c++ Add exception handling to the MyStack class (e.g. an instance of the class should throw...
c++ Add exception handling to the MyStack class (e.g. an instance of the class should throw an exception if an attempt is made to remove a value from an empty stack) and use the MyStack class to measure the execution cost of throwing an exception. Create an empty stack and, within a loop, repeatedly execute the following try block: try { i n t s t a c k . pop ( ) ; } catch ( e x c...
JAVA PROGRAMMING. In this assignment, you are to create a class named Payroll. In the class,...
JAVA PROGRAMMING. In this assignment, you are to create a class named Payroll. In the class, you are to have the following data members: name: String (5 pts) id: String   (5 pts) hours: int   (5 pts) rate: double (5 pts) private members (5 pts) You are to create no-arg and parameterized constructors and the appropriate setters(accessors) and getters (mutators). (20 pts) The class definition should also handle the following exceptions: An employee name should not be empty, otherwise an exception...
Create a Java project called Lab3B and a class named Lab3B. Create a second new class...
Create a Java project called Lab3B and a class named Lab3B. Create a second new class named Book. In the Book class: Add the following private instance variables: title (String) author (String) rating (int) Add a constructor that receives 3 parameters (one for each instance variable) and sets each instance variable equal to the corresponding variable. Add a second constructor that receives only 2 String parameters, inTitle and inAuthor. This constructor should only assign input parameter values to title and...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT