Results 1 to 12 of 12
  1. #1
    vx117 is offline Member
    Join Date
    Dec 2012
    Posts
    22
    Rep Power
    0

    Default Linked list or array

    This is the assignment:

    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

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Linked list or array

    Quote Originally Posted by vx117 View Post
    Does anyone know where to start to writing the main method DemoPair?
    Java Code:
    public class DemoPair {
      public static void main(String[] args) {
        // your code goes here
      }
    }
    Is it possible to use a linked list for it?
    Why would you want to do this? The instructions seem explicit, "create an array ...", and so I think I'd stick with the array.

  3. #3
    vx117 is offline Member
    Join Date
    Dec 2012
    Posts
    22
    Rep Power
    0

    Default Re: Linked list or array

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

    Exception in thread "main" java.lang.Error: Unresolved compilation problem:
    The method toString(long[]) in the type Arrays is not applicable for the arguments (Pair)

    at DemoPair.main(DemoPair.java:21)

    Java Code:
    import java.util.Arrays;
    
    
    public class DemoPair {
    
    	public static void main(String[] args) {
    		
    		Pair [] hundredPairs = new Pair [100];
    		
    		for ( int i = 0; i < hundredPairs.length; i++){
    			
    			hundredPairs[i] = new Pair (Math.random(), Math.random());
    			
    			}
    		
    		for (int i = 0; i < hundredPairs.length; i++){
    			
    			hundredPairs [i].linearCombination(i+1,100-i ); 
    			
    		}
    		System.out.println(Arrays.toString(hundredPairs[1]));
    		             
    	}
    }

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Linked list or array

    Quote Originally Posted by vx117 View Post
    I cannot print out the values of the objects in the array. I keep getting this exception:

    Exception in thread "main" java.lang.Error: Unresolved compilation problem:
    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.

    Java 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).

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Linked list or array

    Quote Originally Posted by vx117 View Post
    I cannot print out the values of the objects in the array. I keep getting this exception:

    Exception in thread "main" java.lang.Error: Unresolved compilation problem:
    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.

    Java 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).

  6. #6
    vx117 is offline Member
    Join Date
    Dec 2012
    Posts
    22
    Rep Power
    0

    Default 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.

    Java 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?

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Linked list or array

    Quote Originally Posted by vx117 View Post
    Okay, so I fixed my error, but now I'm still printing out the memory address location instead of the numbers that I want.

    Java 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.

  8. #8
    vx117 is offline Member
    Join Date
    Dec 2012
    Posts
    22
    Rep Power
    0

    Default 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

    Java 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?

    Java Code:
    public class Pair {
    
    	private double x;
    	private double y;
    	
    	public Pair (double x, double y){
    		this.x = x;
    		this.y = y;
    	}
    
    	public double linearCombination(double a, double b) {
    		
    		return a*x+b*y;
    	}
    	
    	@Override
    	public String toString() {
    		return " x = " + x + ", y = " + y + ", total = "  ;
    	}
    }
    Java Code:
    import java.util.Arrays;
    
    
    public class DemoPair {
    
    	public static void main(String[] args) {
    		
    		Pair [] hundredPairs = new Pair [100];
    		
    		for ( int i = 0; i < hundredPairs.length; i++){
    			
    			hundredPairs[i] = new Pair (Math.random(), Math.random());
    			hundredPairs [i].linearCombination(i+1,100-i );
    			
    			}
    		
    		System.out.println(Arrays.toString(hundredPairs));
    		
    		}
    	
    }

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Linked list or array

    Quote Originally Posted by vx117 View Post
    Alright I got most of what I want, but now i ran into another problem. As you can see I'm calling another method

    Java 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?

    Java Code:
    public class Pair {
    
    	private double x;
    	private double y;
    	
    	public Pair (double x, double y){
    		this.x = x;
    		this.y = y;
    	}
    
    	public double linearCombination(double a, double b) {
    		
    		return a*x+b*y;
    	}
    	
    	@Override
    	public String toString() {
    		return " x = " + x + ", y = " + y + ", total = "  ;
    	}
    }
    Java Code:
    import java.util.Arrays;
    
    
    public class DemoPair {
    
    	public static void main(String[] args) {
    		
    		Pair [] hundredPairs = new Pair [100];
    		
    		for ( int i = 0; i < hundredPairs.length; i++){
    			
    			hundredPairs[i] = new Pair (Math.random(), Math.random());
    			hundredPairs [i].linearCombination(i+1,100-i );
    			
    			}
    		
    		System.out.println(Arrays.toString(hundredPairs));
    		
    		}
    	
    }
    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.

  10. #10
    vx117 is offline Member
    Join Date
    Dec 2012
    Posts
    22
    Rep Power
    0

    Default 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?

  11. #11
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default 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.

  12. #12
    vx117 is offline Member
    Join Date
    Dec 2012
    Posts
    22
    Rep Power
    0

    Default Re: Linked list or array

    Solved, thanks for the help!
    Last edited by vx117; 02-11-2013 at 05:16 PM.

Similar Threads

  1. Replies: 0
    Last Post: 09-25-2012, 04:07 AM
  2. Linked List, Array List time complexity
    By Rick99771977 in forum New To Java
    Replies: 4
    Last Post: 08-18-2011, 05:37 AM
  3. Array to Linked List
    By Aiquoc in forum New To Java
    Replies: 2
    Last Post: 04-23-2011, 01:32 PM
  4. Replies: 4
    Last Post: 02-21-2011, 09:34 AM
  5. linked list or array?
    By sick_peng in forum New To Java
    Replies: 6
    Last Post: 04-15-2009, 07:33 PM

Posting Permissions

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