Advertisement
Guest User

Untitled

a guest
Jan 17th, 2017
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.83 KB | None | 0 0
  1. import java.awt.Color;
  2. import java.util.ArrayList;
  3. import java.util.HashMap;
  4. import java.util.Hashtable;
  5. import java.util.Random;
  6. import java.util.Scanner;
  7.  
  8. /*
  9. File: OMcDonaldPrjDriver.java
  10. Date: Jan 2, 2017
  11. Time: 10:24:13 AM
  12. Programmer: kentpendleton
  13.  
  14. Project: OMDSagaPrj-S0
  15. Location (Option-Return copy/paste location):
  16. /Users/kentpendleton/Dropbox/Java/Projects/OldMacDonaldSaga/OldMacDonaldSaga2017/
  17. * OMDSagaPrj-S0/src/OMcDonaldPrjDriver.java
  18.  
  19. Program Description:
  20. Ultimately, print the words to the song: "Old MacDonald"
  21. First we must get a list of animals and sounds
  22.  
  23. Requirements/Sample Output:
  24. 0. Use the Solution to Exercise 2 as a base
  25. 1. You have a 'corrupted' version of 'farmAnimals.txt' in this project
  26. 2. Open it and determine what the problems with the entries are.
  27. 2.5 Run the project and notice the output: anything wrong?
  28. 3. WITHOUT CHANGING THE TEXT FILE, make the necessary corrections/modifications to get the output shown
  29. 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
  30. 4. What do you conclude about the 'coolness' of the HashMap?
  31.  
  32. Known Issues:
  33.  
  34.  
  35. Future Development:
  36. */
  37.  
  38. @SuppressWarnings("unused")
  39. public class OMcDonaldPrjDriver {
  40. private static OMcDonaldPrjDriver myDriverObj = new OMcDonaldPrjDriver(); //used to get className for termination statement
  41.  
  42. private static final Scanner KB = new Scanner(System.in);
  43.  
  44. //adjust app heading variables here
  45. public static final String APP_TITLE = "Old Mac Donald Saga";
  46. private static final String STAGE_NO_STR = "3 Soln"; //stageNo as a String (ex: "1", "5c" etc)
  47. private static final String CURRENT_JAR = "Java Archive: JavaTNT8-29-16.jar";
  48. private static final char BORDER_SYMBOL = 'o';
  49. private static final boolean SHOW_STANDARD_TIMESTAMP = true;
  50.  
  51. public static void main(String[] args) {
  52. TNTIO.printAppHeadingWithJarLine(APP_TITLE, STAGE_NO_STR, CURRENT_JAR, BORDER_SYMBOL, SHOW_STANDARD_TIMESTAMP, 2);
  53. //------------------------------------------------------------------------------------------------------------------------
  54. ArrayList<String> myAnimals = new ArrayList<String>();
  55. int numEntries = args.length;
  56. if(numEntries > 0){
  57. String msg = "There are " + numEntries + " animals on the farm";
  58. TNTIO.printSimpleHeading(msg, '=', 5);
  59. for(String entry : args){
  60. myAnimals.add(entry);
  61. System.out.println(entry);
  62. }
  63. }else{
  64. TNTIO.printSimpleHeading("There are no animals currently on the farm", '=', 5);
  65. TNTIO.printSimpleHeading("Rounding up some animals for our farm...", '=', 5);
  66. myAnimals = roundUpAnimals("farmAnimals.txt");
  67. HashMap<String, String> animalSoundDictionary = createAnimalSoundDictionary(myAnimals);
  68. //http://stackoverflow.com/questions/1066589/iterate-through-a-hashmap
  69. TNTIO.printSimpleHeading("Here are animals with their sounds", '=', 5);
  70. //iterate through the hash map, which implements the interface map
  71. int c = 1;
  72. for(String animal: animalSoundDictionary.keySet()){
  73. String noise = animalSoundDictionary.get(animal);
  74. System.out.println(c + "] The " + animal + " says: '" + noise + "'");
  75. c +=1;
  76. }
  77. }
  78. //------------------------------------------------------------------------------------------------------------------------
  79. TNTIO.printApplicationEndStatement(myDriverObj,'=');
  80. KB.close();
  81. }//end main method
  82.  
  83. private static ArrayList<String> roundUpAnimals(String fname){
  84. ArrayList<String> entries = new ArrayList<String>();
  85. entries = TNTIO.readStringData(fname);
  86. return entries;
  87. }
  88.  
  89. private static HashMap<String, String> createAnimalSoundDictionary(ArrayList<String> animalEntries){
  90. HashMap <String, String> animalSoundDictionary = new HashMap<String, String>();
  91. /*
  92. Hint: you will need to load each entry from animalEntries and use a method from the 'String' class to split
  93. the phrase into two parts (animal, noise). Those two parts will be stored in the primitive array animalInfo
  94. and those can be used to put the entry into the animalSoundDictionary
  95. */
  96. //primitive array
  97. String animalInfo[] = new String[2];
  98. for(String e : animalEntries){
  99. e = e.toLowerCase();
  100. e = e.replaceAll("\\s+","");
  101. animalInfo = e.split( ",");
  102. String animal = animalInfo[0];
  103. String noise = animalInfo[1];
  104. animalSoundDictionary.put(animal, noise);
  105. }
  106. return animalSoundDictionary;
  107. }
  108.  
  109. }//end driver class
  110.  
  111. //------SAMPLE OUTPUT-------------------------------------------------------------------------------------------------------------
  112. /*
  113. oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
  114. o Old Mac Donald Saga - Stage 3 Soln o
  115. o Java Archive: JavaTNT8-29-16.jar o
  116. o January 02, 2017 @ 12:06:08 PM o
  117. o /Users/kentpendleton/Dropbox/Java/Projects/... o
  118. o ...OldMacDonaldSaga/OldMacDonaldSaga2017/... o
  119. o ...OMDSagaPrj-S3-Soln/ o
  120. o kentpendleton o
  121. oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
  122.  
  123. =====There are no animals currently on the farm=====
  124. =====Rounding up some animals for our farm...=====
  125. =====Here are 11 animals with their sounds=====
  126. 1] The horse says: 'neigh'
  127. 2] The mouse says: 'squeak'
  128. 3] The chicken says: 'cluck'
  129. 4] The frog says: 'croak'
  130. 5] The owl says: 'hoot'
  131. 6] The cat says: 'meow'
  132. 7] The bird says: 'tweet'
  133. 8] The cow says: 'moo'
  134. 9] The dog says: 'woof'
  135. 10] The sheep says: 'baa'
  136. 11] The pig says: 'grunt'
  137.  
  138. ==============Terminating Current Application: OMcDonaldPrjDriver===============
  139.  
  140.  
  141. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement