0

I'm working on a program that calls on a method from another class, and I can't seem to get past that road bump. The example I'm using simply sets a variable in the main method equal to the other class, and the major error I'm getting in my own program is that the symbol I'm using can't be found - I've also looked at other similar questions on here, but I can't wrap my head around on how to use the answers in my own program.

Here a portion of my main method, called MyTriangle:

do
  {
     System.out.print ("Enter the first of a triangle: ");
     side1 = scan.nextDouble();

     System.out.print ("Enter the second side of a triangle: ");
     side2 = scan.nextDouble();

     System.out.print ("Enter the third side of a triangle: ");
     side3 = scan.nextDouble();

     validity = isValid (side1, side2, side3);

     if (validity == true)
     {  
        System.out.println ("Area of the triangle is " + area);
     }

     if (validity == false)
     {
        System.out.println ("Invalid input");
     }   

     System.out.print ("\nContinue? <y/n> ");
     end = scan.next();

  } while (!end.equalsIgnoreCase("n"));

And here's a portion of my class TestMyTriangle:

public class TestMyTriangle
{

 public static double area (double side1, double side2, double side3)
 {
    double resultArea = 0;

    double sum = 0;

    sum = ((side1 + side2 + side3) / 2);

    resultArea = Math.sqrt(sum * ((sum - side1) * (sum - side2) * (sum -side3)));

    return resultArea;
 }        
}

The way I'm invoking TestMyTriangle is identical to the example I'm using, but it's still not working. If anyone can help me see what I'm missing, I would appreciate it.

1
  • How are you invoking the method? Also, post the relevant stacktrace Commented Oct 17, 2015 at 21:13

2 Answers 2

2

If the error is coming from the code samples you posted, my bet would be that your isValid method is in the TestMyTriangle class. So, in order to invoke isValid, you must write it like so:

validity = TestMyTriangle.isValid(side1, side2, side3);

Also make sure that when you call area, you also reference the class like so:

System.out.println("Area of the triangle is " + TestMyTriangle.area(side1, side2, side3));

Lastly, IF your two files are not in the same directory, make sure to import the other class file like so:

import path/to/directory/TestMyTriangle;

Good luck!

Sign up to request clarification or add additional context in comments.

Comments

1

Change this line:

System.out.println ("Area of the triangle is " + area);

to this:

System.out.println ("Area of the triangle is " + TestMyTriangle.area(side1, side2, side3));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.