Advertisement
Guest User

Untitled

a guest
Jan 26th, 2020
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.73 KB | None | 0 0
  1. package question2;
  2. import java.util.Scanner;
  3.  
  4. public class PolynomialTester {
  5.  
  6.     public static void main(String[] args) {
  7.        
  8.         Scanner keyb= new Scanner(System.in);
  9.        
  10.         System.out.print("Enter degree of polynomial: ");
  11.         int degree=keyb.nextInt();
  12.         if(degree<=0) {
  13.             System.out.print("Invalid degree, re-enter: ");
  14.             degree=keyb.nextInt();
  15.         }
  16.        
  17.         Polynomial poly1=new Polynomial(degree);
  18.         double input;
  19.        
  20.         for(int i=0; i<=degree; i++) {
  21.             System.out.print("Enter Coeff for degree "+i+": ");
  22.             input=keyb.nextDouble();
  23.             poly1.setCoefficient(i, input);
  24.         }  
  25.        
  26.         System.out.print("Polynomial: "+poly1.toString());
  27.         boolean exit=false;
  28.         String userInput="";
  29.         double x=0;
  30.        
  31.         do {
  32.             System.out.println();
  33.             System.out.print("Enter a value of x for which to evaluate the polynomial: ");
  34.             userInput=keyb.next();
  35.             try {
  36.                 x=Double.parseDouble(userInput);
  37.                 System.out.print("For x ="+x+", polynomial = "+poly1.evaluate(x));
  38.             }catch (NumberFormatException e){
  39.                 if (userInput.compareTo("quit") == 0) {
  40.                     exit = true;
  41.                 }
  42.                 else {
  43.                     exit = false;
  44.                 }
  45.             }
  46.         }while(!exit);
  47.        
  48.         System.out.print("Created copy of polynomial");
  49.        
  50.         Polynomial poly2=new Polynomial(poly1);
  51.    
  52.        
  53.        
  54.         System.out.println();
  55.         System.out.print("Polynomials are equal: "+poly1.equals(poly2));
  56.         System.out.println();
  57.         System.out.println("To make them unequal enter a index and coefficent to change:");
  58.         int index=keyb.nextInt();
  59.         input=keyb.nextDouble();
  60.         poly2.coefficients[index]=input;
  61.         System.out.println(poly2.coefficients[index]);
  62.         System.out.print("Polynomials are equal: "+poly1.equals(poly2));
  63.     }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement