Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package question2;
- import java.util.Scanner;
- public class PolynomialTester {
- public static void main(String[] args) {
- Scanner keyb= new Scanner(System.in);
- System.out.print("Enter degree of polynomial: ");
- int degree=keyb.nextInt();
- if(degree<=0) {
- System.out.print("Invalid degree, re-enter: ");
- degree=keyb.nextInt();
- }
- Polynomial poly1=new Polynomial(degree);
- double input;
- for(int i=0; i<=degree; i++) {
- System.out.print("Enter Coeff for degree "+i+": ");
- input=keyb.nextDouble();
- poly1.setCoefficient(i, input);
- }
- System.out.print("Polynomial: "+poly1.toString());
- boolean exit=false;
- String userInput="";
- double x=0;
- do {
- System.out.println();
- System.out.print("Enter a value of x for which to evaluate the polynomial: ");
- userInput=keyb.next();
- try {
- x=Double.parseDouble(userInput);
- System.out.print("For x ="+x+", polynomial = "+poly1.evaluate(x));
- }catch (NumberFormatException e){
- if (userInput.compareTo("quit") == 0) {
- exit = true;
- }
- else {
- exit = false;
- }
- }
- }while(!exit);
- System.out.print("Created copy of polynomial");
- Polynomial poly2=new Polynomial(poly1);
- System.out.println();
- System.out.print("Polynomials are equal: "+poly1.equals(poly2));
- System.out.println();
- System.out.println("To make them unequal enter a index and coefficent to change:");
- int index=keyb.nextInt();
- input=keyb.nextDouble();
- poly2.coefficients[index]=input;
- System.out.println(poly2.coefficients[index]);
- System.out.print("Polynomials are equal: "+poly1.equals(poly2));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement