LAB EXERCISE 6:  Distance Traveled

Goal: 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

Program Specifications:
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.

A. Pre-requisites:
1. Create a folder on your desktop Exercise-6

2. Launch Eclipse

3. Setup your Eclipse Workspace to point to the Exercise-6 folder
a. Select File-> Switch Workspace
b. Browse and select your Exercie-6 folder as your Workspace.



B. Requirements:

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.

 

2.  Declare 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

d.  Close 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.
Scanner inputSpeed = new Scanner(System.in);
System.out.println("Enter the car's speed (mph): ");
speed = inputSpeed.nextInt();

// declare the scanner object used to input traveled time
Scanner inputTime = new Scanner(System.in);
System.out.println("Enter the time traveled (hours): ");
time = inputTime.nextInt();

// close the scanner input object
inputSpeed.close();
inputTime.close();

 

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)

b.  Insert the System.out.println("Distance Traveled (miles): " + milesDistance)

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

D. Requirements for the MainApp Class:

1.  Add comments (documentation)– Program Description, Author and Date

2.  Create the instance of object called car from the CarTaveled class

3.  Declare the mph variable and assign the value from the car.speed object’s variable

4.  Declare the hours variable and assign the value from the car.time  object’s variable

5.  Declare 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

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

E. Test:  

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 folder.
NOTE: Right click on the
subfolder and select Send to “Compress Folder”.  The file will have a file extension of .zip.