Question

Can anyone add the 1) add_to_coef, add, multiply, coefficient, eval and equals method in the main...

Can anyone add the 1) add_to_coef, add, multiply, coefficient, eval and equals method in the main function..so can take it from text file.... or 2) make main function with user input for all methods mentioned in the program.

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Scanner;

public class Polynomial {
//array of doubles to store the coefficients so that the coefficient for x^k is stored in the location [k] of the array.
double coefficients[];
static int size;
//fube the following methods:
public Polynomial() {
//STCONDITION: Creates a polynomial represents 0
coefficients=new double[size+1];
for(int i=0;i<=size;i++)
{
coefficients[i]=0.0;
}
}
public Polynomial(double a0){
//STCONDITION: Creates a polynomial has a single x^0 term with coefficient a0
coefficients=new double[size+1];
for(int i=0;i<=size;i++)
{
coefficients[i]=0.0;
}
coefficients[0]=a0;
}
public Polynomial(Polynomial p){
//STCONDITION: Creates a polynomial is the copy of p
coefficients=new double[Polynomial.size+1];
for(int i=0;i<=size;i++)
{
coefficients[i]=p.coefficient(i);
}
}
public void add_to_coef(double amount, int exponent){
// STCONDITION: Adds the given amount to the coefficient of the specified exponent.
if(exponent<=size)
coefficients[exponent]=coefficients[exponent]+amount;
}
public void assign_coef(double coefficient, int exponent){
//STCONDITION: Sets the coefficient for the specified exponent.
coefficients[exponent]=coefficient;
}
public double coefficient(int exponent)
{
//STCONDITION: Returns coefficient at specified exponent of this polynomial.
return coefficients[exponent];
}
public double eval(double x)
{
//STCONDITION: The return value is the value of this polynomial with the given value for the variable x.
double result=0;
for(int i=0;i<=size;i++)
{
result+=coefficients[i]* Math.pow(x, i);
}
return result;
}
public boolean equals (Polynomial p)
{
//STCONDITION: return true if p and this polynomial is same
for(int i=0;i<=size;i++)
{
if(coefficients[i]!=p.coefficient(i))
return false;
}
return true;
}
  
public String toString()
{
//STCONDITION: return the polynomial as a string like "2x^2 + 3x + 4"
//portant only non-zero terms
String result="";
for(int i=size;i>=0;i--)
{
if(coefficients[i]!=0.0)
{
if(i==0)
result+=coefficients[i];
else if(i==1)
result+=coefficients[i]+"x +";
else
result+=coefficients[i]+"x^"+String.valueOf(i)+" + ";
}
}
return result;
}
  
public Polynomial add(Polynomial p)
{
//STCONDITION: return a polynomial that is the sum of p and this polynomial
Polynomial result=new Polynomial();
for(int i=0;i<=size;i++)
{
result.add_to_coef(coefficients[i]+p.coefficient(i), i);
}
return result;
}
public Polynomial multiply(Polynomial p)
{
//STCONDITION: returns a new polynomial obtained by multiplying this term and p. For example, if this polynomial is
//^2 + 3x + 4 and p is 5x^2 - 1x + 7, then at the end of this function, it will return the polynomial 10x^4 + 13x^3 + 31x^2 + 17x + 28.
Polynomial result=new Polynomial();
for (int i=0; i<=size; i++)
{
for (int j=0; j<=size; j++)
{
result.add_to_coef((coefficients[i]*p.coefficient(j)),(i+j));
}
}
return result;
}
  
public static void main(String args[]) throws FileNotFoundException, IOException
{
double coef;
int expo;
//Scanner in = new Scanner(new File("input.txt"));
Scanner in = new Scanner(new File("F:\\Netbeans Files\\Application7\\src\\polynomial\\polynomialA.txt"));
Polynomial.size=Integer.parseInt(in.nextLine());

//create object for polynomial class
Polynomial polynomialA=new Polynomial();

while (in.hasNext()) {
String ss=in.nextLine();
coef = Double.parseDouble(ss.substring(ss.indexOf("(") + 1, ss.indexOf(",")));
expo=Integer.parseInt(ss.substring(ss.indexOf(",") + 1, ss.indexOf(")")));
polynomialA.assign_coef(coef, expo);
}
if (in != null) {
in.close();
}
System.out.println(polynomialA);
}
}

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

Solution==========================

Note: The question appears to be ambigous,

  • Please provide, how the input file actually looks like in the comments section..
  • If there are no rules of how input file looks like, let me know that too..

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Scanner;

public class Polynomial {
   // array of doubles to store the coefficients so that the coefficient for
   // x^k is stored in the location [k] of the array.
   double coefficients[];
   static int size;

   // fube the following methods:
   public Polynomial() {
       // STCONDITION: Creates a polynomial represents 0
       coefficients = new double[size + 1];
       for (int i = 0; i <= size; i++) {
           coefficients[i] = 0.0;
       }
   }

