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

1. Member Join Date
Jun 2013
Posts
17
Rep Power
0 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:
Java 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:
Java 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.  Reply With Quote

2. Senior Member Join Date
Apr 2013
Location
Sweden
Posts
272
Rep Power
7 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  Reply With Quote

3. Member Join Date
Jun 2013
Posts
17
Rep Power
0 Re: Writing 6 Math Methods to Print Results In a Driver From Two Integers 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
Java 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:
Java 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?  Reply With Quote

4. Re: Writing 6 Math Methods to Print Results In a Driver From Two Integers

This incomplete method:

Java 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?  Reply With Quote

5. Member Join Date
Jun 2013
Posts
17
Rep Power
0 Re: Writing 6 Math Methods to Print Results In a Driver From Two Integers Originally Posted by Fubarable This incomplete method:

Java 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:
Java 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.  Reply With Quote

6. Senior Member Join Date
Apr 2013
Location
Sweden
Posts
272
Rep Power
7 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.

Java 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  Reply With Quote

7. Member Join Date
Jun 2013
Posts
17
Rep Power
0 Re: Writing 6 Math Methods to Print Results In a Driver From Two Integers 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.

Java 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:
Java 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.
Java 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 );  Reply With Quote

8. Senior Member Join Date
Apr 2013
Location
Sweden
Posts
272
Rep Power
7 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.  Reply With Quote

9. Member Join Date
Jun 2013
Posts
17
Rep Power
0 Re: Writing 6 Math Methods to Print Results In a Driver From Two Integers Originally Posted by superhaNds 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.
WOO finally got it, lol thank you so much sorry that took a little bit to click in my brain.  Reply With Quote Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•