Results 1 to 7 of 7
Thread: LinkedList help
- 09-19-2009, 03:29 AM #1
Senior 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 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:
Why does it not make the list bigger? And the numbers should be adding up. Any ideas?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; 09-19-2009 at 03:35 AM.
- 09-19-2009, 03:33 AM #2
Member
- 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:
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.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"
- 09-19-2009, 03:41 AM #3
Member
- 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:
I'm making the assumption the rest of your code works as expected.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; } }
- 09-19-2009, 04:27 AM #4
Senior 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.
- 09-19-2009, 04:37 AM #5
Member
- 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:
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 fewJava Code:double j = ((Double) PolyDriver.linkedlist.get(i)).doubleValue(); vrz = vrz+j;
- 09-19-2009, 04:42 AM #6
Member
- 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() } }
- 09-19-2009, 07:24 AM #7
Senior Member
- Join Date
- Nov 2008
- Posts
- 105
- Rep Power
- 0
Similar Threads
-
LinkedList problem
By Mika in forum New To JavaReplies: 7Last Post: 02-18-2009, 02:10 PM -
Please help me with LinkedList in Java
By lenny in forum New To JavaReplies: 2Last Post: 07-31-2007, 02:24 PM -
how to use LinkedList
By fred in forum Advanced JavaReplies: 1Last Post: 07-24-2007, 01:52 AM -
Problem with LinkedList
By Eric in forum Advanced JavaReplies: 1Last Post: 07-05-2007, 06:08 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks