COMP1210 Activity2-Data and Expressions Solved

30.00 $

Category:

Description

5/5 - (1 vote)

Terminology:

string literal

method parameter string concatenation escape sequence variable variable declaration variable initialization

assignment statement assignment operator constant

byte, short, int, long float, double char boolean

arithmetic operator operator precedence

increment, decrement operators assignment conversion promotion casting delimiter

character literal

 

Course Coding Standard Additions

  • Follow the standards in the book when naming variables and constants.
  • Class names should begin with a capital letter, and method names and variables should start with a lower case. Both should use medial capitals (also called “camelCase”. Examples:

FirstProgram, HelloWorld, main, numberPeople, xAxis, result, finalResult o Local variables should be declared at the beginning of the method in which they are used.

  • Constants should be in all caps with words separated by an underscore. In addition to the type, a constant must be declared as static final (e.g., static final int MAX_NUMBER).

Constants must be placed at the class level (e.g., before the first method in the class).

Examples: MAX_NUMBER, BASELINE, TAX_RATE

  • Both variables and constants should have descriptive variable names. This convention is not enforced by Checkstyle; however, it will be graded by your Instructor/GTA in the projects. Example:

Good Jint numberJobs, numEmployees;

Bad Lint n, m;  

 

Goals:

By the end of this activity you should be able to do the following: Ø Get user input using the Scanner class

  • Create variables and concatenate variables with a string
  • Use arithmetic operators to manipulate integer and double data types
  • Use a basic if statement to determine which output is appropriate
  • Run your program in a viewer canvas , including creating and saving the canvas file

 

Program Description:

The goal of this activity is to create a program that displays someone’s name, age, and gender, along with calculations for age in minutes and centuries well as max heart rate based on age and gender.

 

Directions:  After you enter each segment of code below, be sure to compile and/or run your program.  You should correct any errors prior to entering additional code.  

Part 1: Getting user’s name and age

  • Create the following class in jGRASP and save it as AgeStatistics.java as soon as you have entered the skeleton code for the class.

 

  • Add Javadoc comments for the class and main method.

 

  • Declare the following variables in the main method then compile your program:
  • Add lines to prompt for and input the user’s gender after the previous code. Make sure that your program has the EXACT output as below.

 

—-jGRASP exec: java -ea AgeStatistics

Enter your name: Pat

Enter your age in years: 71

Enter your gender (1 for female and 0 for male): 1

 

—-jGRASP: operation complete.

 

 

Part 2: Converting age

  • First, calculate and print the age of the user in minutes (minutes = years * 525600). Note that the tab escape sequence is included in the print statement; tab character is set to three spaces in jGRASP.

 

  • Now calculate the age in centuries. You should be familiar with type conversion between numeric types.

 

 

 

—-jGRASP exec: java -ea AgeStatistics

Enter your name: Pat

Enter your age in years: 71

Enter your gender (1 for female and 0 for male): 1

Your age in minutes is 37317600 minutes.

Your age in centuries is 0.71 centuries.

 

—-jGRASP: operation complete.

 

 

Part 3: Max heart rate prompt

  • Add the code to print max heart rate (note the print rather than println):

 

  • Add an if-else statement that will be used to assign maxHeartRate to the max heart rate calculation for male and female. The comments below are there to help you in the Part 4

where you will insert the code to do the calculations on the line after each comment.

 

  • Add one more print statement to print the max heart rate:

 

Part 4: Max heart rate calculations

 

  • On your own, complete the if-else statement by adding an assignment statement for female and another for male to assign the appropriate expression below to the variable maxHeartRate.

 

The maximum heart rate is calculated as follows:

  • Max heart rate for females:

209 – (0.7 * ageInYears)

 

  • Max heart rate for males:

214 – (0.8 * ageInYears)

 

Test your program and make sure that the output is exactly as shown below. Your lab instructor may ask you to use a different set of values to test your program when you demo it for your activity grade.

 

Output # 1:

—-jGRASP exec: java -ea AgeStatistics

Enter your name: Lucy

Enter your age in years: 25

Enter your gender (1 for female and 0 for male): 1

Your age in minutes is 13140000 minutes.

Your age in centuries is 0.25 centuries.

Your max heart rate is 191.5 beats per minute.

 

—-jGRASP: operation complete.

 

 

Output # 2:

—-jGRASP exec: java -ea AgeStatistics

Enter your name: Kao

Enter your age in years: 19

Enter your gender (1 for female and 0 for male): 0

Your age in minutes is 9986400 minutes.

Your age in centuries is 0.19 centuries.

Your max heart rate is 198.8 beats per minute.

 

—-jGRASP: operation complete.

 

 

 

 

Web-CAT – After you have completed Part 4, you should submit your AgeStatistics.java file to Web-CAT.  This is the only file you will submit for this activity.

Part 5: Using the jGRASP Viewer Canvas

 

In this part, you will run AgeStatistics.java in a viewer canvas.  In the process, you will create visualization of your program that can provide insight into what exactly happens when the program executes (runs).  Although AgeStatistics.java is a simple program, this exercise is intended to introduce you to viewer canvas so that you’ll know how to use it when you need to debug more complicated programs.  For more information on using the canvas see Section 2.10 in Getting Started with jGRASP 2.0 (Shift-click to open the link in separate browser window).                http://www.jgrasp.org/tutorials200/jGRASP_02_Getting_Started.pdf

 

  • With AgeStatistics.java in the CSD edit window, click the Run in Canvas button on the toolbar.  You should see an empty viewer canvas window as shown below.  In the CSD window, you should see that the program is stopped at the first executable statement:

Scanner userInput = new Scanner(System.in);

 

  • As indicated by the message in the canvas, click the debug step button to execute the statement to create a Scanner object on System.in which is the keyboard. You should now see the variable name userInput in the Debug tab.  Now select and drag userInput from the Debug tab onto the canvas (i.e., left-click the mouse on userInput and hold as you drag it onto the canvas).   You should now see a viewer for userInput in the canvas.

This will show the stream of characters coming in from the keyboard after you press ENTER.

 

  • four more times so that you see name, ageInYears,

gender, and maxHeartRate in the Debug tab.  Now drag each of these variables into the

canvas window and arrange them as shown in the figure below.  Click the Save button  on the canvas toolbar.  You have now constructed and saved your first canvas.

 

 

  • To complete the run in canvas, you can continue to click the step button and enter the required input (name, ageInYears, etc.), or alternatively, since the canvas has all the viewers we need, you can click the play button  (which means auto step-in) on the canvas.  After the run in canvas ends, close the canvas window.  Now click the run in canvas button   on the toolbar as you did in the first step above.  Your saved canvas file should open in the canvas window.  Click the play button , and your program will begin auto-stepping until is pauses waiting for input.  After you enter the requested input each time, the viewers on canvas will be updated.  The run in canvas will continue until your program terminates at which time the viewers on the canvas will display their last values as shown in the example below.  Note that the Scanner userInput indicates that “18” was entered followed by a press of the Enter key (which produces the escape characters \r\n on Windows or \n on Mac OS X or Linux), then “1” was entered followed by a press of the Enter key.  The green arrowhead indicates that the “1” has been read but not the escape character(s) \r\n or \n.

 

  • Activity_02-da3tt6.zip