Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Need help with this app in java. California is arguably one of the best places i

ID: 3838790 • Letter: N

Question

Need help with this app in java.

California is arguably one of the best places in the country (if not the world) for hiking. Earlier in the semester, when you shared remarkable facts about yourselves, several of you expressed a fondness (or even obsession!) with hiking.

In today's in-class assignment, we are going to be developing an application that logs hiking data and computes statistics based on the data entered. We are going to utilize the HashMap data structure, built into the Java programming language. As we learned, a HashMap is a collection that consists of key-value pairs, in which the "key" is the unique identifier and the "value" is the value we want to store for that key.

In our case, we are going to be using the names of hikes (e.g. "Mount San Antonio") as the key and the value will be the number of times we have hiked that trail (e.g. 2). Specifically, we are going to create a HashMap with the following definition:

HashMap<String, Integer> hikingLog = new HashMap<String, Integer>( );

In a loop, we're going to prompt the user to enter the name of a hike they have completed and the date. The loop will continue as long as the user does not input the word "quit". For example, a sample transaction with the user might be:

~~~~~~~~~~~~~~~~~Welcome to the Hiking Log ~~~~~~~~~~~~~~~~~

Please enter name of hike completed (or "quit" to exit): Mt. San Antonio
Please enter the date of completion (MM/DD/YYYY) : 09/05/2016

Please enter name of hike completed (or "quit" to exit): Mt. San Antonio
Please enter the date of completion (MM/DD/YYYY) : 10/03/2016

Please enter name of hike completed (or "quit" to exit): Mt. Whitney
Please enter the date of completion (MM/DD/YYYY) : 10/10/2016

Please enter name of hike completed (or "quit" to exit): Top of the World
Please enter the date of completion (MM/DD/YYYY) : 10/17/2016

Please enter name of hike completed (or "quit" to exit): Mt. San Jacinto
Please enter the date of completion (MM/DD/YYYY) : 10/31/2016  

Please enter name of hike completed (or "quit" to exit): Mt. San Antonio
Please enter the date of completion (MM/DD/YYYY) : 11/07/2016

Please enter name of hike completed (or "quit" to exit): quit

~~~~~~~~~~~~~~~~~~~~~Hiking Statistics~~~~~~~~~~~~~~~~~~~~~

Unique Hikes Completed: 4
Total Hikes Completed : 6
Most Frequent Hike : Mt. San Antonio

Explanation / Answer

import java.util.Collections;
import java.util.HashMap;
import java.util.Map.Entry;
import java.util.Scanner;

public class HikingStatistics {

   /**
   * @param args
   */
   public static void main(String[] args) {

       // declaration
       Scanner scanner = null;
       try {
           // initialization
           scanner = new Scanner(System.in);
           HashMap<String, Integer> hikingLog = new HashMap<String, Integer>();
           int count = 0;

           System.out
                   .println("~~~~~~~~~~~~~~~~~Welcome to the Hiking Log ~~~~~~~~~~~~~~~~~");

           do {
               // prompt to enter input
               System.out
                       .print("Please enter name of hike completed (or "quit" to exit): ");
               String name = scanner.nextLine();

               // break if enters quit
               if (name.equals("quit"))
                   break;

               System.out
                       .print("Please enter the date of completion (MM/DD/YYYY) : ");
               String date = scanner.nextLine();

               // assign value to 1
               Integer value = new Integer(1);

               // if name contains in the map
               if (hikingLog.containsKey(name)) {
                   value = hikingLog.get(name);
                   // increment the value
                   hikingLog.put(name, ++value);
               } else {
                   hikingLog.put(name, value);
               }
               // count the number of hikes
               count++;
               System.out.println();
           } while (true);

           // get the max value from map
           int maxValueInMap = (Collections.max(hikingLog.values()));
           System.out
                   .println("~~~~~~~~~~~~~~~~~~~~~Hiking Statistics~~~~~~~~~~~~~~~~~~~~~");
           System.out.println("Unique Hikes Completed:" + maxValueInMap);
           System.out.println("Total Hikes Completed : " + count);
           System.out.print("Most Frequent Hike : ");
           // Itrate through hashmap
           for (Entry<String, Integer> entry : hikingLog.entrySet()) {
               if (entry.getValue() == maxValueInMap) {
                   // Print the key with max value
                   System.out.println(entry.getKey());
               }
           }

       } catch (Exception e) {
           // TODO: handle exception
       }

   }
}

OUTPUT:

~~~~~~~~~~~~~~~~~Welcome to the Hiking Log ~~~~~~~~~~~~~~~~~
Please enter name of hike completed (or "quit" to exit): Srinivas
Please enter the date of completion (MM/DD/YYYY) : 09/05/2016

Please enter name of hike completed (or "quit" to exit): Kishore
Please enter the date of completion (MM/DD/YYYY) : 11/03/2016

Please enter name of hike completed (or "quit" to exit): Pavan
Please enter the date of completion (MM/DD/YYYY) : 23/05/2016

Please enter name of hike completed (or "quit" to exit): Srinivas
Please enter the date of completion (MM/DD/YYYY) : 25/05/2016
value=2

Please enter name of hike completed (or "quit" to exit): quit
~~~~~~~~~~~~~~~~~~~~~Hiking Statistics~~~~~~~~~~~~~~~~~~~~~
Unique Hikes Completed:2
Total Hikes Completed : 4
Most Frequent Hike : Srinivas

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote