Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- import java.io.*;
- public class assignment6b {
- public static void main(String[] args) throws IOException {
- //-------------------------
- int counter=0;
- int i=0; //makes i an int and 0
- int n=0; //makes n an int and 0
- int voters[]=new int[10]; //creates an array called voters with 10 int elements to store both bronx and brooklyn voters
- int zipcodes[]=new int[10]; // creates an array called zipcodes with 10 int elements to store both bronx and brooklyn zipcodes
- int bklnzip[]=new int[1000]; //creates an array called bklnzip with 1000 int elements to store brooklyn zipcodes
- int bklnvote[]=new int[1000]; //creates an array called bklnvote with 1000 int elements to store brooklyn voters
- int bxzip[]=new int[1000]; // creates an array called bxzip with 1000 int elements to store bronx zipcodes
- int bxvote[]=new int[1000]; // creates an array called bxvote with 1000 int elements to store bronx vote
- Scanner x=new Scanner(new File("information.txt")); // creates a scanner called x that reads the file "information.txt"
- while (x.hasNext()){ //while loop. If x has any more values do a loop
- zipcodes[i]=x.nextInt(); //reads both bronx and brooklyn zipcodes
- voters[i]=x.nextInt(); //reads both bronx and brooklyn voters
- if (zipcodes[i]>11199 && zipcodes[i]<11300){ //if the zipcodes in the array are greater than 11199 and less than 11300 do something
- bklnzip[n]=zipcodes[i]; // assigns bklnzip[n] to zipcodes[i]. Taking a zipcode and making it a brooklyn zipcode
- bklnvote[n]=voters[i]; //assigns voters that are from brooklyn to bklnvote[n]
- System.out.println("Brooklyn Zipcode: "+bklnzip[n] + " Brooklyn Voters: " +bklnvote[n]); //prints brooklyn zipcode and voter
- n++; //makes n adds itself at the end
- }
- else{ // if if statement isn't satisfied do this
- bxzip[n]=zipcodes[i]; //assigns zipcode to bronx
- bxvote[n]=voters[i]; //assigns voters from that zipcode to bronx voters
- System.out.println("Bronx Zipcode: "+bxzip[n] + " Bronx Voters: " + bxvote[n]); //prints out bronx zipcode and votes
- }
- i++; //makes i add itself
- }
- dataPrint(zipcodes,voters); //Question: Why does zipcodes and voters not need the [i] next to it?
- for (int y=0;y<10; y++){ //starts a for loop
- if(isBrooklyn(zipcodes,counter)){ //if isBrooklyn returns true do
- System.out.println(zipcodes[y]+ " is in Brooklyn"); //prints out the zipcode saying its brooklyn
- }
- else{ //if if statement not satisfied do
- System.out.println(zipcodes[y]+" is in Bronx"); //prints out zipcode and say its in the bronx
- }
- counter++;// adds one to the counter
- }
- 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
- 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
- }
- public static void dataPrint(int [] zip, int [] vote){ //makes a method called dataPrint that takes in int arrays
- for (int i=0;i<10; i++){ //makes a for loop
- System.out.println("Zipcodes: " + zip[i] + " Voters: " + vote[i]); //prints out zipcodes
- }
- }
- 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
- boolean isBrook=false; //isBrook is equal to false
- if (zip1[count]>11199 && zip1[count]<11300){ //if zipcode is less than 11199 and less than 11300 do
- isBrook= true; //makes isBrook true if if statement is satisfied
- }
- return isBrook; //return true or false
- }
- public static int maxVotesbk(int [] a){ //makes a method for highest voter location in brooklyn
- int maxbk=a[0]; //maxbk is first value of array
- int maxindexbk=0; //max indexbk is assigned to 0
- for (int r=0;r<a.length;r++){ //for loop
- if (maxbk<a[r]){ //if maxbk is less than the array element
- maxbk=a[r]; // make maxbk the one that is greater than the previous
- maxindexbk=r; //assigns maxindexbk to the location of the array
- }
- }
- return maxindexbk;//returns the location of the highest voter
- }
- public static int maxVotesbx(int [] b){ //makes method to see highset voter's location in bronx
- int maxbx=b[0]; //declares maxbx to the first element of the array
- int maxindexbx=0; //declares maxindexbx to 0 and int
- for (int t=0;t<b.length;t++){
- if (maxbx<b[t]){ //if maxbx is less than the lement in array b do this
- maxbx=b[t]; //assugbs maxbx to b[t]
- maxindexbx=t; //assigns maxindex to t
- }
- }
- return maxindexbx; //returns the location of the highest voter
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement