Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

For JAVA Programming: Create a package named cs520.hw3.part2. Using this package, create the following classes. 1.Create a class named Senator as follows: The instance (or

For JAVA Programming:

Create a package named cs520.hw3.part2. Using this package, create the following classes.

1.Create a class named Senator as follows: The instance (or member) private variables name, party, state (String), and yearsInOffice (integer). Use a single constructor with name as its argument and the public get and set methods for the instance variables.

2. Override the toString method to return the string representation of this object in the format () from has been the senator for years.

3. Create a Test class to test the following functionality in its main method. Use the BufferedReader class to read the data.txt file. The contents of the file are shown below. Create the data.txt file in HW3_lastName folder. Read the contents of the text file one line at a time using a loop. The program should work for any number of input lines. Invoke the processInputData method for each line read.

4. Write a private method processInputData which processes its string input argument as follows: Tokenize the string argument using the StringTokenizer class using the comma as the delimiter then extract the name token. Create a Senator object and assign to the variable currentSenator. Finally, read the rest of the tokens one token at a time. Use the corresponding set method on the currentSenator object to set the instance value. If the yearsInOffice is not a valid number, i.e., throws an exception when parsed, set the value as -99 in the exception handling part. Display the string representation of the currentSenator object to the console.

Sample Input data.txt file:

Al Franken,8,Democrat,Minnesota

Amy Klobuchar,10,Democrat,Minnesota

Angus King,4,Independent,Maine

Ben Cardin,10,Democrat,Maryland

Ben Sasse,2,Republican,Nebraska

Bernie Sanders,10,Independent,Vermont

image text in transcribed

Sample Output: Al Franken (Democrat) from Minnesota has been the senator for 8 years Amy Klobuchar (Democrat) from Minnesota has been the senator for 10 years Angus King (Independent) from Maine has been the senator for 4 years Ben Cardin (Democrat) from Maryland has been the senator for 10 years Ben Sasse (Republican) from Nebraska has been the senator for 2 years Bernie Sanders (Independent) from Vermont has been the senator for 10 years

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Beginning C# 2005 Databases

Authors: Karli Watson

1st Edition

0470044063, 978-0470044063

More Books

Students also viewed these Databases questions

Question

3. Write a policy statement to address these issues.

Answered: 1 week ago