Results 1 to 6 of 6

Thread: Plot Program

  1. #1
    lsmitherman is offline Member
    Join Date
    Feb 2010
    Posts
    3
    Rep Power
    0

    Default Plot Program

    Hi guys. I'm new to java and signed up for a college java class and was assigned to create a plot program that places an * of a function at a point at execution. I keep getting an ArrayIndexOutOfBoundsException when i try to compile the program. here is the code i have so far. If anyone can help me debug it would be greatly appreciated. Btw I'm writing the code in Eclipse if that helps?

    Java Code:
    public class SmithermanProgram {
    
    	public static void main(String[] args) {
    		double a= Double.parseDouble(args[0]);
    		double b= Double.parseDouble(args[1]);
    		int n= Integer.parseInt(args[2]);
    		int dx= (int)(b-a)/ n;
    		int i;
    		double x = 0, min = 0, max = 0, y, sf;
    		for (i = 0;i <= n;);{
    			x = a + i * dx;
    			y = x;
    		if (y > max)	
    			max = y; i++;
    		if (y < min)
    			min = y; i++;
    			}
    		sf = 160/(max - min);
    		{
    			x = a + i* dx;
    			y = x;
    			double nSpaces = (y - min) * sf;
    			int j = 0;
    			
    			if (j <= nSpaces)
    				System.out.print(" ");
    			else System.out.println("*"); i++;
    				
    		}
    		System.out.println("a= " + a);
    		System.out.println("b= " + b);
    		System.out.println("n= " + n);
    		System.out.println("dx= " + dx);
    		System.out.println("min= " + min);
    		System.out.println("max= " + max);
    		System.out.println("sf= " + sf);
    Last edited by Fubarable; 02-07-2010 at 03:53 PM. Reason: code tags added

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Hello and welcome to the forum. Your program is incomplete -- it's missing its bottom half. Also, which line causes the exception? Also, please use code tags when posting code (see my signature below).

    Much luck!

  3. #3
    lsmitherman is offline Member
    Join Date
    Feb 2010
    Posts
    3
    Rep Power
    0

    Default

    very sorry bout the code thing. It says the ArrayOutOfBoundsException is on the line of args[0]. And what do you mean the program is incomplete?

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Also, this for loop is incorrect:
    Java Code:
    for (i = 0;i <= n;);{
       x = a + i * dx;
       y = x;
       //...
       i++
    For one, that semicolon at first line just before the opening brace: ; { is going short circuit the loop. It means that no statement will occur within the loop body. Also, you'll want to increment "i" within that first line of the for loop. So don't do this:
    Java Code:
        int i;
        for (i = 0; i <= n;);{
          x = a + i * dx;
          y = x;
          if (y > max)
            max = y;
          i++;
          if (y < min)
            min = y;
          i++;
        }
    but instead something more like this:
    Java Code:
        //int i;
        for (int i = 0; i <= n; i++){
          x = a + i * dx;
          y = x;
          if (y > max) {
            max = y;
          }
          //i++;
          if (y < min) { // always enclose if's else's for loops, while loops in braces
            min = y;
          }
          //i++;
        }

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by lsmitherman View Post
    very sorry bout the code thing. It says the ArrayOutOfBoundsException is on the line of args[0]. And what do you mean the program is incomplete?
    Then you're not calling your code with command line args. If you're not running the code on the command line but in eclipse, you'll have to tell eclipse to do this for you via it's Run - Run Configurations - Arguments menu item.

    By incomplete, I see that your main method has no closing brace nor does the class. The class shows its beginning but not its ending.

    You may want to start over on this one.

  6. #6
    lsmitherman is offline Member
    Join Date
    Feb 2010
    Posts
    3
    Rep Power
    0

Similar Threads

  1. ECG plot in a textfile
    By samson in forum Java 2D
    Replies: 13
    Last Post: 01-28-2012, 04:02 PM
  2. using thermometer plot in jfree
    By nitish_coder in forum New To Java
    Replies: 1
    Last Post: 10-07-2009, 06:31 PM
  3. Plot-applet
    By zkab in forum Java Applets
    Replies: 4
    Last Post: 10-03-2009, 03:03 PM
  4. How to plot a dot graph
    By Manfizy in forum Java 2D
    Replies: 3
    Last Post: 01-28-2009, 02:57 PM
  5. Need Help for Dot Plot Graph
    By BHCluster in forum Java 2D
    Replies: 5
    Last Post: 04-15-2008, 02:54 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
  •