I have a class which includes a constructor that holds hours and speed, getters and setters, and a method which calculates distace using the hours and speed variable. I need this to display a table which takes however many hours the user inputs, then for each hour display a row that shows how far per hour. here is an example:

hour           distance traveled
--------------------------------
1                             10
2                             20
3                             30
etc.

Right now my program is mostly okay, except when I run it I get someting like this:

hour           distance traveled
--------------------------------
1                             30
2                             30
3                             30
etc.          

So I can tell it is a problem in my for loop but I just can't figure out what exactly I am supposed to do next to make it work. Any help is appreciated, thanks. Here is the code for my main program, I can post the class as well if needed:

import java.util.Scanner;

public class DistanceTraveledTest {

public static void main(String[] args) {

    Scanner input = new Scanner(System.in);

    double testSpeed;
    int testHours;

    System.out.println("What is the speed of the vehicle?");
    testSpeed = input.nextDouble();
    while (testSpeed <= 1){
        System.out.print("The speed must be equal to or greater than one");
        System.out.println("What is the speed of the vehicle?");
        testSpeed = input.nextDouble();
    }

    System.out.println("How many hours has the vehicle been travelling?");
    testHours = input.nextInt();

    while (testHours <= 1){
        System.out.print("The hours must be equal to or greater than one");
        System.out.println("How many hours has the vehicle been traveling?");
        testHours = input.nextInt();
    }


    DistanceTraveled traveler = new DistanceTraveled(testSpeed, testHours);

    System.out.println("Hour\t\tDistance Travelled");
    System.out.println("------------------------------");

    for(int i=0;i<=testHours;i++){
           System.out.println(i + "\t\t" + traveler.findDistance());



    }

}
}

I was able to solve this issue, if you are here and wondering what I did, here is my code. Class:

 package distancetraveled;

    public class DistanceTraveled {

    private double speed;       //speed
    private int hours;      //hours
    private double distance;    //distance

    //distanceTravelled constructor
    public DistanceTraveled(double s, int h) {
        speed = s;
        hours = h;
    }

    // findDistance method
    public double findDistanceOverTime() {
        return getSpeed() * hours;
    }

    // setSpeed method
    public void setSpeed(double s) {
        s = speed;
    }

    //setHours method
    public void setHours(int h) {
        h = hours;
    }

    // set distance method
    public void setDistance(int h) {
        hours = h;
    }

    // getSpeed method
    public double getSpeed() {
        return speed;
    }

    //getDistance method
    public double getDistance() {
        return distance;
    }


    }

and the main program:

package distancetraveled;

import java.util.Scanner;

public class DistanceTraveledTest {

public static void main(String[] args) {

    //initialize scanner
    Scanner input = new Scanner(System.in);

    //declare variables
    double testSpeed;
    int testHours;

    //ask the user the speed of the vehicle
    System.out.println("What is the speed of the vehicle?");
    testSpeed = input.nextDouble();
    while (testSpeed <= 1){
        System.out.print("The speed must be equal to or greater than one");
        System.out.println("\nWhat is the speed of the vehicle?");
        testSpeed = input.nextDouble();
    }

    //ask the user the hours travelled
    System.out.println("How many hours has the vehicle been travelling?");
    testHours = input.nextInt();
    while (testHours <= 1){
        System.out.print("The hours must be equal to or greater than one");
        System.out.println("\nHow many hours has the vehicle been traveling?");
        testHours = input.nextInt();
    }


    //construct a DistanceTraveled object
    DistanceTraveled traveler = new DistanceTraveled(testSpeed, testHours);

    //display the table
    System.out.println("Hour\t\tDistance Travelled");
    System.out.println("------------------------------");

    for(int i=1;i<testHours;i++){
        traveler.setDistance(i);   
        System.out.println(i + "\t\t" + traveler.findDistanceOverTime());



    }

}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.