   public Polynomial(double a0) {
       // STCONDITION: Creates a polynomial has a single x^0 term with
       // coefficient a0
       coefficients = new double[size + 1];
       for (int i = 0; i <= size; i++) {
           coefficients[i] = 0.0;
       }
       coefficients[0] = a0;
   }

   public Polynomial(Polynomial p) {
       // STCONDITION: Creates a polynomial is the copy of p
       coefficients = new double[Polynomial.size + 1];
       for (int i = 0; i <= size; i++) {
           coefficients[i] = p.coefficient(i);
       }
   }

   public void add_to_coef(double amount, int exponent) {
       // STCONDITION: Adds the given amount to the coefficient of the
       // specified exponent.
       if (exponent <= size)
           coefficients[exponent] = coefficients[exponent] + amount;
   }

   public void assign_coef(double coefficient, int exponent) {
       // STCONDITION: Sets the coefficient for the specified exponent.
       coefficients[exponent] = coefficient;
   }

   public double coefficient(int exponent) {
       // STCONDITION: Returns coefficient at specified exponent of this
       // polynomial.
       return coefficients[exponent];
   }

   public double eval(double x) {
       // STCONDITION: The return value is the value of this polynomial with
       // the given value for the variable x.
       double result = 0;
       for (int i = 0; i <= size; i++) {
           result += coefficients[i] * Math.pow(x, i);
       }
       return result;
   }

   public boolean equals(Polynomial p) {
       // STCONDITION: return true if p and this polynomial is same
       for (int i = 0; i <= size; i++) {
           if (coefficients[i] != p.coefficient(i))
               return false;
       }
       return true;
   }

   public String toString() {
       // STCONDITION: return the polynomial as a string like "2x^2 + 3x + 4"
       // portant only non-zero terms
       String result = "";
       for (int i = size; i >= 0; i--) {
           if (coefficients[i] != 0.0) {
               if (i == 0)
                   result += coefficients[i];
               else if (i == 1)
                   result += coefficients[i] + "x +";
               else
                   result += coefficients[i] + "x^" + String.valueOf(i) + " + ";
           }
       }
       return result;
   }

   public Polynomial add(Polynomial p) {
       // STCONDITION: return a polynomial that is the sum of p and this
       // polynomial
       Polynomial result = new Polynomial();
       for (int i = 0; i <= size; i++) {
           result.add_to_coef(coefficients[i] + p.coefficient(i), i);
       }
       return result;
   }

   public Polynomial multiply(Polynomial p) {
       // STCONDITION: returns a new polynomial obtained by multiplying this
       // term and p. For example, if this polynomial is
       // ^2 + 3x + 4 and p is 5x^2 - 1x + 7, then at the end of this function,
       // it will return the polynomial 10x^4 + 13x^3 + 31x^2 + 17x + 28.
       Polynomial result = new Polynomial();
       for (int i = 0; i <= size; i++) {
           for (int j = 0; j <= size; j++) {
               result.add_to_coef((coefficients[i] * p.coefficient(j)), (i + j));
           }
       }
       return result;
   }

   public static void main(String args[]) throws FileNotFoundException, IOException {
       double coef;
       int expo;
       Scanner in = new Scanner(new File("input.txt"));
       //Scanner in = new Scanner(new File("F:\\Netbeans Files\\Application7\\src\\polynomial\\polynomialA.txt"));
      
       Polynomial.size = Integer.parseInt(in.nextLine());

       // create object for polynomial class
       Polynomial polynomialA = new Polynomial();
      
       while (in.hasNext()) {
           String ss = in.nextLine();

//Added trim method, to eleminate any errouneous whitespaces
coef = Double.parseDouble(ss.substring(ss.indexOf("(") + 1, ss.indexOf(",")).trim());
           expo = Integer.parseInt(ss.substring(ss.indexOf(",") + 1, ss.indexOf(")")).trim());
           polynomialA.assign_coef(coef, expo);
       }
       if (in != null) {
           in.close();
       }
      
      
       System.out.println(polynomialA);
       System.out.println("For x = 5");
       System.out.println(polynomialA.eval(5));
      
   }
}

================

For input file which looks like this (as provided in comments)

D Polynomial javaoinput.brt Polynomial.java 1 100 2 (3,0) 3 (-1,3) 4 (4,100) polynomialA.txt

The output looks as:

4.0x*100+ -1.0x*3 + 3.0 For x- 5 3.155443620884047E70

