Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

JAVA create a class `People` that will store a table of people's first names and ages (using a `HashMap` , the people's names as the

JAVA create a class `People` that will store a table of people's first names and ages (using a `HashMap`, the people's names as the key, and integer ages as the value). Read people and ages from the console (e.g. `Bob 19`) and add to the `HashMap`. For this example, assume ages are integers and names do not contain spaces. After a list of name, age pairs, a name will be entered without an age (e.g. `Mary`) and the user will terminate their input by typing `ctrl-D`, by pressing the control and 'D' keys (`ctrl-Z` on Windows). Treat the last name (`Mary` in this example) as a query. Print out the age of the person entered last, or print `unknown` if the person's name is unknown (i.e. it did not appear earlier in the list). 

The test file :

 import java.io.ByteArrayInputStream; import java.nio.charset.StandardCharsets; import java.util.HashMap; import java.util.InputMismatchException; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.NoSuchElementException; import java.util.Random; import org.junit.Assert; import org.junit.Test; public class PeopleTest extends StdIOTest { private String[] names = new String[]{"Fred", "Sarah", "John", "Mark", "Alice", "Robert", "Marvin", "Patricia"}; private String unknown = "Sue"; public PeopleTest() { } private HashMap genRandomMap(int size) { Random r = new Random(); List n = Shuffle.shuffleStrings(this.names, size); HashMap ages = new HashMap(); Iterator var5 = n.iterator(); while(var5.hasNext()) { String name = (String)var5.next(); ages.put(name, 2 + r.nextInt(98)); } return ages; } private void testN(int n, boolean known) { Map map = this.genRandomMap(n); String key = ""; if (known) { Iterator var5 = map.keySet().iterator(); if (var5.hasNext()) { String s = (String)var5.next(); key = s; } } else { key = this.unknown; } this.test(map, key); } private void test(Map people, String lookup) { try { this.outContent.reset(); this.errContent.reset(); String input = ""; String n; for(Iterator var4 = people.keySet().iterator(); var4.hasNext(); input = input + n + " " + people.get(n) + System.lineSeparator()) { n = (String)var4.next(); } input = input + lookup + System.lineSeparator(); System.setIn(new ByteArrayInputStream(input.getBytes(StandardCharsets.UTF_8))); People.main((String[])null); String output = this.cleanupCRLF(this.outContent.toString(), false); n = (people.containsKey(lookup) ? Integer.toString(((Integer)people.get(lookup)).intValue()) : "unknown") + System.lineSeparator(); n = this.cleanupCRLF(n, false); Assert.assertTrue("Incorrect output for input " + System.lineSeparator() + "---" + System.lineSeparator() + input + "---" + System.lineSeparator() + "expected: " + System.lineSeparator() + "---" + System.lineSeparator() + n + "---" + System.lineSeparator() + "got: " + System.lineSeparator() + "---" + System.lineSeparator() + output + "---" + System.lineSeparator(), output.equals(n)); } catch (InputMismatchException var6) { Assert.assertTrue("Unexpected output", false); } catch (NoSuchElementException var7) { Assert.assertTrue("No output", false); } } @Test public void testOne() { this.testN(1, true); } @Test public void testOneUnknown() { this.testN(1, false); } @Test public void testTwo() { this.testN(1, true); } @Test public void testTwoUnknown() { this.testN(1, false); } @Test public void testRandom() { Random r = new Random(); for(int i = 0; i < 10; ++i) { this.testN(1 + r.nextInt(this.names.length - 1), r.nextBoolean()); } } } 

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

Intelligent Information And Database Systems 12th Asian Conference ACIIDS 2020 Phuket Thailand March 23 26 2020 Proceedings

Authors: Pawel Sitek ,Marcin Pietranik ,Marek Krotkiewicz ,Chutimet Srinilta

1st Edition

9811533792, 978-9811533792

More Books

Students also viewed these Databases questions

Question

1. Have you ever taken any digital marketing courses?

Answered: 1 week ago

Question

What is focal length? Explain with a diagram and give an example.

Answered: 1 week ago

Question

What is physics and how does it apply in daily life?

Answered: 1 week ago

Question

=+5 Evaluate whether the CCT program was effective.

Answered: 1 week ago

Question

=+Identify the type of global assignment for which CCT is needed.

Answered: 1 week ago