need help to create a linear equation

I am new to the world of java, and having a hard time creating a way to

input a set of coefficients and having "X" attached to them.

For example if i enter the coefficients 7, -4, 15, -5 I would like an output

answer in the form of " 7x^3 - 4x^2 + 15x -5 = 0. If any one has some

suggestions I would greatly appreciate it.

[356 byte] By [hawk1968a] at [2007-11-26 18:58:25]
# 1
What have you tried so far?
CaptainMorgan08a at 2007-7-9 20:38:45 > top of Java-index,Java Essentials,New To Java...
# 2

> I am new to the world of java, and having a hard time

> creating a way to

>

> input a set of coefficients and having "X" attached

> to them.

>

> For example if i enter the coefficients 7, -4, 15, -5

> I would like an output

>

> answer in the form of " 7x^3 - 4x^2 + 15x -5 = 0.

> If any one has some

>

> suggestions I would greatly appreciate it.

My suggestion would be to enter those numbers into a String array. Have a statement that first stores the numbers into a string array and then after they're all entered, depending on how many elements there are, add the right x to the power of.

ie.

say your array has 4 elements (like above)

(array insertion code above)

Find the length of the array. Then write a for loop to go through each element adding an X to everything. Now you can either than write another for loop that says to put an x^3 at the first element based on 4-1, the second element would be x^2 based on 4-2 etc... or you can write it all in the same for loop at the same time. Concatonate the X and a ^ (on every element except the last 2... you don't need to write a ^1 or ^0) to the strings that you input and then just add the approperiate exponent based on the array length - your current element positon - 1.

CrimsonYoshia at 2007-7-9 20:38:45 > top of Java-index,Java Essentials,New To Java...
# 3

this is what I have so far.

import java.io.*;

public class Factorial {

//Define the main method

public static void main (String[] args) throws IOException {

// Define maximum array size

final int MAXVAL = 1000;

//Define variables

double degree = 0;// degree of polynomial

int [] number = new int [MAXVAL];//array of x

String str;//Input String

int nvals = 0;

//Create a BufferedReader object

BufferedReader in1 = new BufferedReader(

new InputStreamReader(System.in));

//Ask the user for number of input values

System.out.print("Enter amount of coefficients: ");

degree = Integer.parseInt(in1.readLine());

// Calculate squares

for (int i = 0; i < degree; i++)

{

System.out.print("Enter coefficient value a"+ i +": ");

str = in1.readLine();

nvals = Integer.parseInt(str);

}

System.out.println ("The linear equation is : "+ number [nvals] + Factorial.fact(degree));

}

// Define the square method

public static double fact(double degree) {

double answer = 0;//sum of coefficients

char x = 0;

// Calculate squares

for (int i = 0; i < degree; i++)

if (degree == 0)

answer = (x);

else if (degree <= 1)

answer = (degree*Math.pow(x,1));

else

answer = (degree*Math.pow(x,degree-1));

return answer;

}

}

hawk1968a at 2007-7-9 20:38:45 > top of Java-index,Java Essentials,New To Java...
# 4
Definatley use a string array ;)
CrimsonYoshia at 2007-7-9 20:38:45 > top of Java-index,Java Essentials,New To Java...
# 5
Can you see what I am doing wrong?
hawk1968a at 2007-7-9 20:38:45 > top of Java-index,Java Essentials,New To Java...
# 6

> this is what I have so far.

>

>

> import java.io.*;

