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. import java.io.*; import java.util.*; public class Variable implements Comparable, Cloneable { public char id; public int exp; public Variable() { } public Variable(char c, int i) { id = c; exp = i; } public int compareTo(Object v) { return id - ((Variable)v).id; } public boolean equals(Object v) { return id == ((Variable)v).id && exp == ((Variable)v).exp; } public Object clone() { return new Variable(id,exp); } } public class Term implements Comparable, Cloneable { public Term() { } public int coeff; public ArrayList vars = new ArrayList(); public Object clone() { Term t = new Term(); t.coeff = coeff; t.vars = (ArrayList) vars.clone(); for (int i = 0; i < vars.size(); i++) t.vars.set(i,((Variable)vars.get(i)).clone()); return t; } public boolean equals(Object term) { int i; for (i = 0; i < Math.min(vars.size(),((Term)term).vars.size()) && vars.get(i).equals(((Term)term).vars.get(i)); i++); return i == vars.size() && vars.size() == ((Term)term).vars.size(); } public int compareTo(Object term2) { if (vars.size() == 0) return 1; // this is just a coefficient; else if (((Term)term2).vars.size() == 0) return -1; // term2 is just a coefficient; Variable var1, var2; for (int i = 0; i < Math.min(vars.size(),((Term)term2).vars.size()); i++) { var1 = (Variable)vars.get(i); var2 = (Variable)((Term)term2).vars.get(i); if (var1.id < var2.id) return -1; // this precedes term2; else if (var2.id < var1.id) return 1; // term2 precedes this; else if (var1.exp < var2.exp) return -1; // this precedes term2; else if (var2.exp < var1.exp) return 1; // term2 precedes this; } return vars.size() - ((Term)term2).vars.size(); } }
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.
import java.io.*;
import java.util.*;
public class Variable implements Comparable, Cloneable {
public char id;
public int exp;
public Variable() {
}
public Variable(char c, int i) {
id = c; exp = i;
}
public int compareTo(Object v) {
return id - ((Variable)v).id;
}
public boolean equals(Object v) {
return id == ((Variable)v).id && exp == ((Variable)v).exp;
}
public Object clone() {
return new Variable(id,exp);
}
}
public class Term implements Comparable, Cloneable
{
public Term() {
}
public int coeff;
public ArrayList vars = new ArrayList();
public Object clone() {
Term t = new Term();
t.coeff = coeff;
t.vars = (ArrayList) vars.clone();
for (int i = 0; i < vars.size(); i++)
t.vars.set(i,((Variable)vars.get(i)).clone());
return t;
}
public boolean equals(Object term) {
int i;
for (i = 0; i < Math.min(vars.size(),((Term)term).vars.size()) &&
vars.get(i).equals(((Term)term).vars.get(i)); i++);
return i == vars.size() && vars.size() == ((Term)term).vars.size();
}
public int compareTo(Object term2) {
if (vars.size() == 0)
return 1; // this is just a coefficient;
else if (((Term)term2).vars.size() == 0)
return -1; // term2 is just a coefficient;
Variable var1, var2;
for (int i = 0; i < Math.min(vars.size(),((Term)term2).vars.size()); i++) {
var1 = (Variable)vars.get(i);
var2 = (Variable)((Term)term2).vars.get(i);
if (var1.id < var2.id)
return -1; // this precedes term2;
else if (var2.id < var1.id)
return 1; // term2 precedes this;
else if (var1.exp < var2.exp)
return -1; // this precedes term2;
else if (var2.exp < var1.exp)
return 1; // term2 precedes this;
}
return vars.size() - ((Term)term2).vars.size();
}
}
Step by step
Solved in 3 steps with 7 images