Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Distance traveled problem

  1. #1
    Junior Member
    Join Date
    Jun 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Distance traveled problem

    I am extremely new to java and having a hard time grasping it for my summer class. I am stuck on one problem so if anybody could help that would be great!

    The distance a vehicle travels can be calculated as follows:
    Distance = Speed * Time

    Write a program that asks the user for the speed of a vehicle (in MPH) and the number of hours they traveled. It should use a loop to display the distance a vehicle has traveled for each hour of a time period specified by the user. For example, if a vehicle is traveling at 40 MPH for a three hour time period, it should show something like this:

    Hour Distance Traveled
    -----------------------------------
    1 40
    2 80
    3 120

    Input Validation: Do not accept a negative number for speed and do not acept any value less than 1 for time traveled


    This is what I Have so far...


    package lab4;

    public class num2
    {
    public static void main(String[] args)
    {

    final int Time_Incrememnt = 1;

    int d;
    int s;
    int t;
    int i;
    String input;

    System.out.println("What is the speed of the vehicle in MPH?");

    s = keyboard.nextInt();

    while (s <= 0)
    {

    System.out.print("Your speed must be greater than zero. Please re-enter.");

    }

    System.out.print("Enter the number of hours that you have traveled: ");

    t = keyboard.nextInt();


    while (t <= 1)
    {

    System.out.print("Your hours must be at least 1 or more. Please re-enter.");


    System.out.println("Hours Distance Traveled");
    System.out.println("--------------------------------");

    for (i = 1; i <= t; i++)

    d = s*t;


    }
    }

    }


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Distance traveled problem

    When posting code, please use the highlight tags to preserve formatting.

    What does that code do? Does it compile? Does it run? Does it do what you expect? If not, where does its behavior differ from what you want? Where exactly are you stuck?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Jun 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Distance traveled problem

    That one is not finished at the end. I've had trouble getting it to correctly compute the distance traveled and get the hours as displayed in the example. It asks the right questions and everything I just can't get the final chart

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Distance traveled problem

    Well, time to debug your program. What happened when you stepped through it with a debugger, or at least added some print statements to figure out what's going on? What happens on each line?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Calculating Fuel After Distance
    By floorplay in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 23rd, 2013, 11:12 PM
  2. Distance function help
    By abf617 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: August 2nd, 2012, 06:32 PM
  3. Getting Distance between two addresses
    By Shockwave786 in forum Java Theory & Questions
    Replies: 3
    Last Post: July 23rd, 2012, 10:19 AM
  4. i am having a problem with the distance formula,help needed!
    By Bentino in forum What's Wrong With My Code?
    Replies: 4
    Last Post: March 22nd, 2012, 07:07 PM
  5. Distance between 2 points
    By captain in forum Java Theory & Questions
    Replies: 3
    Last Post: February 22nd, 2012, 12:53 AM