# Writing 6 Math Methods to Print Results In a Driver From Two Integers

• 06-23-2013, 06:19 PM
Writing 6 Math Methods to Print Results In a Driver From Two Integers
The project asks to write a program that prompts the user for two integers and then prints the sum, difference, product, average, max and min. There has to be a class and a separate driver for this. The class has to have a default constructor and a regular constructor that creates and assigns both variables, the 6 methods that compute all the desired math equations, and accessor and mutator methods for each instance field.

The driver program should prompt the user for two integers, construct a pair object, call the methods of the Pair class to complete the results, and display them using the Scanner class to get user input.

This is what I have so far for the class:
Code:

``` public class Pair   {       private int aFirst;       private int aSecond;         public Pair()       {         aFirst =0;         aSecond =0;       }         public int getaFirst()       {         return aFirst;       }         public int getaSecond()       {         return aSecond;       }         public void setaFirst (int newFirst)       {         aFirst=newFirst;       }         public void setaSecond (int newSecond)       {         aSecond=newSecond;       }         public static double sum(int aFirst,int aSecond)       {         int sum=aFirst+aSecond;                         return sum;       }             }```
And for the driver:
Code:

```import java.util.Scanner; import java.lang.Math; public class PairTester { public static void main (String [] args) { int aFirst; int aSecond; Scanner input = new Scanner(System.in); System.out.print("Enter first number: "); aFirst = input.nextInt(); System.out.print("Enter second number: "); aSecond = input.nextInt(); Pair newPair = new Pair(aFirst,aSecond); System.out.println(newPair.sum(aFirst,aSecond)); } }```
Obviously this is not compiling and I am mostly not understanding how they want us to implement the methods for the math problems within the class itself, I can figure how to do it in the driver but that's not what he wants us to do...thanks in advance for guidance.
• 06-23-2013, 06:30 PM
superhaNds
Re: Writing 6 Math Methods to Print Results In a Driver From Two Integers
It is not compiling because you call a constructor that is undefined, you have the default, but you are missing the constructor with parameters, namely, the 2 numbers. Make one
I think that your sum method is missing the point, you want to manipulate the two fields of the class not other input numbers, so consider making it an instance method that returns num1 + num2
and follow this philosophy with the rest of the methods. That is how I understood it
• 06-23-2013, 07:19 PM
Re: Writing 6 Math Methods to Print Results In a Driver From Two Integers
Quote:

Originally Posted by superhaNds
It is not compiling because you call a constructor that is undefined, you have the default, but you are missing the constructor with parameters, namely, the 2 numbers. Make one
I think that your sum method is missing the point, you want to manipulate the two fields of the class not other input numbers, so consider making it an instance method that returns num1 + num2
and follow this philosophy with the rest of the methods. That is how I understood it

Code:

```public class Pair   {       private int aFirst;       private int aSecond;         public Pair()       {         aFirst =0;         aSecond =0;       }                                 public Pair(int initialaFirst, int initialaSecond)                 {                 aFirst=initialaFirst;                 aSecond=initialaSecond;                 }         public int getaFirst()       {         return aFirst;       }         public int getaSecond()       {         return aSecond;       }         public void setaFirst (int newFirst)       {         aFirst=newFirst;       }         public void setaSecond (int newSecond)       {         aSecond=newSecond;       }         public static double expression(int aFirst,int aSecond)       {       return aFirst + aSecond;```
So with this static double expression I've officially gotten the driver to display the sum of the two integers:
Code:

```import java.util.Scanner; import java.lang.Math; public class PairTester { public static void main (String [] args) { int aFirst; int aSecond; Scanner input = new Scanner(System.in); System.out.print("Enter first number: "); aFirst = input.nextInt(); System.out.print("Enter second number: "); aSecond = input.nextInt(); Pair newPair = new Pair(aFirst,aSecond); System.out.println(newPair.getaFirst()+newPair.getaSecond()); Pair newPair1 = new Pair(aFirst,aSecond); System.out.println(newPair1.getaFirst()+newPair1.getaSecond()); } }```
But now I can't make methods for any of the other expressions without it giving me an error how do I implement the rest of the expressions?
• 06-23-2013, 07:25 PM
Fubarable
Re: Writing 6 Math Methods to Print Results In a Driver From Two Integers
This incomplete method:

Code:

