I am posting because one of my programs is not working properly. It is a program, well, two programs, that are supposed to check if a triangle is valid and then print the area. The problem is, it never says if it is invalid even when it is. I tried messing around with the loops and it will say if it is invalid but it will not print the area properly. Any help is greatly appreciated! Thanks in advanced!
/* Class: CS1301
* Section: 8
* Term: Fall 2015
* Name: Matthew Woolridge
* Instructor: Mr. Robert Thorsen
* Assignment: Assignment 8
* Program: 3
* ProgramName: MyTriangle
* Purpose: Implements the methods in test my triangle
* Operation: The information is statically instantiated in the code and
* the data is output to the screen.
* Input(s): The input is the sides
* Output(s): The output will be if valid and if so the area
* Methodology: The program will use loops and methods, as well as another jar to produce an output
*
*/
import java.util.Scanner;
public class MyTriangle
{
public static void main(String[] args)
{
/******************************************************************************
* Declarations Section *
******************************************************************************/
/****************************CONSTANTS********************************/
double side1;
double side2; //Declares side
double side3;
String restart = "y"; //Restart string
Scanner scan = new Scanner(System.in); //Intializes the scanner
TestMyTriangle valid = new TestMyTriangle(); //Creates a new triangle test from the other program
/******************************************************************************
* Inputs Section *
******************************************************************************/
while(restart.equals("y"))
{
System.out.print("Enter three edges of the triangle: "); //Prompts the user for the input of the sides
side1 = scan.nextDouble();
side2 = scan.nextDouble();
side3 = scan.nextDouble();
/******************************************************************************
* Processing Section *
******************************************************************************/
if (!valid.isValid(side1,side2,side3)) { //Prints if the input is invalid
System.out.print("The input is invalid");
System.exit(0);
}
/******************************************************************************
* Outputs Section *
******************************************************************************/
System.out.print("The area of the triangle is " //Prints the area of the triangle if valid
+ valid.area(side1,side2,side3) + "\n");
System.out.println("Would you like to restart? Enter y or n: "); //Asks to restart
restart = scan.next();
}
System.out.print("The program has ended!");
} //Ends string
} //Ends program
/* Class: CS1301
* Section: 8
* Term: Fall 2015
* Name: Matthew Woolridge
* Instructor: Mr. Robert Thorsen
* Assignment: Assignment 8
* Program: 3
* ProgramName: TestMyTriangle
* Purpose: Tests the triangles sides to see if they are valid or invalid
* Operation: The information is statically instantiated in the code and
* the data is output to the screen.
* Input(s): The input is the sides, in the other program
* Output(s): The output will be valid or invalid in the boolean
* Methodology: The program uses booleans to see if the sides are valid and then it calculates the area and returns to MyTriangle
*
*/
public class TestMyTriangle extends MyTriangle
{
public static boolean isValid(double side1, double side2, double side3)
{
return !(side1>side2+side3||side2>side1+side3||side3>side1+side2);
}
public static double area(double side1, double side2, double side3)
{
double s;
double area;
s = (side1 + side2 + side3)/2;
area = Math.sqrt(s*(s-side1)*(s-side2)*(s-side3));
return area;
}
}
Aucun commentaire:
Enregistrer un commentaire