Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class exe{
- public static void main(String args[]){
- Scanner in= new Scanner(System.in);
- System.out.println("##### ENTER-POLY METHOD #####");
- System.out.println();
- System.out.print("Enter degree of the polynomial: ");
- int degree= in.nextInt();
- PolyNode p=null;
- PolyNode t=null;
- for(int k=degree;k>= 0;k--)
- {
- System.out.print("Enter coefficient for X^"+ k+" If no term exists, enter 0 ===>>");
- int i=in.nextInt();
- if(i==0)
- ;
- else{
- p=new PolyNode(i,k,t);
- t=p;
- }
- }
- System.out.println();System.out.println();
- System.out.println("##### ENTER-XVALUE METHOD #####");
- System.out.println();
- System.out.print("Enter the X value of the polynomial: ");
- int x=in.nextInt();
- System.out.println();
- System.out.println();
- System.out.println("##### DISPLAY POLY METHOD #####");
- System.out.println();
- System.out.print("Y= ");
- PolyNode front=p;
- while (p!=null) {
- if(p.getDegree()==0) {
- System.out.print(p.getCoeff());
- }
- else if(p.getDegree()==1){
- if(p.getCoeff()==1)
- System.out.print( "X");
- else
- System.out.print(p.getCoeff() + "X");
- }
- else{
- if(p.getCoeff()==1)
- System.out.print( "X^"+ p.getDegree());
- else
- System.out.print(p.getCoeff() + "X^"+ p.getDegree());
- }
- if(p.getNext()!=null)
- System.out.print(" + ");
- p=p.getNext();
- }
- System.out.println();
- System.out.println("##### COMPUTE POLY METHOD #####");
- System.out.println();
- System.out.println("##### DISPLAY VALUE METHOD #####");
- System.out.println();
- System.out.print("Y("+ x+")= ");
- int sum=0;
- p=front;
- while (p!=null){
- sum+=Math.pow(x,p.getDegree()) * p.getCoeff();
- p=p.getNext();
- }
- System.out.print(sum);
- }
- }
- class PolyNode
- {
- private int coeff; // coefficient of each term
- private int degree; // degree of each term
- private PolyNode next; // link to the next term node
- public PolyNode (int c, int d, PolyNode initNext)
- {
- coeff = c;
- degree = d;
- next = initNext;
- }
- public int getCoeff()
- {
- return coeff;
- }
- public int getDegree()
- {
- return degree;
- }
- public PolyNode getNext()
- {
- return next;
- }
- public void setCoeff (int newCoeff)
- {
- coeff = newCoeff;
- }
- public void setDegree (int newDegree)
- {
- degree = newDegree;
- }
- public void setNext (PolyNode newNext)
- {
- next = newNext;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement