Results 1 to 4 of 4
  1. #1
    acole5 is offline Member
    Join Date
    Jun 2011
    Posts
    10
    Rep Power
    0

    Default HELP with a plotter program ASAP!

    I'm SUPER lost on this project and keep getting this error:

    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
    at Plotter.main(Plotter.java:3)

    Here's the assingment:
    Create a simple plotter (x-axis runs vertically, y -axis runs horizontally) using command line arguments for a (left end point), b (right end point), and n (number of sub-intervals of [a, b].)

    Steps:

    compute maximum and minimum values of the function on [a, b].
    compute scaling factor sf. For example, does the following accomplish all we need?
    sf = graphPageWidth/(max - min).
    construct for each point in the interval the required number of spaces (one for each column) preceding the plot (an *)
    #cols = sf ( f(a +idx) - min ).



    and here's my code:public class Plotter {
    public static void main(String[] args) {
    double a = Double.parseDouble(args[0]);
    double b = Double.parseDouble(args[1]);
    double x, sf, y;
    double max;
    double min;
    double nSpaces;
    int n = Integer.parseInt(args[2]);
    System.out.println(" "+args[0]+" "+args[1]+" "+args[2]);
    double dx = (b-a)/n;
    max=0;
    min=0;
    for (int i =0; i<=n; i++) {
    x= a + i * dx;
    y= x*x*x;
    if (y>max) {
    max=y;
    }
    else if (y<min) {
    min=y;
    }
    }
    sf = 120/(max - min);
    System.out.println(dx);
    System.out.println(max);
    System.out.println(min);

    for(int k=0; k<=n; k++) {
    x = a + k * dx;
    y = x*x*x;
    nSpaces = (y - min) * sf;
    int j = 0;
    if(j<nSpaces)
    {
    System.out.print(" ");
    j++;
    }
    else {
    System.out.println("*");
    }
    }
    }
    }

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,331
    Rep Power
    25

    Default

    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
    at Plotter.main(Plotter.java:3)
    You need to learn how to read the error message.
    It says that at line 3 of your program there is an array that does not have a 0th element. In other words the array is empty.
    You should test if the length of the array is greater than 0 before trying to get the 0th element.

    What do you expect to be in the args array?

    Are you passing arguments to your program when you start its execution?

  3. #3
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    5

    Cool

    Quote Originally Posted by Norm View Post
    Are you passing arguments to your program when you start its execution?
    acole5,

    Read this above line again and again. If you can understand this, then that's it.

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  4. #4
    acole5 is offline Member
    Join Date
    Jun 2011
    Posts
    10
    Rep Power
    0

    Default

    Thank you both! I figured it out and turned in the project :)

Similar Threads

  1. Replies: 0
    Last Post: 01-23-2011, 09:14 PM
  2. Need help ASAP, please
    By KAM0002 in forum New To Java
    Replies: 6
    Last Post: 12-08-2010, 04:58 PM
  3. Need help asap please!
    By mbm4ever in forum Java Applets
    Replies: 4
    Last Post: 08-12-2010, 03:57 PM
  4. Assistance needed ASAP: Postpix program
    By Debonairj in forum New To Java
    Replies: 18
    Last Post: 07-27-2010, 01:37 PM
  5. Need help ASAP with Payroll Program Part 2
    By arrech326 in forum New To Java
    Replies: 10
    Last Post: 11-17-2009, 10:17 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •