Results 1 to 7 of 7

Thread: LinkedList help

  1. #1
    jigglywiggly is offline Senior Member
    Join Date
    Nov 2008
    Posts
    105
    Rep Power
    0

    Default LinkedList help

    Ok so I just learned quickly about lists, so I have a not too hard project I think, but am having a few issues:

    Here is the project. Create a lovely polynomial calculator, that takes the equation from the user, and a number for x.
    Then solve it, but I have to use lists. As well as that I have to printout the equation with x at the end. And I will also have to have to add a method for finding the derivative of the equation.

    I'll worry about the printout and the derivative at the end, except I have a problem right now... I'll show my code first



    Here is the lovely main
    Java Code:
    import java.util.*;
    
    public class PolyDriver {
    
        public static void main(String[] args) {
           
            
            boolean isrunning = true;
            String userinput="no";
            String no="no";
            String yes="yes";
            double whatisx=0;
                Scanner scan = new Scanner(System.in);
    
            System.out.println("What is X?");
            whatisx =scan.nextDouble();
            while(userinput.equals(no)) {
                System.out.println("Enter the first integer");
                double d = scan.nextDouble();
                System.out.println("Enter the exponent");
                int e = scan.nextInt();
                Poly m = new Poly(d, e, whatisx);
                System.out.println("Done?");
                String j = scan.next();
                if(j.equals(yes)) {
    
                System.exit(0);
                }
            }
    
    
    
            // System.out.println("Enter the number for x");
            // Scanner scan = new Scanner(System.in);
    
    
    
    
    
    
    
        }
    }




    Then here is the Poly class


    Java Code:
    import java.util.*;
    
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    
    /**
     *
     * @author JigglyWiggly-laptop
     */
    
    public class Poly  {
        double integer=0;
        int exponent=0;
        double variable=0;
        double vrz=0;
        double whatisx=0;
    
        LinkedList linkedlist = new LinkedList();
        
        public Poly(double d, int e, double x) {
            integer=d;
            exponent=e;
            whatisx=x;
            calculations();
           // calculations();
            
        }
    
        public double calculations() {
            double v = Math.pow(whatisx, exponent);
            double j = v*integer;
            linkedlist.add(j);
            printout();
            return j;
        }
    
        public double printout() {
            for(int i=0; i<linkedlist.size(); i++) {
               Object j= linkedlist.get(i);
                vrz =+((Double)j).doubleValue();
            }
            
            System.out.println(linkedlist.size());
            System.out.println(vrz);
            return vrz;
    
        }
    
    
    
    
    
    
    }

    When I run it:
    Java Code:
    run:
    What is X?
    2
    Enter the first integer
    2
    Enter the exponent
    2
    ---
    1
    8.0
    Done?
    no
    Enter the first integer
    2
    Enter the exponent
    2
    ---
    1
    8.0
    Done?
    no
    Enter the first integer
    2
    Enter the exponent
    2
    ---
    1
    8.0
    Done?
    Why does it not make the list bigger? And the numbers should be adding up. Any ideas?

    (Also ignore the funky way of getting the user input, this is the way we were told to do so)
    Last edited by jigglywiggly; 09-19-2009 at 04:35 AM.

  2. #2
    Moncleared is offline Member
    Join Date
    Jan 2009
    Posts
    92
    Rep Power
    0

    Default

    Very very quick glance over the code, one thing I see that stands out is the following:
    Java Code:
                Poly m = new Poly(d, e, whatisx);
    Are you sure you want to do this? If the user selects no, you create a new instance of the Poly class over top of the one you just created, therefore losing all previous data. So you are pretty much killing your first LinkedList, and any created after.

    Make sense? The only list that will exist is the last one created as the user says "yes"

  3. #3
    Moncleared is offline Member
    Join Date
    Jan 2009
    Posts
    92
    Rep Power
    0

    Default

    So if you want to keep the contents of the LinkedList you are adding to. You may want to consider something like putting the LinkedList in a safe spot. For example:

    Java Code:
    import java.util.*;
    
    public class PolyDriver {
        public static LinkedList linkedlist = new LinkedList();
    
        public static void main(String[] args) {
          
            boolean isrunning = true;
            String userinput="no";
            String no="no";
            String yes="yes";
            double whatisx=0;
                Scanner scan = new Scanner(System.in);
    
            System.out.println("What is X?");
            whatisx =scan.nextDouble();
            while(userinput.equals(no)) {
                System.out.println("Enter the first integer");
                double d = scan.nextDouble();
                System.out.println("Enter the exponent");
                int e = scan.nextInt();
                Poly m = new Poly(d, e, whatisx);
                System.out.println("Done?");
                String j = scan.next();
                if(j.equals(yes)) {
    
                System.exit(0);
                }
            }
    
    
        }
    }
    
    public class Poly  {
        double integer=0;
        int exponent=0;
        double variable=0;
        double vrz=0;
        double whatisx=0;
    
        
        public Poly(double d, int e, double x) {
            integer=d;
            exponent=e;
            whatisx=x;
            calculations();
           // calculations();
            
        }
    
        public double calculations() {
            double v = Math.pow(whatisx, exponent);
            double j = v*integer;
            PolyDriver.linkedlist.add(j);
            printout();
            return j;
        }
    
        public double printout() {
            for(int i=0; i<PolyDriver.linkedlist.size(); i++) {
               Object j= PolyDriver.linkedlist.get(i);
                vrz =+((Double)j).doubleValue();
            }
            
            System.out.println(PolyDriver.linkedlist.size());
            System.out.println(vrz);
            return vrz;
    
        }
    }
    I'm making the assumption the rest of your code works as expected.

  4. #4
    jigglywiggly is offline Senior Member
    Join Date
    Nov 2008
    Posts
    105
    Rep Power
    0

    Default

    Ahh that fixed one major part, now it grows :D, however it doesn't add up the values. I will look closer, but that was one major step in the right direction, thanks.

  5. #5
    Moncleared is offline Member
    Join Date
    Jan 2009
    Posts
    92
    Rep Power
    0

    Default

    If you'd like additional help, I don't mind. But update me on what your Output is now that you are properly storing the double values into the LinkedList. And tell me what you are expecting to come out. The only thing that looks like it might be causing me is that Object converting to a double. My method was this:

    Java Code:
            	double j = ((Double) PolyDriver.linkedlist.get(i)).doubleValue();
                vrz = vrz+j;
    Also note that the current code will printout every time a user inputs "no". So if that is what you want, cool. If you want to print out ONCE, meaning that once the user has given "yes" value, print out everything in the LinkedList. I would move that method up to your Main class. I'll give an example. in a few

  6. #6
    Moncleared is offline Member
    Join Date
    Jan 2009
    Posts
    92
    Rep Power
    0

    Default

    Java Code:
    import java.util.*;
    
    public class PolyDriver {
    	public static LinkedList linkedlist = new LinkedList();
    
    	public static void main(String[] args) {
    
    		boolean isrunning = true;
    		String userinput="no";
    		String no="no";
    		String yes="yes";
    		double whatisx=0;
    		Scanner scan = new Scanner(System.in);
    
    		System.out.println("What is X?");
    		whatisx =scan.nextDouble();
    		while(userinput.equals(no)) {
    			System.out.println("Enter the first integer");
    			double d = scan.nextDouble();
    			System.out.println("Enter the exponent");
    			int e = scan.nextInt();
    			Poly m = new Poly(d, e, whatisx);
    			System.out.println("Done?");
    			String j = scan.next();
    			if(j.equals(yes)) {
    				double vrz=0;
    				//Print out at this point before we System.exit(0)
    				for(int i=0; i<PolyDriver.linkedlist.size(); i++) {
    					double out = ((Double) PolyDriver.linkedlist.get(i)).doubleValue();
    					vrz = vrz+out;
    				}
    				System.out.println(PolyDriver.linkedlist.size());
    				System.out.println(vrz);
    				System.exit(0);
    			}
    		}
    
    
    	}
    }
    
    public class Poly  {
    	double integer=0;
    	int exponent=0;
    	double variable=0;
    	double vrz=0;
    	double whatisx=0;
    
    
    	public Poly(double d, int e, double x) {
    		integer=d;
    		exponent=e;
    		whatisx=x;
    		calculations();
    	}
    
    	public double calculations() {
    		double v = Math.pow(whatisx, exponent);
    		double j = v*integer;
    		PolyDriver.linkedlist.add(j);
    		return j; // Why return a value you aren't using? remove this and change public void calculations()
    	}
    }

  7. #7
    jigglywiggly is offline Senior Member
    Join Date
    Nov 2008
    Posts
    105
    Rep Power
    0

    Default

    Java Code:
            	double j = ((Double) PolyDriver.linkedlist.get(i)).doubleValue();
                vrz = vrz+j;
    Yup that works, and I don't mind it printing. Oh and why was it returning? Well it doesn't hurt that it's returning, it doesn't need to. But I changed it to void anyway.

Similar Threads

  1. LinkedList problem
    By Mika in forum New To Java
    Replies: 7
    Last Post: 02-18-2009, 03:10 PM
  2. Please help me with LinkedList in Java
    By lenny in forum New To Java
    Replies: 2
    Last Post: 07-31-2007, 03:24 PM
  3. how to use LinkedList
    By fred in forum Advanced Java
    Replies: 1
    Last Post: 07-24-2007, 02:52 AM
  4. Problem with LinkedList
    By Eric in forum Advanced Java
    Replies: 1
    Last Post: 07-05-2007, 07:08 AM

Posting Permissions

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