Advertisement
Guest User

Untitled

a guest
Nov 20th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.88 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.io.*;
  3.  
  4. public class assignment6b {
  5. public static void main(String[] args) throws IOException {
  6. //-------------------------
  7. int counter=0;
  8. int i=0; //makes i an int and 0
  9. int n=0; //makes n an int and 0
  10. int voters[]=new int[10]; //creates an array called voters with 10 int elements to store both bronx and brooklyn voters
  11. int zipcodes[]=new int[10]; // creates an array called zipcodes with 10 int elements to store both bronx and brooklyn zipcodes
  12. int bklnzip[]=new int[1000]; //creates an array called bklnzip with 1000 int elements to store brooklyn zipcodes
  13. int bklnvote[]=new int[1000]; //creates an array called bklnvote with 1000 int elements to store brooklyn voters
  14. int bxzip[]=new int[1000]; // creates an array called bxzip with 1000 int elements to store bronx zipcodes
  15. int bxvote[]=new int[1000]; // creates an array called bxvote with 1000 int elements to store bronx vote
  16. Scanner x=new Scanner(new File("information.txt")); // creates a scanner called x that reads the file "information.txt"
  17. while (x.hasNext()){ //while loop. If x has any more values do a loop
  18. zipcodes[i]=x.nextInt(); //reads both bronx and brooklyn zipcodes
  19. voters[i]=x.nextInt(); //reads both bronx and brooklyn voters
  20. if (zipcodes[i]>11199 && zipcodes[i]<11300){ //if the zipcodes in the array are greater than 11199 and less than 11300 do something
  21. bklnzip[n]=zipcodes[i]; // assigns bklnzip[n] to zipcodes[i]. Taking a zipcode and making it a brooklyn zipcode
  22. bklnvote[n]=voters[i]; //assigns voters that are from brooklyn to bklnvote[n]
  23. System.out.println("Brooklyn Zipcode: "+bklnzip[n] + " Brooklyn Voters: " +bklnvote[n]); //prints brooklyn zipcode and voter
  24. n++; //makes n adds itself at the end
  25. }
  26. else{ // if if statement isn't satisfied do this
  27. bxzip[n]=zipcodes[i]; //assigns zipcode to bronx
  28. bxvote[n]=voters[i]; //assigns voters from that zipcode to bronx voters
  29. System.out.println("Bronx Zipcode: "+bxzip[n] + " Bronx Voters: " + bxvote[n]); //prints out bronx zipcode and votes
  30.  
  31. }
  32. i++; //makes i add itself
  33. }
  34. dataPrint(zipcodes,voters); //Question: Why does zipcodes and voters not need the [i] next to it?
  35. for (int y=0;y<10; y++){ //starts a for loop
  36. if(isBrooklyn(zipcodes,counter)){ //if isBrooklyn returns true do
  37. System.out.println(zipcodes[y]+ " is in Brooklyn"); //prints out the zipcode saying its brooklyn
  38. }
  39. else{ //if if statement not satisfied do
  40. System.out.println(zipcodes[y]+" is in Bronx"); //prints out zipcode and say its in the bronx
  41. }
  42. counter++;// adds one to the counter
  43. }
  44. System.out.println("The highest voters in Brooklyn is located in "+ bklnzip[maxVotesbk(bklnvote)]+" with "+bklnvote[maxVotesbk(bklnvote)]+" votes"); //tells you location of the highest voter in brooklyn
  45. System.out.println("The highest voters in Bronx is located in "+ bxzip[maxVotesbx(bxvote)]+" with "+bxvote[maxVotesbx(bxvote)]+" votes"); //tells you the location of the highest voter in Bronx
  46. }
  47.  
  48. public static void dataPrint(int [] zip, int [] vote){ //makes a method called dataPrint that takes in int arrays
  49. for (int i=0;i<10; i++){ //makes a for loop
  50. System.out.println("Zipcodes: " + zip[i] + " Voters: " + vote[i]); //prints out zipcodes
  51. }
  52. }
  53. public static boolean isBrooklyn(int [] zip1,int count){ //makes a boolean method that takes in array to see if a zipcode is in brooklyn or not
  54. boolean isBrook=false; //isBrook is equal to false
  55.  
  56. if (zip1[count]>11199 && zip1[count]<11300){ //if zipcode is less than 11199 and less than 11300 do
  57. isBrook= true; //makes isBrook true if if statement is satisfied
  58. }
  59.  
  60.  
  61. return isBrook; //return true or false
  62. }
  63. public static int maxVotesbk(int [] a){ //makes a method for highest voter location in brooklyn
  64. int maxbk=a[0]; //maxbk is first value of array
  65. int maxindexbk=0; //max indexbk is assigned to 0
  66. for (int r=0;r<a.length;r++){ //for loop
  67. if (maxbk<a[r]){ //if maxbk is less than the array element
  68. maxbk=a[r]; // make maxbk the one that is greater than the previous
  69. maxindexbk=r; //assigns maxindexbk to the location of the array
  70. }
  71. }
  72. return maxindexbk;//returns the location of the highest voter
  73. }
  74. public static int maxVotesbx(int [] b){ //makes method to see highset voter's location in bronx
  75. int maxbx=b[0]; //declares maxbx to the first element of the array
  76. int maxindexbx=0; //declares maxindexbx to 0 and int
  77. for (int t=0;t<b.length;t++){
  78. if (maxbx<b[t]){ //if maxbx is less than the lement in array b do this
  79. maxbx=b[t]; //assugbs maxbx to b[t]
  80. maxindexbx=t; //assigns maxindex to t
  81. }
  82. }
  83. return maxindexbx; //returns the location of the highest voter
  84. }
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement