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:

Pair Test Program

Diana Magers
Greenhorn
Posts: 9
Yes, I am a beginner but I think I have a good start on this program that I have to do for the week. The concept is pretty simple. But my problem is that I am running into an error in eclipse.

I need to include

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

The instructions are as follows:

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

Just trying to give you as much information as I can. Here is my program.

What is giving me trouble and has a big red X next to it is the

Which is the only line that has a red mark next to it.

Is there someone that is more advance than myself that can help me understand what is going wrong?

I appreciate any help that anyone can give.

Joe Harry
Ranch Hand
Posts: 10128
3
What is Pair? Is that a method? If yes, then where is the return type? If Pair is a constructor, then it should be PairTest and not Pair! I also do not understand why Pair is inside the main method?

Diana Magers
Greenhorn
Posts: 9
I will say that is very embarrassing. They are suppose to be Constructors. I guess I just showed how much of a beginner I truly am.

Maybe I should create a class separately? I thought I had a good hand on this, being my first Java class.

Campbell Ritchie
Marshal
Posts: 56578
172
Diana Magers wrote:I will say that is very embarrassing. . . .
Don't be embarrassed. All of us who have done lots of programming have made the same mistake.

Yes, you should create a Pair class. It should have two private fields, a constructor, get methods for both fields, toString, and maybe also equals() and hashCode(), though the last two can be very awkward to override. You have been give instructions about other methods. You write one method at a time and you have another class whose method demonstrates each method working.You add to that code gradually, one method and one testing line at a time.

You can add the prompt for numbers later.

And welcome to the Ranch

Campbell Ritchie
Marshal
Posts: 56578
172
The reason you had the first compiler error is that you look as though you are creating a constructor inside another method; you can't do that, and you can't usually use the keyword public inside a method.

Diana Magers
Greenhorn
Posts: 9
Ok, Campbell,

Let's see, if I take the line that is giving me an error public class PairTest(double num1, double num2) and put it into a new file and also insert the get values with a function of the calculations? Is that maybe what you are speaking about?

Thank you btw for making me feel at ease. I really appreciate it.

Diana Magers
Greenhorn
Posts: 9
Maybe like this:

Diana Magers
Greenhorn
Posts: 9
And then change the main program to this:

Campbell Ritchie
Marshal
Posts: 56578
172
Diana Magers wrote:Maybe like this:

'Fraid not. That won't come anywhere near compiling.

Remind yourself what a class and constructor look like in the Java Tutorials. Give your Pair class one private field, which you set in its constructor, and give it a get method. This Tutorial link might be better; look for the bit about returning values. Test it with code like what I posted earlier (but 1 constructor argument only) in a main method in PairTest, until you have the getFirst method working. Leave everything else out until you have that bit working. You can add it later.

Campbell Ritchie
Marshal
Posts: 56578
172
Don't try to write a complete Pair class. Cut it down to the bare minimum. When you have got it working with one number and one get method, only then should you add the other number, with its get method and the additional constructor parameter.

Then you can sort out the compiler errors because the constructor now requires two arguments
What I said earlier: add to it gradually.

Joe Harry
Ranch Hand
Posts: 10128
3
Diana Magers wrote:I will say that is very embarrassing.

That is how you learn!

Winston Gutkowski
Bartender
Posts: 10575
66
Diana Magers wrote:And then change the main program to this...

I still don't see any sign of a Pair class, which was specifically requested in your instructions.

You could put your 'pair' logic and its main() code in the same class, but it's not a great habit to get into. When you write a Java application, only ONE class needs a main() method, and it should generally be very small, and separate from the classes that it runs. For more info, you might want to have a look at the MainIsAPain page.

Tip: try and separate, in your mind, what a Pair does from all that boilerplate code that you're writing to test it.

Winston