Question: This program uses a HashMap to associate a race distance in kilometers with a runner's race time. If a race distance does not exist, the
This program uses a HashMap to associate a race distance in kilometers with a runner's race time. If a race distance does not exist, the program prints "null". Modify the program to use the containsKey() method to check if the runner has run a race of the specified distance, and display a message of "No race of the specified distance exists."
import java.util.HashMap; import java.util.Scanner;
public class RunDistTimeMap { public static void main (String[] args) { HashMap
while(userDistKm != 0) {
System.out.print("Best time for " + userDistKm + " km race is: "); System.out.print(raceTimes.get(userDistKm)); System.out.println(" minutes."); System.out.println(); System.out.println("Enter race distance in km (0 to exit): "); userDistKm = scnr.nextInt(); } } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
