Question

CISC 1115 Assignment 6 Write a complete program, including javadoc comments, to process voter statistics Input...

CISC 1115 Assignment 6 Write a complete program, including javadoc comments, to process voter statistics Input to Program: A file containing lines of data, such that each line has 2 integers on it The first integer represents a zip code (in Brooklyn or the Bronx), and the second represents the number of voters in that zip code. Output: All output may be displayed to the screen. In main: 1. Your program will read in all of the data in the file, one line at a time. The data will be put into 2 parallel arrays, the first called ZipCodes, and the second called voters. 2. Once populated, the program should print both arrays by calling a method that prints ONE array of integers. The method is called twice, once for each array. 3. The goal is now to split the data in the array according to zip code. All zip codes that begin with 112 are in Brooklyn, and those that begin with 104 are in the Bronx. Create 4 arrays, 2 for Brooklyn (zip codes and voters), and similarly 2 for Bronx. This can be done in main or in a method. 4. You should call a boolean method to determine whether a given zip code is in Brooklyn, i.e. begins with 112. The method returns true if the zip code is in Brooklyn, and false otherwise. You may do the same for the Bronx (or you may assume that all others are in the Bronx). 5. At the end, print the zip code in Brooklyn with the most voters, and the zip code in the Bronx with the most voters. This should be done by calling a method that finds the maximum value in an array of integers. Note: the method should return the index of the maximum value. INPUT DATA: You should have at least (not necessarily exactly) TEN lines of data in the input file.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Note: Could you plz go through this code and let me know if u need any changes in this.Thank You
_________________

___________________________

// zipcodes.txt

11207 45654
11208 45245
11203 25566
11264 55677
11276 78978
10456 45677
10476 78678
10488 67788
10475 45667
10472 57688

___________________

// ReadFile.java

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class ReadFile {

   public static void main(String[] args) {
       int cnt=0,cntBrooklyn=0,cntBronx=0;
       Scanner sc=null;
       try {
           sc=new Scanner(new File("zipcodes.txt"));
           while(sc.hasNext())
           {
               sc.nextLine();
               cnt++;
              
           }
           int zipcodes[]=new int[cnt];
           int voters[]=new int[cnt];
      
           sc.close();
           sc=new Scanner(new File("zipcodes.txt"));
           for(int i=0;i<cnt;i++)
           {
               zipcodes[i]=sc.nextInt();
               voters[i]=sc.nextInt();
           }
           sc.close();
           System.out.println("Displaying the Zipcodes :");
           printArray(zipcodes);
           System.out.println("Displaying the Voters :");
           printArray(voters);
          
           for(int i=0;i<cnt;i++)
           {
               if(String.valueOf(zipcodes[i]).startsWith("112"))
               {
                   cntBrooklyn++;
               }
               else if(String.valueOf(zipcodes[i]).startsWith("104"))
               {
                   cntBronx++;
               }
           }
           int brookCnt=0,bronyxCnt=0;
          
           int brooklynZip[]=new int[cntBrooklyn];
           int brooklynVoters[]=new int[cntBrooklyn];
           int bronxZip[]=new int[cntBronx];
           int bronxVoters[]=new int[cntBronx];
          
          
           for(int i=0;i<cnt;i++)
           {
               if(String.valueOf(zipcodes[i]).startsWith("112"))
               {
                   brooklynZip[brookCnt]=zipcodes[i];
                   brooklynVoters[brookCnt]=voters[i];
                   brookCnt++;
               }
               else if(String.valueOf(zipcodes[i]).startsWith("104"))
               {
                  
                   bronxZip[bronyxCnt]=zipcodes[i];
                   bronxVoters[bronyxCnt]=voters[i];
                   bronyxCnt++;
               }
           }      
          
           /*
           * Creating an Scanner class object which is used to get the inputs
           * entered by the user
           */
           sc = new Scanner(System.in);
          
           System.out.print("Enter Zipcode to search in Brooklyn:");
           int searchZip=sc.nextInt();
           boolean b=searchZipCode(brooklynZip,searchZip);
           if(b)
           {
               System.out.println(searchZip+" is found in the array.");
           }
           else
           {
               System.out.println(searchZip+" is not found in the array.");
           }
           System.out.print("Enter Zipcode to search in Bronyx:");
           searchZip=sc.nextInt();
           b=searchZipCode(bronxZip,searchZip);
           if(b)
           {
               System.out.println(searchZip+" is found in the array.");
           }
           else
           {
               System.out.println(searchZip+" is not found in the array.");
           }
                  
           int maxBrooklynIndx=highestVoters(brooklynVoters);
           int maxBronyxIndx=highestVoters(bronxVoters);
  
           System.out.println("Zipcode "+brooklynZip[maxBrooklynIndx]+" has the maximum voters :"+brooklynVoters[maxBrooklynIndx]);
           System.out.println("Zipcode "+bronxZip[maxBronyxIndx]+" has the maximum voters :"+bronxVoters[maxBronyxIndx]);
          
          
       } catch (FileNotFoundException e) {
           System.out.println("** File Not Found **");
       }

   }

   private static boolean searchZipCode(int[] brooklynZip, int searchZip) {
       String str=String.valueOf(searchZip).substring(0,3);
       String str2=null;
       for(int i=0;i<brooklynZip.length;i++)
       {
           str2=String.valueOf(brooklynZip[i]).substring(0, 3);
           if(str.equals(str2))
           {
               return true;
           }
          
       }
       return false;
   }

   private static int highestVoters(int[] voters) {
       int max=voters[0],maxIndx=0;
       for(int i=0;i<voters.length;i++)
       {
           if(max<voters[i])
           {
               max=voters[i];
               maxIndx=i;
           }
       }
       return maxIndx;
   }

   private static void printArray(int[] nums) {
       for(int i=0;i<nums.length;i++)
       {
           System.out.print(nums[i]+" ");
       }
       System.out.println();
      
   }

}
____________________________

output:

Displaying the Zipcodes :
11207 11208 11203 11264 11276 10456 10476 10488 10475 10472
Displaying the Voters :
45654 45245 25566 55677 78978 45677 78678 67788 45667 57688
Enter Zipcode to search in Brooklyn:11276
11276 is found in the array.
Enter Zipcode to search in Bronyx:10475
10475 is found in the array.
Zipcode 11276 has the maximum voters :78978
Zipcode 10476 has the maximum voters :78678

_______________Could you plz rate me well.Thank You

Add a comment
Know the answer?
Add Answer to:
CISC 1115 Assignment 6 Write a complete program, including javadoc comments, to process voter statistics Input...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • For this c++ assignment, Overview write a program that will process two sets of numeric information....

    For this c++ assignment, Overview write a program that will process two sets of numeric information. The information will be needed for later processing, so it will be stored in two arrays that will be displayed, sorted, and displayed (again). One set of numeric information will be read from a file while the other will be randomly generated. The arrays that will be used in the assignment should be declared to hold a maximum of 50 double or float elements....

  • Write a complete program that uses the functions listed below. Except for the printOdd function, main...

    Write a complete program that uses the functions listed below. Except for the printOdd function, main should print the results after each function call to a file. Be sure to declare all necessary variables to properly call each function. Pay attention to the order of your function calls. Be sure to read in data from the input file. Using the input file provided, run your program to generate an output file. Upload the output file your program generates. •Write a...

  • write a complete Java program with comments in main and in each method. Data: The input data for this program is given as two columns of numbers. All data will be entered from a fle named input.t...

    write a complete Java program with comments in main and in each method. Data: The input data for this program is given as two columns of numbers. All data will be entered from a fle named input.txt and all output will go to the screen Assume there will not be more than 100 7 23.56 16 88.12 10 75.1 Design a Java class with a main method that does the following 1) Reads the data into two arrays of doubles,...

  • 29. (20 points) Write the complete C++ program that implements the following program, as if you...

    29. (20 points) Write the complete C++ program that implements the following program, as if you were turning this in for homework. Your program should have a commented header (your name, etc), comments within the program and a well-built display for the output. Don't forget the pre-processor commands Define an array called nums with a maximum of 20 integers, and fill the array with numbers, recei the keyboard (prompt for the numbers.) In this program, exactly 20 integers will be...

  • Write a complete JAVA program to do the following program. The main program calls a method...

    Write a complete JAVA program to do the following program. The main program calls a method to read in (from an input file) a set of people's three-digit ID numbers and their donations to a charity (hint: use parallel arrays)Then the main program calls a method to sort the ID numbers into numerical order, being sure to carry along the corresponding donations. The main program then calls a method to print the sorted 1ists in tabular form, giving both ID...

  • C Program Assignment: 2-Add comments to your program to full document it by describing the most...

    C Program Assignment: 2-Add comments to your program to full document it by describing the most important processes. 3-Your variables names should be very friendly. This means that the names should have meaning related to what they are storing. Example: Instead of naming the variable that stores the hours worked for an employee in a variable called ‘x’ you should name it HoursWorked. 5-Submit your .c program (note that I only need your source code and not all files that...

  • Write a complete Java program, including comments in both the main program and in each method,...

    Write a complete Java program, including comments in both the main program and in each method, which will do the following: 0. The main program starts by calling a method named introduction which prints out a description of what the program will do. This method is called just once.      This method is not sent any parameters, and it does not return a value. The method should print your name. Then it prints several lines of output explaining what the...

  • please use c++ please write down the input file information please add comments for some codes...

    please use c++ please write down the input file information please add comments for some codes please do not use #include <bits/stdc++.h> we did not learn it yet thank you CSIT 575-Take Home Lab #11: Arrays To learn to code, compile and run a program processing characters. Assignment Plan and code a top-down modular program utilizing ARRAYS to solve the following problem, using at least 3 functions (called from the main() section or from another function) to solve the problem....

  • c++ write a program that reads all values from a text file "data.txt" and stores them in ID array valuesl I. The input process from the file should be terminated when a negative value is detec...

    c++ write a program that reads all values from a text file "data.txt" and stores them in ID array valuesl I. The input process from the file should be terminated when a negative value is detected. (An example of such a file is shown below). Also, the program should copy from values[ 1 any value which has even sum of digits and its index (location) to two new arrays evenArr 1 and idxArrl I, respectively. In addition, the program must...

  • Write a complete Java program in a file caled Module1Progrom.java that reads all the tokens from...

    Write a complete Java program in a file caled Module1Progrom.java that reads all the tokens from a file named dota.txt that is to be found in the same directory as the running program. The program should ignore al tokens that cannot be read as an integer and read only the ones that can. After reading all of the integers, the program should print all the integers back to the screen, one per line, from the smalest to the largest. For...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT