Modify the Triangle class (from previous code, will post under this) to throw an exception in the constructor and set routines if the triangle is not valid (i.e., does not satisfy the triangle inequality). Modify the main routine to prompt the user for the sides of the triangle and to catch the exception and re-prompt the user for valid triangle sides. In addition, for any valid triangle supply a method to compute and display the area of the triangle using Herod’s method: where a, b, and c are the lengths of the sides and p is 1/2 of the perimeter of the triangle, A = [p(p-a)(p-b)(p-c)]^1/2. The area, A, should be a double displayed with 2 digits after the decimal point. Supply a getPerimeter method to compute the perimeter as an integer. Display the triangle, its area and perimeter. Allow the user to repeatedly enter triangle sides and do not exit the process until the user enters 0 for a side length. Here is a sample dialog:
Enter the sides of the triangle as integers
Enter 0 to quit:
Enter side 1: 3
Enter side 2: 4
Enter side 3: 5
Triangle with sides: 3 4 5
Is right
Is not isosceles
Is not equilateral
Area = 6.00
Perimeter = 12
Enter the sides of the triangle as integers
Enter 0 to quit:
Enter side 1: 1
Enter side 2: 1
Enter side 3: 2
Not a valid triangle
Enter the sides of the triangle as integers
Enter 0 to quit:
Enter side 1: -1
Enter side 2: 2
Enter side 3: 3
Not a valid triangle
Enter the sides of the triangle as integers
Enter 0 to quit:
Enter side 1: 1
Enter side 2: 2
Enter side 3: 2
Is not right
Is isosceles
Is not equilateral
Area = 0.97
Perimeter = 5
Enter the sides of the triangle as integers
Enter 0 to quit:
Enter side 1: 0
Exiting
---------------------------------------------------------------------------
Program to modify:
TriangleDriver.java
package chegg1;
import java.util.Scanner;
public class TriangleDriver {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
Triangle t;
int x, y, z;
System.out.println("Enter side A : ");
x = in.nextInt();
System.out.println("Enter side B : ");
y = in.nextInt();
System.out.println("Enter side C : ");
z = in.nextInt();
t = new Triangle(x, y, z);
while(t.isInvalid()) {
System.out.println("Set side A: ");
t.setA(in.nextInt());
System.out.println("Set side B: ");
t.setB(in.nextInt());
System.out.println("Set side C: ");
t.setC(in.nextInt());
}
System.out.println(t.toString());
System.out.println("Right angled: " + t.isRight());
System.out.println("Equilateral: " + t.isEquilateral());
System.out.println("Isosceles: " + t.isIsosceles());
}
}
Triangle.java
package chegg1;
public class Triangle {
private int a, b, c;
boolean isValid;
Triangle(int a, int b, int c){
if(a+b<=c || b+c<=a || c+a<=b) {
this.isValid = false;
}
else {
this.isValid = true;
this.a = a;
this.b = b;
this.c = c;
}
}
/**
* @return the a
*/
public int getA() {
return a;
}
/**
* @return the b
*/
public int getB() {
return b;
}
/**
* @return the c
*/
public int getC() {
return c;
}
/**
* @param a the a to set
*/
public void setA(int a) {
this.a = a;
this.isValid = !this.isInvalid();
}
/**
* @param b the b to set
*/
public void setB(int b) {
this.b = b;
this.isValid = !this.isInvalid();
}
/**
* @param c the c to set
*/
public void setC(int c) {
this.c = c;
this.isValid = !this.isInvalid();
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "Triangle [Side A =" + this.getA() +
", Side B =" + this.getB() +
", Side C =" + this.getC() + ", isValid =" + isValid + "]";
}
public boolean isInvalid() {
if(this.a+this.b<=this.c ||this.b+this.c<=this.a ||this.c+this.a<=this.b)
return true;
else
return false;
}
public boolean isRight() {
boolean t = false;
if(this.a<this.c && this.b<this.c) { //If c is the largest side
if(this.c*this.c == (this.a*this.a + this.b*this.b))
t = true;
}
else if(this.b<this.a && this.c<this.a) { //if a is the largest side
if(this.a*this.a == (this.b*this.b + this.c*this.c))
t = true;
}
else{ //if b is the largest side
if(this.b*this.b == (this.a*this.a + this.c*this.c))
t = true;
}
return t;
}
public boolean isIsosceles() {
if(this.a==this.b || this.a==this.c || this.b==this.c)
return true;
else
return false;
}
public boolean isEquilateral() {
if(this.a==this.b && this.a==this.c)
return true;
else
return false;
}
}
---------------------------------------
Sorry for it being so long, please and thank you!!
import java.util.Scanner; class Triangle { private int a, b, c; Triangle(int a, int b, int c) { if (isInvalid(a, b, c)) { throw new IllegalArgumentException("invalid sides for triangle"); } this.a = a; this.b = b; this.c = c; } /** * @return the a */ public int getA() { return a; } /** * @return the b */ public int getB() { return b; } /** * @return the c */ public int getC() { return c; } /** * @param a the a to set */ public void setA(int a) { if (isInvalid(a, b, c)) { throw new IllegalArgumentException("invalid side for triangle"); } this.a = a; } /** * @param b the b to set */ public void setB(int b) { if (isInvalid(a, b, c)) { throw new IllegalArgumentException("invalid side for triangle"); } this.b = b; } /** * @param c the c to set */ public void setC(int c) { if (isInvalid(a, b, c)) { throw new IllegalArgumentException("invalid side for triangle"); } this.c = c; } /* * (non-Javadoc) * * @see java.lang.Object#toString() */ @Override public String toString() { return String.format("Triangle [Side A =" + this.getA() + ", Side B =" + this.getB() + ", Side C =" + this.getC() + "]\n" + "Area: %.2f, Perimiter: %.2f", getArea(), getPerimeter()) ; } private boolean isInvalid(int a, int b, int c) { return (a + b <= c || b + c <= a || c + a <= b); } public boolean isRight() { boolean t = false; if (this.a < this.c && this.b < this.c) { // If c is the largest side if (this.c * this.c == (this.a * this.a + this.b * this.b)) t = true; } else if (this.b < this.a && this.c < this.a) { // if a is the largest side if (this.a * this.a == (this.b * this.b + this.c * this.c)) t = true; } else { // if b is the largest side if (this.b * this.b == (this.a * this.a + this.c * this.c)) t = true; } return t; } public boolean isIsosceles() { if (this.a == this.b || this.a == this.c || this.b == this.c) return true; else return false; } public boolean isEquilateral() { if (this.a == this.b && this.a == this.c) return true; else return false; } public double getArea() { double s = (a + b + c) / 2.0; return Math.sqrt(s * (s - a) * (s - b) * (s - c)); } public double getPerimeter() { return a + b + c; } } public class TriangleDriver { public static void main(String[] args) { Scanner in = new Scanner(System.in); Triangle t; int x, y, z; while(true) { try { System.out.println("Enter side A : "); x = in.nextInt(); System.out.println("Enter side B : "); y = in.nextInt(); System.out.println("Enter side C : "); z = in.nextInt(); if(x == 0 || y == 0 || z == 0) { break; } t = new Triangle(x, y, z); System.out.println(t.toString()); System.out.println("Right angled: " + t.isRight()); System.out.println("Equilateral: " + t.isEquilateral()); System.out.println("Isosceles: " + t.isIsosceles()); System.out.println(); } catch(Exception e) { System.out.println(e.getLocalizedMessage()); System.out.println(); } } } }
************************************************** Thanks for your question. We try our best to help you with detailed answers, But in any case, if you need any modification or have a query/issue with respect to above answer, Please ask that in the comment section. We will surely try to address your query ASAP and resolve the issue.
Please consider providing a thumbs up to this question if it helps you. by Doing that, You will help other students, who are facing similar issue.
Modify the Triangle class (from previous code, will post under this) to throw an exception in...
Java Please - Design and implement a class Triangle. A constructor should accept the lengths of a triangle’s 3 sides (as integers) and verify that the sum of any 2 sides is greater than the 3rd(i.e., that the 3 sides satisfy the triangle inequality). The constructor should mark the triangle as valid or invalid; do not throw an exception. Provide get and set methods for the 3 sides, and recheck for validity in the set methods. Provide a toString method...
(Need to complete the methods and pass a Junit test i can email them to you.) public class Triangle implements Comparable { /** * Stores the number of objects instantiated from the {@code Triangle} class */ private static int numTriangles; /** * The shortest side of the triangle */ private final double a; /** * The side of medium length of the triangle */ private final double b;...
Need help debugging. first class seems fine. second class is shooting an error on s = super.getString(prompt); third class is giving me an error in the while loop where int num = console.getInt("Enter an integer:"); //-------------------------------------------------------------------------- import java.util.Scanner; public class Console { private Scanner sc; boolean isValid; int i; double d; public Console() { sc = new Scanner(System.in); } public String getString(String prompt) { System.out.print(prompt); return sc.nextLine();...
Why is my program returning 0 for the area of a triangle? public class GeometricObjects { private String color = " white "; private boolean filled; private java.util.Date dateCreated; public GeometricObjects() { dateCreated = new java.util.Date(); } public GeometricObjects(String color, boolean filled) { dateCreated = new java.util.Date(); this.color = color; this.filled = filled; } public String getColor() { return color; } public void setColor(String color)...
Given the following class: class Q2 { private int a; private int b; private int c; public void setA(int a){this.a = a; } public void setB(int b){this.b = b;} public void setc(int c){this.c = c;} public int geta(){return a; } public int gets(){return b;} public int getc(){return c;} public int m1(int a, int b){ return a + b; public boolean m2 (int x, int y){ return m1(x, y) + x + y < 10; What is the output of the...
I am unsure how to add the following methods onto this code?? please help - rowValuesIncrease(int[][] t) A method that returns true if from left to right in any row, the integers are increasing, otherwise false. - columnValuesIncrease(int[][] t) A method that returns true if from top to bottom in any column, the integers are increasing, otherwise false. - isSetOf1toN(int[][] t) A method that returns true if the set of integers used is {1, 2, . . . , n}...
Objective Extend the class GeometicShapes to include a Triangle class.. Background Reading ZyBooks Chapter 10 Task Create the following fields and methods for a Triangle class that extends the provided GeometricShapes class public class GeometricShapes { private String color = "red"; private boolean filled; private java.util.Date dateCreated; public GeometricShapes() { dateCreated = new java.util.Date(); } public GeometricShapes(String color, boolean filled) { dateCreated = new java.util.Date(); this.color = color; this.filled = filled; } public void setColor (String color)...
1) Using the Quadratic class you have already developed, make it Comparable. A Quadratic is bigger than another Quadratic if it opens faster. 2) Write a driver for Quadratic.java. In the driver program create a few objects and compare them . then create a list of those objects and sort them. import java.util.*; import java.lang.*; class Quadratic{ private double a,b,c; // Determines/declares the class variables to store the coefficients Quadratic(){ // Determine/declare the default constructor ...
Project 7-3 Guessing Game import java.util.Scanner; public class GuessNumberApp { public static void main(String[] args) { displayWelcomeMessage(); // create the Scanner object Scanner sc = new Scanner(System.in); String choice = "y"; while (choice.equalsIgnoreCase("y")) { // generate the random number and invite user to guess it int number = getRandomNumber(); displayPleaseGuessMessage(); // continue until the user guesses the number int guessNumber = 0; int counter = 1; while (guessNumber != number) { // get a valid int from user guessNumber...
java programming how would i modify the code below to add a GUI (with a main menu with graphics and buttons, etc...) and include an option to print a list of all students added in a grid format and short by name, course, instructor, and location. ///main public static void main(String[] args) { Scanner in = new Scanner(System.in); ArrayList<Student>students = new ArrayList<>(); int choice; while(true) { displayMenu(); choice = in.nextInt(); switch(choice) { case 1: in.nextLine(); System.out.println("Enter Student Name"); String name...