The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
April 2000

Advertisement

Advertisement

This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.

Message:

Polynomials Program

Posted by Mahen Mudalige on October 01, 2001 at 3:26 PM

Dear sir,
My programs objective was to add two polynomials of max degree 100 and multiply two polynomials but the program i design i could not add my coeffient
to the array i want and i dont know how to initialize the this array ? problem arises in the action methord when i try to add coeeficients..(mind you that i am not a java expert...this may be a simple question but can you please help...i dont know what to do next ?)


/*
* Author: Mahen Mudalige
* Created: August 08, 2001 00:05:43
* Modified: August 08, 2001 00:05:43
*/

import java.io.*;
import java.awt.*;
import java.util.*;


class Poly extends Frame{

public Label eq1;
public String list3s="";
public static TextArea sum;
public static TextArea pro;
public TextField eq11;
public TextField eq22;
public static TextArea eq111;
public static TextArea eq222;
public TextField maxeq1;
public TextField maxeq2;
private Button sumB;
private Button proB;
private StringTokenizer num1;
private TextField list;
/* This methad creates the Graphical User Interface of the Program */

public void makeGui(){
setLayout(new FlowLayout());
Panel top=new Panel();


Label meq1=new Label(" Max Degree of Equation 1: ? ");
add(meq1);

maxeq1=new TextField(15);
add(maxeq1);
Label eq1=new Label("Enter Equation 1: ");
add(eq1);

eq11=new TextField(20);
add(eq11);


Label meq2=new Label(" Max Degree of Equation 2: ? ");
add(meq2);

maxeq2=new TextField(15);
add(maxeq2);
Label eq2=new Label("Enter Equation 2: ");
add(eq2);
eq22=new TextField(20);
add(eq22);

TextArea eq111=new TextArea("Sum :\n ",2,50);
add(eq111);

TextArea eq222=new TextArea("Product :\n ",2,50);
add(eq222);



list=new TextField(60);
list.setEditable(false);
add(list);

sumB=new Button(" SUM ");
add(sumB);
proB=new Button(" PRODUCT ");
add(proB);

}

/* This method acompanies with the GUI interface provides basic actions */

public boolean action(Event evt,Object obj){

if(evt.target==sumB){
String eq111=eq11.getText();
String maxeq111=maxeq1.getText();
int maxeq11=Integer.parseInt(maxeq111);
Polynomial lhs=new Polynomial(maxeq11);
lhs.highPower=maxeq11;
int max1=maxeq11;
StringTokenizer num1=new StringTokenizer(eq111,",");
for(max1=max1;num1.hasMoreTokens();max1--){
String item=num1.nextToken();
int coeff=Integer.parseInt(item);
lhs.coeffArray[max1]=coeff;
max1--;
}
String eq222=eq22.getText();
String maxeq222=maxeq2.getText();
int maxeq22=Integer.parseInt(maxeq222);
Polynomial rhs=new Polynomial(maxeq22);
rhs.highPower=maxeq22;
int max2=maxeq22;
StringTokenizer num2=new StringTokenizer(eq222,",");
while(num1.hasMoreTokens()){
String item1=num2.nextToken();
int coeff1=Integer.parseInt(item1);
lhs.coeffArray[max2]=coeff1;
max2--;
}
Polynomial sum=new Polynomial();
sum=sum.operaterSum(lhs,rhs);
sum.print(sum);
}//End of if statement
if(evt.target==proB){
String eq111=eq11.getText();
Polynomial lhs=new Polynomial();
String maxeq111=maxeq1.getText();
int maxeq11=Integer.parseInt(maxeq111);
lhs.highPower=maxeq11;
int max1=maxeq11;
StringTokenizer num1=new StringTokenizer(eq111,",");
while(num1.hasMoreTokens()){
String item=num1.nextToken();
int coeff=Integer.parseInt(item);
lhs.coeffArray[max1]=coeff;
max1--;
}
String eq222=eq22.getText();
Polynomial rhs=new Polynomial();
String maxeq222=maxeq2.getText();
int maxeq22=Integer.parseInt(maxeq222);
rhs.highPower=maxeq22;
int max2=maxeq22;
StringTokenizer num2=new StringTokenizer(eq222,",");
while(num1.hasMoreTokens()){
String item1=num2.nextToken();
int coeff1=Integer.parseInt(item1);
lhs.coeffArray[max2]=coeff1;
max2--;
}
Polynomial pro=new Polynomial();
pro=pro.operaterPro(lhs,rhs);
pro.print(pro);

}//End of if statement

return true;
}//End of action methord

/* This method Handles the event Window destroy by User */

public boolean handleEvent(Event event){
if(event.id==Event.WINDOW_DESTROY)
System.exit(0);
return super.handleEvent(event);
}

/* Main method */

public static void main(String[] args){

Poly file=new Poly();
file.resize(470,320);
file.makeGui();
file.show();

}//End of the main

}//end of class

class Polynomial {

public int coeffArray[];
public int highPower;
public final static int Max_Degree=100;
public static int max1;
public static int max2;

public Polynomial(int highPower){

int coeffArray[]=new int[highPower];
//zeroPolynomial();
}
public Polynomial(){

int coeffArray[]=new int[highPower];
//zeroPolynomial();
}
public int max(int a,int b){
return a>b?a:b;
}
/*
public void zeroPolynomial(){
for(int i=0;i<=highPower;i++)
coeffArray[i]=0;
highPower=0;
} */

public Polynomial operaterSum(Polynomial lhs,Polynomial rhs){
Polynomial sum=new Polynomial();
sum.highPower=max(lhs.highPower,rhs.highPower);
for(int i=sum.highPower;i>=0;i--)
coeffArray[i]=lhs.coeffArray[i]+rhs.coeffArray[i];
return sum;
}

public Polynomial operaterPro(Polynomial lhs,Polynomial rhs){
Polynomial product=new Polynomial();

product.highPower=lhs.highPower+rhs.highPower;

if(product.highPower>Max_Degree)
System.out.println("Operater * Exceed The Max Degree ");
for(int i=0;i for(int j=0;j<=rhs.highPower;j++)
product.coeffArray[i+j]+=lhs.coeffArray[i]*rhs.coeffArray[j];

return product;
}
public void print(Polynomial q){

for(int i=highPower;i>0;i--){
Poly.eq111.setText(q.coeffArray[i]+"X^"+i+"+");
System.out.print(q.coeffArray[i]+"X^"+i+"+");
}
Poly.eq111.appendText(""+q.coeffArray[0]);
System.out.print(+q.coeffArray[0]);
System.out.println("\n");

Poly.eq111.appendText("Error");
}
}





Replies:

Sponsored Links



Google
  Web Artima.com   
Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us