Write a Java program consisting of two files Pair.java and DemoPair.java.

The public class Pair

Has two private fields, x and y, each a double.
Has a public constructor with two double parameters that become the field values of the created object.
Has one public instance (i.e. not static) accessor method linearCombination(double a, double b) that returns the (double) value a*x+b*y.

DemoPair.java contains the main program that

Creates an array of 100 Pair's, giving each Pair constructor two double's returned by Math.random().
Uses the accessor method to compute 1 times the first x plus 100 times the first y. The main program then prints the x, y, and computed value on one line.
Uses the accessor method to compute 2 times the second x plus 99 times the second y and prints the x, y, and computed value on one line.
Uses the accessor method to compute 3 times the third x plus 98 times the third y and prints the x, y, and the computed value on one line.
...
Does the same computation and print for the sum of 100 times the 100th x and 1 times the 100th y.

Does anyone know where to start to writing the main method DemoPair? Is it possible to use a linked list for it?
Thanks

This means that you're trying to run code that can't compile. *Never* do this. Instead you'll want to fix all compilation issues first before trying to run the code.

Quote:

Code:

The method toString(long[]) in the type Arrays is not applicable for the arguments (Pair)

at DemoPair.main(DemoPair.java:21)

The error message is telling you exactly what's wrong. You're calling Arrays.toString(...) but are not passing an array into the method call but instead are passing in a single Pair object (the second one in the array), and that doesn't make sense to the compiler (or to me).

02-11-2013, 01:30 AM

Fubarable

Re: Linked list or array

Quote:

Originally Posted by vx117

I cannot print out the values of the objects in the array. I keep getting this exception:

This means that you're trying to run code that can't compile. *Never* do this. Instead you'll want to fix all compilation issues first before trying to run the code.

Quote:

Code:

The method toString(long[]) in the type Arrays is not applicable for the arguments (Pair)

at DemoPair.main(DemoPair.java:21)

The error message is telling you exactly what's wrong. You're calling Arrays.toString(...) but are not passing an array into the method call but instead are passing in a single Pair object (the second one in the array), and that doesn't make sense to the compiler (or to me).

02-11-2013, 01:41 AM

vx117

Re: Linked list or array

Okay, so I fixed my error, but now I'm still printing out the memory address location instead of the numbers that I want.

Code:

System.out.println(Arrays.toString(hundredPairs));

And I'm getting something like this:

[Pair@7d420fea, Pair@7c230be4....]

Doesn't the toString method allow you to print out the values of the objects?

02-11-2013, 02:03 AM

Fubarable

Re: Linked list or array

Quote:

Originally Posted by vx117

Okay, so I fixed my error, but now I'm still printing out the memory address location instead of the numbers that I want.

Code:

System.out.println(Arrays.toString(hundredPairs));

And I'm getting something like this:

[Pair@7d420fea, Pair@7c230be4....]

Doesn't the toString method allow you to print out the values of the objects?

Yes it does, but you first need to make sure that the class of the object being printed has a valid toString() method override. Does your Pair in fact have a toString() override? I can answer that without seeing your code based on your question -- no. Solution: give it a decent toString() method that returns a String with the information that you want to see.

02-11-2013, 03:07 AM

vx117

Re: Linked list or array

Alright I got most of what I want, but now i ran into another problem. As you can see I'm calling another method

Code:

public double linearCombination(double a, double b) {

return a*x+b*y;
}

to perform on the Array of objects, with my toString method I was able to display the x and y values but not the value after the linearCombination method is used. How do I got about doing that?

The toString method should have nothing to do with the linearCombination since this isn't an intrinsic property of your Pair instance.

I think that perhaps you want to re-think how you're printing things out. Perhaps you want to print out information *inside* of the for loop. Perhaps you want to assign the double returned from your linearCombination() method call and print that too, again *inside* of the for loop.

02-11-2013, 04:21 AM

vx117

Re: Linked list or array

I get what you're saying but my problem is just trying to print out actual numbers and not the memory address. Is there a alternative to the toString method to print the values of objects?

02-11-2013, 06:11 AM

Fubarable

Re: Linked list or array

Yes, you could call your getter methods inside the for loop and print the values returned. Again, this should all be done *inside* the for loop; this is critical.