Question

Its a report . Implement and test a preliminary version of the polynomial class, using a...

Its a report . Implement and test a preliminary version of the polynomial class, using a small array to store the coefficients of the polynomial in order of increasing exponents, following the design pattern of object-oriented approach. Suppose the range of the exponents is in [0, 30). • Use an array as a private member variable. • insert(), adding a new term on specific exponent. • remove(), deleting a term. • Have a add() method, deciding the parameters and the return value by yourself. • Have a sub() method, deciding the parameters and the return value by yourself. • A printout() method to display the polynomial expression. • And other variables, or methods if needed. • Do the algorithm analysis on the implemented method, and give the big O notation estimation. You will also need to write a Polynomial Test program to initialize two linear polynomial objects and apply the operation on it and also display the result. For example, P1=x + 3x5 - 5x8; P2= 2x3 - 4x5+ 2x7; So P3= P1+P2= x +2x3 - x5+ 2x7 - 5x8, P4 = P1-P2 = x - 2x3 + 7x5- 2x7 - 5x8. In this project, only one variable is considered, we don’t have the polynomial such as x+3x5 3y3 .

Problem Statement Specifying the problem requirements forces you to state the problem clearly and unambiguously to gain a precise understanding of what is required for its solution. Your objective is to eliminate unimportant aspects and zero in on the root problem.

Analysis Analyzing the problem involves identifying the problem inputs (the data you have to work with), outputs (the desired results), and any additional requirements for or constraints on the solution. At this stage, you should also determine the format in which the results should be displayed and develop a list of problem variables and their relationships. These relationships may be expressed as formulas. Design & Class Prototype Designing the algorithm to solve the problem requires you to develop a list of steps (an algorithm) to solve the problem and then verify that the algorithm solves the problem as intended.

Design information, in particular, can frequently be conveyed much more effectively with drawings or charts than with words alone. Class prototype is the contract of a class, as an outline class declaration showing only public fields and the heading of the constructor and methods. Display the class prototype and also show the relationship between different classes.

Testing and verifying the program requires some testing cases to verify the completed program working as desired. Don’t rely on just one test case; run the program several times using different sets of data, making sure that it works correctly for every situation provided for in the algorithm.

Conclusion If it is an unsuccessful one, then what is the reason why you failed?

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Answer:-

The below is the required source code for the given problem in JAVA

Code:-

import java.util.Scanner;


public class PolyNomialTest {
   public static void main(String[] args) throws Exception{

       Scanner sc = new Scanner(System.in);
       System.out.println("Enter polynomial 1");
       Polynomial pn1 = PolyNomialTest.readPn(sc);
       System.out.println("Enter polynomial 2");
       Polynomial pn2 = PolyNomialTest.readPn(sc);
      
       pn1.print();
       pn2.print();
       Polynomial pn3 = pn1.add(pn2);
       System.out.println(" Addition ");
       pn3.print();
      
       Polynomial pn4 = pn1.subtract(pn2);
       System.out.println(" Subtraction ");
       pn4.print();
   }
  
   public static Polynomial readPn(Scanner sc) {
       Polynomial pn = new Polynomial();
       int[] expCoeff = new int[Polynomial.MAX_EXPONENT];
       System.out.println("Enter -1 for exponent power to quit \n Enter polynomial terms: ");
       while(true) {
           System.out.println("Enter exponent power : ");
           int exp = sc.nextInt();
           if(exp < 0)
               break;
           System.out.println("Enter Coefficient of exponent : ");
           int coeff = sc.nextInt();
           pn.insert(exp, coeff);
          
       }
       return pn;
   }
}


class Polynomial {
   private int[] expCoeff; //in increasing order o = x^0, 1 = x^1 etc
  
   public static int MAX_EXPONENT = 30;
  
   public Polynomial() {
       expCoeff = new int[MAX_EXPONENT];
       for(int i = 0; i <MAX_EXPONENT; i++ )
           expCoeff[i] = 0;
   }


   public void insert(int term, int coeff) {
       if(term < MAX_EXPONENT)
       expCoeff[term] = coeff;
   }
  
   public void remove(int term) {
       expCoeff[term] = 0;
   }
  
   public Polynomial add(Polynomial p2) {
       Polynomial p3 = new Polynomial();
       int[] coeff = new int[MAX_EXPONENT];
       for(int i = 0; i <MAX_EXPONENT; i++ ) {
           coeff[i]= this.getExpCoeff(i)+ p2.getExpCoeff(i);
       }
      
       p3.setExpCoeff(coeff);
       return p3;
   }
  
   public Polynomial subtract(Polynomial p2) {
       Polynomial p3 = new Polynomial();
       int[] coeff = new int[MAX_EXPONENT];
       for(int i = 0; i <MAX_EXPONENT; i++ ) {
           coeff[i]= this.getExpCoeff(i) - p2.getExpCoeff(i);
          
       }
       p3.setExpCoeff(coeff);
       return p3;
   }

  
   public void print() {
       String pn = "";
       for(int i = MAX_EXPONENT-1; i >=0; i-- ) {
           pn += pn.equals("") ? "" : "+";
           if(expCoeff[i] != 0) {
               pn += expCoeff[i];
               if(i != 0)
                   pn += " * x^"+i;
              
           }
       }
       System.out.println(pn);
   }
  
   public int getExpCoeff(int term) {
       return expCoeff[term];
   }
  
   public int[] getExpCoeff() {
       return expCoeff;
   }



   public void setExpCoeff(int[] expCoeff) {
       this.expCoeff = expCoeff;
   }




  
}

If you find any difficulty with the code, please let know know I will try for any modification in the code. Hope this answer will helps you. If you have even any small doubt, please let me know by comments. I am there to help you. Please give Thumbs Up,Thank You!! All the best

Add a comment
Know the answer?
Add Answer to:
Its a report . Implement and test a preliminary version of the polynomial class, using a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT