Issue
I'm writing a program that implements a linked list, in order to read polynomials from a file and put them into linked list nodes. I've now read the polynomials from the file, split them into their coefficients and exponents. Now my task is to find a way to call an add function in a Polynomial class that creates the nodes. This function takes two integers as an argument. How can I write code that takes the two terms and calls the method without running into an error? Constants will have no exponent, only a coefficient.
The ultimate goal is to call a method that creates a node. This method accepts two integers as an argument, the coefficient and exponent of each term in the polynomial.
Here's what I have so far in my main method. I've written some print statements in certain areas of my loops to better understand what's happening at various places in the program.
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class Proj2 {
public static void main(String[] args) {
Polynomial list1 = new Polynomial();
File file = new File("polynomials.txt");
try (BufferedReader br = new BufferedReader(new FileReader(file))) {
String line;
while ((line = br.readLine()) != null) {
String[] term = line.split("[+]");
for (int i = 0; i < term.length; i++) {
while (term[i] != null) {
String[] s3 = term[i].split("[ ^//x]");
for (int u = 0; u < s3.length; u++) {
System.out.println(s3[u]);
// put the data into the polynomial class starting here with the add
// method
// constants get an exponent of 0 and monomials get an exponent of 1 see
// email
System.out.println("each element of the polynomial is divided here");
// Integer r = Integer.getInteger(s3.toString());
// System.out.println(r);
}
System.out.println("each term is divided here");
break;
}
}
System.out.println("each line/polynomial is divided here");
}
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
catch (NumberFormatException e) {
e.printStackTrace();
}
}
}
Here's the file the program is reading from:
4x^2+2x^1+3
5x^3+15
2x^2+2x^1+5
I've tried several different loops, several different methods of converting the Strings to Integers, such as:
Integer r = Integer.getInteger(s3.toString());
and passing r as argument to my method call, but I can't figure out how to iterate through in a way that makes sense, or as of right now, even a way to get r equal anything other than a null value. Any help at all would be greatly appreciated.
Solution
"... How can I write code that takes the two terms and calls the method ..."
Create a class to hold the each polynomial term.
Subsequently, create a static method to parse a term.
class Polynomial {
BigDecimal coefficient;
BigInteger power;
static Polynomial parse(String s) {
Polynomial p = new Polynomial();
int i = s.indexOf('^');
if (i == -1) p.coefficient = new BigDecimal(s);
else {
if (i != 1) p.coefficient = new BigDecimal(s.substring(0, i - 1));
p.power = new BigInteger(s.substring(i + 1));
}
return p;
}
}
From here, use a 2-dimensional LinkedList to contain each polynomial expression.
LinkedList<LinkedList<Polynomial>> l = new LinkedList<>();
String f = "polynomials.txt";
try (BufferedReader r = new BufferedReader(new FileReader(f))) {
String s;
LinkedList<Polynomial> t;
while ((s = r.readLine()) != null) {
t = new LinkedList<>();
for (String v : s.split("[+*-]")) t.add(Polynomial.parse(v));
l.add(t);
}
}
Output
coefficient = 4, power = 2
coefficient = 2, power = 1
coefficient = 3, power = null
coefficient = 5, power = 3
coefficient = 15, power = null
coefficient = 2, power = 2
coefficient = 2, power = 1
coefficient = 5, power = null
Answered By - Reilas
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.