Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Here is your starter file: Potus.java DO NOT USE COMMAND LINE ARGS. I HAVE HARDCODED THE INPUT FILE NAMES INSIDE THE STARTER FILE EXECUTE LINE

Here is your starter file: Potus.java

DO NOT USE COMMAND LINE ARGS. I HAVE HARDCODED THE INPUT FILE NAMES INSIDE THE STARTER FILE

EXECUTE LINE THIS: c:> java Potus (no command line arg filenames!)

STEP #1 worth 70% SAMPLE CODE TO DO => STEP#1

Read the state2Presidents.txt file into a map such that when you print the map out the rows are sorted vertically by state and each row's set of presidents on that row are sorted alphabetically

STEP #1 output is worth 70%

Arkansas Clinton California Nixon Connecticut Bush_GW Georgia Carter Hawaii Obama Illinois Reagan Iowa Hoover Kentucky Lincoln Massachusetts Bush_GHW Kennedy Missouri Truman Nebraska Ford New_Hampshire Pierce New_Jersey Cleveland New_York Fillmore Roosevelt_F Roosevelt_T VanBuren North_Carolina Johnson_A Polk Ohio Garfield Grant Harding Harrison_B Hayes McKinley Taft Pennsylvania Buchanan Texas Eisenhower Johnson_L Vermont Arthur Coolidge Virginia Taylor Tyler Wilson

STEP #2 OUTPUT worth 15%.

Print the inverse of the state2presidents map. List each president on his own line with the state where he was born. You do not have to explicitly store this inversion into a map but you must generate the following output exactly. The lines are sorted by president alphabetically. You may Use the allPresidents.txt file in any way needed for this step but be aware the allPresidents.txt file contains all the presidents of the US - and some of them do not have a birth state associated with them since they were not born before the states were formed. You are only to list those presidents who have a birth state.

Arthur Vermont Buchanan Pennsylvania Bush_GHW Massachusetts Bush_GW Connecticut Carter Georgia Cleveland New_Jersey Clinton Arkansas Coolidge Vermont Eisenhower Texas Fillmore New_York Ford Nebraska Garfield Ohio Grant Ohio Harding Ohio Harrison_B Ohio Hayes Ohio Hoover Iowa Johnson_A North_Carolina Johnson_L Texas Kennedy Massachusetts Lincoln Kentucky McKinley Ohio Nixon California Obama Hawaii Pierce New_Hampshire Polk North_Carolina Reagan Illinois Roosevelt_F New_York Roosevelt_T New_York Taft Ohio Taylor Virginia Truman Missouri Tyler Virginia VanBuren New_York Wilson Virginia

STEP #3 OUTPUT worth 10%

Print a sorted list of presidents who have no birth state associated with them. These presidents were actually born on this continent but were born before the states were created.

Adams_J Adams_JQ Harrison_W Jackson Jefferson Madison Monroe Washington

STEP #4 worth 5%

Print a sorted list of states that have no president born in them. You may use the allStates.txt file for this.

Alabama Alaska Arizona Colorado Delaware Florida Idaho Indiana Kansas Louisiana Maine Maryland Michigan Minnesota Mississippi Montana Nevada New_Mexico North_Dakota Oklahoma Oregon Rhode_Island South_Carolina South_Dakota Tennessee Utah Washington West_Virginia Wisconsin Wyoming

Potus.java:

import java.util.*;

import java.io.*; public class Potus { public static void main( String[] args ) throws Exception { BufferedReader infile1 = new BufferedReader( new FileReader("state2Presidents.txt") ); BufferedReader infile2 = new BufferedReader( new FileReader("allPresidents.txt") ); BufferedReader infile3 = new BufferedReader( new FileReader("allStates.txt") ); // YOUR CODE HERE TO DECLARE MAPS OR SETS TO HOLD THE DATA OF THESE THREE INPUT FILES // ######### STEP #1: READ INFILE 1 INTO A MAP AND PRINT IT FORMATTED. worth 70% ########## // YOUR CODE HERE TO READ INFILE1 INTO A MAP AND PRINT IT // ######### STEP #2: PRINT THE INVERSION OF THE MAP. worth 15% ############# System.out.println(); // LEAVE THIS HERE TO PUT A BLANK LINE BETWEEN SECTIONS // YOUR CODE HERE TO PRINT THE INVERSION OF THE MAP // ############ STEP #3: PRINT THE NAMES OF PRESIDENTS BORN BEFORE STATES FORMED worth 10% ########## System.out.println(); // LEAVE THIS HERE TO PUT A BLANK LINE BETWEEN SECTIONS // YOUR CODE HERE TO PRINT THE NAMES OF ALL PRESIDENTS BORN BEFORE STATES FORMED // ###STEP #4 PRINT THE NAME(S) OF ANY STATE(s) WHICH HAD NO PRESIDENT BORN IN THEM worth 5% ####### System.out.println(); // LEAVE THIS HERE TO PUT A BLANK LINE BETWEEN SECTIONS // YOUR CODE HERE TO PRINT THE NAME(S) OF ANY STATE(s) WHICH HAD NO PRESIDENT BORN IN THEM } // END MAIN // - - - - - - - - - - - H E L P E R M E T H O D S D O W N H E R E - - - - - - - - - - } // END POTUS CLASS

state2Presidents.txt:

Virginia Tyler Taylor Wilson Ohio Grant Hayes Garfield Harrison_B McKinley Taft Harding Massachusetts Kennedy Bush_GHW New_York VanBuren Fillmore Roosevelt_T Roosevelt_F North_Carolina Polk Johnson_A Texas Eisenhower Johnson_L Vermont Arthur Coolidge Arkansas Clinton California Nixon Connecticut Bush_GW Georgia Carter Hawaii Obama Illinois Reagan Iowa Hoover Kentucky Lincoln Missouri Truman Nebraska Ford New_Hampshire Pierce New_Jersey Cleveland Pennsylvania Buchanan

Step 1 help:

// STEP #1 ASSUME A SINGLE INPUT LINE LOOKS LIKE THIS => "Ohio Grant Buchannon Tyler" TreeMap> state2Presidents = new TreeMap>(); while ( infile.ready() ) // using a BufferedReader line at a time { String[] tokens = infile.readLine().split("\\s+"); String state = tokens[0]; // tokens => [Ohio][Grant][Buchannon][Tyler] TreeSet presidents = new TreeSet(); for (int i = 1 ; i < tokens.length ; ++i ) presidents.add(tokens[i] ); state2Presidents.put( state, presidents ); // JUST ADDED A ROW TO THE TABLE } for ( String state : state2Presidents.keySet() ) // PRINT OUT A ROW FROM THE TABLE { System.out.print( state + " " ); for ( String president : state2Presidents.get(state)) System.out.print( president + " "); System.out.println(); } 

Now, what about step #2 - the inverse of the map?

You can build the inverse map inside the same chunk of code you wrote for step 1. The step 1 map is defined as mapping a state to the set of presidents born there. The step 2 map is president to the state he was born in.

Up above the step #1 code, define a TreeSet of strings called allPresidents, and define a TreeMap named president2state.

Now, inside the step 1 code after you split that line. Add each of those presidents to the allPresidents set. As you add each president to the set, add a new row to your president2state table like this:

president2state.put( president, state ); // where tokens[1], [2] .. is the president

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

Learn To Program Databases With Visual Basic 6

Authors: John Smiley

1st Edition

1902745035, 978-1902745039

More Books

Students also viewed these Databases questions

Question

=+ b. What is the per-worker production function, y = f(k)?

Answered: 1 week ago