Add a comment
Know the answer?
Add Answer to:
Can anyone add the 1) add_to_coef, add, multiply, coefficient, eval and equals method in the main...
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
  • Polynomial Using LinkedList class of Java Language Description: Implement a polynomial class using a LinkedList defined...

    Polynomial Using LinkedList class of Java Language Description: Implement a polynomial class using a LinkedList defined in Java (1) Define a polynomial that has the following methods for Polynomial a. public Polynomial() POSTCONDITION: Creates a polynomial represents 0 b. public Polynomial(double a0) POSTCONDITION: Creates a polynomial has a single x^0 term with coefficient a0 c. public Polynomial(Polynomial p) POSTCONDITION: Creates a polynomial is the copy of p d. public void add_to_coef(double amount, int exponent) POSTCONDITION: Adds the given amount to...

  • In this same program I need to create a new method called “int findItem(String[] shoppingList, String...

    In this same program I need to create a new method called “int findItem(String[] shoppingList, String item)” that takes an array of strings that is a shopping list and a string for an item name and searches through the “shoppingList” array for find if the “item” exists. If it does exist print a confirmation and return the item index. If the item does not exist in the array print a failure message and return -1. import java.util.Scanner; public class ShoppingList...

  • I need help filling in the the code in the methods add, multiply, and evaluate package...

    I need help filling in the the code in the methods add, multiply, and evaluate package poly; import java.io.IOException; import java.util.Scanner; /** * This class implements evaluate, add and multiply for polynomials. * * * */ public class Polynomial {       /**    * Reads a polynomial from an input stream (file or keyboard). The storage format    * of the polynomial is:    * <pre>    * <coeff> <degree>    * <coeff> <degree>    * ...    *...

  • /** this is my code, and I want to invoke the method, doubleArraySize() in main method....

    /** this is my code, and I want to invoke the method, doubleArraySize() in main method. and I need to display some result in cmd or terminal. also, I have classes such as Average and RandomN needed for piping(pipe). please help me and thank you. **/ import java.util.Scanner; public class StdDev { static double[] arr; static int N; public static void main(String[] args) { if(args.length==0){ arr= new double[N]; Scanner input = new Scanner(System.in); int i=0; while(input.hasNextDouble()){ arr[i] = i++; }...

  • Can anyone helps to create a Test.java for the following classes please? Where the Test.java will...

    Can anyone helps to create a Test.java for the following classes please? Where the Test.java will have a Scanner roster = new Scanner(new FileReader(“roster.txt”); will be needed in this main method to read the roster.txt. public interface List {    public int size();    public boolean isEmpty();    public Object get(int i) throws OutOfRangeException;    public void set(int i, Object e) throws OutOfRangeException;    public void add(int i, Object e) throws OutOfRangeException; public Object remove(int i) throws OutOfRangeException;    } public class ArrayList implements List {   ...

  • In the code shown above are two parts of two different exercises. HOW WE CAN HAVE...

    In the code shown above are two parts of two different exercises. HOW WE CAN HAVE BOTH OF THEM ON THE SAME CLASS BUT SO WE CAN RECALL them threw different methods. Thank you. 1-First exercise import java.util.Scanner; public class first { public static int average(int[] array){    int total = 0;    for(int x: array)        total += x;    return total / array.length;    } public static double average(double[] array){    double total = 0;    for(double x: array)        total += x;    return total /...

  • java programming how would i modify the code below to add a GUI (with a main menu with graphics and buttons, etc...) and include an option to print a list of all students added in a grid format and sh...

    java programming how would i modify the code below to add a GUI (with a main menu with graphics and buttons, etc...) and include an option to print a list of all students added in a grid format and short by name, course, instructor, and location. ///main public static void main(String[] args) { Scanner in = new Scanner(System.in); ArrayList<Student>students = new ArrayList<>(); int choice; while(true) { displayMenu(); choice = in.nextInt(); switch(choice) { case 1: in.nextLine(); System.out.println("Enter Student Name"); String name...

  • import java.util.Scanner; public class TriangleMaker {    public static void main(String[] args) {        //...

    import java.util.Scanner; public class TriangleMaker {    public static void main(String[] args) {        // TODO Auto-generated method stub        System.out.println("Welcome to the Triangle Maker! Enter the size of the triangle.");        Scanner keyboard = new Scanner(System.in);    int size = keyboard.nextInt();    for (int i = 1; i <= size; i++)    {    for (int j = 0; j < i; j++)    {    System.out.print("*");    }    System.out.println();    }    for (int...

  • PrintArray vi Create a class called PrintArray. This is the class that contains the main method....

    PrintArray vi Create a class called PrintArray. This is the class that contains the main method. Your program must print each of the elements of the array of ints called aa on a separate line (see examples). The method getArray (included in the starter code) reads integers from input and returns them in an array of ints. Use the following starter code: //for this program Arrays.toString(array) is forbidden import java.util.Scanner; public class PrintArray { static Scanner in = new Scanner(System.in);...

  • Can Anyone help me to convert Below code to C++! Thanks For example, in C++, the...

    Can Anyone help me to convert Below code to C++! Thanks For example, in C++, the function headers would be the following: class MaxHeap { vector<int> data; public: MaxHeap() { // ... } int size() { // ... } int maxLookup() { // ... } void extractMax() { // ... } void insert(int data) { // ... } void remove(int index) { // ... } }; ======================== import java.util.Arrays; import java.util.Scanner; public class MaxHeap { Integer[] a; int size; //...

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