Java代写:CS115 Candidate Information Program

根据题目要求,实现所需函数,代写一个候选人信息库查询程序。

Project Specifications

Each student is to create, compile, debug, and run a candidate information program. This program will use a given data file in a particular format to read into the program and use for data information used when running the program. The data file name is posted and is named cipcs115.txt (Candidate Information Program cs115 text file). This file may be viewed using the Wordpad or Notepad applications of Windows.

This file represents candidates that participated in an election. State is a string that represents the next part of the data file, i.e., what state the following candidates are from.

Following the State, is Number_of_candidates_in_state, an integer that represents the number of candidate lines that follow next in the data file. For example, the data file might have the following two lines.

Wisconsin 4 which would refer to the next section of the file listing 4 Wisconsonite candidates. The next 4 lines in the file would then be candidate lines with the data for those 4 candidates. After the 4 candidate lines, another section would begin with a different state, and different number of candidates from that state. This format would continue throughout the data file.

After the integer Number_of_candidates_in_state follows the candidates’ data lines. Each data line has 6 fields in it.

Each field is separated by a tab character (i.e., one character).

This file represents candidates that have run in some election for a particular office. Name_of_Candidate is the last name of a particular person who ran for office. Election_Office is the particular election the candidate ran in (for example, Mayor, Governor, President, etc.). Party is one character either ‘r’ for republican, ‘d’ for democrat, ‘i’ for independent, or ‘o’ for other. Number_of_Votes is the total number of votes the candidate received. Dollars_Spent is the amount of money spent by the candidate on the campaign. Motto is the slogan used in the campaign by the candidate. You can assume that Name_of_Candidate and Election_Office are strings that contain no spaces. Motto, however, may have spaces in it and continues until the end of the line.

The last line of the file has the string END_OF_FILE in the State field.

The student’s program will consist of two parts: a client program (with a main() method), and a service class. The service class will represent a candidate.

The candidate class MUST be named your_last_name_your_first_nameCandidate (e.g., hanrathjonCandidate).

Each candidate should have at least the following private attributes: name, office, party, number of votes, dollars spent, and motto (as described previously in this specification). The student should provide suitable constructor, accessor, and mutator methods for use in the client program (as well as, theoretically, any other program that might want to use the class). The class should also have a display() method that will display all attributes of an candidate, a toString() method that returns a String of all attributes of the candidate, and an equals() method that returns a boolean. The student may write other methods for the class as they see fit.

The student’s client program will first read from the cipcs115.txt file all of the information into an array of candidate class objects. After this is completed, the program will prompt a user running the program for various options that will manipulate the data. The student should use Notepad or WordPad to read cipcs115.txt and familiarize themselves with the project data.

The menu() function in the program will allow the user to enter one of the following options:

‘L’ or ‘l’ : List all of the candidate data available (including all information read in from the cipcs115.txt file).
‘C’ or ‘c’ : Display information for a particular candidate.
‘V’ or ‘v’ : Display voting information.
‘S’ or ‘s’ : Display state information
‘D’ or ‘d’ : Display dollars spent information.
‘P’ or ‘p’ : Display party information.
‘Q’ or ‘q’ : Quit and return to main().

The program will allow multiple opportunities for the user to run the menu() function. Once the program ends, it should display a list of information detailed later in this document .

The .java file for the client program MUST be named your_last_name_your_first_name.java (e.g., hanrathjon.java). Your program MUST work using the posted cipcs115.txt file. Your program MUST be able to support more or less entries than are in the cipcs115.txt file (up to a limit described later in this document).

Phase I of the project is in the student’s lab session. Phase I entails:

  1. Completing any constant declarations, and import statements.
  2. Completing the main() method, including reading in the information from cipcs115.txt.
  3. Writing “stub methods” for at least all of the required methods in the project.
  4. It is strongly recommended that the student have more completed than the above to reasonably be able to finish the project.

The completed project the student must hand in to their TA:

  1. their lastname_firstname.java file,
  2. their latname_firstnameCandidate.java file
  3. the cipcs115.txt file, and
  4. a Microsoft Word file with an analysis of what the student learned while doing the project. This should be between 1 and 3 pages of information in regard to problems encountered, concepts learned, things the student would do differently the next time, other things that might be tried, etc.

