- This lab’s purpose is to calculate user’s pace, marathon time, and 10k time and then display it in the Eclipse console.
- User’s Name
- Recent distance he/she ran
- His/her time in hours, mins, and seconds.
- User’s name
- Marathon time
- 10k time
To Download This Code:
Go to this Link: http://pastebin.com/v2TA0Hem
-This is the same header I use for all my codes. The header contains: My name, Date, Purpose, Inputs, and Outputs. As you can see, the header is in the color green because I commented it out. To comment something in Eclipse you can use // for a line or /* commented code */ for several lines of text that is to be commented.
Line 7: Importing the Java utilities class because I will be using the Scanner Object. *note: Java has built in libraries that you can import to use.
Lines 8-11: Typical statements for a Java program. Line 8 is the class name and line 10 is the main statement that Eclipse will run from.
Line 12:Creating a Scanner object that will be used to take my inputs from the keyboard. I called this object keyboard.
Lines 14-35: Declaring multiple data type variables. I have three different data types: String, Double, Integer.
String: The first string variable is called name because this variable will store the name of the user when the program asks the user to type in their name.
Double: Marathon and ten are both declared with a value because the value is the # of miles in a marathon and 10k. The other double variables are all declared without a value because the value will be given to them later in the program.
Integer: These variables are all declared as integers because this is the variables that the program will display to the User in the form of a clock: hrs: mins: secs. Clocks do not give the time with decimals so that is why we will be using integers.
Lines 1-3: The program will output dashes on top and bottom of “What is your name?”. I added the dashes because I wanted it to be easy for user to distinguish between the question and the input user puts.
Lines 4: The program will wait until User types characters into the console. The program will then reference the String name as what the User typed in.
Lines 5: The program displays a blank line between the User’s previous input and the next question for readability.
Lines 6-8: The program will ask User how far he/she ran that day in miles.
Line 9: The program will take in a double number that User inputs and reference distance to it.
Lines 10: The program displays a blank line.
Lines 11-30: Same set of actions that the program will take like lines 1-9. However, the program will be asking the time it took the user to run their given distance that day in hours, minutes, and seconds separately and also referencing them to integer variables after each question.
Line 1: Formula for calculating the total time the User ran in minutes.
Line 2: To calculate User pace, take total minutes divided by total distance ran.
Line 3: Casting the double pace to int because we want whole mins only.
Lines 4-5: Use modulus (%) to get the remainder of the pace and then multiply by 60 to get seconds.
Line 10: To take marathon time(in minutes) of that pace, just multiply pace by marathon distance.
Lines 11-12: divine the time by 60 for hours and modulus 60 for minutes.
Lines 13-15: Remove the minutes by casting and then subtracting the total by the minutes. Then, multiply by 60 for seconds.
Lines 18-23: Repeat the same process as you did for the marathon but with the distance as the 10k.
Lines 1-4: Eclipse prints out hello and the User name. *note: name has no quotes cause it is a variable and not a string.
Lines 5-6: Prints out the User’s pace from that day and a blank line.
Lines 7-13: Prints out the User’s marathon and 10k time with that pace. Also prints out Good Luck to User; and the last two curly brackets are for closing the main and class.