Calculate Distance Traveled (using Methods)
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: Calculate Distance Traveled (using Methods)

  1. #1
    Join Date
    Nov 2011
    Posts
    1

    Calculate Distance Traveled (using Methods)

    Hey guys - I need some help...

    I'm trying to create a program that calculates Distance Traveled using user-input for their Speed and Time. However, the assignment requires that I write a separate method for the computation of the Distance and another to DisplayData, so I can recall it back in the Main method, then display the results in another method. But - I keep getting some errors. Any help will surely be appreciated. Thanks in advance.

    Here's the Errors I'm getting from the compiler:

    DistTravel.java:25: error: variable speed might not have been initialized
    displayData(speed, time);
    ^
    DistTravel.java:25: error: variable time might not have been initialized
    displayData(speed, time);
    ^
    2 errors


    P.S. Bare with me on my coding. I am an amateur at Java Programming. Still learning.

    Code:
    import javax.swing.JOptionPane;
    import java.io.*;
    
    public class DistTravel
    {
    	public static void main(String[] args) throws IOException
    	{	
    		// Declare the variables.
    		String input;
    		int speed, time;
    			
    		// The 'Distance Traveled' equation.
    		Distance();
    		
    		// Display Data
    		displayData(speed, time);
    		
    		System.exit(0);
    	}
    	
    	public static int Distance()
    	{	
    		// Get the vehicle speed from the user.
    		
    		// Declare the variables
    		int speed;
    		int time;
    		
    		String input;	// To hold user's input.
    		
    		input = JOptionPane.showInputDialog("What is your vehicle's speed in miles-per-hours? ");
    		
    		// Convert the input to a double.
    		speed = Integer.parseInt(input);
    		
    	   while (speed <= 0)
    			{
    				input = JOptionPane.showInputDialog("Your speed must be greater than zero. Please re-enter. ");
    				
    				speed = Integer.parseInt(input);
    			}
    		
    		// Get the hours traveled from the user.
    		input = JOptionPane.showInputDialog("How many hours have you traveled, thus far? ");
    		
    			time = Integer.parseInt(input);
    		
    	   while (time <= 1)
    		{
    				input = JOptionPane.showInputDialog("How many hours have you traveled for? ");
    			
    				time = Integer.parseInt(input);
    		}
    				
    		return speed * time;
    	}
    	
    	public static void displayData(int speed, int time)
    	{		
    		for(int i = 1; i <= time; i++)
    			{
    			JOptionPane.showMessageDialog(null, "Hour(s): Distance Traveled" + 
    			"----------------------------" + "Hour" + ": " + i + (speed * i) + " miles traveled");
    			}
    	}
    }

  2. #2
    Join Date
    May 2006
    Location
    UK
    Posts
    4,474

    Re: Calculate Distance Traveled (using Methods)

    The error messages tell you what is wrong ie:

    variable speed might not have been initialized: means the variable called 'speed' is being used before you have definitely assigned a value to it - remember class and instance variable are given default values when they are declared but local variables are not.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

Tags for this Thread

Posting Permissions

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


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center