Advertisement
Guest User

Untitled

a guest
May 5th, 2014
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.75 KB | None | 0 0
  1. public class YearlyVisitorsWMH
  2. {
  3. public static int numVis; //This variable holds the number of visitors. This number will be assigned to whichever month the user has chosen to assign it to.
  4. public static int numMonths; //This variable will serve as our counter. It will be incremented every time the user adds data to a month.
  5. public static int[] monthlyVisitors; //This is the array we will use hold the visitors for each month. Jan=0, Feb=1, etc.
  6.  
  7. public YearlyVisitorsWMH() //This instantiates the array and both the visitor count and the counter for months.
  8. {
  9. monthlyVisitors = new int[12];
  10. numVis=0;
  11. numMonths=0;
  12. }
  13.  
  14. public void setMonthCount(int month,int visitorCount) //This variable gets the number of visitors for a particular month and holds it in the assigned month.
  15. {
  16. monthlyVisitors[month]=visitorCount;
  17. numMonths++;
  18. }
  19.  
  20. public int getMonthCount(int month) //This method returns the current values for the number of visitors in each month.
  21. {
  22. return monthlyVisitors[month];
  23. }
  24.  
  25. public void printMonthlyTotals() //This method returns the total number of visitors for each month.
  26. {
  27. for(int i=0;i<=numMonths;i++)
  28. {
  29. System.out.println(monthlyVisitors[i]);
  30. }
  31. }
  32.  
  33. public int average() //This method calculates and returns the average
  34. {
  35. int total=0;
  36. int average=0.0;
  37. for(int i=0;i<=numMonths;i++)
  38. {
  39. total+=monthlyVisitors[i];
  40. }
  41. average=total/(numMonths);
  42. return average;
  43. }
  44.  
  45. public int min() //This method determines the minimum number of visitors in a single month that has had data submitted and returns that value.
  46. {
  47. int c=-1;
  48. for(int i=0;i<=numMonths;i++)
  49. {
  50. if(monthlyVisitors[i]<c || c==-1)
  51. {
  52. if(monthlyVisitors[i]==0)
  53. {
  54. boolean t =true; //Boolean variable prevents min from returning a zero in a non-submitted month as a minimum.
  55. }
  56. else
  57. {
  58. c=monthlyVisitors[i];
  59. }
  60. }
  61. }
  62. return c;
  63. }
  64.  
  65. public int max() //The following method determines the maximum number of visitors in a single month and returns that value.
  66. {
  67. int c=-1;
  68. for(int i=0;i<=numMonths;i++)
  69. {
  70. if(monthlyVisitors[i]>c || c==-1)
  71. {
  72. if(monthlyVisitors[i]==0)
  73. {
  74. boolean t=true;
  75. }
  76. else
  77. {
  78. c=monthlyVisitors[i];
  79. }
  80. }
  81. }
  82. return c;
  83. }
  84. }
  85.  
  86.  
  87.  
  88.  
  89. import java.util.*; //importing utils
  90. import java.io.*;
  91. public class ParkManagerWMH
  92. {
  93. public static YearlyVisitorsWMH a = new YearlyVisitorsWMH();
  94.  
  95. public static void addMonth() //this method adds visitors to the chosen month
  96. {
  97. Scanner input = new Scanner(System.in);
  98. System.out.println("Please enter the month number. Enter 0 for January, 1 for February, and so on.");
  99. int month = input.nextInt();
  100. System.out.println("Please enter the visitor count for the month");
  101. int visitors = input.nextInt();
  102. a.setMonthCount(month,visitors);
  103. }
  104.  
  105. public static void getMonth() //This method returns the value of the visitor count for the chosen month
  106. {
  107. Scanner input = new Scanner(System.in);
  108. System.out.println("Please enter the month number.");
  109. int month = input.nextInt();
  110.  
  111. int visitors=a.getMonthCount(month);
  112.  
  113. System.out.println("The visitor count for month "+month+" is "+visitors);
  114. }
  115.  
  116. public static void printTotals() //This calls the printTotals method, which displays the total number of visitors for each month
  117. {
  118. a.printMonthlyTotals();
  119. }
  120.  
  121.  
  122.  
  123. public static void printAvg() //This calls the average method, which calculates and return the average number of visitors per month submitted.
  124. {
  125. int average = a.average();
  126. System.out.println("The yearly visitor average is "+average);
  127. }
  128. public static void printMin() //This calls the Min method, which determines the lowest number of visitors in a month submitted and returns that value.
  129. {
  130. int min = a.min();
  131.  
  132. System.out.println("The minimum number of visitors in a single month is "+min);
  133. }
  134. public static void printMax() //This calls the Max method, which determines the highest number of visitors in a month submitted and returns that value.
  135. {
  136. int max = a.max();
  137.  
  138. System.out.println("The maximum number of visitors in a single month is "+max);
  139. }
  140. public static void main (String[] args)
  141. {
  142. Scanner input = new Scanner(System.in);
  143. int choice=0;
  144.  
  145.  
  146. while(choice!=7) //This while loop contains the switch, which has the user's options. Through this, they select what they want to do or end the program.
  147. {
  148. System.out.println("1. Add a Monthly Count");
  149. System.out.println("2. Get a Monthly Count");
  150. System.out.println("3. Print the monthly totals");
  151. System.out.println("4. Print the Yearly Average");
  152. System.out.println("5. Print the Yearly Minimum");
  153. System.out.println("6. Print the Yearly Maximum");
  154. System.out.println("7. Exit the menu");
  155. choice = input.nextInt();
  156. switch(choice)
  157. {
  158. case 1: addMonth();
  159. break;
  160. case 2: getMonth();
  161. break;
  162. case 3: printTotals();
  163. break;
  164. case 4: printAvg();
  165. break;
  166. case 5: printMin();
  167. break;
  168. case 6: printMax();
  169. break;
  170. case 7: choice=7;
  171. }
  172. }
  173. System.exit(0); //This ends the program
  174. }
  175. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement