These two codes are part of the case study of the polynomials 1- explain in detail how to extract the information from the case study and how the code is written. 2- extract the variable. 3- extract the methods from the case study and write the related code. public class Polynomial { private LinkedList terms = new LinkedList(); public Polynomial() { } private void error(String s) { System.out.println(s); Runtime.getRuntime().exit(-1); } public Polynomial add(Polynomial polyn2) { ListIterator p1, p2; Polynomial result = new Polynomial(); int i; for (p1 = terms.listIterator(); p1.hasNext(); ) // create new polynomial result.terms.add(((Term)p1.next()).clone());// out of copies for (p2 = polyn2.terms.listIterator(); p2.hasNext(); ) // of this result.terms.add(((Term)p2.next()).clone());// polynomial and polyn2; for (i = 0, p1 = result.terms.listIterator(); p1.hasNext(); i++, p1 = result.terms.listIterator(i)) { Term term1 = (Term) p1.next(); for (p2 = p1; p2.hasNext(); ) { Term term2 = (Term) p2.next(); if (term1.equals(term2)) { term2.coeff += term1.coeff; result.terms.remove(term1); if (term2.coeff == 0) // remove terms with zero result.terms.remove(term2); // coefficients; i = -1; // to become i = 0 after autoincrement; break; } } } Collections.sort(result.terms); return result; } public void get(InputStream fIn) { int ch = ' ', i, sign, exp; boolean coeffUsed; char id; Term term = new Term(); try { while (ch > -1) { coeffUsed = false; while (true) if (ch > -1 && Character.isWhitespace((char)ch)) // skip ch = fIn.read(); // blanks; else break; if (!Character.isLetterOrDigit((char)ch) && ch != ';' && ch != '-' && ch != '+') error("Wrong character entered2"); if (ch == -1) break; sign = 1; while (ch == '-' || ch == '+') { // first get sign(s) of Term if (ch == '-') sign *= -1; ch = fIn.read(); while (Character.isWhitespace((char)ch)) ch = fIn.read(); } if (Character.isDigit((char)ch)) { // and then its coefficient; String number = ""; while (Character.isDigit((char)ch)) { number += (char) ch; ch = fIn.read(); } while (Character.isWhitespace((char)ch)) ch = fIn.read(); term.coeff = sign * Integer.valueOf(number).intValue(); coeffUsed = true; } else term.coeff = sign; for (i = 0; Character.isLetterOrDigit((char)ch); i++) { id = (char) ch; // process this term: ch = fIn.read(); // get a variable name if (Character.isDigit((char)ch)) { // and an exponent String number = ""; // (if any); while (Character.isDigit((char)ch)) { number += (char) ch; ch = fIn.read(); } exp = Integer.valueOf(number).intValue(); while (Character.isWhitespace((char)ch)) ch = fIn.read(); } else exp = 1; term.vars.add(new Variable(id,exp)); } terms.add(term.clone()); term.vars = new ArrayList(); while (Character.isWhitespace((char)ch)) ch = fIn.read(); if (ch == ';') // finish if a semicolon is entered; if (coeffUsed || i > 0) break; else error("Term is missing"); // e.g., 2x - ; or just ';' else if (ch != '-' && ch != '+') // e.g., 2x 4y; error("wrong character entered"); } } catch (IOException io) { } for (Iterator p = terms.iterator(); p.hasNext(); ) { term = (Term) p.next(); // order alphabetically variables if (term.vars.size() > 1) // in each term separately; Collections.sort(term.vars); } } public void display() { boolean afterFirstTerm = false; for (Iterator it = terms.iterator(); it.hasNext(); ) { Term term = (Term) it.next(); System.out.print(" "); if (term.coeff < 0) // put '-' before polynomial System.out.print("-"); // and between terms (if needed); else if (afterFirstTerm) // don't put '+' in front of System.out.print("+"); // polynomial; afterFirstTerm = true; System.out.print(" "); // print a coefficient if if (term.vars.size() == 0 || // the term has only Math.abs(term.coeff) != 1) // a coefficient or coefficient System.out.print(Math.abs(term.coeff)); // is not 1 or -1; for (int i = 1; i <= term.vars.size(); i++) { Variable var = (Variable) term.vars.get(i-1); System.out.print(var.id); // print a variable name if (var.exp != 1) // and an exponent, only System.out.print(var.exp); // if it is not 1. } } System.out.println(); } } public class AddPolyn { static public void main(String[] a) { Polynomial polyn1 = new Polynomial(), polyn2 = new Polynomial(); System.out.println("Enter two polynomials, each ended with a semicolon:"); polyn1.get(System.in); polyn2.get(System.in); System.out.println("The result is:"); polyn1.add(polyn2).display(); } }

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question
100%

These two codes are part of the case study of the polynomials

1- explain in detail how to extract the information from the case study and how the code is written.

2- extract the variable.

3- extract the methods from the case study and write the related code.

public class Polynomial {
private LinkedList terms = new LinkedList();
public Polynomial() {
}
private void error(String s) {
System.out.println(s);
Runtime.getRuntime().exit(-1);
}
public Polynomial add(Polynomial polyn2) {
ListIterator p1, p2;
Polynomial result = new Polynomial();
int i;
for (p1 = terms.listIterator(); p1.hasNext(); ) // create new polynomial
result.terms.add(((Term)p1.next()).clone());// out of copies
for (p2 = polyn2.terms.listIterator(); p2.hasNext(); ) // of this
result.terms.add(((Term)p2.next()).clone());// polynomial and polyn2;
for (i = 0, p1 = result.terms.listIterator(); p1.hasNext();
i++, p1 = result.terms.listIterator(i)) {
Term term1 = (Term) p1.next();
for (p2 = p1; p2.hasNext(); ) {
Term term2 = (Term) p2.next();
if (term1.equals(term2)) {
term2.coeff += term1.coeff;
result.terms.remove(term1);
if (term2.coeff == 0) // remove terms with zero
result.terms.remove(term2); // coefficients;
i = -1; // to become i = 0 after autoincrement;
break;
}
}
}
Collections.sort(result.terms);
return result;
}
public void get(InputStream fIn) {
int ch = ' ', i, sign, exp;
boolean coeffUsed;
char id;
Term term = new Term();
try {
while (ch > -1) {
coeffUsed = false;
while (true)
if (ch > -1 && Character.isWhitespace((char)ch)) // skip
ch = fIn.read(); // blanks;
else break;
if (!Character.isLetterOrDigit((char)ch) &&
ch != ';' && ch != '-' && ch != '+')
error("Wrong character entered2");
if (ch == -1)
break;
sign = 1;
while (ch == '-' || ch == '+') { // first get sign(s) of Term
if (ch == '-')
sign *= -1;
ch = fIn.read();
while (Character.isWhitespace((char)ch))
ch = fIn.read();
}
if (Character.isDigit((char)ch)) { // and then its coefficient;
String number = "";
while (Character.isDigit((char)ch)) {
number += (char) ch;
ch = fIn.read();
}
while (Character.isWhitespace((char)ch))
ch = fIn.read();
term.coeff = sign * Integer.valueOf(number).intValue();
coeffUsed = true;
}
else term.coeff = sign;
for (i = 0; Character.isLetterOrDigit((char)ch); i++) {
id = (char) ch; // process this term:
ch = fIn.read(); // get a variable name
if (Character.isDigit((char)ch)) { // and an exponent
String number = ""; // (if any);
while (Character.isDigit((char)ch)) {
number += (char) ch;
ch = fIn.read();
}
exp = Integer.valueOf(number).intValue();
while (Character.isWhitespace((char)ch))
ch = fIn.read();
}
else exp = 1;
term.vars.add(new Variable(id,exp));
}
terms.add(term.clone());
term.vars = new ArrayList();
while (Character.isWhitespace((char)ch))
ch = fIn.read();
if (ch == ';') // finish if a semicolon is entered;
if (coeffUsed || i > 0)
break;
else error("Term is missing"); // e.g., 2x - ; or just ';'
else if (ch != '-' && ch != '+') // e.g., 2x 4y;
error("wrong character entered");
}
} catch (IOException io) {
}
for (Iterator p = terms.iterator(); p.hasNext(); ) {
term = (Term) p.next(); // order alphabetically variables
if (term.vars.size() > 1) // in each term separately;
Collections.sort(term.vars);
}
}
public void display() {
boolean afterFirstTerm = false;
for (Iterator it = terms.iterator(); it.hasNext(); ) {
Term term = (Term) it.next();
System.out.print(" ");
if (term.coeff < 0) // put '-' before polynomial
System.out.print("-"); // and between terms (if needed);
else if (afterFirstTerm) // don't put '+' in front of
System.out.print("+"); // polynomial;
afterFirstTerm = true;
System.out.print(" "); // print a coefficient if
if (term.vars.size() == 0 || // the term has only
Math.abs(term.coeff) != 1) // a coefficient or coefficient
System.out.print(Math.abs(term.coeff)); // is not 1 or -1;
for (int i = 1; i <= term.vars.size(); i++) {

Variable var = (Variable) term.vars.get(i-1);
System.out.print(var.id); // print a variable name
if (var.exp != 1) // and an exponent, only
System.out.print(var.exp); // if it is not 1.
}
}
System.out.println();
}

}

public class AddPolyn {
static public void main(String[] a) {
Polynomial polyn1 = new Polynomial(), polyn2 = new Polynomial();
System.out.println("Enter two polynomials, each ended with a semicolon:");
polyn1.get(System.in);
polyn2.get(System.in);
System.out.println("The result is:");
polyn1.add(polyn2).display();
}

}

Expert Solution
steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
Threads in linked list
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education