Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hello, I have this homework to do. I will paste the assignment request on an image below. I have done the two classes: Senator and

Hello, I have this homework to do. I will paste the assignment request on an image below. I have done the two classes: Senator and SenatorTest.java. I wanted someone to go over both, specially the SenatorTest.java and let me know if there's anything missing or anything I can improve?

Senator.java:

public class Senator {

// set instance variables

private String name, party, state;

private int yearsInOffice;

// set name

public Senator(String name) {

this.name = name;

}

// get and set methods for the instance variables

public void setName(String name) {

this.name = name;

}

public String getName() {

return name;

}

public void setParty(String party) {

this.party = party;

}

public String getParty() {

return party;

}

public void setState(String state) {

this.state = state;

}

public String getState() {

return state;

}

public void setYearsInOffice(int yearsInOffice) {

this.yearsInOffice = yearsInOffice;

}

public int getYearsInOffice() {

return yearsInOffice;

}

@Override

public String toString() {

return this.name + " (" + party + ") from " + state + " has been the senator for " + yearsInOffice + " years.";

}

}

senatortest.java

public class SenatorTest {

// senatorTest class to test functionality in its main method

public static void main(String[] args) {

// declare and initialize an empty queue of senator objects named senator queue

LinkedList senatorQueue;

senatorQueue = new LinkedList();

// declare and initialize an empty HashMap named senatorMap

HashMap senatorMap = new HashMap();

// opening the file and using the bufferedReader to read the data.txt

FileReader fileReader = null;

BufferedReader reader = null;

try {

String inputFileName = "data";

// create the FileReader object

fileReader = new FileReader(inputFileName);

reader = new BufferedReader(fileReader);

// set reading one line at a time

String input = reader.readLine();

while (input != null) {

Senator senator = processInputData(input);

input = reader.readLine();

senatorQueue.add(senator);

senatorMap.put(senator.getName(), senator);

}

// Close input

} catch (Exception e) {

e.printStackTrace();

} finally {

try {

if (reader != null) {

reader.close();

}

fileReader.close();

} catch (IOException ioe) {

ioe.printStackTrace();

}

}

}

Exercise:

private static Senator processInputData(String line) {

// here I am token-nizin' the string argument using comma as delimiter

StringTokenizer st = new StringTokenizer(line, ",");

// extract name token, create senator object and assigning to thelocal variable current senator

String name = st.nextToken();

Senator currentSenator = new Senator(name);

try {

int yearsInOffice = Integer.parseInt(st.nextToken().trim());

currentSenator.setYearsInOffice(yearsInOffice);

} catch (NumberFormatException e) {

currentSenator.setYearsInOffice(-99);

}

String party = st.nextToken();

currentSenator.setParty(party);

String state = st.nextToken();

currentSenator.setState(state);

// display to console

System.out.println(currentSenator);

return currentSenator;

}

}

image text in transcribed
2. Create a SenatorTest class to test the following functionality in its main method. a. Declare and initialize an empty Queue of Senator objects named senatorQueue. b. Declare and initialize an empty HashMap named senatorMap. The keys will be the names of the senators and the entries in the map will be the corresponding senator objects. c.Use the BufferedReader class to read the data.txt file. The contents of the le are shown below. Create the data.txt file in HW5_IastName. d. 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. In this loop, 1. Invoke the processlnputData method for each line read. This method returns the corresponding Senator object. 2. Add this Senator object to the senatorQueue. 3. Insert this Senator object into the senatorMap using the senator's name as the key. e. After the loop is processed, do the following. 1. Iterate over the senatorQueue and display each element to the console. 2. Access the keys of the senatorMap and assign them to an appropriate variable. Create an iterator over the keys. Iterate over each key in this set and display the associated object in the map to the console. Write a private method processlnputData with return type Senator which processes its string input argument and returns the corresponding Senator object as follows. 1. Tokenize the string argument using the Stn'ngTokenizer class using the comma as the delimiter, or using the String split method. Extract the name token. Create a Senator object and assign to the local variable currentSenator. . 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 yearsanffice is not a valid number, i.e., throws an exception when parsed, set the value as -99 in the exception handling part. . The method should retum the currentSenator object

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

Financial management theory and practice

Authors: Eugene F. Brigham and Michael C. Ehrhardt

12th Edition

978-0030243998, 30243998, 324422695, 978-0324422696

Students also viewed these Programming questions

Question

Mention the bases on which consumer market can be segmented.

Answered: 1 week ago

Question

Explain consumer behaviour.

Answered: 1 week ago

Question

Explain the factors influencing consumer behaviour.

Answered: 1 week ago