Results 1 to 7 of 7
Thread: LinkedList help
 09192009, 04:29 AM #1Senior Member
 Join Date
 Nov 2008
 Posts
 105
 Rep Power
 0
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 JigglyWigglylaptop */ 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?
(Also ignore the funky way of getting the user input, this is the way we were told to do so)Last edited by jigglywiggly; 09192009 at 04:35 AM.
 09192009, 04:33 AM #2Member
 Join Date
 Jan 2009
 Posts
 92
 Rep Power
 0
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);
Make sense? The only list that will exist is the last one created as the user says "yes"
 09192009, 04:41 AM #3Member
 Join Date
 Jan 2009
 Posts
 92
 Rep Power
 0
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; } }
 09192009, 05:27 AM #4Senior Member
 Join Date
 Nov 2008
 Posts
 105
 Rep Power
 0
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.
 09192009, 05:37 AM #5Member
 Join Date
 Jan 2009
 Posts
 92
 Rep Power
 0
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;
 09192009, 05:42 AM #6Member
 Join Date
 Jan 2009
 Posts
 92
 Rep Power
 0
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() } }
 09192009, 08:24 AM #7Senior Member
 Join Date
 Nov 2008
 Posts
 105
 Rep Power
 0
Similar Threads

LinkedList problem
By Mika in forum New To JavaReplies: 7Last Post: 02182009, 03:10 PM 
Please help me with LinkedList in Java
By lenny in forum New To JavaReplies: 2Last Post: 07312007, 03:24 PM 
how to use LinkedList
By fred in forum Advanced JavaReplies: 1Last Post: 07242007, 02:52 AM 
Problem with LinkedList
By Eric in forum Advanced JavaReplies: 1Last Post: 07052007, 07:08 AM
Bookmarks