Search code examples
javajava.util.scannerarea

Scanner + TesterClass?


So for this I have to create a program in Java that asks the user to input all three points of a triangle, and then I must find the sides and area. All the math must be done separately from the tester class, where I will prompt the user with the questions...
- how do I ask the user to input something in the tester class but get those integers back in the original program?


Solution

  • In the tester class's main method, you can make an instance of the non-tester class containing the functions where you do the math, i.e.:

    TriangleMath tMath = new TriangleMath();
    // where TriangleMath is the name of the other class, and "tMath" is
    // an instance of it. then:
    
    Scanner keyboard = new Scanner(System.in);
    int point1 = (int) keyboard.nextLine().charAt(0);
    int point2 = (int) keyboard.nextLine().charAt(0);
    int point3 = (int) keyboard.nextLine().charAt(0);
    int area = tMath.area(point1, point2, point3);
    

    in this, you're making an object of the class containing all of the math functions and stuff, then getting the input in the main method of the tester class, then passing the inputs into the area function of the instance of the TriangleMath class (tMath). The .charAt(0) turns it into a char, and the (int) casts it as an int.

    I hope I was of some help!