LAB EXERCISE 16: Calories Burned

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 and Initialize
4.  Use a Try-Catch block statement for the error handling and exceptions
5.  Create Methods that accept arguments
6.  Use the For Loop to allow the iteration of the code execution
7.  Create the Object from the Class
8.  Call the Object’s Method and pass arguments to the method


Program Specifications:
Create a Calories Burned Application that will perform the following tasks:
1. Get the calories burned for each day of the week (Monday to Sunday)
2. Compute the total calories burned using a running total
3. Calculate the average calories burned
4. Get the highest and lowest calories burned during the week.

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

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-16 folder
a. Select File-> Switch Workspace
b. Browse and select your Exercie-16 folder as your Workspace.



B. Requirements:

1.  Create a Java Project and name it as CaloriesBurned

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

        

Create the second Class that will have the Constructor and Method

c.      Name the Class as Calories

d.      Do not select the main method stub

   

C. Requirements for the Calories Class:

1.  Insert the import java.util.Scanner class which will allow data to be inputted from the console.

 

2.  Declare the variables totalCal, avgCal, highestCal and lowestCal using the integer data type.

 

3.  Declare the calories array using the integer data type.  The array will have 7 storage locations which will store the calories burned for each day of the week.

 

4.  Declare and initialize the dayOfWeek array which will store the {"Mon","Tue","Wed","Thu","Fri","Sat","Sun"} 
 

5.  Create the Method called calcTotalCalories () that that will prompt the user to input the data and keep a running total of calories burned.

a.  Declare the scanner input class objects - Scanner inputCalories = new Scanner(System.in)

 

b.  Use the Try-Catch block statement to perform error handling. 
1.  Use a For Loop to prompt the user for the calories burned for each day of the week (Monday to Sunday)

System.out.print("Enter the number of calories burned on " + dayOfWeek[i] + " :");
caloriesArray[i] = inputCalories.nextInt();

 

2. In the For Loop, keep a running total of the calories burned

3. If the user inputs an invalid data, the code in the Catch block will be executed:

System.out.println("Invalid data entered.")

 

c.    Close the scanner input class object - inputData.close();

d.   Return the total calories burned - totalCal

 

 

6.  Create the Method called calcWeeklyAvg to calculate the weekly average. 
Accept the totalBurned as an argument from the calling object's method

a.  The formula is avgCal = totalBurned/ 7

b.  Return the avgCal value to the calling object’s method

 

7.  Create the Method called getHighestCal to get the highest calories burned during the week. 
Accept the calArray[] as an argument from the calling object's method

a.  Use the For Loop to determine the highest value in the calArray

b.  Return the highestCal value to the calling object’s method

 

8.  Create the Method called getLowestCal to get the lowest calories burned during the week. 
Accept the calArray[] as an argument from the calling object's method

a.  Initialize the lowestCal variable with the value of the first element in the calArray[]

b.  Use the For Loop to determine the lowest value in the calArray

c.   Return the lowestCal value to the calling object’s method

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


D. Requirements for the MainApp Class:

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

2.  Create the instance of object called person from the Calories class

 

3.  Call the object’s method person.calcTotalCalories() to get the total calories burned. No arguments passed
Store the returned value to the
totalCalBurned variable with a integer data type. 

4.  Call the object’s method person.calcWeeklyAvg to calculate the average calories burned. 
Pass the
totalCalBurned value
Store the returned value to the
avgCalBurned variable with a integer data type. 

 

5.  Call the object’s method person.getHighestCal to get the highest calories burned during the week.
Pass the object’s array
- person.caloriesArray
Store the returned value to the
highCalBurned variable with a integer data type

 

6.  Call the object’s method person.getLowestCal to get the lowest calories burned during the week.
Pass the object’s array
- person.caloriesArray
Store the returned value to the
lowestCalBurned variable with a integer data type

 

7.  Display the results on the console.  Below is the code

System.out.println();
System.out.println("Total calories burned: " + totCalBurned);
System.out.println("Average calories burned: " + avgCalBurned);
System.out.println("Highest calories burned: " + highCalBurned);
System.out.println("Lowest calories burned: " + lowestCalBurned);

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


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 running properly

 

 

 

 

 

 

 

 

 

 


F. Submit your exercise in the Canvas Lab Exercise #16 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 CaloriesBurned subfolder that is in the Exercise-16 folder.
NOTE: Right click on the
subfolder and select Send to “Compress Folder”.  The file will have a file extension of .zip.