```      public static double expression(int aFirst,int aSecond)       {       return aFirst + aSecond;```
doesn't make much sense, and even if it were complete, it wouldn't make sense. Why static? Where are your sum, product, divide, and subtract methods?
• 06-23-2013, 07:32 PM
Re: Writing 6 Math Methods to Print Results In a Driver From Two Integers
Quote:

Originally Posted by Fubarable
This incomplete method:

Code:

```      public static double expression(int aFirst,int aSecond)       {       return aFirst + aSecond;```
doesn't make much sense, and even if it were complete, it wouldn't make sense. Why static? Where are your sum, product, divide, and subtract methods?

I haven't implemented the other methods yet because I'm honestly not sure what I am supposed to be doing for them I did static because when I was researching on the API it had an expression like that as an example but that was for only ONE expression I couldn't find an example where you are implement 6 different types of expressions as methods.

This is my first programming class so this is still all pretty foreign to me. I started doing this:
Code:

```public static double add(int aFirst,int aSecond)       {       int sum;                 sum = aFirst + aSecond;                 return sum;                 }                                 public static double subtract(int aFirst,int aSecond)                 {                 int difference;                 difference = aFirst - aSecond;                 return difference;```
Thinking I had something going but my driver still only prints the sum of the two integers that are input by the user. To which now I am lost on. I figured all 6 expressions are supposed to be within one method but I just have no idea how I am supposed to be doing that.
• 06-23-2013, 07:33 PM
superhaNds
Re: Writing 6 Math Methods to Print Results In a Driver From Two Integers
the static makes it a class method, it belongs to the class, thus you can call it like CLASSNAME.method
I think you should have instance methods because you want to manipulate your data fields.

Code:

```public static double expression(int aFirst,int aSecond) { return aFirst + aSecond; }```
this method computes 2 integers passed in as arguments rather than the class' data fields. It should't be static because it depends on the values of your data fields and why double?
I was talking about making it like this: public int sum() { return aFirst + aSecond; } this would return the sum of your fields.

in your system out print you computed the sum by adding the two numbers separately using the get methods
• 06-23-2013, 08:22 PM
Re: Writing 6 Math Methods to Print Results In a Driver From Two Integers
Quote:

Originally Posted by superhaNds
the static makes it a class method, it belongs to the class, thus you can call it like CLASSNAME.method
I think you should have instance methods because you want to manipulate your data fields.

Code:

```public static double expression(int aFirst,int aSecond) { return aFirst + aSecond; }```
this method computes 2 integers passed in as arguments rather than the class' data fields. It should't be static because it depends on the values of your data fields and why double?
I was talking about making it like this: public int sum() { return aFirst + aSecond; } this would return the sum of your fields.

in your system out print you computed the sum by adding the two numbers separately using the get methods

Ok so now I've done the six different methods for the expressions:
Code:

```public int sum()       {         return aFirst + aSecond;       }         public int difference()       {         return aFirst - aSecond;       }                                 public int product()                 {                 return aFirst * aSecond;                 }                                 public double average()                 {                 return (aFirst + aSecond)/2.0;                 }                                 public int max()                 {                 return aFirst>aSecond?aFirst:aSecond;                 }                                 public int min()                 {                 return aFirst<aSecond?aFirst:aSecond;                 }```
But not 100% sure now on how to organize the driver to print out all 6 expressions I mean it says I am supposed construct a Pair object and then call all methods of the Pair class to compute the results. So I got the Pair object part but as far as calling the methods am I supposed to do something like this? This...doesn't work properly cause it computes 0 but need to know I am on the right track.
Code:

```System.out.print("Enter first number: "); aFirst = input.nextInt(); System.out.print("Enter second number: "); aSecond = input.nextInt(); Pair newPair = new Pair(); int add = newPair.sum(); System.out.println("The sum is: "+ add );```
• 06-23-2013, 08:35 PM
superhaNds
Re: Writing 6 Math Methods to Print Results In a Driver From Two Integers
It computes zero because 0 + 0 is zero, you used the default constructor again.
1. Get the input and pass them to the constructor that takes 2 integers.
2. Print every result by using all your methods.

You don't need to store the sum to a variable necessarily, you can say System.out.println("The sum is: "+ newPair.sum());
Continue to print using the other methods the same way.
• 06-23-2013, 08:42 PM