Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Distance Traveled Program - Loop Questions

843789Jul 18 2010 — edited Jul 18 2010
Hello I'm new to java programming as well as these forums. I have a question about a project I am doing. The project is supposed to take a user input for speed(miles/hour) and time(hours) and output a distance, but the program must output the distance at 1 hour, 2 hours, 3 hours, etc. If that doesn't make sense take for example a car traveling at 20 mph for 4 hours. At the first hour it's gone 20 miles, the second hour 40 miles, the third hour 60 miles, and the fourth hour 80 miles. The program is supposed to output something like this.


Hour Distance Traveled
----------------------------------------------------------------
1 20
2 40
3 60
4 80

Also if the user inputs a negative for speed, or a zero or negative for the distance the program should run an infinite loop and keep asking the user to input appropriate values.

I sort of have the program working, but I can't get the loops to work and I can't get it to display for each hour. It just displays the last value for time and distance. Obviously the loops that I have set up are not working the way I want them too, but I can't figure out how to fix it. I have 2 classes.
public class Vehicle {

    private int time;
    private int speed;
    private int distance;
    private String chart;

    public Vehicle() {
        time = 0;
        speed = 0;
        distance = 0;
        chart = null;
    }

    public void setTime(int time) {
        this.time = time;
    }

    public double getTime() {
        return time;

    }

    public void setSpeed(int speed) {
        this.speed = speed;
    }

    public int getSpeed() {
        return speed;
    }

    public void inputTimeSpeed(int time, int speed) {
        setTime(time);
        setSpeed(speed);
    }

    public int Distance() {
        int loopCount = 1;
        while (loopCount <= time) {
            distance = speed * time;
            loopCount++;
        }
        return distance;
    }

    public String chart() {
//        for (int count = 1; count <= time; count++) {
        int count = 1;
        do {
            count++;
            chart = "The time in hours: " + getTime() + "          " + "The distance in miles: " + Distance();
        } while (count <= getTime());
        return chart;
    }

    public String display() {
        boolean done = true;
        while (!done) {
            if ((speed != 0) && (time != 0)) {
                return chart;
            } else {
                chart = "You have entered invalid values. Please enter"
                        + "\npositive values for speed and time: ";
            }
        }
        return chart;
    }
}
public class VehicleTester {

    public static void main(String[] args) {
        Vehicle input = new Vehicle();

        input.inputTimeSpeed(inputTime(), inputSpeed());

//        System.out.println(input.Distance());

        System.out.println(input.display());

        System.out.println(input.chart());
    }

    public static int inputSpeed() {
        Scanner keys = new Scanner(System.in);
        System.out.print("Please enter the speed that you will"
                + "\nbe traveling in mile(s) per hour: ");
        return keys.nextInt();
//        int i = keys.nextInt();
//        while (i <= 0) {
//            System.out.print("Please enter a nonzero value: ");
//        }
//        return i;
    }

    public static int inputTime() {
        Scanner keys = new Scanner(System.in);
        System.out.print("Please enter the time that you will"
                + "\nbe traveling in hours: ");
        return keys.nextInt();
//        int i = keys.nextInt();
//        while (i <= 0) {
//            System.out.print("Please enter a nonzero value: ");
//        }
//        return i;
    }
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Aug 15 2010
Added on Jul 18 2010
4 comments
635 views