Custom LinkedList implementation to store terms of a polynomial
For a homework assignment I'm working on you're required to create a custom LinkedList data structure to hold the terms of a polynomial. I'm having a problem at the moment with my constructor adding terms to the data structure because it needs to accept a string like "5.2 3 2.1 2" (which would be the equivalent of 5.2^3 + 2.1^2) and store it in the my custom LinkedList. Some of the requirements include that terms cannot have coefficients of zero, exponents must be integers, and coefficients can be either integers or doubles. When I trace the program using my IDE's debugger what I've seen is that for some reason valid coefficients are getting caught on the clause that I've marked with a "#" and the reference to the head of my list (the Term first variable) doesn't seem to be getting additional variables from the inputted string linked to it properly. Any help you can give would be much appreciated, thanks in advance. There are many required methods but this the relevant code for the problem I'm experiencing:
import java.util.regex.Pattern;
import java.util.regex.Matcher;
public class Polynomial
{
// instance variables
private Term first;
private int numTerms;
/**
* Constructor for objects of class Polynomial
*/
public Polynomial(String s)
{
Pattern whiteSpace = Pattern.compile(" ");
String poly = whiteSpace.split(s);
double coefficient;
int exponent;
if(poly.length % 2 == 1) {
throw new IllegalArgumentException();
}
first = new Term(); // dummy variable so that checking the first term specially is unnecessary
for(int term = 0; term < poly.length; term += 2) {
if(poly[term].matches("[\-][0-9]||[0-9]||[0-9][\.][0-9]||[\-][0-9][\.][0-9]")) {
coefficient = Double.parseDouble(poly[term]);
if(poly[term + 1].matches("[0-9]")) {
exponent = Integer.parseInt(poly[term++]);
} else {
throw new IllegalArgumentException(); //#
}
numTerms++;
this.addTerm(coefficient, exponent);
}
}
}
public void addTerm(double coef, int exp)
{
if(coef == 0) {
throw new IllegalArgumentException();
}
Term pointer = first;
while(pointer.next != null) {
if(exp == pointer.next.exponent) {
if(coef + pointer.next.coefficient == 0) {
pointer.next = pointer.next.next;
numTerms--;
} else {
pointer.next.coefficient += coef;
break;
}
} else if(pointer.next.exponent < exp) {
Term newTerm = new Term(coef, exp, pointer.next.next);
pointer.next = newTerm;
numTerms++;
break;
}
pointer = pointer.next;
}
}
private class Term {
double coefficient;
int exponent;
Term next;
Term() {
next = null;
}
Term(double coef, int exp, Term nextTerm) {
coefficient = coef;
exponent = exp;
next = nextTerm;
}
}`
java singly-linked-list polynomials
add a comment |
For a homework assignment I'm working on you're required to create a custom LinkedList data structure to hold the terms of a polynomial. I'm having a problem at the moment with my constructor adding terms to the data structure because it needs to accept a string like "5.2 3 2.1 2" (which would be the equivalent of 5.2^3 + 2.1^2) and store it in the my custom LinkedList. Some of the requirements include that terms cannot have coefficients of zero, exponents must be integers, and coefficients can be either integers or doubles. When I trace the program using my IDE's debugger what I've seen is that for some reason valid coefficients are getting caught on the clause that I've marked with a "#" and the reference to the head of my list (the Term first variable) doesn't seem to be getting additional variables from the inputted string linked to it properly. Any help you can give would be much appreciated, thanks in advance. There are many required methods but this the relevant code for the problem I'm experiencing:
import java.util.regex.Pattern;
import java.util.regex.Matcher;
public class Polynomial
{
// instance variables
private Term first;
private int numTerms;
/**
* Constructor for objects of class Polynomial
*/
public Polynomial(String s)
{
Pattern whiteSpace = Pattern.compile(" ");
String poly = whiteSpace.split(s);
double coefficient;
int exponent;
if(poly.length % 2 == 1) {
throw new IllegalArgumentException();
}
first = new Term(); // dummy variable so that checking the first term specially is unnecessary
for(int term = 0; term < poly.length; term += 2) {
if(poly[term].matches("[\-][0-9]||[0-9]||[0-9][\.][0-9]||[\-][0-9][\.][0-9]")) {
coefficient = Double.parseDouble(poly[term]);
if(poly[term + 1].matches("[0-9]")) {
exponent = Integer.parseInt(poly[term++]);
} else {
throw new IllegalArgumentException(); //#
}
numTerms++;
this.addTerm(coefficient, exponent);
}
}
}
public void addTerm(double coef, int exp)
{
if(coef == 0) {
throw new IllegalArgumentException();
}
Term pointer = first;
while(pointer.next != null) {
if(exp == pointer.next.exponent) {
if(coef + pointer.next.coefficient == 0) {
pointer.next = pointer.next.next;
numTerms--;
} else {
pointer.next.coefficient += coef;
break;
}
} else if(pointer.next.exponent < exp) {
Term newTerm = new Term(coef, exp, pointer.next.next);
pointer.next = newTerm;
numTerms++;
break;
}
pointer = pointer.next;
}
}
private class Term {
double coefficient;
int exponent;
Term next;
Term() {
next = null;
}
Term(double coef, int exp, Term nextTerm) {
coefficient = coef;
exponent = exp;
next = nextTerm;
}
}`
java singly-linked-list polynomials
can you add sample input
– swayamraina
Nov 18 '18 at 6:47
you forgot to add the input?
– swayamraina
Nov 18 '18 at 6:58
This project is meant to not have a user interface and only be tested through JUnit testing but here are some examples of some valid statements: Polynomial p = new Polynomial("4 5 3 2 -5 0") and Polynomial p = new Polynomial("4.8 5 -3.5 2 1.5 0"). I hope this helps!
– Rosa
Nov 18 '18 at 6:59
add a comment |
For a homework assignment I'm working on you're required to create a custom LinkedList data structure to hold the terms of a polynomial. I'm having a problem at the moment with my constructor adding terms to the data structure because it needs to accept a string like "5.2 3 2.1 2" (which would be the equivalent of 5.2^3 + 2.1^2) and store it in the my custom LinkedList. Some of the requirements include that terms cannot have coefficients of zero, exponents must be integers, and coefficients can be either integers or doubles. When I trace the program using my IDE's debugger what I've seen is that for some reason valid coefficients are getting caught on the clause that I've marked with a "#" and the reference to the head of my list (the Term first variable) doesn't seem to be getting additional variables from the inputted string linked to it properly. Any help you can give would be much appreciated, thanks in advance. There are many required methods but this the relevant code for the problem I'm experiencing:
import java.util.regex.Pattern;
import java.util.regex.Matcher;
public class Polynomial
{
// instance variables
private Term first;
private int numTerms;
/**
* Constructor for objects of class Polynomial
*/
public Polynomial(String s)
{
Pattern whiteSpace = Pattern.compile(" ");
String poly = whiteSpace.split(s);
double coefficient;
int exponent;
if(poly.length % 2 == 1) {
throw new IllegalArgumentException();
}
first = new Term(); // dummy variable so that checking the first term specially is unnecessary
for(int term = 0; term < poly.length; term += 2) {
if(poly[term].matches("[\-][0-9]||[0-9]||[0-9][\.][0-9]||[\-][0-9][\.][0-9]")) {
coefficient = Double.parseDouble(poly[term]);
if(poly[term + 1].matches("[0-9]")) {
exponent = Integer.parseInt(poly[term++]);
} else {
throw new IllegalArgumentException(); //#
}
numTerms++;
this.addTerm(coefficient, exponent);
}
}
}
public void addTerm(double coef, int exp)
{
if(coef == 0) {
throw new IllegalArgumentException();
}
Term pointer = first;
while(pointer.next != null) {
if(exp == pointer.next.exponent) {
if(coef + pointer.next.coefficient == 0) {
pointer.next = pointer.next.next;
numTerms--;
} else {
pointer.next.coefficient += coef;
break;
}
} else if(pointer.next.exponent < exp) {
Term newTerm = new Term(coef, exp, pointer.next.next);
pointer.next = newTerm;
numTerms++;
break;
}
pointer = pointer.next;
}
}
private class Term {
double coefficient;
int exponent;
Term next;
Term() {
next = null;
}
Term(double coef, int exp, Term nextTerm) {
coefficient = coef;
exponent = exp;
next = nextTerm;
}
}`
java singly-linked-list polynomials
For a homework assignment I'm working on you're required to create a custom LinkedList data structure to hold the terms of a polynomial. I'm having a problem at the moment with my constructor adding terms to the data structure because it needs to accept a string like "5.2 3 2.1 2" (which would be the equivalent of 5.2^3 + 2.1^2) and store it in the my custom LinkedList. Some of the requirements include that terms cannot have coefficients of zero, exponents must be integers, and coefficients can be either integers or doubles. When I trace the program using my IDE's debugger what I've seen is that for some reason valid coefficients are getting caught on the clause that I've marked with a "#" and the reference to the head of my list (the Term first variable) doesn't seem to be getting additional variables from the inputted string linked to it properly. Any help you can give would be much appreciated, thanks in advance. There are many required methods but this the relevant code for the problem I'm experiencing:
import java.util.regex.Pattern;
import java.util.regex.Matcher;
public class Polynomial
{
// instance variables
private Term first;
private int numTerms;
/**
* Constructor for objects of class Polynomial
*/
public Polynomial(String s)
{
Pattern whiteSpace = Pattern.compile(" ");
String poly = whiteSpace.split(s);
double coefficient;
int exponent;
if(poly.length % 2 == 1) {
throw new IllegalArgumentException();
}
first = new Term(); // dummy variable so that checking the first term specially is unnecessary
for(int term = 0; term < poly.length; term += 2) {
if(poly[term].matches("[\-][0-9]||[0-9]||[0-9][\.][0-9]||[\-][0-9][\.][0-9]")) {
coefficient = Double.parseDouble(poly[term]);
if(poly[term + 1].matches("[0-9]")) {
exponent = Integer.parseInt(poly[term++]);
} else {
throw new IllegalArgumentException(); //#
}
numTerms++;
this.addTerm(coefficient, exponent);
}
}
}
public void addTerm(double coef, int exp)
{
if(coef == 0) {
throw new IllegalArgumentException();
}
Term pointer = first;
while(pointer.next != null) {
if(exp == pointer.next.exponent) {
if(coef + pointer.next.coefficient == 0) {
pointer.next = pointer.next.next;
numTerms--;
} else {
pointer.next.coefficient += coef;
break;
}
} else if(pointer.next.exponent < exp) {
Term newTerm = new Term(coef, exp, pointer.next.next);
pointer.next = newTerm;
numTerms++;
break;
}
pointer = pointer.next;
}
}
private class Term {
double coefficient;
int exponent;
Term next;
Term() {
next = null;
}
Term(double coef, int exp, Term nextTerm) {
coefficient = coef;
exponent = exp;
next = nextTerm;
}
}`
java singly-linked-list polynomials
java singly-linked-list polynomials
edited Nov 18 '18 at 6:51
Rosa
asked Nov 18 '18 at 6:44
RosaRosa
32
32
can you add sample input
– swayamraina
Nov 18 '18 at 6:47
you forgot to add the input?
– swayamraina
Nov 18 '18 at 6:58
This project is meant to not have a user interface and only be tested through JUnit testing but here are some examples of some valid statements: Polynomial p = new Polynomial("4 5 3 2 -5 0") and Polynomial p = new Polynomial("4.8 5 -3.5 2 1.5 0"). I hope this helps!
– Rosa
Nov 18 '18 at 6:59
add a comment |
can you add sample input
– swayamraina
Nov 18 '18 at 6:47
you forgot to add the input?
– swayamraina
Nov 18 '18 at 6:58
This project is meant to not have a user interface and only be tested through JUnit testing but here are some examples of some valid statements: Polynomial p = new Polynomial("4 5 3 2 -5 0") and Polynomial p = new Polynomial("4.8 5 -3.5 2 1.5 0"). I hope this helps!
– Rosa
Nov 18 '18 at 6:59
can you add sample input
– swayamraina
Nov 18 '18 at 6:47
can you add sample input
– swayamraina
Nov 18 '18 at 6:47
you forgot to add the input?
– swayamraina
Nov 18 '18 at 6:58
you forgot to add the input?
– swayamraina
Nov 18 '18 at 6:58
This project is meant to not have a user interface and only be tested through JUnit testing but here are some examples of some valid statements: Polynomial p = new Polynomial("4 5 3 2 -5 0") and Polynomial p = new Polynomial("4.8 5 -3.5 2 1.5 0"). I hope this helps!
– Rosa
Nov 18 '18 at 6:59
This project is meant to not have a user interface and only be tested through JUnit testing but here are some examples of some valid statements: Polynomial p = new Polynomial("4 5 3 2 -5 0") and Polynomial p = new Polynomial("4.8 5 -3.5 2 1.5 0"). I hope this helps!
– Rosa
Nov 18 '18 at 6:59
add a comment |
1 Answer
1
active
oldest
votes
You do not need regex
to check for validity ParseXXX()
takes care of that. If parsing fails, an Exception is thrown and then you can simply throw IllegalArgumentException
for (int term = 0; term < poly.length - 1; term += 2) {
try {
coefficient = Double.parseDouble(poly[term]);
exponent = Integer.parseInt(poly[term + 1]);
numTerms++;
this.addTerm(coefficient, exponent);
} catch (NumberFormatException e) {
throw new IllegalArgumentException();
}
}
Moreover, using regex
= "[0-9]" means only a single digit exponent. You might want to change it to "[0-9]+"
Thanks so much! Your advice helped solve the problem I was having with the exception being thrown from format checking. Now I just need to figure out how to get the terms into the list. If you have any advice for that that would be awesome!
– Rosa
Nov 18 '18 at 16:12
If the solution helped. you can upvote or mark the solution as accepted.
– swayamraina
Nov 21 '18 at 11:26
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53358531%2fcustom-linkedlist-implementation-to-store-terms-of-a-polynomial%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You do not need regex
to check for validity ParseXXX()
takes care of that. If parsing fails, an Exception is thrown and then you can simply throw IllegalArgumentException
for (int term = 0; term < poly.length - 1; term += 2) {
try {
coefficient = Double.parseDouble(poly[term]);
exponent = Integer.parseInt(poly[term + 1]);
numTerms++;
this.addTerm(coefficient, exponent);
} catch (NumberFormatException e) {
throw new IllegalArgumentException();
}
}
Moreover, using regex
= "[0-9]" means only a single digit exponent. You might want to change it to "[0-9]+"
Thanks so much! Your advice helped solve the problem I was having with the exception being thrown from format checking. Now I just need to figure out how to get the terms into the list. If you have any advice for that that would be awesome!
– Rosa
Nov 18 '18 at 16:12
If the solution helped. you can upvote or mark the solution as accepted.
– swayamraina
Nov 21 '18 at 11:26
add a comment |
You do not need regex
to check for validity ParseXXX()
takes care of that. If parsing fails, an Exception is thrown and then you can simply throw IllegalArgumentException
for (int term = 0; term < poly.length - 1; term += 2) {
try {
coefficient = Double.parseDouble(poly[term]);
exponent = Integer.parseInt(poly[term + 1]);
numTerms++;
this.addTerm(coefficient, exponent);
} catch (NumberFormatException e) {
throw new IllegalArgumentException();
}
}
Moreover, using regex
= "[0-9]" means only a single digit exponent. You might want to change it to "[0-9]+"
Thanks so much! Your advice helped solve the problem I was having with the exception being thrown from format checking. Now I just need to figure out how to get the terms into the list. If you have any advice for that that would be awesome!
– Rosa
Nov 18 '18 at 16:12
If the solution helped. you can upvote or mark the solution as accepted.
– swayamraina
Nov 21 '18 at 11:26
add a comment |
You do not need regex
to check for validity ParseXXX()
takes care of that. If parsing fails, an Exception is thrown and then you can simply throw IllegalArgumentException
for (int term = 0; term < poly.length - 1; term += 2) {
try {
coefficient = Double.parseDouble(poly[term]);
exponent = Integer.parseInt(poly[term + 1]);
numTerms++;
this.addTerm(coefficient, exponent);
} catch (NumberFormatException e) {
throw new IllegalArgumentException();
}
}
Moreover, using regex
= "[0-9]" means only a single digit exponent. You might want to change it to "[0-9]+"
You do not need regex
to check for validity ParseXXX()
takes care of that. If parsing fails, an Exception is thrown and then you can simply throw IllegalArgumentException
for (int term = 0; term < poly.length - 1; term += 2) {
try {
coefficient = Double.parseDouble(poly[term]);
exponent = Integer.parseInt(poly[term + 1]);
numTerms++;
this.addTerm(coefficient, exponent);
} catch (NumberFormatException e) {
throw new IllegalArgumentException();
}
}
Moreover, using regex
= "[0-9]" means only a single digit exponent. You might want to change it to "[0-9]+"
answered Nov 18 '18 at 7:24
swayamrainaswayamraina
608711
608711
Thanks so much! Your advice helped solve the problem I was having with the exception being thrown from format checking. Now I just need to figure out how to get the terms into the list. If you have any advice for that that would be awesome!
– Rosa
Nov 18 '18 at 16:12
If the solution helped. you can upvote or mark the solution as accepted.
– swayamraina
Nov 21 '18 at 11:26
add a comment |
Thanks so much! Your advice helped solve the problem I was having with the exception being thrown from format checking. Now I just need to figure out how to get the terms into the list. If you have any advice for that that would be awesome!
– Rosa
Nov 18 '18 at 16:12
If the solution helped. you can upvote or mark the solution as accepted.
– swayamraina
Nov 21 '18 at 11:26
Thanks so much! Your advice helped solve the problem I was having with the exception being thrown from format checking. Now I just need to figure out how to get the terms into the list. If you have any advice for that that would be awesome!
– Rosa
Nov 18 '18 at 16:12
Thanks so much! Your advice helped solve the problem I was having with the exception being thrown from format checking. Now I just need to figure out how to get the terms into the list. If you have any advice for that that would be awesome!
– Rosa
Nov 18 '18 at 16:12
If the solution helped. you can upvote or mark the solution as accepted.
– swayamraina
Nov 21 '18 at 11:26
If the solution helped. you can upvote or mark the solution as accepted.
– swayamraina
Nov 21 '18 at 11:26
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53358531%2fcustom-linkedlist-implementation-to-store-terms-of-a-polynomial%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
can you add sample input
– swayamraina
Nov 18 '18 at 6:47
you forgot to add the input?
– swayamraina
Nov 18 '18 at 6:58
This project is meant to not have a user interface and only be tested through JUnit testing but here are some examples of some valid statements: Polynomial p = new Polynomial("4 5 3 2 -5 0") and Polynomial p = new Polynomial("4.8 5 -3.5 2 1.5 0"). I hope this helps!
– Rosa
Nov 18 '18 at 6:59