Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write the body of the following static method. It has the exact same contract as

ID: 3858868 • Letter: W

Question

Write the body of the following static method. It has the exact same contract as the previous one, but uses only standard Java components, including java.util.Map<String, Integer>.

/**

* Raises the salary of all the employees in {@code map} whose name starts

* with the given {@code initial} by the given {@code raisePercent}.

*

* @param map

*            the name to salary map

* @param initial

*            the initial of names of employees to be given a raise

* @param raisePercent

*            the raise to be given as a percentage of the current salary

* @updates map

* @requires <pre>

* [the salaries in map are positive] and raisePercent > 0 and

* [the dynamic types of map and of all objects reachable from map

* (including any objects returned by operations (such as entrySet() and,

* from there, iterator()), and so on, recursively) support all

* optional operations]

* </pre>

* @ensures <pre>

* DOMAIN(map) = DOMAIN(#map) and

* [the salaries of the employees in map whose names start with the given

* initial have been increased by raisePercent percent (and truncated to

* the nearest integer); all other employees have the same salary]

* </pre>

*/

private static void giveRaise(java.util.Map<String, Integer> map,

        char initial, int raisePercent) {...}

Explanation / Answer

Below is your program. I have added a simple main function also to test it.

import java.util.HashMap;

import java.util.Map;

public class EmployeeMap {

/**

* Raises the salary of all the employees in {@code map} whose name starts

* with the given {@code initial} by the given {@code raisePercent}.

*

* @param map

* the name to salary map

* @param initial

* the initial of names of employees to be given a raise

* @param raisePercent

* the raise to be given as a percentage of the current salary

* @updates map

* @requires

*

* <pre>

* [the salaries in map are positive] and raisePercent > 0 and

* [the dynamic types of map and of all objects reachable from map

* (including any objects returned by operations (such as entrySet() and,

* from there, iterator()), and so on, recursively) support all

* optional operations]

* </pre>

*

* @ensures

*

* <pre>

* DOMAIN(map) = DOMAIN(#map) and

* [the salaries of the employees in map whose names start with the given

* initial have been increased by raisePercent percent (and truncated to

* the nearest integer); all other employees have the same salary]

* </pre>

*/

private static void giveRaise(java.util.Map<String, Integer> map, char initial, int raisePercent) {

int salary;

String name;

for (Map.Entry<String, Integer> entry : map.entrySet()) {

name = entry.getKey();

if (name.charAt(0) == initial) {

salary = entry.getValue();

salary = salary + new Double(Math.round(raisePercent * salary / 100.0)).intValue();

entry.setValue(salary);

}

}

}

public static void main(String[] args) {

Map<String, Integer> emps = new HashMap<>();

emps.put("Salil", 10000);

emps.put("Ricky", 12000);

emps.put("Sanjay", 95111);

emps.put("Vijay", 20000);

emps.put("Gosai", 10050);

System.out.println("Inital map entries: ");

for (Map.Entry<String, Integer> entry : emps.entrySet()) {

System.out.println("Name: " + entry.getKey() + " , Salary: " + entry.getValue());

}

giveRaise(emps, 'S', 22);

System.out.println(" Updated map entries: ");

for (Map.Entry<String, Integer> entry : emps.entrySet()) {

System.out.println("Name: " + entry.getKey() + " , Salary: " + entry.getValue());

}

}

}

Sample run: -

Inital map entries:
Name: Vijay , Salary: 20000
Name: Salil , Salary: 10000
Name: Ricky , Salary: 12000
Name: Sanjay , Salary: 95111
Name: Gosai , Salary: 10050

Updated map entries:
Name: Vijay , Salary: 20000
Name: Salil , Salary: 12200
Name: Ricky , Salary: 12000
Name: Sanjay , Salary: 116035
Name: Gosai , Salary: 10050