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.

*EDIT: Programming Language: Java*

Solutions

Expert Solution

IllegalTriangleSideException.java :

//Java class which extends Exception
public class IllegalTriangleSideException extends Exception {
   //constructor
   public IllegalTriangleSideException(String ex)
   {
       super(ex);//call base call constructor
   }

}

*****************************

Triangle.java :

//Java class
public class Triangle {
   // double variables
   private double side1;
   private double side2;
   private double side3;
   //constructor
   public Triangle(double s1,double s2,double s3)
   {
       side1=s1;
       side2=s2;
       side3=s3;
   }
   //method to check sides
   public boolean checkSides() throws IllegalTriangleSideException
   {
       //checking three sides of the triangle
       if((side1+ side2)>side3 && (side2+ side3)>side1 && (side1+ side3)>side2)
       {
           //if above criteria match then
           return true;//return true
       }
       else {
           throw new IllegalTriangleSideException("Invalid triangle sides");
       }
   }

}
**********************************************

TriangleTest.java :

//Java class
public class TriangleTest {
   //main() method
   public static void main(String[] args) {
       //try catch block
       try {
           //object of Triangle class
           Triangle t=new Triangle(6,5,4);
           //calling method to check Triangle
           System.out.println(t.checkSides());
       }
       catch(Exception ex)
       {
           System.out.println("Exception :"+ex);
       }

   }

}
===========================================

Screen showing details when condition is satisfied :

Screen showing details when condition is not satisfied :


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...
An exception with finally block lab. Create a new class named ReadArray Create simple array and...
An exception with finally block lab. Create a new class named ReadArray Create simple array and read an element using a try block Catch any exception Add a finally block and print a message that the operation if complete
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...
Design a class named Message to represent a sentence or phrase. The class will contain: •...
Design a class named Message to represent a sentence or phrase. The class will contain: • a private string data field to hold the sentence or phrase. • A no-arg constructor with an empty string message. • A constructor that create a message object with the specified string sentence or phrase. • Accessor and mutator (getter/setter) for string data field. • A method named getVowels ( ) that returns the number of vowels in a sentence or phrase. • A...
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...
Create a class named Employee and its child class named Salesperson. Save each class in its...
Create a class named Employee and its child class named Salesperson. Save each class in its own file. Name your class and source code file containing the main method homework.java. Make sure each class follows these specifications: 1. An employee has a name (String), employee id number (integer), hourly pay rate (double), a timesheet which holds the hours worked for the current week (double array) and email address (String). A salesperson also has a commission rate, which is a percentage...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT