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 raceTimes = new HashMap(); Scanner scnr = new Scanner(System.in); int userDistKm; raceTimes.put(5, 23.14); raceTimes.put(15, 78.5); raceTimes.put(25, 120.75); System.out.println("Enter race distance in km (0 to exit): "); userDistKm = scnr.nextInt();

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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!