Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

1. Rewrite this following code using text file to save data. (Welcome to CECS277) package Pk; import java.util.*; import java.util.Map.Entry; public class Main { static

1. Rewrite this following code using text file to save data. (Welcome to CECS277)

package Pk;

import java.util.*;

import java.util.Map.Entry;

public class Main {

static Map < Character, Integer > m = new HashMap < Character, Integer > ();

public static void main(String[] args) {

// Declaring variables

String str;

/*

* Creating an Scanner class object which is used to get the inputs

* entered by the user

*/

Scanner sc = new Scanner(System.in);

// Getting the input entered by the user

System.out.print("Enter the String :");

str = sc.nextLine();

char ch;

// Populating each character into the Map

for (int i = 0; i < str.length(); i++) {

ch=str.charAt(i);

if(Character.isAlphabetic(ch))

{

checkMap(Character.toLowerCase(ch));

}

}

Set < Entry < Character, Integer >> set = m.entrySet();

List < Entry < Character, Integer >> list = new ArrayList < Entry < Character, Integer >> (set);

// Sorting based on values

Collections.sort(list, new Comparator < Map.Entry < Character, Integer >> () {

public int compare(Map.Entry < Character, Integer > o1,Map.Entry < Character, Integer > o2) {

return (o2.getValue()).compareTo(o1.getValue());

}

});

System.out.println("Map contains: ");

System.out.println("Sort by values ");

System.out.println("Key\tValue");

// Displaying the Key value Pairs

for (Map.Entry < Character, Integer > entry: list) {

System.out.println(entry.getKey() + " \t " + entry.getValue());

}

// Sorting based on keys

Collections.sort(list, new Comparator < Map.Entry < Character, Integer >> () {

public int compare(Map.Entry < Character, Integer > o1,Map.Entry < Character, Integer > o2) {

return (o1.getKey()).compareTo(o2.getKey());

}

});

System.out.println("Sort by keys ");

System.out.println("Key\tValue");

// Displaying the Key value Pairs

for (Map.Entry < Character, Integer > entry: list) {

System.out.println(entry.getKey() + " \t " + entry.getValue());

}

}

private static void checkMap(char c) {

// Checking whether the word is available in the HashMap

if (m.containsKey(c)) {

m.put(c, m.get(c) + 1);

} else {

// If not available Adding the word to the HashMa

m.put(c, 1);

}

}

}

2. [20 points] Write a program that keeps a map in which the keys of the map are objects of class Student. An student should have a first name, a last name, and a unique integer student identification. For the class grading (A, B, C, D, and F) changes and removals, lookup should be by student identification. Prompt the user of the program to add or remove students, to modify the grade, or to print all the grades including student names. 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 student identification. Supply compatible hashCode and equals methods to the Student class. Test the hash code by adding the student objects to a hash set.

Implementing the following methods:

public static printMenuAndGetChoice()

/** Adds a student based on user input. Prints an error if a student is added that already exists in the map. @param GradeMap the map to associate student object with a grade @param StudentMap the map to associate student id with an student. */

/** Removes a student from the map based on user input @param GradeMap the map to associate student object with a grade. @param StudentMap the map to associate student id with an student. */

/** Modifies an entry based on user input. Prints an error if an invalid student is modified. @param GradeMap the map to associate student object with a grade. @param StudentMap the map to associate student id with an student */

/** Prints the students and grades @param GradeMap the map to print */

Hint:

Use two maps.

Calculates a hashcode by combining the hashcodes of the instance variables. @return a hashcode dependant on the instance variables */ public int hashCode() { final int HASH_MULTIPLIER = 29; int h = HASH_MULTIPLIER * firstName.hashCode() + lastName.hashCode(); h = HASH_MULTIPLIER * h + ((Integer)id).hashCode(); return h; }

3. [5 points] Provide options to save and load the file that stores two Map objects in the problem 2.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Design Application Development And Administration

Authors: Michael V. Mannino

3rd Edition

0071107010, 978-0071107013

Students also viewed these Databases questions

Question

What is a confidence interval?

Answered: 1 week ago

Question

Have roles been defined and assigned?

Answered: 1 week ago

Question

Are these written ground rules?

Answered: 1 week ago

Question

How do members envision the ideal team?

Answered: 1 week ago