Advertisement
Shamel

Untitled

Feb 6th, 2020
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.45 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3.  
  4. public class exe{
  5.     public static void main(String args[]){
  6.         Scanner in= new Scanner(System.in);
  7.  
  8.         System.out.println("#####   ENTER-POLY METHOD   #####");
  9.         System.out.println();
  10.         System.out.print("Enter degree of the polynomial: ");
  11.         int degree= in.nextInt();
  12.         PolyNode p=null;
  13.         PolyNode t=null;
  14.         for(int k=degree;k>= 0;k--)
  15.         {
  16.             System.out.print("Enter coefficient for X^"+ k+" If no term exists, enter 0 ===>>");
  17.             int i=in.nextInt();
  18.             if(i==0)
  19.                 ;
  20.             else{
  21.  
  22.                 p=new PolyNode(i,k,t);
  23.                 t=p;
  24.             }
  25.  
  26.         }
  27.  
  28.         System.out.println();System.out.println();
  29.         System.out.println("##### ENTER-XVALUE METHOD #####");
  30.         System.out.println();
  31.         System.out.print("Enter the X value of the polynomial: ");
  32.         int x=in.nextInt();
  33.         System.out.println();
  34.         System.out.println();
  35.  
  36.         System.out.println("##### DISPLAY POLY METHOD #####");
  37.         System.out.println();
  38.         System.out.print("Y= ");
  39.         PolyNode front=p;
  40.         while (p!=null) {
  41.             if(p.getDegree()==0) {
  42.                 System.out.print(p.getCoeff());
  43.             }
  44.             else if(p.getDegree()==1){
  45.                 if(p.getCoeff()==1)
  46.                     System.out.print( "X");
  47.                 else
  48.                     System.out.print(p.getCoeff() + "X");
  49.             }
  50.             else{
  51.                 if(p.getCoeff()==1)
  52.                     System.out.print( "X^"+ p.getDegree());
  53.                 else
  54.                 System.out.print(p.getCoeff() + "X^"+ p.getDegree());
  55.  
  56.             }
  57.             if(p.getNext()!=null)
  58.                 System.out.print(" + ");
  59.             p=p.getNext();
  60.         }
  61.         System.out.println();
  62.         System.out.println("##### COMPUTE POLY METHOD #####");
  63.         System.out.println();
  64.         System.out.println("##### DISPLAY VALUE METHOD #####");
  65.         System.out.println();
  66.         System.out.print("Y("+ x+")= ");
  67.         int sum=0;
  68.         p=front;
  69.         while (p!=null){
  70.             sum+=Math.pow(x,p.getDegree()) * p.getCoeff();
  71.             p=p.getNext();
  72.         }
  73.         System.out.print(sum);
  74.  
  75.  
  76.  
  77.  
  78.  
  79.  
  80.     }
  81. }
  82.  
  83. class PolyNode
  84. {
  85.     private int coeff;      // coefficient of each term
  86.     private int degree;     // degree of each term
  87.     private PolyNode next;  // link to the next term node
  88.  
  89.  
  90.     public PolyNode (int c, int d, PolyNode initNext)
  91.     {
  92.         coeff = c;
  93.         degree = d;
  94.         next = initNext;
  95.     }
  96.  
  97.     public int getCoeff()
  98.     {
  99.         return coeff;
  100.     }
  101.  
  102.     public int getDegree()
  103.     {
  104.         return degree;
  105.     }
  106.  
  107.     public PolyNode getNext()
  108.     {
  109.         return next;
  110.     }
  111.  
  112.     public void setCoeff (int newCoeff)
  113.     {
  114.         coeff = newCoeff;
  115.     }
  116.  
  117.     public void setDegree (int newDegree)
  118.     {
  119.         degree = newDegree;
  120.     }
  121.  
  122.     public void setNext (PolyNode newNext)
  123.     {
  124.         next = newNext;
  125.     }
  126.  
  127.  
  128.  
  129.  
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement