Advertisement
Guest User

Untitled

a guest
Oct 2nd, 2014
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.32 KB | None | 0 0
  1. import java.util.*;
  2. import java.io.*;
  3. import java.util.Collections;
  4. public class NameYear
  5. {
  6. private String year;
  7. ArrayList<OneName> oneName = new ArrayList<OneName>();
  8.  
  9. public NameYear(String year)
  10. {
  11. String line = "";
  12. String Top = "";
  13. Scanner sc = null;
  14. try
  15. {
  16. sc = new Scanner(new File
  17. ("/home/mathcs/courses/cs225/koch/names/yob"+year+".txt"));
  18. }
  19. catch (Exception e)
  20. {
  21. System.out.println("Error Year should be between 1880 and 2013 not "+ year);
  22. System.exit(1);
  23. }
  24.  
  25. while(sc.hasNextLine())
  26. {
  27.  
  28. // read a line from the input file via sc into line
  29. line = sc.nextLine();
  30.  
  31.  
  32.  
  33. StringTokenizer stk = new StringTokenizer(line, ",");
  34. String name = stk.nextToken();
  35. char sex = stk.nextToken().charAt(0);
  36. int count = Integer.parseInt(stk.nextToken());
  37.  
  38.  
  39. OneName list = new OneName(name, sex, count);
  40.  
  41. oneName.add(list);
  42. }
  43. for (int i = 0 ; i < 10; i++)
  44. {
  45. System.out.println(descending());
  46. }
  47.  
  48. public String descending()
  49. {
  50. String x = "";
  51. Collections.sort(oneName, new OneNameCountCompare());
  52. for(OneName b: oneName)
  53. {
  54. x = b.toString();
  55. }
  56. return x;
  57.  
  58. public class OneName
  59. {
  60. private String Name;
  61. private char Sex;
  62. private int Count;
  63.  
  64. public OneName(String name, char sex, int count)
  65. {
  66. Name = name;
  67. Sex = sex;
  68. Count = count;
  69.  
  70. }
  71. public String getName()
  72. {
  73. return Name;
  74. }
  75. public char getSex()
  76. {
  77. return Sex;
  78. }
  79. public int getCount()
  80. {
  81. return Count;
  82. }
  83. public void setName(String name)
  84. {
  85. if (name.length() < 1)
  86. {
  87. throw new NullPointerException("Baby name is missing");
  88. }
  89.  
  90. Name = name;
  91.  
  92. }
  93. private char M;
  94. private char F;
  95. public void setSex(char sex)
  96. {
  97. if( sex != M)
  98. {
  99. if(sex != F)
  100. {
  101. throw new IllegalArgumentException("Sex has to be M or F");
  102. }
  103. }
  104. Sex = sex;
  105.  
  106. }
  107. public void setCount(int count)
  108. {
  109. if(count < 0)
  110. {
  111. throw new IllegalArgumentException("Count cant be negative");
  112. }
  113.  
  114. Count = count;
  115.  
  116. }
  117. public String toString()
  118. {
  119. return String.format("%s %c %d", Name, Sex, Count);
  120.  
  121. }
  122. }
  123.  
  124. import java.util.Comparator;
  125. import java.util.Collections;
  126.  
  127. public class OneNameCountCompare implements Comparator<OneName>
  128. {
  129. public int compare(OneName b1, OneName b2)
  130. {
  131. if(b1.getCount() <b2.getCount())
  132. {
  133. return 1;
  134. }
  135. else
  136. {
  137. return -1;
  138. }
  139. }
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement