1

I have created a Triangle class program, but I need a tester class for it. In the tester class, it must ask the user to input the points, and then it would calculate the sides and so fourth. How do I ask the user to input x1 and y1 in the tester, and then get that data in the triangle class? BTW I need to use Scanner for this.

import java.util.Scanner;

public class Triangle {

    private double x1;
    private double y1;
    private double x2;
    private double y2;
    private double x3;
    private double y3;
    private double side1;
    private double side2;
    private double side3;
    private double angleA;
    private double angleB;
    private double angleC;

    public Triangle(double a1, double a2, double b1, double b2, double c1, double c2){
    x1=a1;
    y1=a2;
    x2=b1;
    y2=b2;
    x3=c1;
    y3=c2;
    }

    public double getSide1(){
        side1 = Math.sqrt(Math.pow(x2-x1,2)+Math.pow(y2-y1,2));
        return side1;
    }
    public double getSide2(){
        side2 = Math.sqrt(Math.pow(x3-x2,2)+Math.pow(y3-y2,2));
        return side2;
    }
    public double getSide3(){
        side3 = Math.sqrt(Math.pow(x3-x1,2)+Math.pow(y3-y1,2));
        return side3;
    }
    public double getAngleA(){
        angleA = side1 + side2 + side3 - (side2 * side3);
        return angleA;
    }
    public double getAngleB(){
        angleB = side2 + side1 + side3 - (side1 * side3);
        return angleA;
    }
    public double getAngleC(){
        angleC = side3 + side1 + side2 - (side1 * side2);
        return angleA;
    }

}
Nosferatu2
  • 21
  • 3
  • 1
    And what have you tried so far? Where is the problem using the Scanner? There are a lot of questions (I would say the majority) which paste code using a Scanner. – Heri Oct 31 '16 at 20:08

1 Answers1

0

You can use Scanner class in the following way:

....

private static Scanner input;

public static void main(String[] args) {
    input = new Scanner(System.in);

    //... Read x1 from the console.
    System.out.print("Enter x1 : ");
    int x1 = input.nextInt();

    //... Read y1 from the console.
    System.out.print("Enter y1: ");
    int y1 = input.nextInt();

    System.out.print("x1=" + x1 + ", y1=" + y1);

}
....
eparvan
  • 1,639
  • 1
  • 15
  • 26