1. Create a folder named LastName_FirstName in your local drive. (ex. Reyes_Mark)
2. Using NetBeans, create a Java project named StudentList. Set the project location to your own
folder.
3. Import Scanner, Map, and HashMap from the java.util package.
4. Create an empty hash map named students.
5. The output shall:
5.1. Ask three (3) of your classmates to enter their student number (key) and first name (value).
5.2. Display the keys and values of the map.
5.3. Delete the mapping of the third entry.
5.4. Enter your student number and first name. This would be the new third entry.
5.5. Display the entries in separate lines.
6. Create a Python script that meets the same specifications. Feel free to use lists to store input.
7. Save the script as student_list.py to your folder.
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
Map<Integer, String> students = new HashMap<>();
System.out.println("Ask three (3) of your classmates to enter their student number (key) and first name (value).");
for (int i = 0; i < 3; i++) {
students.put(Integer.parseInt(in.nextLine()), in.nextLine());
}
int i = 0;
for (Map.Entry<Integer, String> entry : students.entrySet()) {
System.out.println(entry.getKey() + " " + entry.getValue());
if (i == 2) {
students.remove(entry.getKey());
break;
}
i++;
}
System.out.println("Enter your student number and first name.");
students.put(Integer.parseInt(in.nextLine()), in.nextLine());
for (Map.Entry<Integer, String> entry : students.entrySet()) {
System.out.println(entry.getKey() + " " + entry.getValue());
}
}
}
Comments
Leave a comment