Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Part #2 - Programming (20 pts) Your assignment is to write a class definition (n

ID: 3762232 • Letter: P

Question

Part #2 - Programming (20 pts)

Your assignment is to 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.

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. A sample output is shown below.

Important

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.

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 Outputs

Sample 1:
Enter the sides of the triangle: 345
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: 456
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: 444

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: 377
3 7 7 triangle:

It is isosceles
It is not a equilateral It is not scalene

Check another Triangle (y/n)? n Press any key to continue . . .

Explanation / Answer

Program code:

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

import java.util.Scanner;

class Triangle {

// private static char in1;

int side1, side2, side3;

Triangle (int a, int b, int c) {

side1 = a; side2 = b; side3 = c;

}

boolean isScalene() {

return (side1 != side2 ||

side1 != side3 ||

side2 != side3);

}

boolean isIsosceles() {

return (side1 == side2 ||

side1 == side3 ||

side2 == side3);

}

boolean isEquilateral() {

return (side1 == side2 && side1 == side3);

}

public static void main(String[] args) throws IOException {

char in1;

BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

do

{

System.out.println("Enter the sides of triangle:");

int x,y,z;

x=new Scanner(System.in).nextInt();

y=new Scanner(System.in).nextInt();

z=new Scanner(System.in).nextInt();

Triangle a, b, c;

a = new Triangle( x, y, z);

boolean flag=a.isIsosceles();

if(flag)

{

System.out.println("it is Isosceles triangle");

}

else

System.out.println("it is not Isosceles triangle");

boolean flag1=a.isEquilateral();

if(flag1)

{

System.out.println("it is Equilateral triangle");

}

else

System.out.println("it is not Equilateral triangle");

boolean flag2=a.isScalene();

if(flag2)

{

System.out.println("it is Scalene triangle");

}

else

System.out.println("it is not Scalene triangle");

System.out.println("sides of triangle are:"+ a.toString());

System.out.println("Do you want to Continue [Y/N] ");

in1=(char)in.read();

} while(in1=='y'||in1=='Y');

}

public String toString() {

return + side1 + ", " + side2 + ", " + side3;

}

}

Sample output: