Issue
Here is my task:
Create a class named MyTriangle that contains the following two methods:
/** Return true if the sum of any two sides is * greater than the third side. */ public static boolean isValid (double side1, double side2, double side3) /** Return the area of the triangle. */ public static double area (double side1, double side2, double side3)
Write a test program that reads three sides for a triangle and computes the area if the input is valid. Otherwise, it displays that the input is invalid.
Attempt below: Question: I cannot figure this out and the constantly rereading the chapter isn't breaking through any walls. The issue is commented in the code below:
import java.util.Scanner;
public class NewClass1 {
double area;
double side1, side2, side3;
double x1, x2, x3, y1, y2, y3;
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter two integers for side 1:");
double x1 = input.nextDouble();
double y1 = input.nextDouble();
System.out.print("Enter two integers for side 2:");
double x2 = input.nextDouble();
double y2 = input.nextDouble();
System.out.print("Enter two integers for side 3:");
double x3 = input.nextDouble();
double y3 = input.nextDouble();
boolean isValid = true;
if (isValid) {
System.out.println("Input is invalid");
}
else
area(side1, side2, side3); //Using area does not work and I don't know how to remedy this. I've read the chapter over and over... I cannot get it to work.
}
public static double area(double side1, double side2, double side3) {
double x1 = 0;
double x2 = 0;
double y1 = 0;
double y2 = 0;
double x3 = 0;
double y3 = 0;
side1 = Math.pow(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2), 0.5);
side2 = Math.pow(Math.pow(x3 - x1, 2) + Math.pow(y3 - y1, 2), 0.5);
side3 = Math.pow(Math.pow(x3 - x2, 2) + Math.pow(y3 - y2, 2), 0.5);
//Calculates the sides/angles using Heron's formula
double s = (side1 + side2 + side3)/2;
double area = Math.pow(s * (s - side1) * (s - side2) * (s - side3), 0.5);
return (area);
}
public static boolean isValid(double side1, double side2, double side3) {
return (((side1 + side2) > side3) && ((side1 + side3) > side2) && ((side2 + side3) > side1));
}
}
Reviewing the code, can someone please explain what it is that I'm doing wrong, and explain a possible remedy. Everything is there, I simply cannot connect the dots.
Revision--Code
import java.util.Scanner;
public class NewClass1 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter side 1: ");
double side1 = input.nextDouble();
System.out.print("Enter side 2: ");
double side2 = input.nextDouble();
System.out.print("Enter side 3: ");
double side3 = input.nextDouble();
double a = area(side1, side2, side3);
boolean isV = isValid(side1, side2, side3);
if (isV)
System.out.println("Inout is Invalid");
else
System.out.println("Area is: " + a);
}
public static boolean isValid(double side1, double side2, double side3) {
return (((side1 + side2) > side3) && ((side1 + side3) > side2) && ((side2 + side3) > side1));
}
public static double area(double side1, double side2, double side3) {
//Calculates the sides/angles using Heron's formula
double s = (side1 + side2 + side3)/2;
double theArea = Math.pow(s * (s - side1) * (s - side2) * (s - side3), 0.5);
return (theArea);
}
}
I keep getting NaN
as the answer for the area. What am I doing wrong?
Solution
So you write the following: if the input is valid, show error message. Otherwise (else) compute area. You should just swap your if and else parts! Your program never calls the area() method if you are calling it with points for a valid triangle. Moreover, you never call the isValid() method. You assign true to the variable, and then check it in the next line, but the method that actually checks it has never been called.
ALSO you need side variables for isValid(), but you only compute them in the area() method. You should compute them right after you get the points.
Answered By - mashaned
Answer Checked By - Katrina (JavaFixing Volunteer)