Question

In: Computer Science

Create JAVA PROGRAM, and write comment for codes also. 3) You want to keep track of...

Create JAVA PROGRAM, and write comment for codes also.

3) You want to keep track of your progress towards running a 10K. There are two kinds of races - 5K and 10K. The program needs to ask what race was run and what the time was in seconds until the user quits. When they quit, display the average and best time for each type of race in minutes.

Solutions

Expert Solution

Thanks for the question.


Here is the completed code for this problem. 

Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. 

If you are satisfied with the solution, please rate the answer. Thanks


===========================================================================

import java.util.Scanner;

public class Race {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);
        
        // variables needed
        int total5KSeconds = 0;    // total 5k seconds for all races
        int total10KSeconds = 0; // total 10k seconds for all races
        int total5KRuns = 0, total10KRuns = 0; // how many 5k and 10k races to calculate average
        int raceTime = 0; // current race time
        int choice = 0; // user select 0 to quit 1 to enter 5k race 2 for 10k race times
        int bestTime5K = 0, bestTime10K = 0; // trakc the best time for 5k and 10k races
        
        // keep asking until user enters 0 to quit
        while (true) {
            System.out.println("Enter [1] - 5000 km ");
            System.out.println("Enter [2] - 10,000 km ");
            System.out.println("Enter [0] - exit ");
            choice = scanner.nextInt();

            if (choice == 1) {
                System.out.print("Enter time (in seconds) to complete 5,000 km race: ");
                raceTime = scanner.nextInt();
                total5KSeconds += raceTime;
                total5KRuns += 1;
                // when the user enters the time for 5K first time update
                // the best time to the racetime
                if (total5KRuns == 1) bestTime5K = raceTime;
                else {
                    //// update the best time to the least time
                    if (bestTime5K > raceTime) bestTime5K = raceTime;
                }
            } else if (choice == 2) {
                System.out.print("Enter time (in seconds) to complete 10,000 km race: ");
                raceTime = scanner.nextInt();
                total10KSeconds += raceTime;
                total10KRuns += 1;
                // when the user enters the time for 10K first time update
                // the best time to the racetime
                if (total10KRuns == 1) bestTime10K = raceTime;
                else {
                    // update the best time to the least time
                    if (bestTime10K > raceTime) bestTime10K = raceTime;
                }
            } else if (choice == 0) {
                break;
            } else {
                System.out.println("Invalid choice. Please choose either 1 or 2 or 0. Try again!");
            }
        }
        
        // prints the best time and average time for both 5k and 10k races
        if (total5KRuns == 0) {
            System.out.println("No data for 5,000 km race");
        } else {
            System.out.println("5,000 km race stats - ");
            System.out.println("Best time    : " + (bestTime5K / 60) + " minutes");
            System.out.println("Average time : " + (total5KSeconds / (60 * total5KRuns)) + " minutes");
        }
        System.out.println();
        if (total10KRuns == 0) {
            System.out.println("No data for 10,000 km race");
        } else {
            System.out.println("10,000 km race stats - ");
            System.out.println("Best time    : " + (bestTime10K / 60) + " minutes");
            System.out.println("Average time : " + (total10KSeconds / (60 * total10KRuns)) + " minutes");
        }
    }
}

========================================================================

thank you, please do upvote : )

thanks a lot !!


Related Solutions

Create JAVA PROGRAM, and write comment for codes also. 4) Prompt the user for how much...
Create JAVA PROGRAM, and write comment for codes also. 4) Prompt the user for how much stuff a truck can carry, in pounds. Then ask them for the weight of each thing they add until they stop. Don't forget to be safe.
Create JAVA PROGRAM, and write comment for codes also. 1) Let the user enter in candidate...
Create JAVA PROGRAM, and write comment for codes also. 1) Let the user enter in candidate names for a ballot. Then imagine the program is moved to a voting booth. Each voter enters their unique name and who they vote for, and when there are no more voters display who won. (Imagine something outside your program is telling it there are no more voters.)
Create JAVA PROGRAM, and write comment for codes also. 2) Every extra 3500 calories means gaining...
Create JAVA PROGRAM, and write comment for codes also. 2) Every extra 3500 calories means gaining a pound. Falling short by 3500 means losing a pound. Get the base amount of calories the person needs in a day (BMR). Then have the user enter in calorie totals for each day and display their weight every week. They might eat more than once in a day.
Write a Java program that lets the user keep track of their homemade salsa sales. Use...
Write a Java program that lets the user keep track of their homemade salsa sales. Use 5-element arrays to track the following. The salsa names mild, medium, sweet, hot, and zesty. The number of each type sold. The price of each type of salsa. Show gross amount of money made (before tax). Calculate how much the user owes in sales tax (6% of the amount made). Then display the net profit (after subtracting the tax).
You are going to create a console based program to keep track of a small business...
You are going to create a console based program to keep track of a small business that sells Doodads. First you will need to create a class Doodad that keeps track of two integers and two Strings. Next, create a constructor for the Doodad. Next, add getters and setters for each of the fields (two integers and two Strings). You need to use Doodad.java (see the starter code) Inside your main method ask the user to read in the two...
Create a C program that performs the following (please comment the codes): a) Create a Stack...
Create a C program that performs the following (please comment the codes): a) Create a Stack ADT. Stack should be implemented using the linked list. b) Enter 10 random integer numbers between 0 to 50 in the stack. c) After pushing each element, print the content of the top of the stack. c) Then pop out those 10 integer numbers and print those numbers. d) Finally destroy the Stack.
USE C++ and please keep program as simple as possible and also comment so it is...
USE C++ and please keep program as simple as possible and also comment so it is easy to understad Create a structure called time. Its three members, all type int, should be called hours, minutes, and seconds. Write a program that prompts the user to enter a time value in hours, minutes, and seconds. This should be in 12:59:59 format. This entire input should be assigned first to a string variable. Then the string should be tokenized thereby assigning the...
Write a c++ program for the Sales Department to keep track of the monthly sales of...
Write a c++ program for the Sales Department to keep track of the monthly sales of its salespersons. The program shall perform the following tasks: Create a base class “Employees” with a protected variable “phone_no” Create a derived class “Sales” from the base class “Employees” with two public variables “emp_no” and “emp_name” Create a second level of derived class “Salesperson” from the derived class “Sales” with two public variables “location” and “monthly_sales” Create a function “employee_details” under the class “Salesperson”...
ASSIGNMENT: Write a program to keep track of the total number of bugs collected in a...
ASSIGNMENT: Write a program to keep track of the total number of bugs collected in a 7 day period. Ask the user for the number of bugs collected on each day, and using an accumulator, keep a running total of the number of bugs collected. Display the total number of bugs collected, the count of the number of days, and the average number of bugs collected every day. Create a constant for the number of days the bugs are being...
Accounting Program in c++ Write a class to keep track of a balance in a bank...
Accounting Program in c++ Write a class to keep track of a balance in a bank account with a varying annual interest rate. The constructor will set both the balance and the interest rate to some initial values (with defaults of zero). The class should have member functions to change or retrieve the current balance or interest rate. There should also be functions to make a deposit (add to the balance) or withdrawal (subtract from the balance). You should not...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT