# Thread: Polynomial Arithmetic Java

1. Member
Join Date
Mar 2010
Posts
8
Rep Power
0

## Polynomial Arithmetic Java

Incomplete Main class.
-----------------------------------
import java.util.*;
public class ProgramTester {
public static void main(String[]args){
Polynomial poly1, poly2;

poly1 = enterPolynomial("Enter Polynomial 1: ");
poly2 = enterPolynomial("\nEnter Polynomial 2: ");
Polynomial input;
Scanner kbd = new Scanner(System.in);
int[] grades = null;
do {
showMenu();
int choice = input("Enter choice: ", 1, 7);
switch(choice){
case 1:
poly1 = enterPolynomial("Enter Polynomial 1: ");
break;
case 2:
poly2 = enterPolynomial("\nEnter Polynomial 2: ");
break;
case 3:
add();
break;
case 4:

break;
case 5:

break;
case 6:

break;
case 7:
System.out.println("Thank you and good day...");
System.exit(1);
}
System.out.print("\nPress a key to continue...");
kbd.nextLine();
} while (true);
}

private static void showMenu(){
System.out.println("\n+----------------------------------+");
System.out.println("| M E N U |");
System.out.println("+----------------------------------+");
System.out.println("| (1) Enter Polynomial 1 |");
System.out.println("| (2) Enter Polynomial 2 |");
System.out.println("| (3) Add the Polynomials |");
System.out.println("| (4) Subtract Polynomial 2 from 1 |");
System.out.println("| (5) Multiply the Polynomials |");
System.out.println("| (6) Evaluate a Polynomial |");
System.out.println("| (7) Quit |");
System.out.println("+----------------------------------+\n");
}

public static int input(String a, int b, int c){
Scanner kbd = new Scanner(System.in);
int choice;
do{
System.out.print(a);
choice = kbd.nextInt();
}while((choice < 1) && (choice > 7));
return choice;
}

public static Polynomial enterPolynomial(String entPoly){
Scanner kb = new Scanner(System.in);
System.out.print(entPoly + "\n\tEnter Polynomial Variable(a-z): ");
char variable = kb.nextLine().charAt(0);
System.out.print("\tEnter number of terms of Polynomial: ");
String[] temp = kb.nextLine().split(" ");

int[] coefsDegs = new int[temp.length];
for(int i = 0; i<coefsDegs.length; i++){
coefsDegs[i] = Integer.parseInt(temp[i]);
}
Polynomial poly = new Polynomial(variable, coefsDegs);
return poly;
}
}

----------------------------------------------------------------
import java.util.*;

class Term {
private int coefficient;
private int degree;

public Term(int coefficient, int degree) {
this.coefficient = coefficient;
this.degree = degree;
}

public int getCoefficient() {
return coefficient;
}

public int getDegree() {
return degree;
}
}

public class Polynomial {
private char polyVar;
private ArrayList<Term> terms;

public Polynomial(char polyVar, int ... coefsAndDegs) {
// accept only 'a' through 'z' as the polynomial variable..
if (polyVar < 'a' || polyVar > 'z') {
throw new IllegalArgumentException("Invalid polynomial variable.");
} else {
this.polyVar = polyVar;
}

// check that coefsAndDegs are provided in pairs..
if (coefsAndDegs.length % 2 != 0) {
throw new IllegalArgumentException("Coefficients and Degrees must be provided in pairs.");
} else {
terms = new ArrayList<Term>();

for (int i = 0; i < coefsAndDegs.length / 2; i++) {
Term term = new Term(coefsAndDegs[i * 2], coefsAndDegs[i * 2 + 1]);
terms.add(term);
}
}
}

public static void add(Polynomial poly1, Polynomial poly2){
ArrayList<Term> a = poly1.terms;
ArrayList<Term> b = poly2.terms;
ArrayList<Term> c = new ArrayList<Term>();
ArrayList<Integer> aCoeff = new ArrayList<Integer>();
ArrayList<Integer> aExpo = new ArrayList<Integer>();
ArrayList<Integer> bCoeff = new ArrayList<Integer>();
ArrayList<Integer> bExpo = new ArrayList<Integer>();

for (int i = 0; i<a.size();i+=2){
Integer tempA = new Integer(a.indexOf(i));
aCoeff.add(tempA);
}
for (int i = 0; i<b.size();i+=2){
Integer tempB = new Integer(b.indexOf(i));
bCoeff.add(tempB);
}
for (int i = 1; i<a.size();i+=2){
Integer tempA = new Integer(a.indexOf(i));
aExpo.add(tempA);
}
for (int i = 1; i<b.size();i+=2){
Integer tempB = new Integer(b.indexOf(i));
bExpo.add(tempB);
}

if(a.size() > b.size()){
for (int i = 0; i<a.size(); i++){
if(aExpo.indexOf(i)==bExpo.indexOf(i)){
int d = (aCoeff.indexOf(i) + bCoeff.indexOf(i));
Term temp = new Term(d, aExpo.indexOf(i));
c.add(temp);
}else{
Term temp = new Term(aCoeff.indexOf(i),aExpo.indexOf(i));
c.add(temp);
}
}
}else if(a.size() < b.size()){
for (int i = 0; i<b.size(); i++){
if(bExpo.indexOf(i)==aExpo.indexOf(i)){
int d = (bCoeff.indexOf(i) + aCoeff.indexOf(i));
Term temp = new Term(d, bExpo.indexOf(i));
c.add(temp);
}else{
Term temp = new Term(bCoeff.indexOf(i),bExpo.indexOf(i));
c.add(temp);
}
}
}else{
for (int i = 0; i<a.size(); i++){
if(aExpo.indexOf(i)==bExpo.indexOf(i)){
int d = (aCoeff.indexOf(i) + bCoeff.indexOf(i));
Term temp = new Term(d, aExpo.indexOf(i));
c.add(temp);
}else{
Term temp = new Term(aCoeff.indexOf(i), aExpo.indexOf(i));
c.add(temp);
}
}
}
}
}

Can someone make a method to add this polynomials?
I am having hard time doing it. so i better remove the add method then i will ask to experts here.

-thanks

2. Originally Posted by thisisIT
Can someone make a method to add this polynomials?
I am having hard time doing it. so i better remove the add method then i will ask to experts here.
Why are you simply ignoring my suggestion of using a Map<Integer, Integer> for your polynomials in your other (identical) thread? Better stick to one thread for one problem.

kind regards,

Jos

3. Member
Join Date
Mar 2010
Posts
8
Rep Power
0
sir sorry for that. but i really cant understand your suggestion.(im just new in java)
regarding posting another thread, because i didn't post all of my codes at the first post. so maybe there will be a conflict . but i really appreciated your suggestion the only problem is i dont know to initialize it.

-

4. Originally Posted by thisisIT
sir sorry for that. but i really cant understand your suggestion.(im just new in java)
regarding posting another thread, because i didn't post all of my codes at the first post. so maybe there will be a conflict . but i really appreciated your suggestion the only problem is i dont know to initialize it.

-
Why don't you download and read the documentation for the Map interface and its implementing classes?

kind regards,

Jos

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•