• 09-19-2009, 04:29 AM
jigglywiggly
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
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

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:
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)
• 09-19-2009, 04:33 AM
Moncleared
Very very quick glance over the code, one thing I see that stands out is the following:
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"
• 09-19-2009, 04:41 AM
Moncleared
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:

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.
• 09-19-2009, 05:27 AM
jigglywiggly
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, 05:37 AM
Moncleared
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:

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
• 09-19-2009, 05:42 AM
Moncleared
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, 08:24 AM
jigglywiggly
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.