# Math Program

• 04-02-2009, 07:49 PM
stlboi
Math Program
Any idea how to do this? I'm going to need a java file plus a java tester file as well, right?

Write a program that prompts the user for two numbers, then prints
The sum
The difference
The product
The average
The distance (absolute value of the difference)
The maximum (the larger of the two)
The minimum (the smaller of the two)

To do so, implement a class
public class Pair
{
/**
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) { . . . }

/**
Computes the sum of the values of this pair.
@return the sum of the first and second values
*/
public double getSum() { . . . }
. . .
}

Then implement a class PairTester that constructs a Pair object, invokes its methods, and prin
• 04-02-2009, 08:24 PM
mcfrog
Hi,

yes it seems really easy: 2 classes: Pair and PairTester, anyway IMHO a main method in the class Pair would suffice. Well, move on with your homework ;)

edit:
oh yes: don't forget to format your double values appropriate, because nobody likes doubles like 0.100000000000000893473
• 04-02-2009, 08:29 PM
stlboi
Now if I only knew where to start lol. My teacher spends about 90% of the class talking about the old days. Sucks.
• 04-02-2009, 08:48 PM
mcfrog
well that's easy, but I don't know what knowledge you already have, so here's the long version:

* you create two files named after the classes and
* create the constructor given by your teacher
Code:

`public Pair(double aFirst, double aSecond) { . . . }`
* to have values to work with you need also to member variables of the same type, double, ... um... make them private members to keep nosy classes away :D
* create the mentioned methods and let your mathematical genius roll and return the values in the fashion of
Code:

`/** your javadoc here */ public double getSum() { return this.firstValue + this.secondValue; }`
This really would suffice for sum. Anything clear?

so long
• 04-02-2009, 08:50 PM
stlboi
Ah, I think I see now. I will let you all know. Thanks bud!
• 04-02-2009, 08:54 PM
mcfrog
no prob, you're welcome. :) Anyway, consider to set the RESOLVED-marker for this thread soon.

But if you intend to get deeper into Java it maybe is a good idea to buy a good book and have Java programmers around you in order to shortcut the back-in-the-days-where-we-had-punchcards if your teacher doesn't teach you very well.
• 04-02-2009, 08:58 PM
stlboi
Funny you mention punch cards cuz he brings that up a lot.
• 04-02-2009, 09:03 PM
mcfrog
every teacher comes up with that story. Personally I think all these punchcard-talk is urban myth. There never have been punchcards. it's a created diversion in order not to see the real problems: the lack of good teaching :)
• 04-02-2009, 09:34 PM
angryboy