These three files should be emailed or otherwise turned in to the student’s TA. The student is responsible for confirming that their TA has received the three files.

NO LATE PROJECTS WILL BE ACCEPTED.

NO CHEATING. A supercomputer program will be run on all projects to determine if there has been collaboration. Last term, 6(!) students were caught and project grades of 0 were given to 5 of them, the others spent a very difficult time proving their innocence. Furthermore, 3 students was Infoed to the student’s department head and the student is now on probation. A 50% on the project is much better than a 0. Anyone caught cheating is also subject to having the infraction documented on their permanent record.

ANY STUDENT MAY BE CALLED IN TO THE INSTRUCTOR’S OFFICE TO EXPLAIN THEIR CODE. If it is clear that they do not understand the code, the student may receive a 0. The decision of the instructor is final. Any student caught using another person to write the program or any part of the program will be given a 0, and will be Infoed to the student’s department. In addition, any student caught writing a program or any part of a program for another student will also be Infoed.

NO WORKING TOGETHER, NO HELP FROM ANYONE. NO INTERNET HELP. Asking general JAVA questions, general algorithm questions, using your own notes and the notes from class, and using other documentation regarding the JAVA language are acceptable. A general rule to follow is to

WRITE YOUR OWN CODE – do NOT copy or otherwise use ANY other current, previous, or other code written by ANYONE else.

SECURE YOUR WORK!! Do not leave flash drives or floppies lying around in your room or elsewhere. Do not leave copies on another machine. Do not let others (even those not in class) “borrow” or “sneak a peek” at your work. Students have had other students, roommates, office workers, and the like copy their work unknowingly and the result is PAINFUL!

BACKUP YOUR WORK FREQUENTLY! Backing up your work to a flash drive or in email attachments to your self will protect you from losing your only copy of your work. It is also good evidence of your continuing work toward completion of your project if is necessary to document your work. Losing your project due to disk or other hardware failure is not an acceptable reason for not completing your project on time.

The student MUST NOT use any global variables. The student will be down graded if there is any use of global or external variables. Global constants are encouraged.

The student MUST NOT anything not covered in class. While using some of these would possibly make the solution easier, the task is to use what has been covered in class to come to a solution. The student MUST NOT use any packages, libraries, methods, or other JAVA constructs not covered in class UNLESS previously approved via email by the instructor. Using anything outside the content covered in cs115 may result in a 0. If you have questions, ask! If you are not happy about this, too bad).

Since there are no global variables allowed, the student will have to determine what information (i.e., variables) to pass to methods, and what information to return from methods. Remember that when arrays are passed to methods, modification of the array in the method does modify the original array in the calling method. The student MUST write all required methods or face down grading.

It is highly recommended that the student write pseudocode and design the solution to the problem before they begin coding. This will solve many problems that become large problems after coding. It is suggested that the student code one method at a time, compile the program, test it to their satisfaction, and then go on. It is also recommended that the student save a copy of each working step .java file in another location in case they need to go back and start again at that earlier point.

It is also highly recommended that when a student designs a solution to this problem, he or she draws pictures of the data structures (i.e., arrays, classes, or other variables) that will be used to store the data. Drawing a picture and labeling the data structures will greatly reduce the confusion and errors in coming to a working solution.

The student may write as many other methods as they see fit.

The student may assume:

  1. There will be no more than 50 candidates in any state in the cipcs115.txt file.
  2. There will be no more than 20 states in the cipcs115.txt file.
  3. There will be no more than 100 calls to any method in the program.
  4. The only legitimate value for Party are ‘r’, ‘d’, ‘i’, or ‘o’.

The student MUST comment the program appropriately. The student’s name, Section, and student ID must be in a comment at the top of the program. Comments should also include a summary comment on the intent of the program at the top of the program, and comments for any main variables and constants used. There should also be a comment for any unobvious code.

Requirement

The following are required methods in each student’s project:

