Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.Color;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.Hashtable;
- import java.util.Random;
- import java.util.Scanner;
- /*
- File: OMcDonaldPrjDriver.java
- Date: Jan 2, 2017
- Time: 10:24:13 AM
- Programmer: kentpendleton
- Project: OMDSagaPrj-S0
- Location (Option-Return copy/paste location):
- /Users/kentpendleton/Dropbox/Java/Projects/OldMacDonaldSaga/OldMacDonaldSaga2017/
- * OMDSagaPrj-S0/src/OMcDonaldPrjDriver.java
- Program Description:
- Ultimately, print the words to the song: "Old MacDonald"
- First we must get a list of animals and sounds
- Requirements/Sample Output:
- 0. Use the Solution to Exercise 2 as a base
- 1. You have a 'corrupted' version of 'farmAnimals.txt' in this project
- 2. Open it and determine what the problems with the entries are.
- 2.5 Run the project and notice the output: anything wrong?
- 3. WITHOUT CHANGING THE TEXT FILE, make the necessary corrections/modifications to get the output shown
- 4. Make sure you include the numbers next to the entries (and the total count in the heading) so you can count them more easily
- 4. What do you conclude about the 'coolness' of the HashMap?
- Known Issues:
- Future Development:
- */
- @SuppressWarnings("unused")
- public class OMcDonaldPrjDriver {
- private static OMcDonaldPrjDriver myDriverObj = new OMcDonaldPrjDriver(); //used to get className for termination statement
- private static final Scanner KB = new Scanner(System.in);
- //adjust app heading variables here
- public static final String APP_TITLE = "Old Mac Donald Saga";
- private static final String STAGE_NO_STR = "3 Soln"; //stageNo as a String (ex: "1", "5c" etc)
- private static final String CURRENT_JAR = "Java Archive: JavaTNT8-29-16.jar";
- private static final char BORDER_SYMBOL = 'o';
- private static final boolean SHOW_STANDARD_TIMESTAMP = true;
- public static void main(String[] args) {
- TNTIO.printAppHeadingWithJarLine(APP_TITLE, STAGE_NO_STR, CURRENT_JAR, BORDER_SYMBOL, SHOW_STANDARD_TIMESTAMP, 2);
- //------------------------------------------------------------------------------------------------------------------------
- ArrayList<String> myAnimals = new ArrayList<String>();
- int numEntries = args.length;
- if(numEntries > 0){
- String msg = "There are " + numEntries + " animals on the farm";
- TNTIO.printSimpleHeading(msg, '=', 5);
- for(String entry : args){
- myAnimals.add(entry);
- System.out.println(entry);
- }
- }else{
- TNTIO.printSimpleHeading("There are no animals currently on the farm", '=', 5);
- TNTIO.printSimpleHeading("Rounding up some animals for our farm...", '=', 5);
- myAnimals = roundUpAnimals("farmAnimals.txt");
- HashMap<String, String> animalSoundDictionary = createAnimalSoundDictionary(myAnimals);
- //http://stackoverflow.com/questions/1066589/iterate-through-a-hashmap
- TNTIO.printSimpleHeading("Here are animals with their sounds", '=', 5);
- //iterate through the hash map, which implements the interface map
- int c = 1;
- for(String animal: animalSoundDictionary.keySet()){
- String noise = animalSoundDictionary.get(animal);
- System.out.println(c + "] The " + animal + " says: '" + noise + "'");
- c +=1;
- }
- }
- //------------------------------------------------------------------------------------------------------------------------
- TNTIO.printApplicationEndStatement(myDriverObj,'=');
- KB.close();
- }//end main method
- private static ArrayList<String> roundUpAnimals(String fname){
- ArrayList<String> entries = new ArrayList<String>();
- entries = TNTIO.readStringData(fname);
- return entries;
- }
- private static HashMap<String, String> createAnimalSoundDictionary(ArrayList<String> animalEntries){
- HashMap <String, String> animalSoundDictionary = new HashMap<String, String>();
- /*
- Hint: you will need to load each entry from animalEntries and use a method from the 'String' class to split
- the phrase into two parts (animal, noise). Those two parts will be stored in the primitive array animalInfo
- and those can be used to put the entry into the animalSoundDictionary
- */
- //primitive array
- String animalInfo[] = new String[2];
- for(String e : animalEntries){
- e = e.toLowerCase();
- e = e.replaceAll("\\s+","");
- animalInfo = e.split( ",");
- String animal = animalInfo[0];
- String noise = animalInfo[1];
- animalSoundDictionary.put(animal, noise);
- }
- return animalSoundDictionary;
- }
- }//end driver class
- //------SAMPLE OUTPUT-------------------------------------------------------------------------------------------------------------
- /*
- oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
- o Old Mac Donald Saga - Stage 3 Soln o
- o Java Archive: JavaTNT8-29-16.jar o
- o January 02, 2017 @ 12:06:08 PM o
- o /Users/kentpendleton/Dropbox/Java/Projects/... o
- o ...OldMacDonaldSaga/OldMacDonaldSaga2017/... o
- o ...OMDSagaPrj-S3-Soln/ o
- o kentpendleton o
- oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
- =====There are no animals currently on the farm=====
- =====Rounding up some animals for our farm...=====
- =====Here are 11 animals with their sounds=====
- 1] The horse says: 'neigh'
- 2] The mouse says: 'squeak'
- 3] The chicken says: 'cluck'
- 4] The frog says: 'croak'
- 5] The owl says: 'hoot'
- 6] The cat says: 'meow'
- 7] The bird says: 'tweet'
- 8] The cow says: 'moo'
- 9] The dog says: 'woof'
- 10] The sheep says: 'baa'
- 11] The pig says: 'grunt'
- ==============Terminating Current Application: OMcDonaldPrjDriver===============
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement