Question

In: Computer Science

Program 4(Total Point 15): You will use the scanner class and ask users following things. -...

Program 4(Total Point 15): You will use the scanner class and ask users following things.

- Student Age (Value)

- Student Name (Key)

You will store information for at least 10 students on Map. You will then use iterator to print all the values. You will print the youngest student’s name.

Solutions

Expert Solution

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Scanner;

public class YoungestStudent {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        Map<String, Integer> map = new HashMap<>();
        String name;
        int age;
        for (int i = 0; i < 10; i++) {
            System.out.print("Enter name of student: ");
            name = in.nextLine();
            System.out.print("Enter age of student: ");
            age = in.nextInt();
            in.nextLine();
            System.out.println();
            map.put(name, age);
        }

        System.out.println("Students are");
        Iterator<String> iterator = map.keySet().iterator();
        int minAge = Integer.MAX_VALUE;
        String youngest = "";
        while (iterator.hasNext()) {
            name = iterator.next();
            System.out.println("Name: " + name + ", Age: " + map.get(name));
            if (map.get(name) < minAge) {
                minAge = map.get(name);
                youngest = name;
            }
        }
        System.out.println("Youngest student is " + youngest + " with an age of " + minAge);
    }
}


Related Solutions

In Java INSTRUCTIONS Write a java program called InputValidation.java that will use Scanner to ask the...
In Java INSTRUCTIONS Write a java program called InputValidation.java that will use Scanner to ask the computer user to enter his/her: • Name: a string representing the user’s first name • month of birthday: an integer representing the month of the user’s birthday • day of birthday: an integer representing the day of the user’s birthday • year of birthday: an integer representing the year of the user’s birthday Consider the following aspects that your program shall perform: VALIDATE USER’S...
Write a program that does the following things: in C++ 1 ) ask the user for...
Write a program that does the following things: in C++ 1 ) ask the user for 2 primary colors (red, blue, yellow) [should be in main] 2 ) determine the secondary color the 2 primarily colors make [should be in newColor function] red + blue = purple red + yellow = orange blue + yellow = green 3 ) Print the following message <color1> and <color2> makes <color3>
Create a program that ask to input two users and the result will vary on their...
Create a program that ask to input two users and the result will vary on their name with similar digits. In a game of F.L.A.M.E.S , it will count and repeat depends on their name that has a similar digit. For an Example (JOE RIZAL) and (JACKLYN BRACKEN) - JOE RIZAL has - 5 similar digits , while JACKLYN BRACKEN has 6 similar digits so a total of 11. F - Friends - 1,7 L - Lover - 2,8 A...
JAVA program Create a class called Array Outside of the class, import the Scanner library Outside...
JAVA program Create a class called Array Outside of the class, import the Scanner library Outside of main declare two static final variables and integer for number of days in the week and a double for the revenue per pizza (which is $8.50). Create a method called main Inside main: Declare an integer array that can hold the number of pizzas purchased each day for one week. Declare two additional variables one to hold the total sum of pizzas sold...
Make a program that calculates the total of a retail sale. The program should ask the...
Make a program that calculates the total of a retail sale. The program should ask the user for the following: the retail price of the item being purchased and the sales tax rate. Once the information has been entered, the program should calculate and display the following: the sales tax for the purchase and the total sale.
User the Scanner class for your input Write a java program to calculate the area of...
User the Scanner class for your input Write a java program to calculate the area of a rectangle. Rectangle Area is calculated by multiplying the length by the width   display the output as follow: Length =   Width = Area = Load: 1. Design (Pseudocode ) 2. Source file (Java file, make sure to include comments) 3. Output file (word or pdf or jpig file)
n-class: Write a program to store exam scores into a file. The program will ask the...
n-class: Write a program to store exam scores into a file. The program will ask the user how many exam scores to enter. Then it will ask the user for each exam score and store them in a file called scores.dat The file output should have the following format . Exam 1: 97 Exam 2: 85 C++ Coding please
3) Create a Java program that uses NO methods, but use scanner: Write a program where...
3) Create a Java program that uses NO methods, but use scanner: Write a program where you will enter the flying distance from one continent to another, you will take the plane in one country, then you will enter miles per gallon and price of gallon and in the end it will calculate how much gas was spend for that distance in miles. Steps: 1) Prompt user to enter the name of country that you are 2) Declare variable to...
Java guessing game: For this program you will use Linear Search. - Ask the user to...
Java guessing game: For this program you will use Linear Search. - Ask the user to choose between a number from 0 to 2000. - The user will guess how long the linear search will take in nanoseconds, - After the user puts in their answer, show the user the difference between the actual answer and their answer. (Example: The user puts in 5 nanoseconds. The actual time is 11 nanoseconds. The program will show 6 nanoseconds.) There should be...
You need to create a Java class library to support a program to simulate a Point-...
You need to create a Java class library to support a program to simulate a Point- of-Sale (POS) system. General Requirements: You should create your programs with good programming style and form using proper blank spaces, indentation and braces to make your code easy to read and understand; You should create identifiers with sensible names; You should make comments to describe your code segments where they are necessary for readers to understand what your code intends to achieve. Logical structures...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT