1. Write a program that reads text from the text file and breaks it up into indi
ID: 3555905 • Letter: 1
Question
1. Write a program that reads text from the text file and breaks it up into individual words. Insert the words into a tree set. At the end of the input file, print all words, followed by the size of the resulting set. This program determines how many unique words a text file has.
2. Write a program that keeps a map in which both keys and values are Strings -- the names of the 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: Car: B+ Joe: C Sarah: A.
3. Reimplement Exercise 2 so that the keys of the map are objects of class Student. A student should have a first name, a last name and a unique integer ID. For grade changes and removals, lookup should be by ID. The printout should be sorted by last name. If two students have the same last name, then use the first name as a tie breaker. If the first names are also identical, then use the integer ID. Hint use two maps.
Explanation / Answer
Q1:
-----------------------------------------------------------------------------------------------------------------------------
Filename : Q1.java
rt java.io.FileReader;
import java.io.IOException;
import java.util.Iterator;
import java.util.TreeSet;
public class Q1 {
public static void main(String[] args) throws FileNotFoundException, IOException {
String filename = "input.txt";
TreeSet words = new TreeSet();
BufferedReader br = null;
br = new BufferedReader(new FileReader(filename));
String line;
while ((line = br.readLine()) != null) {
String[] words_line = line.split("\s+");
for(int i=0; i<words_line.length; ++i) {
words.add(words_line[i]);
}
}
br.close();
System.out.println("Printing all words : ");
Iterator it = words.iterator();
while (it.hasNext()) {
System.out.println(it.next());
}
System.out.println("Printing words count : " + words.size());
}
}
-----------------------------------------------------------------------------------------------------------------------------
Q2:
Filename : Q2.java
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class Q2 {
public static void main(String[] args) {
Map<String, String> map = new HashMap();
int option = 4;
while(option != 0) {
System.out.println("Choose an option : ");
System.out.println("To add a (student ,grade) pair enter '1' : ");
System.out.println("To delete a student record enter '2' : ");
System.out.println("To modify a student's grade record enter '3' : ");
System.out.println("To display all records enter '4' : ");
System.out.println("To exit enter '0' : ");
Scanner scan = new Scanner(System.in);
option = Integer.parseInt(scan.nextLine().trim());
switch(option) {
case 1:
System.out.println("Enter (student , grade) pair separated a comma : ");
String addEntry = scan.nextLine();
String[] options = addEntry.split(",");
if(options.length < 2) {
System.out.println("Error : Incorrect entry. Continuing ....");
continue;
}
map.put(options[0].trim(), options[1].trim());
break;
case 2:
System.out.println("Enter student's name : ");
String deleteEntry = scan.nextLine();
map.remove(deleteEntry.trim());
break;
case 3:
System.out.println("Enter (student , grade) pair separated a comma : ");
String updateEntry = scan.nextLine();
String[] options2 = updateEntry.split(",");
if(options2.length < 2) {
System.out.println("Error : Incorrect entry. Continuing ....");
continue;
}
if(map.get(options2[0].trim()) == null) {
System.out.println("Error : Student's record not present. Continuing ....");
continue;
}
map.put(options2[0].trim(), options2[1].trim());
break;
case 4:
System.out.println("Displaying all records : ");
for(String student : map.keySet()) {
System.out.println(student + " : " + map.get(student));
}
break;
default:
};
}
}
}
-----------------------------------------------------------------------------------------------------------------------------
Q3:
Filename : Student.java
public class Student {
public int id;
public String first_name;
public String last_name;
public Student(int arg_id, String arg_first_name, String arg_last_name) {
id = arg_id;
first_name = arg_first_name;
last_name = arg_last_name;
}
}
-------------------------------------------------------------------------------------------------
Filename : StudentComparatorId.java
import java.util.Comparator;
public class StudentComparatorId implements Comparator<Student>{
@Override
public int compare(Student s1, Student s2) {
if(s1.id < s2.id)
return -1;
if(s1.id > s2.id)
return 1;
else {
if(s1.last_name.compareTo(s2.last_name) < 0)
return -1;
if(s1.last_name.compareTo(s2.last_name) > 0)
return 1;
else {
if(s1.first_name.compareTo(s2.first_name) < 0)
return -1;
if(s1.first_name.compareTo(s2.first_name) > 0)
return 1;
else
return 0;
}
}
}
}
-------------------------------------------------------------------------------------------------
Filename : StudentComparatorLastName.java
import java.util.Comparator;
public class StudentComparatorLastName implements Comparator<Student>{
@Override
public int compare(Student s1, Student s2) {
if(s1.last_name.compareTo(s2.last_name) < 0)
return -1;
if(s1.last_name.compareTo(s2.last_name) > 0)
return 1;
else {
if(s1.first_name.compareTo(s2.first_name) < 0)
return -1;
if(s1.first_name.compareTo(s2.first_name) > 0)
return 1;
else {
if(s1.id < s2.id)
return -1;
if(s1.id > s2.id)
return 1;
else
return 0;
}
}
}
}
-------------------------------------------------------------------------------------------------
Filename : Q3.java
import java.util.Map;
import java.util.Scanner;
import java.util.TreeMap;
public class Q3 {
public static void main(String[] args) {
Map<Student, String> mapId = new TreeMap(new StudentComparatorId());
Map<Student, String> mapLastName = new TreeMap(new StudentComparatorLastName());
int option = 4;
while(option != 0) {
System.out.println("Choose an option : ");
System.out.println("To add a (student ,grade) pair enter '1' : ");
System.out.println("To delete a student record enter '2' : ");
System.out.println("To modify a student's grade record enter '3' : ");
System.out.println("To display all records enter '4' : ");
System.out.println("To exit enter '0' : ");
Scanner scan = new Scanner(System.in);
option = Integer.parseInt(scan.nextLine().trim());
switch(option) {
case 1:
System.out.println("Enter (student_id , student_first_name, student_last_name, student_grade) separated a comma : ");
String addEntry = scan.nextLine();
String[] addOptions = addEntry.split(",");
if(addOptions.length < 4) {
System.out.println("Error : Incorrect entry. Continuing ....");
continue;
}
mapId.put( new Student( Integer.parseInt(addOptions[0].trim()), addOptions[1].trim() , addOptions[2].trim()) , addOptions[3].trim());
mapLastName.put( new Student( Integer.parseInt(addOptions[0].trim()), addOptions[1].trim() , addOptions[2].trim()) , addOptions[3].trim());
break;
case 2:
System.out.println("Enter (student_id , student_first_name, student_last_name) separated a comma : ");
String deleteEntry = scan.nextLine();
String[] deleteOptions = deleteEntry.split(",");
if(deleteOptions.length < 3) {
System.out.println("Error : Incorrect entry. Continuing ....");
continue;
}
mapId.remove(new Student( Integer.parseInt(deleteOptions[0].trim()), deleteOptions[1].trim() , deleteOptions[2].trim()));
mapLastName.remove(new Student( Integer.parseInt(deleteOptions[0].trim()), deleteOptions[1].trim() , deleteOptions[2].trim()));
break;
case 3:
System.out.println("Enter (student_id , student_first_name, student_last_name, student_grade) separated a comma : ");
String updateEntry = scan.nextLine();
String[] updateOptions = updateEntry.split(",");
if(updateOptions.length < 4) {
System.out.println("Error : Incorrect entry. Continuing ....");
continue;
}
mapId.put( new Student( Integer.parseInt(updateOptions[0].trim()), updateOptions[1].trim() , updateOptions[2].trim()) , updateOptions[3].trim());
mapLastName.put( new Student( Integer.parseInt(updateOptions[0].trim()), updateOptions[1].trim() , updateOptions[2].trim()) , updateOptions[3].trim());
break;
case 4:
System.out.println("Displaying all records : ");
for(Student student : mapLastName.keySet()) {
System.out.println("Id : " + student.id + " First Name : " + student.first_name + " Last Name : " + student.last_name + " --> " + mapLastName.get(student));
}
break;
default:
};
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.