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
  • Create a class to represent a term in an algebraic expression. As defined here, a term...

    Create a class to represent a term in an algebraic expression. As defined here, a term consists of an integer coefficient and a nonnegative integer exponent. E.g. in the term 4x2, the coefficient is 4 and the exponent 2 in -6x8, the coefficient is -6 and the exponent 8 Your class will have a constructor that creates a Term object with a coefficient and exponent passed as parameters, and accessor methods that return the coefficient and the exponent. Your class...

  • For this lab, you must define a class called Polynomial. This class definition must include the f...

    Please answer this in python 3, thank you. For this lab, you must define a class called Polynomial. This class definition must include the following methods: ._init_0- the initialiser for the class ._str_0- returns a formatted string representation of a Polynomial object add_term) - adds a new term (coefficient and exponent) to the Polynomial .addo-modifies the existing Polynomial by adding another one to it ._add_0-returns a new Polynomial object that is the sum of two polynomials scale) - scales a...

  • For this lab, you must define a class called Polynomial. This class definition must include the f...

    please answer this question in python 3 For this lab, you must define a class called Polynomial. This class definition must include the following methods: ._init_0- the initialiser for the class ._str_0- returns a formatted string representation of a Polynomial object add_term) - adds a new term (coefficient and exponent) to the Polynomial .addo-modifies the existing Polynomial by adding another one to it ._add_0-returns a new Polynomial object that is the sum of two polynomials scale) - scales a Polynomial...

  • A polynomial p(x) is an expression in variable x which is in the form axn + bxn-1 + …. + jx + k, where a, b, …, j, k are...

    A polynomial p(x) is an expression in variable x which is in the form axn + bxn-1 + …. + jx + k, where a, b, …, j, k are real numbers, and n is a non-negative integer. n is called the degree of polynomial. Every term in a polynomial consists of a coefficient and an exponent. For example, for the first term axn, a is the coefficient and n is the exponent. This assignment is about representing and computing...

  • Description Create a polynomial class with linked lists and implement some basic functions. Specifications Polynomials are...

    Description Create a polynomial class with linked lists and implement some basic functions. Specifications Polynomials are stored in a linked list of Term objects (two classes: Polynomial and Term). Term objects have two data fields: coefficient and exponent, both positive integers. Polynomial will have some methods that create linked list functionality, but not all list operations are needed; include only those used by this program. Do not use a separate linked list class. The list will be non-circular. Make a...

  • Write a Python class called polynomial that represents a polynomial in the following fashion, The initializer...

    Write a Python class called polynomial that represents a polynomial in the following fashion, The initializer of your polynomial class will be designed to take any number of numeric coefficients, The coefficient with the highest power will be the first coefficient. The last coefficient will be the constant value, that is, the value that is associated with . power 0. • Therefore, the number of the coefficients specified will directly indicate the highest power of the polynomial to be represented....

  • (The interface class-like) Assume you have the Edible interface with its abstract method. Design a class named Animal a...

    (The interface class-like) Assume you have the Edible interface with its abstract method. Design a class named Animal and its two subclasses named Mammal and Dairy. Make Sheep and Bear as subclasses of Mammal and make implement the Edible interface. howToEat() and sound() are the main two methods for all edible classes while sound() is the main method for the non-edible classes. 1. Draw the UML diagram for the classes and the interface 2. Use Arraylist class to create an...

  • Start by using the starter code provided for the Line class. It implements operator overloading so...

    Start by using the starter code provided for the Line class. It implements operator overloading so you can use cin/cout with line objects. A line will be made up of two points. Create an object to implement a “line” class which allows the programmer to store a line. This class must use the “point” class developed in Part B. The object should have two constructors, appropriate set/get functions, and overloaded I/O (cin/cout) functions. It should include functions the return the...

  • Write a class called Point that contains two doubles that represent its x- and y-coordinates. It...

    Write a class called Point that contains two doubles that represent its x- and y-coordinates. It should have get and set methods for both fields. It should have a constructor that takes two double parameters and initializes its coordinates with those values. It should have a default constructor that initializes both coordinates to zero. It should also contain a method called distanceTo that takes as a parameter another Point and returns the distance from the Point that was passed as...

  • Implement the Point class (code for this up to the isHigher method was discussed in the...

    Implement the Point class (code for this up to the isHigher method was discussed in the lecture). The class has the following instance variables: • x coordinate (an int) • y coordinate (an int) and the following methods: • Constructor that sets the x and y coordinates • Get and set methods • Method equals if this point is equal to another point object (if the x and y coordinates are the same). • Method isHigher if this point is...

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