COMP 110-001 - Introduction to Programming (Spring 2009)

Lab 3

25 points

Assigned: February 6th

Due: February 12th by midnight

Description

Part 1

This part of the lab will give you some practice with boolean expressions and the boolean operators && and ||.

On the course webpage, you will find ThreeNumbers.java. This program asks the user to enter in three integers on a line separated by spaces. Copy and paste "ThreeNumbers.java" into a new Java file in jGRASP, and edit the Program Header to reflect your name and assignment information

Add code to the program to check for the following relationships between the three numbers. You should add your code where indicated by the comment //YOUR CODE HERE. The table below describes the relationships you must check for and the output message your code should produce in each case.


Condition

Output

All three numbers are equal All three of those numbers are equal.
Any two numbers are equal Two of those numbers are equal.
None of the numbers are equal None of those numbers are equal.

Part 2

For this part, you will be converting a switch statement into an equivalent multi-branch if-statement.

On the course webpage, you will find DaysOfTheWeekSwitch.java. This is a complete program that uses a switch statement to print out a message depending on the day of the week (1-7) entered by the user. You will also find DaysOfTheWeekIf.java, which is an incomplete program that you will be working with.

Copy and paste DaysOfTheWeekIf.java into a new Java file in jGRASP, and edit the Program Header to reflect your name and assignment information. Add a multi-branch if-statement to "DaysOfTheWeekIf.java" that is equivalent to the switch statement in "DaysOfTheWeekSwitch.java". The if-statement you add will be considered equivalent to the switch statement if it produces the same output for any input.

You should NOT edit or submit "DaysOfTheWeekSwitch.java" as part of this assignment

Part 3

Enumerations are useful for many things including enumerating the possible errors a program may encounter. In this part of the lab, you will write part of an error handler that simulates the error handling that occurs in production software packages.

On the course webpage, you will find ErrorHandler.java. Copy and paste "ErrorHandler.java" into a new Java file in jGRASP, and edit the Program Header to reflect your name and assignment information. In this program, an enumeration is defined that describes the different errors that may be encountered:

enum Error {NULL_POINTER, OUT_OF_MEMORY, DIVIDE_BY_ZERO, OVER_FLOW, UNKNOWN_ERROR}

This program as given, generates an error of the enumerated type Error and stores it in a variable called "error". The program also prints out a numeric error code indicating the type of error encountered. For example:

Error 0x3000 enountered!

Since an error code by itself is not very useful, you will need add a switch statement to the program where indicated by the comment //YOUR CODE HERE that switches on the variable "error" and prints an appropriate error message to the screen depending on which error was encountered. The table below describes what your program should print for each error.


Error

Output

NULL_POINTER Null Pointer
OUT_OF_MEMORY Out of Memory
DIVIDE_BY_ZERO Divide by Zero
OVER_FLOW Over Flow
UNKNOWN_ERROR Unknown Error

Submission

For this assignment you will need to create a Jar file that contains the source code for ThreeNumbers.java, DaysOfTheWeekIf.java, AND ErrorHandler.java that you edited as part of this assignment. Follow these instructions for creating a Jar file.
The Jar file you submit must be named yourlastname_lab3.jar, where yourlastname is your last name

Submit yourlastname_lab3.jar via the UNC Blackboard System under "Assignments → Lab 3" before the deadline.

Grading