Lab 6

CS 111, Programming Fundamentals II, Spring 2014
13 May, Dr. Filip Jagodzinski
Lab 6: UML Diagrams, Inheritance, and Catching Errors
Computer Science
This lab is meant to demo a UML tool in jGRASP, and to introduce you to try­catch code blocks.
If you do not complete this lab by the end of the lab hour, please complete it by Monday, 19 May, 11:59pm.
I. JGrasp's UML diagrams – 50 points
In this part of the lab, you'll gain practice in jGRASP's tool that generates UML and inheritance diagrams.
Although the exercises here pertain to jGRASP, most IDEs have similar features. Moreover, jGRASP can only
generate UML diagrams for java files that have been grouped into projects, so you'll begin with making a Ship
project, using the Ship, CruiseShip, CargoShip, and the ShipDemo java files from last week's lab
(available now on the course website). To complete this part of the lab:
1. Create a folder, lab6, on your computer, and in it create another folder, Ships.
2. Open jGRASP, and in the Browse panel, navigate to the just-created Ships folder.
3. Select the “Project” menu in jGRASP, and select “New”. In the Create Project window that opens, make
sure that “Add files to project now” is checked, and type Ships into the Project Name input field (See
Figure 1 below). Press Next.
4. On the Confirm Project Creation Page, retain all default options, and select Create, to confirm making
the project. Once you've done that, a project file, Ships.gpj, will have been created inside of your Ships
directory.
Figure 1
CS111 Spring 2014, CWU, Lab 6, Dr. Filip Jagodzinski
page 1 of 7
5. An Add Source Files panel will appear. Do not close it. Download from the course website, the Ships,
CruiseShip, CargoShip, and ShipDemo java files, and save them to your Ships
directory. Once you've done that, click on the refresh button (shown on right), and a listing of
the files that you just saved to your Ships directory should appear in the Browse window of
jGRASP (Figure 2). Using the mouse, select all of the files, and click on the Add button, to add these
files to the Ships folder, and project.
Figure 2
6. At this point, the Open Projects Browse panel of jGRASP will include a list of the java files that are part
of the project (Figure 3). In the project explorer section, your java files will be listed as Source Files, and
a UML option will be listed above the Source Files.
Figure 3
CS111 Spring 2014, CWU, Lab 6, Dr. Filip Jagodzinski
page 2 of 7
7. Either double click on the UML icon (shown on the right) in the Project
explorer pane, or right-mouse click on the UML icon, and select “Open”.
At this point, you have not yet compiled your java files, so jGRASP does
not know about the inheritance relationships that exist among your four java files. All that it knows is
that there are four java files (classes), which are displayed in the UML inheritance diagram (Figure 4).
Figure 4
8. To enable jGRASP to discern the inheritance relationships among the four classes, click on the compile
button (Green Plus) on the Project Explorer Panel. When you do this, all of the .java files in the project
will be compiled, and the UML diagram will be updated (Figure 5). Notice that ShipDemo is labeled as
{main}. Also, the Ship class is now labeled as {abstract}, because that class has an abstract method,
toString().
Figure 5
9. Rearrange (by using the mouse, to drag and move) the boxes for the classes, so that the superclass Ship
is at the “top”, and the subclasses CargoShip and CruiseShip, are underneath the Ship class. Do
this so that the UML diagram is similar to the diagrams that are shown in the book, and which have been
described in lecture. A sample rearranged UML inheritance diagram is shown in Figure 6.
Figure 6
CS111 Spring 2014, CWU, Lab 6, Dr. Filip Jagodzinski
page 3 of 7
10. Another useful feature of jGRASP is that it will generate a UML diagram of each class, in your project.
To do this, right click on any of the class names in the UML diagram, and select “Show Class Info”. The
UML diagram of the selected class will appear in the UML diagram panel (Figure 7).
Figure 7
II. Catching Exceptions, and the Stack – 50 points
We've discussed in lecture, how to catch exceptions, so that your program exits 'gracefully'. Catching exceptions
also allows you to write programs that generate an appropriate response, based on the error that is thrown. In
this part of the lab, you'll be given buggy code, to which you will add try­catch blocks, so that the program
does not crash when it encounters an error.
Before you begin this part of the lab, download from the course website, the following two files:
•
•
ABuggyProgram.java
aDataFile.txt
II-a: Handling the FileNotFoundException
1. The java program ABuggyProgram has three intentional run-time errors. Your goal is to catch those
errors, so that the program exits 'gracefully'. Compile and run the program. When prompted, choose the
first option (which invokes the case of the switch statement that throws a FileNotFoundException). You
will receive an error, because the code is looking for a file called aFile.txt, which does not exist.
2. Look a the stack that is printed to the screen, when you run the program ABuggyProgram. As was
discussed in lecture, at the top of the stack, is the most recent method that was executing when the error
was thrown. At the bottom of the stack was the part of the program (main) that originally made the call
to the Scanner; the error is on line 29 of ABuggyProgram. More importantly, the stack tells you the
TYPE of error that was caught: FileNotFoundException. You are not expected to memorize all of
the types of errors that Java can throw, but some of the more common ones you'll become familiar with
as you continue to develop code. Because all errors in java are objects of a certain class that are genrated
when an error is thrown, you can navigate to Java's API to learn about each error:
http://docs.oracle.com/javase/7/docs/api/java/io/FileNotFoundException.html
CS111 Spring 2014, CWU, Lab 6, Dr. Filip Jagodzinski
page 4 of 7
3. Modify the program ABuggyProgram, so that the entire body of the main method is in a try block,
and add a single catch statement that catches exceptions of type FileNotFoundException. Refer
to the lecture slides, for examples how to structure the try-catch block:
try {
} catch (ExceptionType reference_variable_to_exception_object){
}
4. Inside of the catch block for the FileNotFoundException error, issue two
System.out.println methods. The first, is a custom message, and the other, uses the
getMessage method of the exception class, to retrieve the name of the error:
System.out.println(“Ack, a file doesn't exist!”);
System.out.println(e.getMessage());
5. Compile and run your program. Because you've added a catch block, when an error of type
FileNotFoundException occurs, your program will catch the error, and execute the two
println statements in the catch block for the FileNotFoundException.
II-b: Handling Errors of NumberFormatException
1. Run the ABuggyProgram again, and choose the second option; you will receive a
NumberFormatException error:
http://docs.oracle.com/javase/7/docs/api/java/lang/NumberFormatException.html
2. The NumberFormatException is thrown, when a method that is expecting a certain primitive data
type, instead receives another primitive data type. The program throws an error because it invokes the
parseInt method of the Integer class, which expects a String that is a number (such as “342”),
but instead the method is passed a String that is not a number, “hey there”:
String str = "hey there";
int intFormOfStr = Integer.parseInt(str);
3. As you did for the FileNotFoundException, add a catch block, to handle the
NumberFormatException error. It should be its own catch block, separate from the first one that
you've already added to the file. Refer to the lecture slides, for an example on how to have multiple
catch statements:
try {
} catch (ExceptionType1 reference_variable_to_exception1_object){
} catch (ExceptionType2 reference_variable_to_exception2_object){
}
CS111 Spring 2014, CWU, Lab 6, Dr. Filip Jagodzinski
page 5 of 7
4. Inside of the catch block for the NumberFormatException error, invoke
System.out.println, and have it print the message: A conversion error happened.
5. Save and compile your program. Now, when you run your program, and invoke option 2, the error will
be caught, and the program will not crash, but exit gracefully.
II-c: Handling Errors of InputMismatchException
1. Run your program again, and choose the third option; you will receive an
InputMismatchException:
http://docs.oracle.com/javase/7/docs/api/java/util/InputMismatchException.html
2. The InputMismatchException is thrown when a Scanner receives a token that does not match the
type that the Scanner expects. The program throws an error because it invokes the nextDouble()
method to retrieve the next double from the file aDataFile.txt (which is available on the course website).
That file has a misprint, because it contains the letter B (shown in red below) on the
third-from-the-bottom line of the aDataFile.txt file:
846.9
­0.98
1566.41
16.02
136.98
44.02
.9
­0.98
­55.41
0B.02
1546.77
1326.98
3. As you did for the FileNotFoundException, add a catch block, to handle the
InputMismatchException error. It should be its own catch block, separate from the first two
catch blocks.
4. Inside of the catch block for the InputMismatchException error, invoke
System.out.println, and have it print the message: The input was incorrect.
5. Save and compile your program. Now, when you run your program, and invoke option 3, the error will
be caught, and the program will not crash, but exit gracefully.
CS111 Spring 2014, CWU, Lab 6, Dr. Filip Jagodzinski
page 6 of 7
III. What to Hand In, Grading, etc.
Make sure that:
1. Each .java file is commented. If your code does not compile because you've been unable to fix a syntax
error, then the comments that you provide in your code will allow you to receive partial credit.
2. The code in each .java is indented, so that the code is easy to read.
3. Variables should be appropriately named. Thus, amountMoneyInBank is a good variable name, but
variableName is not, because it is not descriptive enough.
From now on, because labs and homework assignments are getting larger and larger, and contain many files,
please upload a single zip file that contains all of the files of your submission.
Create a zip file that contains all of the below files for this lab. On the lab computers that are running the
Windows operating system, right click on the lab6 folder that you created for this lab, and select the option to
zip the folder.
ABuggyProgram.java
aDataFile.txt
Ship.java
CargoShip.java
CruiseShip.java
ShipDemo.java
Ships.gpj
// Proof that you've made a project
Upload the zip file to your canvas account. If you cannot create a zip file, ask one of the lab assistants.
IV. Rubric
File / lab requirement
Points
A Ship project has been created, which includes the Ships.gpj file
50
ABuggyProgram.java written, with three catch blocks
50
Total 100
CS111 Spring 2014, CWU, Lab 6, Dr. Filip Jagodzinski
page 7 of 7