Question
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 = new LinkedList
// declare and initialize an empty HashMap named senatorMap
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;
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started