Hey guys, I'm new here. I'm taking an intro programming course and I'm having problems getting this code to work. It needs to take two user input numbers and calculate figures based on them. The first part of the code compiles and works properly with a force induced test code. But the simulator won't work with actual user input numbers. Any thoughts? Thanks

public class Pair
{

   public Pair(double aFirst, double aSecond) 
   {

       P1 = aFirst;
       P2 = aSecond;
    }
    

    
    private double aFirst;
    private double aSecond;
    private double P1;
    private double P2;
    private double Sum;
    private double Difference;
    private double Product;
    private double Average;
    private double Distance;
    private double Maximum;
    private double Minimum;


   public double getSum()
   {
      Sum = (P1 + P2);
      return Sum;
    }

    public double getDifference()
    {
        Difference = (P1 - P2);
        return Difference;
    }
    
    public double getProduct()
    {
        Product = (P1 * P2);
        return Product;
    }
    
    public double getAverage()
    {
        Average = (P1 + P2)/2;
        return Average;
    }
    
    public double getDistance()
    {
        Distance = Math.abs(P1 - P2);
        return Distance;
    }
    
    public double getMaximum()
    {
        Maximum = Math.max(P1, P2);
        return Maximum;
    }
    
    public double getMinimum()
    {
        Minimum = Math.min(P1, P2);
        return Minimum;
    }
}
public class PairSimulator
{
    public static void main(String[] args)
    {

        Scanner in = new Scanner(System.in);
        Pair pair1 = new Pair(aFirst, aSecond) ;
        String number1 = "Enter first number:";
        double P1 = in.nextDouble();

        String number2 = "Enter second number:";
        double P2 = in.nextDouble();

       System.out.println("The sum is: " + pair1.getSum());
       System.out.println( "The difference is: " + pair1.getDifference());
       System.out.println( "The product is: " + pair1.getProduct());
       System.out.println( "The average is: " + pair1.getAverage());
       System.out.println( "The distance is: " + pair1.getDistance());
       System.out.println( "The maximum is: " + pair1.getMaximum());
       System.out.println( "The minimum is: " + pair1.getMinimum());


    }
}

The code you're showing here won't even compile -- in the function main, aFirst and aSecond are not in scope.

It compiles for me no problem. I'm using BlueJ if that makes any difference?

So how are aFirst and aSecond in scope then? PairSimulator is not defined as a subclass of Pair, given the code you're showing.

Regardless, the reason your program ignores the user-inputted numbers is because your code doesn't use the numbers. Just look at your code.

So how are aFirst and aSecond in scope then? PairSimulator is not defined as a subclass of Pair, given the code you're showing.

Regardless, the reason your program ignores the user-inputted numbers is because your code doesn't use the numbers. Just look at your code.

Yeah, That's what I'm stumped on. If I leave it as "new Pair()" it won't compile and if I place "new Pair(P1, P2)" it won't work. I'm doing an online course and programming isn't my major. thanks for looking into this though. Any thoughts on what I can alter to make it work?

public class PairSimulator
{
    public static void main(String[] args)
    {

        Scanner in = new Scanner(System.in);
        Pair pair1 = new Pair() ;
        String number1 = "Enter first number:";
        double P1 = in.nextDouble();

        String number2 = "Enter second number:";
        double P2 = in.nextDouble();

       System.out.println("The sum is: " + pair1.getSum());
       System.out.println( "The difference is: " + pair1.getDifference());
       System.out.println( "The product is: " + pair1.getProduct());
       System.out.println( "The average is: " + pair1.getAverage());
       System.out.println( "The distance is: " + pair1.getDistance());
       System.out.println( "The maximum is: " + pair1.getMaximum());
       System.out.println( "The minimum is: " + pair1.getMinimum());


    }
}

And this

public class PairSimulator
{
    public static void main(String[] args)
    {

        Scanner in = new Scanner(System.in);
        Pair pair1 = new Pair(P1, P2) ;
        String number1 = "Enter first number:";
        double P1 = in.nextDouble();

        String number2 = "Enter second number:";
        double P2 = in.nextDouble();

       System.out.println("The sum is: " + pair1.getSum());
       System.out.println( "The difference is: " + pair1.getDifference());
       System.out.println( "The product is: " + pair1.getProduct());
       System.out.println( "The average is: " + pair1.getAverage());
       System.out.println( "The distance is: " + pair1.getDistance());
       System.out.println( "The maximum is: " + pair1.getMaximum());
       System.out.println( "The minimum is: " + pair1.getMinimum());


    }
}

And finally this

public class PairSimulator
{
    public static void main(String[] args)
    {

        Scanner in = new Scanner(System.in);
        Pair pair1 = new Pair(0,0) ;
        String number1 = "Enter first number:";
        double P1 = in.nextDouble();

        String number2 = "Enter second number:";
        double P2 = in.nextDouble();

       System.out.println("The sum is: " + pair1.getSum());
       System.out.println( "The difference is: " + pair1.getDifference());
       System.out.println( "The product is: " + pair1.getProduct());
       System.out.println( "The average is: " + pair1.getAverage());
       System.out.println( "The distance is: " + pair1.getDistance());
       System.out.println( "The maximum is: " + pair1.getMaximum());
       System.out.println( "The minimum is: " + pair1.getMinimum());


    }
}

This is my code tester

public class PairTester
{
   public static void main(String[] args)
   {
       Pair pair1 = new Pair(1, 2);
       

       System.out.println("The sum is: " + pair1.getSum());
       System.out.println( "The difference is: " + pair1.getDifference());
       System.out.println( "The product is: " + pair1.getProduct());
       System.out.println( "The average is: " + pair1.getAverage());
       System.out.println( "The distance is: " + pair1.getDistance());
       System.out.println( "The maximum is: " + pair1.getMaximum());
       System.out.println( "The minimum is: " + pair1.getMinimum());



    }
}

What are you expecting this to do, and why? Please explain line by line. There is something wrong with the way you think Java works.

public class PairSimulator
{
    public static void main(String[] args)
    {

        Scanner in = new Scanner(System.in);
        Pair pair1 = new Pair() ;
        String number1 = "Enter first number:";
        double P1 = in.nextDouble();

        String number2 = "Enter second number:";
        double P2 = in.nextDouble();

       System.out.println("The sum is: " + pair1.getSum());
       System.out.println( "The difference is: " + pair1.getDifference());
       System.out.println( "The product is: " + pair1.getProduct());
       System.out.println( "The average is: " + pair1.getAverage());
       System.out.println( "The distance is: " + pair1.getDistance());
       System.out.println( "The maximum is: " + pair1.getMaximum());
       System.out.println( "The minimum is: " + pair1.getMinimum());
    }
}

Ok I know what's wrong.

In JAVA you must declare and initialize your variables and declare them before any usage.

public class PairSimulator
{
    public static void main(String[] args)
    {
        //If you write " Pair pair1 = new Pair(p1, p2) ;" here, It will not work at all, because JAVA doesn't know till now what are p1, p2. so you have to initialize them first.

        Scanner in = new Scanner(System.in);
        String number1 = "Enter first number:";
        double P1 = in.nextDouble();

        String number2 = "Enter second number:";
        double P2 = in.nextDouble();

         Pair pair1 = new Pair(p1, p2) ; 

       System.out.println( "The sum is: " + pair1.getSum());
       System.out.println( "The difference is: " + pair1.getDifference());
       System.out.println( "The product is: " + pair1.getProduct());
       System.out.println( "The average is: " + pair1.getAverage());
       System.out.println( "The distance is: " + pair1.getDistance());
       System.out.println( "The maximum is: " + pair1.getMaximum());
       System.out.println( "The minimum is: " + pair1.getMinimum());
    }
}

also

Pair pair1 = new Pair() ;//This is wrong too, because you didn't make a default constructor _ the one which doesn't take any parameters_
Comments
That's right, DON'T let the OP figure it out for himself, because YOU saw THE ANSWER!

I want the program to take 2 user inputted numbers and then carry out the calculations I have added at the bottom.
eng.M4AH, When I switch the order like you have stated, the BlueJ doesn't run the program. I think you're on to something though

I want the program to take 2 user inputted numbers and then carry out the calculations I have added at the bottom.
eng.M4AH, When I switch the order like you have stated, the BlueJ doesn't run the program. I think you're on to something though

eng.M4AH is correct, but yes you will get compile errors if you copy paste the code given because of these lines:

double P1 = in.nextDouble();
double P2 = in.nextDouble();

Pair pair1 = new Pair(p1, p2) ;

See what is the problem? The compiler's message should provide you with the answer on what to change at the above lines

EDIT: And please eng.M4AH, don't give him the solution. He will never learn anything if he can't correct simple mistakes like that

