Question: Main issues with the following code:Im not sure if its reading the file correctly because its adding all terms together. In other words the the
Main issues with the following code:Im not sure if its reading the file correctly because its adding all terms together. In other words the the first line of terms is adding the second line of terms but thats not my intention. Each line of term should have their own polynomial expression as shown below
Basic specifications :
The code should add the polynomial numbers using a linked list. Java . Keep in mind the polynomials will be gotten from a file.
Things to keep in mind the list should be in decending order and traverse the list after all coefficient/exponent pair Term nodes from one input line have been inserted.
Here are the specifications
//The Polynomial class would likely have a single private data member (reference to class Term) called head.
//The Term class would likely have three private data members (coefficient and exponent of type int) and next of type reference to Term.
//The latter class would also have the following public methods: constructor(s), gets and sets for each data member, and perhaps a formatting method with the special output requirements.
An example of input is 7 1 2 4 6 2 -3 2 -1 -1 3
Negative negative pair signals the termination
***NO doubly linked list , it should be a single linked list **
Input file contains the following :
7 1 2 4 6 2 -3 2 -1 -1 3 ----> 2x^4 - 3x^2 +6x^2 + 7x
5 1 2 3 6 0 -3 2 -1 -1 3 ----> 2*X^3 + (-3)*X^2 + 5*X + 6 // thats the ouput for one line of the the file , each line of the file should have one polynomial output
Code
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Polynomial {
private Term head;
// Constructor for creating an empty polynomial object
public Polynomial() {
head = null;
}
// Method for adding a term to the polynomial, this is inside the polynomial term
public void addTerm(int coefficient, int exponent) {
// Create a new term object
Term newTerm = new Term(coefficient, exponent);
// If the polynomial is empty, set the new term as the head
if (head == null) {
head = newTerm;
} else {
// Traverse the polynomial until we find the correct position for the new term
Term current = head;
Term previous = null;
while (current != null && current.getExponent() > exponent) {
previous = current;
current = current.getNext();
}
// If a term with the same exponent already exists, add the coefficients
if (current != null && current.getExponent() == exponent) {
current.setCoefficient(current.getCoefficient() + coefficient);
} else {
// Otherwise, insert the new term into the polynomial
if (previous == null) {
head = newTerm;
} else {
previous.setNext(newTerm);
}
newTerm.setNext(current);
}
}
// Method for formatting the polynomial as a string
public String toString() {
StringBuilder sb = new StringBuilder();
if (head != null) {
sb.append(head.toString());
Term current = head.getNext();
while (current != null) {
if (current.getCoefficient() > 0) {
sb.append(" + ");
} else {
sb.append(" - ");
}
sb.append(current.toString());
current = current.getNext();
}
}
return sb.toString();
}
}
public class Term {
private int coefficient;
private int exponent;
private Term next;
// Constructor for creating a new term object
public Term(int coefficient, int exponent) {
this.coefficient = coefficient;
this.exponent = exponent;
next = null;
}
// Getter and setter methods for the coefficient and exponent
public int getCoefficient() {
return coefficient;
}
public void setCoefficient(int coefficient) {
this.coefficient = coefficient;
}
public int getExponent() {
return exponent;
}
public void setExponent(int exponent) {
this.exponent = exponent;
}
// Getter and setter methods for the next term in the polynomial
public Term getNext() {
return next;
}
public void setNext(Term next) {
this.next = next;
}
// Method for formatting the term as a string
public String toString() {
if (exponent == 0) {
return Integer.toString(coefficient);
} else if (exponent == 1) {
if (coefficient == 1) {
return "x";
} else if (coefficient == -1) {
return "-x";
} else {
return coefficient + "x";
}
} else {
if (coefficient == 1) {
return "x^" + exponent;
} else if (coefficient == -1) {
return "-x^" + exponent;
} else {
return coefficient + "x^" + exponent;
}
}
}
}
import java.io.File;
import java.io.FileNotFoundException;
public class Main {
public static void main(String[] args) {
Polynomial p = new Polynomial();
try {
Scanner input = new Scanner(new File("polynomial.txt"));
while (input.hasNextLine()) {
String line = input.nextLine();
String[] tokens = line.split(" ");
int coefficient = Integer.parseInt(tokens[0]);
int exponent = Integer.parseInt(tokens[1]);
p.addTerm(coefficient, exponent);
}
input.close();
} catch (FileNotFoundException e) {
System.out.println("File not found: " + e.getMessage());
System.exit(1);
}
System.out.println("Polynomial: " + p);
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
