Write a class definition not a program there is no main meth

Write a class definition (not a program, there is no main method) named Triangle (saved in a file Triangle.java). A Triangle has 3 instance variables: int side1, side2, side3; The class Triangle must include the following constructors and methods: (If your class does not contain any of the following methods, points will be deducted). • public Triangle (int s1, int s2, int s3) - Sets up a triangle with the specified side lengths. • private int largest() - Returns the length of the longest side of the triangle. This is a helper method. 1 • private int shortest() Returns the length of the shortest side of the triangle. This is a helper method. • public boolean is_equilateral() - Determines whether a triangle is equilateral. If the longest side is equal to the shortest side, then the triangle is equilateral. • public boolean is_isosceles() - Determines whether a triangle is isosceles. Any (and at least) two sides must be equal. • public boolean is_scalene() - Determines whether a triangle is scalene. No two sides are equal. • public String toString() - Prints the sides of the triangle.

Save the Triangle class in a file called Triangle.java and use the following program stored in Assignment4.java which has the main method to create a new Triangle object and to test what kind of Triangle it is.

Your class should have exactly the method headers that are described or otherwise your class will not work with the test driver program (Assignment4.java) that is provided. You should never change the test driver program if the test driver is provided but instead make changes to Triangle class to make it work.

Assignment 4:

A sample output is shown below.

Helpful Hints • Work on it in steps - write one method, test it with a test driver and make sure it works before going on to the next method. • Always make sure your code compiles before you add another method. • Your methods should be able to be called in any order. Triangle equal = new Triangle (6, 6, 6); //example of equilateral (and isosceles) triangle Triangle isosceles = new Triangle (3, 7, 7); // example of isosceles triangle Triangle scalene = new Triangle (4, 5, 6); // example of scalene triangle

----------------------------------------------------------------------------------------------------------------------------------------------------------------------

Sample Output:

Enter the sides of the triangle: 3 4 5 3 4 5

triangle: It is not isosceles It is not a equilateral. It is scalene

Check another Triangle (y/n)? y

Enter the sides of the triangle: 4 5 6 4 5 6

triangle: It is not isosceles It is not a equilateral. It is scalene

Check another Triangle (y/n)? y

Enter the sides of the triangle: 4 4 4 2 4 4 4

triangle: It is isosceles It is equilateral It is not scalene

Check another Triangle (y/n)? y

Enter the sides of the triangle: 3 7 7 3 7 7

triangle: It is isosceles It is not a equilateral It is not scalene Check another Triangle (y/n)? n

Solution

Triangle.java

public class Triangle {

   //Declaring instance variables
   private int num1,num2,num3;

   //Parameterized constructor
   public Triangle(int num1, int num2, int num3) {
       super();
       this.num1 = num1;
       this.num2 = num2;
       this.num3 = num3;
   }

   //Setters and getters
   public int getNum1() {
       return num1;
   }


   public void setNum1(int num1) {
       this.num1 = num1;
   }


   public int getNum2() {
       return num2;
   }


   public void setNum2(int num2) {
       this.num2 = num2;
   }


   public int getNum3() {
       return num3;
   }


   public void setNum3(int num3) {
       this.num3 = num3;
   }


   //This method checks whether the triangle is isosceles or not
   public boolean is_isosceles() {
       if(getNum1()==getNum2() || getNum2()==getNum3() || getNum3()==getNum1())
       return true;
       else
       return false;
   }

   //This method checks whether the triangle is equilateral or not
   public boolean is_equilateral() {
       int min,max;
      
       //Getting the largest side
       if(getNum1()>getNum2() && getNum1()>getNum3())
       {
           max=getNum1();
       }
       else if(getNum2()>getNum3() && getNum2()>getNum1())
       {
           max=getNum2();
       }
       else
       max=getNum3();  
      
      
       //GHetting the minimum size
       if(getNum1()<getNum2() && getNum1()<getNum3())
       {
           min=getNum1();
       }
       else if(getNum2()<getNum3() && getNum2()<getNum1())
       {
           min=getNum2();
       }
       else
           min=getNum3();
      
       if(min==max)
           return true;
       else
           return false;
   }

  
   //This method checks whether the triangle is scalene or not
   public boolean is_scalene() {
       if(getNum1()!=getNum2() && getNum2()!=getNum3() && getNum3()!=getNum1())
       return true;
       else
       return false;
   }


   //toString() method which is used to display the contents of an object inside it
   @Override
   public String toString() {
       return \"\";
   }
  

}

________________________________

Assignment4.java


import java.util.Scanner;
public class Assignment4
{

//===========================================================
// Create and determine properties of various triangles.
//===========================================================
public static void main (String[] args)
{
Scanner console = new Scanner(System.in);
int num1, num2, num3;
String another;
  
// repeat until the user enter \'n\'
do
{
// get the input
System.out.println(\"Enter the sides of the triangle: \");
num1 = console.nextInt();
num2 = console.nextInt();
num3 = console.nextInt();

// create the Triangle
Triangle myTriangle = new Triangle (num1, num2, num3);

// print the Triangle
System.out.println(myTriangle.toString() + \" triangle:\");

//check the isosceles
if (myTriangle.is_isosceles())
System.out.println(\"\\tIt is isosceles\");
else
System.out.println(\"\\tIt is not isosceles\");
  
//check the equilateral
if (myTriangle.is_equilateral())
System.out.println(\"\\tIt is equilateral\");
else
System.out.println(\"\\tIt is not a equilateral\");
  
//check the scalene
if (myTriangle.is_scalene())
System.out.println(\"\\tIt is scalene\");
else
System.out.println(\"\\tIt is not scalene\");

// find if the user want to repeat
System.out.println();
System.out.print(\"Check another Triangle (y/n)? \");
another = console.next();

} while (another.equalsIgnoreCase(\"y\"));


} // method main

} // class Assignment4

_______________________________

Output:

Enter the sides of the triangle:
5 5 5
triangle:
   It is isosceles
   It is equilateral
   It is not scalene

Check another Triangle (y/n)? y
Enter the sides of the triangle:
4 6 6
triangle:
   It is isosceles
   It is not a equilateral
   It is not scalene

Check another Triangle (y/n)? y
Enter the sides of the triangle:
1 2 3
triangle:
   It is not isosceles
   It is not a equilateral
   It is scalene

Check another Triangle (y/n)? n

__________Thank You

Write a class definition (not a program, there is no main method) named Triangle (saved in a file Triangle.java). A Triangle has 3 instance variables: int side1
Write a class definition (not a program, there is no main method) named Triangle (saved in a file Triangle.java). A Triangle has 3 instance variables: int side1
Write a class definition (not a program, there is no main method) named Triangle (saved in a file Triangle.java). A Triangle has 3 instance variables: int side1
Write a class definition (not a program, there is no main method) named Triangle (saved in a file Triangle.java). A Triangle has 3 instance variables: int side1
Write a class definition (not a program, there is no main method) named Triangle (saved in a file Triangle.java). A Triangle has 3 instance variables: int side1

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site