LAB
EXERCISE 4: Mile per Gallon
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. Create a Constructor which is a
Special Method
4. Create an Object
5. Create a Method and call the Method
Program
Specifications: Create a Java program that calculates a car’s
gas mileage. The formula for calculating
the miles that a car can travel per gallon of gas is:
MPG = miles / gallons
The application should prompt the user to enter the miles
the car can be driven on a full tank and the gallons of gas the car can hold.
A. Pre-requisites:
1.
Create a folder on your desktop Exercise-4
2. Launch Eclipse
3. Setup your Eclipse Workspace to point to the Exercise-4
folder
a. Select File-> Switch Workspace
b. Browse and select your Exercie-4 folder as your Workspace.
B. Requirements to setup the Java
Project:
1. Create
a Java Project and name it as MPGCalculator
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 Method
a.
Name the Class as Car
b.
Do not select the main method stub
C. Requirements for the Car Class:
1. Insert
the import java.util.Scanner class
which will allow data to be inputted from the console.
2. Declare
the Class variable carModel
using a String data type
3. Declare
the Class variables- miles, gallons and MPG using a
double data type
4. Create
the Constructor called Car
a. Declare the scanner input class objects
b. Prompt the user to input the data.
c. Assign the inputted data to the variables
d. Close the scanner input class objects
Below is the code you can copy
and paste.
//
declare the scanner object that will be used to input the miles //
declare the scanner object that will be used to input the miles //
declare the scanner object that will be used to input the gallons |
5. Create
the Method called calculateMPG
a. Insert
the formula MPG= miles/ gallons;
b. Insert
return MPG;
Below is sample of the
code:
D. Requirements for the MainApp Class:
1. Add
comments (documentation)– Program Description, Author and Date
2. Create
the instance of object called model from the Car class
3. Declare
the mpg variable
and assign the return value from the called object method – model.calculateMPG
4. Display
the calculated MPG value on the screen.
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:
Miles Gallons Miles per Gallon
375 10 37.50
289 12 24.08
190 15 12.67
F. Submit your exercise in the Canvas Lab
Exercise #4 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 MPGCalculator subfolder that is in the Exercise-04
folder.
NOTE: Right click on the subfolder and select
Send to “Compress Folder”. The file will
have a file extension of .zip.