programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering OS Languages Paradigms IDEs Build Tools Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# A Numbered Pair program

Mason BarnHart
Greenhorn
Posts: 5
I have been having difficulty with the weeks concepts in my online Java class, the program is to be as followed:

For this exercise you will implement a class called Pair, that represents a pair of two numbers.The
Pair class should include the following constructor and methods:

CONSTRUCTORS
public Pair(double num1, double num2) -- Creates an object that represents a pair of double values

METHODS
public double getAverage() -- Returns the average value of the two numbers

public double getDistance() -- Returns the absolute vale of the distance between the two numbers

public double getMaximum() -- Returns the maximum value of the two numbers

public double getMinimum() -- Returns the minimum vale of the two numbers

Write a class called PairTest that tests your Pair implementation. The PairTest should prompt the user for the two values, create a Pair object with the values and then print the average, distance, maximum, and minimum of the pair. The input / output should look like the following:

Enter the first number: 5.5
Enter the second number: 3.0

Average: 4.25
Distance: 2.5
Maximum: 5.5
Minimum: 3.0

NOTE: For this exercise, your solution should not use any conditional statements. Instead you
should use the methods provided by thejava.util.Math.

So far I have:

Sorry for the long post but I just wanted to share as much info as I could.

Greg Charles
Sheriff
Posts: 3015
12
You've done a good job on the test class, though you've called it Main instead of PairTest. However, the assignment isn't asking to do the computations inside the test code. It wants you to create another class, called Pair, that can be constructed with the two numbers, and has methods to perform the various calculations. Your test code, will read the two numbers as it already does, but then use them to create a Pair object and then call its methods. In other words, you're doing all the right stuff, but it just needs to be reorganized to fit the assignment.

Mason BarnHart
Greenhorn
Posts: 5
So something more like this? Does the class "Pair" need to be within the main class and have the values of num1 and num2 passed into it? Then have the values of the average and other things I need to find be passes back into the main class to be printed out?

Campbell Ritchie
Marshal
Posts: 56536
172
Forget about there being such a thing as a Main class. You have a main method, which should be very short.
The Pair class, as you said in your fist post, should be its own top‑level class. You have even been told which methods to give it. you should consider overriding its toString equals and hashCode methods, too.

Diana Magers
Greenhorn
Posts: 9
Ok, I looked at it a little different this time.

Now, I need to create a class to get the values. Sigh. Because clearly this piece of code will not generate the Average: Distance: Maximum: nor the Min:. Back to the drawing board.

Campbell Ritchie
Marshal
Posts: 56536
172

Diana Magers
Greenhorn
Posts: 9
I got it working Mr. Campbell. Thank you for your help.

Campbell Ritchie
Marshal
Posts: 56536
172
Well done Please show us what you have got (on the other thread).