Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

polynom help

807599Nov 13 2006 — edited Nov 14 2006
Hello. I want to write down a class who represent a polynom. ( I've figured out a bit for myself, but I need help. ) It should contain:

A constructor who takes the coefficients ( a field of int ) as a parameter.
public Polynom(int[] coef)
It should also contain three methods:
public Polynom deriv()
Who calculates and returns the derivative of the polynom.
public double computePoly(double x)
Who calculates ( and returns ) the value of the polynom in the point x.
public String toString()
Who returns a string presentation of the polynom. For example "3x^2 + 2x - 6".


Last but not least it should store the polynom and its derivative internal in the class as integer fields.

I've recieved a main class to test my polynom class:
import java.util.*;
public class PolyMain{

  public static void main(String[] args){
    
    if(args.length>0){
      double x = 0.0;
      int[] pfield = new int[args.length];
      for(int i=0;i<args.length;i++)
        pfield=Integer.parseInt(args[i]);
Polynom p = new Polynom(pfield);

if(args.length>1){
System.out.println();
System.out.println("Gives a value to the point where the polynom should be calculated.");
System.out.print("x = ");
Scanner s = new Scanner(System.in);
x = s.nextDouble();
}

System.out.println();
System.out.println("The polynom is: "+p);
System.out.println("The value of the point "+x+" is: "+p.computePoly(x));
System.out.println();
System.out.println("The derivative is: "+p.deriv());
System.out.println("The value of the point "+x+" into the derivative is: "+p.deriv().computePoly(x));
System.out.println();

if(pfield[0]!=0 && args.length>1){
String str = p.toString();
pfield[0]=0;
System.out.println("(Just to make sure that the class Polynom has its own copy of the field.)");
System.out.println("The polynom "+str+" should be equal to");
System.out.println("the polynom "+p);
System.out.println();
}
else
System.out.println("Try using a polynoom that consists of at least two terms where the first coefficient != 0");
}
else
System.err.println("Input is made through command line. You have to state the polynoms integer coefficients.);
}

}



Thanks in advance, any help is appreciated.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Dec 12 2006
Added on Nov 13 2006
21 comments
129 views