11 Replies - 2477 Views - Last Post: 14 November 2011 - 02:24 AM Rate Topic: -----

#1 6r3nt0n  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 10-November 11

Calculate Distance Traveled

Posted 10 November 2011 - 08:20 PM

Hey guys - I need some help...

I'm trying to create a problem 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, 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.

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

import java.util.*;
import java.io.*;
import javax.swing.JOptionPane;

public class distTravel
{
	public static void main(String[] args)
	{
		// Variables
	   int dist, speed, time;
		
		// The 'Distance Traveled' equation.
		distance(speed, time);
		
		// Display Data
		for(int i=1; i<=time; i++)
			displayData(i, speed);
		
		System.exit(0);
	}
	
	public static double distance(int speed, int time)
	{	
		// Get the vehicle speed from the user.
		
		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.parseInteger(input);
		
	   while (speed <= 0)
			{
				input = JOptionPane.showInputDialog("Your speed must be greater than zero. Please re-enter. ");
				
				speed = Integer.parseInteger(input);
			}
		
		// Get the hours traveled from the user.
		input = JOptionPane.showInputDialog("How many hours have you traveled, thus far? ");
		
			time = Integer.parseInteger(input);
		
	   while (time <= 1)
		{
			input = JOptionPane.showInputDialog("How many hours have you traveled for? ");
			
			time = Integer.parseInteger(input);
		}
				
		return speed * time;
	}
	
	public static void displayData(int i, int speed)
	{
			JOptionPane.showMessageDialog(null, "Hour(s): Distance Traveled" + 
			"----------------------------" + "Hour " + i + ": " + (speed * i) + " miles traveled");
	}
}


This post has been edited by macosxnerd101: 10 November 2011 - 08:21 PM
Reason for edit:: Please use a descriptive title


Is This A Good Question/Topic? 0
  • +

Replies To: Calculate Distance Traveled

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10372
  • View blog
  • Posts: 38,411
  • Joined: 27-December 08

Re: Calculate Distance Traveled

Posted 10 November 2011 - 08:21 PM

Please post your error messages exactly as they appear on your compiler.
Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Calculate Distance Traveled

Posted 10 November 2011 - 08:21 PM

And the question is ?
Was This Post Helpful? 0
  • +
  • -

#4 6r3nt0n  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 10-November 11

Re: Calculate Distance Traveled

Posted 10 November 2011 - 09:02 PM

My apologies. I forgot to post the errors.

distTravel.java:40: error: cannot find symbol
speed = Integer.parseInteger(input);
^
symbol: method parseInteger(String)
location: class Integer
distTravel.java:46: error: cannot find symbol
speed = Integer.parseInteger(input);
^
symbol: method parseInteger(String)
location: class Integer
distTravel.java:52: error: cannot find symbol
time = Integer.parseInteger(input);
^
symbol: method parseInteger(String)
location: class Integer
distTravel.java:58: error: cannot find symbol
time = Integer.parseInteger(input);
^
symbol: method parseInteger(String)
location: class Integer
4 errors

Was This Post Helpful? 0
  • +
  • -

#5 x68zeppelin80x  Icon User is offline

  • D.I.C Addict

Reputation: 129
  • View blog
  • Posts: 576
  • Joined: 07-March 09

Re: Calculate Distance Traveled

Posted 10 November 2011 - 09:09 PM

Initialize your variables.

Why does distance have parameters?

Use Integer.parseInt() NOT Integer.parseInteger()

This post has been edited by x68zeppelin80x: 10 November 2011 - 09:21 PM

Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Calculate Distance Traveled

Posted 10 November 2011 - 09:20 PM

Bad OO design

The only static method should be main()
This method should create a distTravel object
and then play with it

More you have staic methods in your class, more bad is your design and more you are away from OO programming

variables like

int dist, speed, time;

should be instance variable of the distTravel class NOT part of the main() method

Review your design
Was This Post Helpful? 0
  • +
  • -

#7 x68zeppelin80x  Icon User is offline

  • D.I.C Addict

Reputation: 129
  • View blog
  • Posts: 576
  • Joined: 07-March 09

Re: Calculate Distance Traveled

Posted 10 November 2011 - 09:47 PM

Try to write a toString() that appends each mile and returns all of the data. Then call the toString() inside the showMessageDialog().
I say this because the code you have makes a popup for every mile traveled and if the user enters 42 miles, they will have to close ALL 42 popup boxes...
Was This Post Helpful? -1
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Calculate Distance Traveled

Posted 10 November 2011 - 10:29 PM

View Postx68zeppelin80x, on 11 November 2011 - 12:47 AM, said:

Try to write a toString() that appends each mile and returns all of the data. Then call the toString() inside the showMessageDialog().
I say this because the code you have makes a popup for every mile traveled and if the user enters 42 miles, they will have to close ALL 42 popup boxes...

Won't work as there is no distTravel object !!!

We have to show this newbie how to get ride of these static methods

main() should just instansiate a distTravel object and then play with it
Was This Post Helpful? 0
  • +
  • -

#9 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Calculate Distance Traveled

Posted 10 November 2011 - 10:49 PM

And please respect Java conventions]a class name start with a CAPITAL letter
Was This Post Helpful? 0
  • +
  • -

#10 6r3nt0n  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 10-November 11

Re: Calculate Distance Traveled

Posted 13 November 2011 - 02:32 PM

Alright - I fixed a few of the errors you guys helped me out with. However, now I'm getting these errors from the compiler. Any ideas? I'm stuck. Thanks again for the help.

Errors:
DistTravel.java:70: error: cannot find symbol
"----------------------------" + "Hour " + i + ": " + (speed * i) + " miles traveled");
^
symbol: variable i
location: class DistTravel
DistTravel.java:70: error: cannot find symbol
"----------------------------" + "Hour " + i + ": " + (speed * i) + " miles traveled");
^
symbol: variable i
location: class DistTravel
2 errors


import javax.swing.JOptionPane;

public class DistTravel
{
	public static void main(String[] args)
	{	
		// Declare the variables.
		int time;
		int speed;
	
		// The 'Distance Traveled' equation.
		distance();
		
		// Display Data
		for(int i=1; i<=time; i++)
			displayData(speed, time);
		
		System.exit(0);
	}
	
	public static double 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)
	{
			JOptionPane.showMessageDialog(null, "Hour(s): Distance Traveled" + 
			"----------------------------" + "Hour " + i + ": " + (speed * i) + " miles traveled");
	}
}


Was This Post Helpful? 0
  • +
  • -

#11 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Calculate Distance Traveled

Posted 13 November 2011 - 09:05 PM

Nobody is short sighted here, we really do not need your bold tags
Better to put your energy in learning Java rather tan fancy forum editing :)

"----------------------------" + "Hour " + i + ": " + (speed * i) + " miles traveled");

What is the variable "i" referenced twice on that line ?
Was This Post Helpful? 0
  • +
  • -

#12 6r3nt0n  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 10-November 11

Re: Calculate Distance Traveled

Posted 14 November 2011 - 02:24 AM

Sorry about the bold. Just trying to separate my post from the Error list.

The variable "i" is referenced twice because I was wanting to show each Hour as it increases.
Similar to the picture below. But I'm getting a few Errors on the displayData() method.

Posted Image
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1