EDIT: And please eng.M4AH, don't give him the solution. He will never learn anything if he can't correct simple mistakes like that

sorry , I'm also new to the forum and it's rules.

Comments
okay okay

eng.M4AH is correct, but yes you will get compile errors if you copy paste the code given because of these lines:

double P1 = in.nextDouble();
double P2 = in.nextDouble();

Pair pair1 = new Pair(p1, p2) ;

See what is the problem? The compiler's message should provide you with the answer on what to change at the above lines

EDIT: And please eng.M4AH, don't give him the solution. He will never learn anything if he can't correct simple mistakes like that

I'm guessing it's because p1 and p2 aren't in caps. I already have that fixed. The compiler says compiled no error, but when I try to run it in BlueJ, it does nothing. BlueJ just looks like it's working with an infinite loop or something so I have to right click it and click restart machine.

public class Pair
{   

 Scanner in = new Scanner(System.in);

    /**
      Constructs a pair.
      @param aFirst the first value of the pair
      @param aSecond the second value of the pair
   */
   public Pair(double aFirst, double aSecond) 
   {

       P1 = aFirst;
       P2 = aSecond;
    }
    

    private double aFirst;
    private double aSecond;
    private double P1;
    private double P2;
    private double Sum;
    private double Difference;
    private double Product;
    private double Average;
    private double Distance;
    private double Maximum;
    private double Minimum;

   /**
      Computes the sum of the values of this pair.
      @return the sum of the first and second values
   */
   public double getSum()
   {
      Sum = (P1 + P2);
      return Sum;
    }
   /**
      Computes the difference of the values of this pair.
      @return the difference of the first and second values
   */
    public double getDifference()
    {
        Difference = (P1 - P2);
        return Difference;
    }
   /**
      Computes the product of the values of this pair.
      @return the product of the first and second values
   */   
    public double getProduct()
    {
        Product = (P1 * P2);
        return Product;
    }
   /**
      Computes the average of the values of this pair.
      @return the average of the first and second values
   */    
    public double getAverage()
    {
        Average = (P1 + P2)/2;
        return Average;
    }
   /**
      Computes the distance of the values of this pair.
      @return the distance of the first and second values
   */
    public double getDistance()
    {
        Distance = Math.abs(P1 - P2);
        return Distance;
    }
   /**
      Computes the maximum of the values of this pair.
      @return the maximum of the first and second values
   */    
    public double getMaximum()
    {
        Maximum = Math.max(P1, P2);
        return Maximum;
    }
   /**
      Computes the minimum of the values of this pair.
      @return the minimum of the first and second values
   */    
    public double getMinimum()
    {
        Minimum = Math.min(P1, P2);
        return Minimum;
    }
        
}

Simulator

public class PairSimulator
{
    public static void main(String[] args)
    {

        Scanner in = new Scanner(System.in);
        String number1 = "Enter first number:";
        double P1 = in.nextDouble();

        String number2 = "Enter second number:";
        double P2 = in.nextDouble();
        
        Pair pair1 = new Pair(P1, P2) ;
        

       System.out.println("The sum is: " + pair1.getSum());
       System.out.println( "The difference is: " + pair1.getDifference());
       System.out.println( "The product is: " + pair1.getProduct());
       System.out.println( "The average is: " + pair1.getAverage());
       System.out.println( "The distance is: " + pair1.getDistance());
       System.out.println( "The maximum is: " + pair1.getMaximum());
       System.out.println( "The minimum is: " + pair1.getMinimum());


    }
}

So did you input 2 numbers for it to use? Your scanner is waiting for input.

That's the problem. the terminal window doesn't come and ask to input the numbers.

String number1 = "Enter first number:";
        double P1 = in.nextDouble();

        String number2 = "Enter second number:";
        double P2 = in.nextDouble();

You need to print the messages:

System.out.println("Enter first number:");
        double P1 = in.nextDouble();

        String number2 = "Enter second number:";
        double P2 = in.nextDouble();

With your code you give value to the number1 and then the program waits for you to input a value. You need to simply enter a number and print <ENTER>

That's the problem. the terminal window doesn't come and ask to input the numbers.

Well, of course it doesn't because you just declared string variables. You didn't ever do anything with them. I had hoped my answer might make you think a little and figure out why you weren't getting the prompts. JavaAddict has answered that for you above though.

This article has been dead for over six months. Start a new discussion instead.