Search code examples
javajoptionpane

How do I use a JOptionPane input from a test class in another class?


I currently have a Triangle class with all of my calculations.

public class Triangle
{
   private double x1;
   private double y1;
   private double x2;
   private double y2;
   private double x3;
   private double y3;
   private double lengthA;
   private double lengthB;
   private double lengthC;
   private double angleA;
   private double angleB;
   private double angleC;
   private double perimeter;
   private double height;
   private double area;

   public double calcArea()
   {
      area = .5 * lengthC * height;
      return area;
   }

   public double calcPerimeter()
   {
      perimeter = lengthA + lengthB + lengthC;
      return perimeter;
   }

   public double lengthA()
   {
      lengthA = Math.sqrt(Math.pow((x2 - x3),2) + Math.pow(height,2));
      return lengthA;
   }

   public double lengthB()
   {
      lengthB = Math.sqrt(Math.pow((x3 - x1),2) + Math.pow(height,2));
      return lengthB;
   }

   public double lengthC()
   {
      lengthC = x2 - x1;
      return lengthC;
   }

   public double getHeight()
   {
      height = y3 - y1;
      return height; 
   }

   public double angleA()
   {
      angleA = Math.abs(Math.toDegrees(Math.asin(height / lengthB)));
      return angleA;
   }

   public double angleB()
   {
      angleB = Math.abs(Math.toDegrees(Math.asin(height / lengthA)));
      return angleB;
   }

   public double angleC()
   {
      angleC = 180 - angleA - angleB;
      return angleC;
   }
}

I also have a TriangleTester class that uses JOptionPane to get coordinates for the triangle.

import javax.swing.*;
public class TriangleTester
{
   public static void main (String [] args)
   {
      double x1;
      double y1;
      double x2;
      double y2;
      double x3;
      double y3;
      String v1;
      String v2;
      String v3;
      String v4;
      String v5;
      String v6;

      v1 = JOptionPane.showInputDialog("Enter x1 for point A");
      v2 = JOptionPane.showInputDialog("Enter y1 for point A");
      v3 = JOptionPane.showInputDialog("Enter x2 for point B");
      v4 = JOptionPane.showInputDialog("Enter y2 for point B");
      v5 = JOptionPane.showInputDialog("Enter x3 for point C");
      v6 = JOptionPane.showInputDialog("Enter y3 for point C");
      x1 = Integer.parseInt(v1);
      y1 = Integer.parseInt(v2);
      x2 = Integer.parseInt(v3);
      y2 = Integer.parseInt(v4);
      x3 = Integer.parseInt(v5);
      y3 = Integer.parseInt(v6);

      Triangle tri = new Triangle();
      double lengthA = tri.lengthA();
      double lengthB = tri.lengthB();
      double lengthC = tri.lengthC();
      double angleA = tri.angleA();
      double angleB = tri.angleB();
      double angleC = tri.angleC();
      double perimeter = tri.calcPerimeter();
      double height = tri.getHeight();
      double area = tri.calcArea();

      System.out.printf("Set up triangle with coordinates (" + x1 + "," + y1 + "), (" + x2 + "," + y2 + "), (" + x3 + "," + y3 + ")");
      System.out.printf("\nArea:\t\t\t\t" + area);
      System.out.printf("\nPerimeter:\t\t" + perimeter);
      System.out.printf("\nLength side a:\t" + lengthA);
      System.out.printf("\nLength side b:\t" + lengthB);
      System.out.printf("\nLength side c:\t" + lengthC);
      System.out.printf("\nHeight h:\t\t" + height);
      System.out.printf("\nAngle A:\t\t\t" + angleA);
      System.out.printf("\nAngle B:\t\t\t" + angleB);
      System.out.printf("\nAngle C:\t\t\t" + angleC);
   }
}

When I run the tester code, it prints the coordinates with the correct x and y values, but everything else ends up printing as 0. I believe this is because I have not made any connection between the x and y values of the Triangle class and the TriangleTester class. How can I get the Triangle class to use the inputted x and y values from the TriangleTester class to calculate the answers? Thanks.


Solution

  • The main idea here was you were using a getter as a setter. You were not passing the data for the setter methods between classes Main program

    package triangles;
    
    import javax.swing.*;
    public class Triangles {
    
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args)
    {
       double x1;
      double y1;
      double x2;
      double y2;
      double x3;
      double y3;
      String v1;
      String v2;
      String v3;
      String v4;
      String v5;
      String v6;
    
      v1 = JOptionPane.showInputDialog("Enter x1 for point A");
      v2 = JOptionPane.showInputDialog("Enter y1 for point A");
      v3 = JOptionPane.showInputDialog("Enter x2 for point B");
      v4 = JOptionPane.showInputDialog("Enter y2 for point B");
      v5 = JOptionPane.showInputDialog("Enter x3 for point C");
      v6 = JOptionPane.showInputDialog("Enter y3 for point C");
      x1 = Integer.parseInt(v1);
      y1 = Integer.parseInt(v2);
      x2 = Integer.parseInt(v3);
      y2 = Integer.parseInt(v4);
      x3 = Integer.parseInt(v5);
      y3 = Integer.parseInt(v6);
    
      Triangle tri = new Triangle();
      //set all needed data
      tri.setLengthA(x2,x3);
      tri.setLengthB(x3,x1);
      tri.setLengthC(x2,x1);
      tri.setHeight(y3,y1);
      // set calculations off the data
      tri.setAngleA();
      tri.setAngleB();
      tri.setAngleC();
    
      double perimeter = tri.calcPerimeter();
    
      double area = tri.calcArea();
    
      System.out.printf("Set up triangle with coordinates (" + x1 + "," + y1 + "), (" + x2 + "," + y2 + "), (" + x3 + "," + y3 + ")");
      System.out.printf("\nArea:\t\t\t\t" + area);
      System.out.printf("\nPerimeter:\t\t" + perimeter);
      System.out.printf("\nLength side a:\t" + tri.lengthA);
      System.out.printf("\nLength side b:\t" + tri.lengthB);
      System.out.printf("\nLength side c:\t" + tri.lengthC);
      System.out.printf("\nHeight h:\t\t" + tri.height);
      System.out.printf("\nAngle A:\t\t\t" + tri.angleA);
      System.out.printf("\nAngle B:\t\t\t" + tri.angleB);
      System.out.printf("\nAngle C:\t\t\t" + tri.angleC);
    
    }
    

    }

    and your class

      /*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */
    package triangles;
    
    /**
     *
     * @author jstil
     */
    public class Triangle {
    
        private double x1;
       private double y1;
       private double x2;
       private double y2;
       private double x3;
       private double y3;
       public double lengthA;
       public double lengthB;
       public double lengthC;
       public double angleA;
       public double angleB;
       public double angleC;
       private double perimeter;
       public double height;
       private double area;
    
       public double calcArea()
       {
          area = .5 * lengthC * height;
          return area;
       }
    
       public double calcPerimeter()
       {
          perimeter = lengthA + lengthB + lengthC;
          return perimeter;
       }
    
       public void setLengthA( double x2 , double x3)
       {
          lengthA = Math.sqrt(Math.pow((x2 - x3),2) + Math.pow(height,2));
    
       }
    
       public void setLengthB(double x3, double x1)
       {
          lengthB = Math.sqrt(Math.pow((x3 - x1),2) + Math.pow(height,2));
    
       }
    
       public void setLengthC(double x2, double x1)
       {
          lengthC = x2 - x1;
    
       }
    
       public void setHeight(double y3, double y1)
       {
          height = y3 - y1;
    
       }
    
       public void setAngleA()
       {
          angleA = Math.abs(Math.toDegrees(Math.asin(height/ lengthB)));
    
       }
    
       public void setAngleB()
       {
          angleB = Math.abs(Math.toDegrees(Math.asin(height / lengthA)));
    
       }
    
       public void setAngleC()
       {
          angleC = 180 - angleA - angleB;
    
       }
    }