Write a class called Triangle.java. The class will have three instance variables
ID: 3690949 • Letter: W
Question
Write a class called Triangle.java. The class will have three instance variables representing the sides of a triangle (sideA, sideB, sideC). You should represent these instance variables as private integers. The triangle class will have two constructors: a default constructor, and a constructor that takes in three arguments. The arguments are the three sides of a triangle. You will then create accessor(getters) and mutator(setters) methods for each instance variable. Next you will implement the following instance methods:
// A scalene triangle has all unequal sides.
public boolean isScalene()
// A right triangle satisfies the Pythagorean theorem.
public boolean isRight()
// An isosceles triangle has TWO equal sides.
public boolean isIsosceles()
// An equilateral triangle has all equal sides.
public boolean isEquilateral()
// Given a Triangle object, see if the Triangle is valid
public boolean isValid()
In the main method you will create triangle objects. You will read in the sides of a triangle from the keyboard as integers. Then with these 3 read in values, create a triangle object. Then print whether or not the triangle object is a valid triangle , a scalene triangle, a right triangle, an isosceles triangle, and an equilateral triangle. Print all return values from each instance methods. Make the output similar to the example in output section. You may use the following main method as a template:
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int a, b, c;
while (true) {
//Prompt user for side a
//Prompt user for side b
//Prompt user for side c
//Create triangle object
//print values for triangle
System.out.print("Read in a new triangle?(-1 for no, 1 for yes): ");
if(input.nextInt() < 0) break;
}
}
Example Output:
run:
Enter the length of side A: 3
Enter the length of side B: 2
Enter the length of side C: 4
Valid Triangle : true
Right Triangle : false
Scalene Triangle : true
Isosceles Triangle : false
Equilateral Triangle : false
Read in a new triangle?(-1 for no, 1 for yes): 1
Enter the length of side A: 1
Enter the length of side B: 2
Enter the length of side C: 3
Valid Triangle : false
Right Triangle : false
Scalene Triangle : true
Isosceles Triangle : false
Equilateral Triangle : false
Read in a new triangle?(-1 for no, 1 for yes): 1
Enter the length of side A: 5
Enter the length of side B: 7
Enter the length of side C: 12
Valid Triangle : false
Right Triangle : false
Scalene Triangle : true
Isosceles Triangle : false
Equilateral Triangle : false
Read in a new triangle?(-1 for no, 1 for yes): -1
BUILD SUCCESSFUL (total time: 42 seconds)
Explanation / Answer
TestTriangle.java
import java.util.Scanner;
public class TestTriangle {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (true){
System.out.print("Read a new triangle? (-1 for no, 1 for yes) :");
if (sc.nextInt() < 0) break;
System.out.println("Enter the length of side A :");
int a = sc.nextInt();
System.out.println("Enter the length of side B :");
int b = sc.nextInt();
System.out.println("Enter the length of side C :");
int c = sc.nextInt();
Triangle t1 = new Triangle(a, b, c); //creating object
System.out.println("Valid Triangle :" + t1.isValid());
System.out.println("Right Triangle :" + t1.isRight());
System.out.println("Scalene Triangle :" + t1.isScalene());
System.out.println("Isosceles Triangle :" + t1.isIsosceles());
System.out.println("Equilateral Triangle :" + t1.isEquilateral());
}
}
}
Triangle.java
public class Triangle {
private int sideA;
private int sideB;
private int sideC;
// default constructor that has three arguments
Triangle(int newSideA, int newSideB, int newSideC){
sideA = newSideA;
sideB = newSideB;
sideC = newSideC;
}
// default constructor (no argument)
Triangle(){
// this(0, 0, 0) ;
sideA=0;
sideB=0;
sideC=0;
}
// accessor of variable side A
public int getA(){
return sideA;
}
// accessor of variable side B
public int getB(){
return sideB;
}
// accessor of variable side C
public int getC(){
return sideC;
}
//mutator of variable sideA
public void setA(int newSideA){
sideA = newSideA;
}
//mutator of variable sideB
public void setB(int newSideB){
sideB = newSideB;
}
//mutator of variable sideC
public void setC(int newSideC){
sideC = newSideC;
}
//A valid triangle has (sideA + sideB < sideC, etc)
public boolean isValid(){
if ( sideA <= 0 || sideB <= 0 || sideC <= 0)
return false;
else if (((sideA + sideB) == sideC) || ((sideA + sideC) == sideB) || ((sideB + sideC) == sideA) )
return false;
else
return true;
}
//A scalene triangle has all unequal sides
public boolean isScalene(){
/* if ((sideA != sideB) && (sideA != sideC) && (sideB != sideC) )
return true;
else
return false; */
return ((sideA != sideB) && (sideA != sideC) && (sideB != sideC) );
}
//A right triangle satisfy the Pythagorean theorem
public boolean isRight(){
if (( sideA*sideA == sideB*sideB + sideC*sideC ) ||
(sideB*sideB == sideA*sideA + sideC*sideC) ||
(sideC*sideC == sideA*sideA + sideB*sideB))
return true;
else
return false;
}
// An Isosceles triangle has two equal sides
public boolean isIsosceles(){
if (((sideA == sideB) && (sideA != sideC) )||
((sideB == sideC) && (sideB != sideA)) ||
((sideA == sideC) && (sideA != sideB)) )
return true;
else
return false;
}
// An Equilateral triangle has all equal sides
public boolean isEquilateral(){
if ((sideA == sideB) && (sideA == sideC) )
return true;
else
return false;
}
}
sample output
Read a new triangle? (-1 for no, 1 for yes) :1
Enter the length of side A :
1
Enter the length of side B :
2
Enter the length of side C :
3
Valid Triangle :false
Right Triangle :false
Scalene Triangle :true
Isosceles Triangle :false
Equilateral Triangle :false
Read a new triangle? (-1 for no, 1 for yes) :-1
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.