main()

  1. Declares variables used to store data read in from data file.
  2. Declares an array of counters used to count the number of times a particular menu option is entered.
  3. Opens data file cipcs115.txt and reads the data into variables for use during the rest of the program.
  4. Prompts the user for what to do next:
    • a. If the user enters a ‘m’ or ‘M’, calls menu().
    • b. If the user enters a ‘q’ or ‘Q’, calls finalStats().
    • c. If the user enters any other character, displays an error message and tries again.
  5. After returning from finalStats(), main() the program ends; otherwise, main() continues to prompt the user for what to do next (i.e., ‘m’ or ‘q’).
  1. Prompts the user for what to do next:
    • a. If the user enters a ‘l’ or ‘L’, calls listall().
    • b. If the user enters a ‘c’ or ‘C’, calls candidateInfo().
    • c. If the user enters a ‘v’ or ‘V’, calls voteInfo().
    • d. If the user enters a ‘s’ or ‘S’, calls stateInfo().
    • e. If the user enters a ‘d’ or ‘D’, calls dollarsSpentInfo().
    • f. If the user enters a ‘p’ or ‘P’, calls partyInfo().
    • g. If the user enters a ‘q’ or ‘Q’, returns to main().
    • h. If the user enters any other character, displays an error message and tries again
  2. Keeps track of how many times each letter is entered by the user. This information will be passed back to main() and used in finalStats().

listall()

  1. Lists out all candidates, their party, and their motto from the data read in from cipcs115.txt.
  2. Returns.

candidateInfo()

  1. Prompts the user for the candidate last name to search for.
  2. Searches the data read in from cipcs115.txt and displays all data from any line that matches the candidate. candidateInfo() then returns true.
  3. If no line matches the candidate’s last name, candidateInfo() displays the message “No candidate found.” and returns false.

voteInfo()

  1. Prompts the user for the election race they would like to search for, OR the word “all”. An election race is defined by a state and an election office.
  2. For the election race entered, displays each candidate that was in that race, the candidate’s number of votes, and the percentage of votes the candidate received. Also displays the total votes in that election. Once completed, voteInfo() returns true.
  3. If the user entered the word “all”, displays information for all election races by each race (i.e., the output should first output the data for one race, followed by another, and so on for each race), including each candidate in the race, their votes, and their percentage of votes. Once completed, voteInfo() returns true.
  4. If no line matched the election race or “all”, voteInfo() displays the message “No election race found.” and returns false.

stateInfo()

  1. Prompts the user for the state they would like to search for, OR the word “all”.
  2. For the state entered, displays each candidate that is from that state by name, each candidate’s office, party, and each candidate’s dollars spent. Also displays the total dollars spent in that state and the average dollars spent in the state. Once completed, stateInfo() returns true.
  3. If the user entered the word “all”, displays information for all states by each state (i.e., the output should first output the data for one state, followed by another, and so on for each state), including each candidate in the state, office and party, and their dollars spent. Also displays the total dollars spent in each state and the average dollars spent in each state. Once completed, stateInfo() returns true.
  4. If no line matches the state or “all”, stateInfo() displays the message “No state found.” and returns false.

dollarsSpentInfo()

  1. Prompts the user for the candidate last name they would like to search for, OR the word “all”.
  2. For the candidate last name entered, displays each candidate that matches that last name, and the candidate’s dollars spent. Once completed, dollarsSpentInfo() returns true.
  3. If the user entered the word “all”, displays information for all candidates that includes each candidate name, office, party, and dollars spent. Once completed, dollarsSpentInfo() returns true.
  4. If no line matched the candidate last name or “all”, dollarsSpentInfo() displays the message “No candidate found.” and returns false.

partyInfo()

  1. Prompts the user for the party they would like to search for, OR the word “all”.
  2. For the party entered, displays each candidate that was in that party, and the election race they were in. Once completed, partyInfo() returns true.
  3. If the user entered the word “all”, displays information for all parties by each party (i.e., the output should first output the data for one party, followed by another, and so on for each party), including each candidate in the party, and their election race. Once completed, partyInfo() returns true.
  4. If no line matched the election race or “all”, partyInfo() displays the message “No party found.” and returns false.

finalStats()

  1. Displays the number of times each menu letter was entered by the user. The output should display the number of L’s (upper and lower case can be considered the same letter in this output), C’s, V’s, S’s, D’s, P’s, Q’s, and “others” (i.e., bad input letters).
  2. Returns.

Candidate Class

Constructor Method(s): Initializes each Candidate attribute appropriately. This should NOT read data from a file. This should simply set “default” values for each attribute of the invoking candidate object.

Accessor Methods: Each returns a value of a Candidate attribute.

Mutator Methods: Each allows modification of a Candidate attribute. The mutator method for dollars spent should make sure the dollars spent is greater than some minimum. The mutator method for party should make sure the party is one of the valid parties.

Display Method: Displays all attributes of the invoking object Candidate.

toString Method: Returns the attributes of the invoking object Candidate in String form.

equals Method: Accepts an Candidate class object as a parameter. Returns true if the the invoking object Candidate is equal to the passed Candidate object. Returns false if the two Candidate objects are not equal.

Hints

  1. Begin by writing out a pseudocode version of your solution. It will help you IMMENSELY in finding problems before you start writing JAVA code. Start by drawing a picture of the date structures (i.e., arrays, vectors, and other counters) that you will use to store the data read in from the cipcs115.txt file. Name each variable in your picture so that you can easily recognize what each variable contains. Once your picture of your data structures is complete, start writing pseudocode for main() and listall(). Move on to menu(), and then the other required methods.
  2. DO NOT WAIT TO GET STARTED. Start now and do the project a little at a time. Don’t wait until finals week to start the project.
  3. Start by writing the class for a candidate, Draw a picture and/or pseudocode the class and what the class should store in data members and should allow in methods. Write the class constructor. Write a basic client program to declare a class object. Test. Add accessor methods to the class file for accessing attributes. Add code to the client program to use the accessor methods. Test. Add code to the class file for mutator methods. Add code to the client program to use the mutator methods. Test. Add code to the class file for the display method. Add code to the client program to use the display method. Test.
  4. Continue by writing the program to read in the data from the cipcs115.txt file. Get this working correctly with the listall() method, and then go from there. You will need arrays to store the data from the file. Get main() and listall() working before going on to menu() and the other methods.
  5. After getting the file data read in correctly, add the menu() method and stub methods for all other methods. Remember, stub methods are methods that are required in the final program, but just output a line to the display saying that they were called, and do nothing else. Get the menu() working correctly and calling the required stub methods before continuing.
  6. After getting the menu() method working, write each other method in turn, testing each as you complete it. Save working copies of your .java file at each step in case you need to go back to a successful version of the program.
  7. Write and test the code for candidateInfo() first after menu() is completed, followed by voteInfo(), dollarsSpentInfo(), stateInfo(), and partyInfo(). Write the code for finalStats() last.
  8. Declare an array in main() to save the counters for each letter entered in the menu() method by the user. Pass the array to menu() and increment the appropriate member for each letter entered. Remember that if the array is modified in menu(), the changes will be reflected in the original array in main(). This array can then be passed to finalStats() at the end of the program.

Extra Credit

Extra credit will be given for completing the following items. It is possible to receive a higher score than 100% on the project. This will have the effect of raising the student’s overall grade.

The following will give the student extra credit:

  1. (5 points) Formatting any output of the candidate information in neat columns.
  2. (5 points) Using vectors or arrayList objects instead of arrays to store candidate information and counter information. Note: If you choose this extra credit, you do not need instructor permission to use vectors or arrayLists.
  3. (5 points) Supporting an option in the listall() method to display only candidates that are from specific parties.
  4. (5 points) Print the finalStats() results to a file as well as the display, and display the file name that they are stored in.
  5. (5 points) Allowing any line in the cipcs115.txt file that begins with a pound sign (‘#’) to be a comment line. A comment line is ignored when reading in the data.
  6. (10 points) Write another method called sortList() that displays all candidates sorted alphabetically by last name. Add an option to the menu() (‘O’ for order) that calls sortList(). You must not use library methods to do the sorting.

Grading

The project comprised 10% of the student’s overall grade. There will be 100 points total on the required parts of the project detailed on previous pages. Points will be given (or taken away) for compiling, commenting, running correctly, and completing the requirements to the specifications listed previously. Points will also be given or taken away from the analysis file that is handed in. Extra credit points will be added if any or all of the extra credit items are completed correctly.