Question:
Java Programming Help?
?
2012-07-11 19:46:06 UTC
Write a program that keeps a map in which both keys and values are strings—the
names of students and their course grades. Prompt the user of the program to add
or remove students, to modify grades, or to print all grades. The printout should be
sorted by name and formatted like this:
Carl: B+
Joe: C
Sarah: A


I cannot figure out how to even start this... :(
Four answers:
?
2012-07-11 19:56:34 UTC
Here's how you start

SortedMap students = new TreeMap();



I will try to whip something up really quick.

http://ideone.com/wN5dn



import java.util.*;



public class ClassRoster {

SortedMap students;

Scanner scan;

ClassRoster(){

scan = new Scanner(System.in);

students = new TreeMap();

int choice;

while ((choice = displayMenu())!=5){

switch(choice){

case 1:

case 2:

addStudent();

break;

case 3:

removeStudent();

break;

case 4:

printList();

break;

case 5:

return;

default:

System.out.println("Bad Option");



}

}

}

public int displayMenu(){

System.out.println("\n");

System.out.println("1. Add Student");

System.out.println("2. Change grade");

System.out.println("3. Remove Student");

System.out.println("4. Print list");

System.out.println("5. Exit");

return scan.nextInt();



}

public void addStudent(){

System.out.print("Name of Student: ");

String name = scan.next();

System.out.print("Enter Letter Grade: ");

String grade = scan.next();

students.put(name, grade);

}



public void removeStudent(){

System.out.print("Name of Student: ");

String name = scan.next();

students.remove(name);

}



public void printList(){

Iterator it = students.keySet().iterator();

while (it.hasNext()) {

String name = it.next().toString();

String grade = students.get(name).toString();



System.out.println(name + "\t:\t" + grade);

}

}

public static void main (String[] args) {

new ClassRoster();

}

}
Bob
2012-07-11 20:01:54 UTC
You want to look at the TreeMap class to achieve this... the methods of interest are...

put(String student, String grade) to add/modify grade and add student

remove(String student) to remove student



TreeMap handles sorting the students... if you didn't care about sorting and just wanted speed I would go with HashMap... but since you're printing out, why not add some formatting :P



Here's a a demo on how to use it:



import java.util.Map;

import java.util.TreeMap;



class Whatever {

public static void main(String[] args) {

//Create an instance and say student/grade are strings aka key/value types

Map students = new TreeMap();

//Let's say the user inputs Kazuma with a grade of A

students.put("Kazuma", "A");

//Wait, never mind, modify it to A+

students.put("Kazuma", "A+");

//Add another student

students.put("Robert", "C");

//Robert dropped out...

students.remove("Robert");

//To print output... read as for each student in students do

for (String student : students.keySet()) {

System.out.println(student + ": " + students.get(student)); // Prints out Kazuma: A+

}

}

}
galt_57
2012-07-11 19:58:19 UTC
Begin by reading your textbook and create a program that can write strings to a text file or read strings from a text file. Then look up the String.split function. The split function can break each line in the file into individual fields such as name and grade.
?
2016-10-16 08:28:09 UTC
Why roll your very own whilst there is in all probability a calendar widget you may reuse? via the way, the Java API itself grants plenty smart instructions and interfaces: Date, Calendar, GregorianCalendar, DateFormat, and SimpleDateFormat.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...