Starting from:

$25

Week 3 Assignment Solution

Week 3 Assignment
General Rules for Homework Assignments
• You are strongly encouraged to add comments throughout the program. Doing
so will help your facilitator to understand your programming logic and grade
you more accurately.
• You must work on your assignments individually. You are not allowed to copy
the answers from the others. However, you are encouraged to discuss the
approaches to the homework assignments with your section mates and the
facilitator in your section via the discussion board.
• Each assignment has a strict deadline. However, you are still allowed to submit
your assignment within 2 days after the deadline with a penalty. 15% of the
credit will be deducted unless you made previous arrangements with your
facilitator and professor. Assignments submitted 2 days after the deadline will
not be graded.
• When the term lastName is referenced in an assignment, please replace it with
your last name.
You are strongly encouraged to add comments into your program!
Create a new Java Project in Eclipse named HW3_lastName and complete the
following two parts.
Part 1 (40 Points) – Strings & Exceptions
Create a package named cs520.hw3.part1. Using this package, create the class
StringTest and implement the specified functionality in its main method.
a. Ask the user, through a single input dialog, for the name, age, and their
city in the format
Name,Age,City
b. Trim the user’s input, in case spaces were entered at the beginning or at
the end.
c. Display the trimmed data to the console. Do the following operations
using this trimmed string.
d. Find the index of the first comma in the input using the indexOf method.
Using this value, extract name part of the input using the substring
method.
e. Display the position of the first comma, the name, and the length of the
name to the console.
f. Find the index of the second comma in the input using the indexOf
method. Using this value, extract the age part of the input using the
substring method.
g. Display the position of the second comma, and the age to the console.
Convert the value to an integer. Display to the console their age in 10
years.
h. Extract the city using the substring method. Display to the console the
city and the length of the city to the console.
i. Now, examine the code for all possible exceptions that could occur.
Handle all the different exceptions explicitly and print out the appropriate
exception message to the console.
Sample Input:
Sample Output:
Also, test the following inputs where exceptions will occur and your program
handles them gracefully -- when Cancel is clicked, when only the name is
entered, when only the name and the age are entered, and when all the
three values are specified but the age is not an integer
Part 2 (60 Points) – StringTokenizer and File Input
Create a package named cs520.hw3.part2. Using this package, create the following
classes.
1. Create a class named Senator as follows.
a. The instance (or member) private variables – name, party, state (String),
and yearsInOffice (integer).
b. A single constructor with name as its argument.
c. The public get and set methods for the instance variables.
d. Override the toString method to return the string representation of this
object in the format “<name (<party) from <state has been
the senator for <yearsInOffice years”.
2. Create a Test class to test the following functionality in its main method.
a. 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.
b. 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.
c. Write a private method processInputData which processes its string
input argument as follows.
1. Tokenize the string argument using the StringTokenizer class
using the comma as the delimiter.
2. Extract the name token. Create a Senator object and assign
to the variable currentSenator.
3. 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.
4. 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.
5. 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
Sample Output:
Submission:
Create an archive of your Eclipse project using the following steps. Select the
HW3_lastName project in the Eclipse IDE’s Package Explorer or the Navigator
window.
Click File-Export. Select the General-Archive File option. Click Next.
Specify the “To archive file:” entry as say, C:\Temp\HW3_lastName.zip.
The zip file will be created and stored in the C:\Temp folder.
Submit this zip file as an attachment in the Assignment Section.

More products