> ublic class Factorial {

>

>//Define the main method

> public static void main (String[] args)

> throws IOException {

>

>// Define maximum array size

>final int MAXVAL = 1000;

>

>//Define variables

> double degree = 0;

> // degree of

> polynomial

> int [] number = new int [MAXVAL];

> //array of x

> String str;

>

> /Input String

>int nvals = 0;

>

>//Create a BufferedReader object

> BufferedReader in1 = new BufferedReader(

> new

> InputStreamReader(System.in));

>

> //Ask the user for number of input values

>

> System.out.print("Enter amount of

> coefficients: ");

> degree =

> Integer.parseInt(in1.readLine());

>

> // Calculate squares

>for (int i = 0; i < degree; i++)

> {

> System.out.print("Enter coefficient

> t value a"+ i +": ");

>str = in1.readLine();

>nvals = Integer.parseInt(str);

>

>}

>

>

>

> System.out.println ("The linear equation is : "+

> number [nvals] + Factorial.fact(degree));

>

>}

>

>// Define the square method

> public static double fact(double degree)

> ) {

>

> double answer = 0;//sum of coefficients

>char x = 0;

>

>// Calculate squares

>for (int i = 0; i < degree; i++)

>

> (degree == 0)

>answer = (x);

>

>else if (degree <= 1)

> answer = (degree*Math.pow(x,1));

>

> else

> answer =

> (degree*Math.pow(x,degree-1));

>

>

>return answer;

>

>}

>

>

>}

Ok from what I'm reading, you're prompting the user for a number from a file to determine the number of elements in the array according to the amount of expressions you want... ie if you want 4 you make the array size 4 and want to enter x^3, x^2, x, ## = 0

in your fact method you have this:

char x = 0

which you don't use and are setting it too null.

and where are you even calling the method fact?

CrimsonYoshia at 2007-7-9 20:38:45 > top of Java-index,Java Essentials,New To Java...
# 7
I can only tell you that when I was using eclipse to write this program I was having trouble with the variable "x", so I tried making it a character.I have only been using java for 5 weeks for a class in college. I have been working on this for 3 days and I am about ready to go
hawk1968a at 2007-7-9 20:38:45 > top of Java-index,Java Essentials,New To Java...
# 8

ok here's what I'd do

ignoring the class and what not i'll ust write a quick method...

public static void main(String args[])

{

int i = 0;

int counter = 0;

String fullExpression = "";

String [] theArray;

//prompt the user for a number or use the file reader and obtain the number and store it in i

theArray = new String[i]

for( int j = 0; j <theArray.length; j++)

{

//obtain the first coefficent for the first element

//I left this out but use a JoptionPane window here

if((i - counter) != 2))

{

fullExpression = i + ("x^") + (i - counter - 1);

}

else

{

fullExpression = i + ("x");

}

if((i - counter) == 1))

{

fullExpression = i;

}

theArray[counter] = fullExpression;

}

}

>

CrimsonYoshia at 2007-7-9 20:38:45 > top of Java-index,Java Essentials,New To Java...
# 9
Characters are bad! :P You can't put a character into a string without first taking the primitive data type char and converting it into a string type object. (You need about 2 lines of code to do that but there's no need for excess work :P)Message was edited by:
CrimsonYoshia at 2007-7-9 20:38:45 > top of Java-index,Java Essentials,New To Java...
# 10
I am trying to create a method in a main method. This is what we were informed to try to create.
hawk1968a at 2007-7-9 20:38:45 > top of Java-index,Java Essentials,New To Java...
# 11

And btw that code up there is slightly flawed (brackets and semicolons arn't quite in line...)

actually... if you're reading in numbers, you can keep them in string format and not worry about turning them into integers... that would solve my last line of

fullExpression = i;

Secondly, yeah I know programming practice dictates that you make a second method...

first of all you can't create a method within a method secondly... i'll re-post my method as it is on the previous page (flaws and all) and put it into another method using a call statement

CrimsonYoshia at 2007-7-9 20:38:45 > top of Java-index,Java Essentials,New To Java...
# 12

> ok here's what I'd do

> ignoring the class and what not i'll ust write a

> quick method...

public static void main(String args[])

{

processing();

}

public static void processing()

{

int i = 0;

int counter = 0;

String fullExpression = "";

String [] theArray;

//prompt the user for a number or use the file

reader and obtain the number and store it in i

theArray = new String[i]

for( int j = 0; j <theArray.length; j++)

{

//obtain the first coefficent for the first

element

//I left this out but use a JoptionPane

window here

if((i - counter) != 2))

{

fullExpression = i + ("x^") + (i

- counter - 1);

}

else

{

fullExpression = i + ("x");

}

if((i - counter) == 1))

{

fullExpression = i;

theArray[counter] = fullExpression;

Message was edited by:

CrimsonYoshi

Message was edited by:

CrimsonYoshi

Message was edited by:

CrimsonYoshi>

CrimsonYoshia at 2007-7-9 20:38:45 > top of Java-index,Java Essentials,New To Java...
# 13
Ok i'm not fixing that... the spacing is horribly out of whack but you get what that was supposed to be.
CrimsonYoshia at 2007-7-9 20:38:45 > top of Java-index,Java Essentials,New To Java...
# 14
Thank you so much for your help and input. I appreciate it.
hawk1968a at 2007-7-9 20:38:45 > top of Java-index,Java Essentials,New To Java...
# 15

no problem... sorry that some of my answers were partially confusing... I have an assignment myself from university involving arrays and circular linked lists... and it's really tough :P

It's in part distracting me since I have about an hour and a half to finish it before it's due and I know I'm not going to come anywhere close to finishing it!

CrimsonYoshia at 2007-7-9 20:38:50 > top of Java-index,Java Essentials,New To Java...