LAB EXERCISE 6: Distance Traveled
In this exercise, you will learn how to:
1. Insert the Main Method (Main Entry Point to the Java Program)
2 Import a Java Class Library
3. Declare Variables
4. Create a Constructor (Special Method)
5. Create a Method and accept arguments
6. Create a Void Method
6. Create an Object from the Class
7. Call a Method and pass arguments
The distance that a car travels down a highway can be calculated with the following formula:
Distance = Speed (mph) x Time (hours)
Create an application that allows the user to input a car’s speed in miles-per-hour and hours traveled. Display the distance traveled.
1. Create a folder on your desktop Exercise-6
2. Launch Java EE- Eclipse
Note: You will need to use the Java Perspective Workbench for this exercise
3. Setup your Eclipse Workspace to point to the Exercise-6
a. Select File-> Switch Workspace
b. Browse and select your Exercie-6 folder as your Workspace.
1. Create a Java Project and name it as DistanceTraveled
2. Create the first Class that will have the Main Method
a. Name the Class as MainApp
b. Choose the main method to insert into the class
3. Create the second Class that will have the Constructor and Methods
a. Name the Class as CarTraveled
b. Do not select the main method stub
C. Requirements for the CarTraveled Class:
1. Insert the import java.util.Scanner class which will allow data to be inputted from the console.
the variables- speed, time and traveled using an integer data type
3. Create the Constructor called CarTraveled that will prompt the user to input the data
a. Declare the scanner input class objects
b. Prompt the user to input the data.
c. Assign the inputted data to the variable
the scanner input class object
Below is the code you can copy and paste
declare the scanner object used to input the car's speed.
declare the scanner object used to input traveled time
close the scanner input object
4. Create the Method called calcDistanceTravel
a. This method should accept the argument parameters (int speedMph, int timeHours)
b. Insert the formula traveled = speedMph * timeHours;
c. Insert return traveled
5. Create a Void Method called displayResults
a. This void method should accept the argument parameter (int milesDistance)
the System.out.println("Distance Traveled (miles): " + milesDistance)
D. Requirements for the MainApp Class:
comments (documentation)– Program Description, Author and Date
the instance of object called car from the CarTaveled class
the mph variable
and assign the value from the car.speed object’s variable
the hours variable and assign the value from the
the distance variable
and assign the return value from the called method- car.calcDistanceTravel(mph,
hours). Pass the mph and hours
values as arguments.
6. Call the object's method - car.displayResults(distance). Pass the distance value as an argument
1. Save your Java code
2. Compile and run your Java program.
3. Verify there is no syntax, logical or run-time errors.
4. Use the following set of test data to determine if the application is calculating properly:
Mph Hours Travel Distance (miles)
60 5 300
65 8 520
70 12 840
F. Submit your exercise in the Canvas Lab Exercise #6 Drop Box.
1. Submit the screen shot of the Eclipse
Workbench window showing the Console output screen.
You can use Paint (save as JPG) or Word to paste the screenshot.
2. Zip up and submit the compressed DistanceTraveled subfolder that is in the Exercise-06
NOTE: Right click on the subfolder and select Send to “Compress Folder”. The file will have a file extension of .zip.