Java Lab 3
Due: Friday, September 8, 11:00 AM EDT
In this lab, you will practice with simple input and output and if/switch statements.
1. Create a project named Lab3. Download the file IOStuff.java from Canvas and copy it to the src directory.
Run the program; enter some sample data when prompted. Then run the program again. See if you can break the program by entering bad values – which input lines can you break?
2. Change the line that reads in the age to the following: create a String variable named ageString; read the age into it using nextLine( ) instead of using age and nextInt( ); convert the String to an int using Integer.parseInt( ) and store it in age. Do something similar with the line that enters the salary: create a String variable named salaryString; read it using nextLine( ) instead of nextDouble( ); convert the String to a double using Double.parseDouble( ). Can you break this by entering bad values?
3. Surround the code for #2 with this:
try {
#2 goes here>
} catch (NumberFormatException e ) {
System.out.println(e);
age = 0;
salary = 0.0;
}
Can you break this?
4. Comment out the println statements that display the output by highlighting those lines, then use the IntelliJ menu choice
Code->Comment with Block Comment
Add one printf( ) statement to display the labels (column headers) and another printf( ) statement to display the data as a (very short) table, with nicely spaced columns, something like this:
5. First, comment out the code that prompts the user to enter employee data at the keyboard, down through and including the answer to #3. Keep the table headers of #4 but comment out the line that prints the employee data (you'll be doing it over below).
Now download the file "employee.csv" from Canvas. Copy it into the project directory (the root Lab3 directory, *not* the src folder). Open it in IntelliJ to see the format – it's just the same data as above as a Comma Separated Value file. Adapt the code from the notes (reading input from a file) to read the file. You'll have to split the data (see the String notes) and convert the age and salary fields to numeric; don't use a try-catch block for these, although a real program should – just use parseInt( ) and parseDouble( ) and hope for the best. Then display a formatted table something like this (adapt the code from #4, too):
6. Now add a new column to the output called "Category". Create a String variable named category and initialize it to null. If the employee’s salary is either negative or greater than 150000, reset category to "Error". If the employee's salary is at least 0 but less than 35000, re-set category to "Low"; if it's at least 35000 but less than 70000, set category to "Medium"; and if it's at least 70000, set it to "High". Display category in the last column. Your output should look something like this:
Deliverable: Add your name and Andrew id to the comment at the top of the file. Upload the file to Canvas.