CSCI 306
Makeup Questions
Due Thursday November 1
Question #1. Max Points: 5
Purpose: Understand
exceptions, basic file handling
Assignment: Write a class named
FileExceptions
that does the following:
- Opens a file named "data.dat"
- Catches a FileNotFoundException
and displays an appropriate message
- Sets up a FileReader and Scanner for input from the file
- Calculate a sum of the valid numbers in the file (i.e., do a
while/hasNext loop).
- If an invalid character is found, display an error message but
continue processing the file. The invalid character should not affect
the total. This handling should be done by catching an InputMismatchException.
HINT: you will have two try/catch blocks, one will be inside the loop.
Data file:
4.5
a
6.2
b
3
Output:
Input was not a number a
Input was not a number b
Total valid items: 13.7
Question #2. Max Points: 8
Purpose: Understand
short-circuit evaluation, raw types, wrapper classes, and
interfaces.
Assignment: Copy the SetDemo
class from chapter 16. Rename it MySetDemo.
Then do the following:
- First remove the <String> from the line: Set<String>
names = new HashSet<String>(); In a comment at the top of
your program, list what warning message you see and explain what a raw
type is.
- Now put <String> back into the line of code.
- Add a new function named countLessThan,
with the following header:
private static int countLessThan(Set<String>
s, String
target)
This function will count all the items in the set which appear before
the target string. To do this you will use an iterator. The
syntax for declaring an iterator is:
Iterator
iter = s.iterator();
Two iterator functions that you will need are:
iter.hasNext() and iter.next().
You can see a description of these functions on the
Java API. Use short-circuit evaluation in your function to
iterate through the file counting the number of elements before the
target (e.g., while (iter.hasNext() &&
!(iter.next().equals(target))) ). In a comment in the
code, indicate how this short-circuit evaluation is working.
- Remove the loop in main that removes
names.
- Add a loop in main that asks for a
name,
calls countLessThan, then
displays the
result. The user can enter Q to end the loop.
- Now change the line that declares the
Set from using a HashSet to
using a TreeSet. What difference do you see when the names are
printed? Put another comment at the top of the program that
explains the purpose of having an interface
like Set, with different implementations like HashSet and
TreeSet.
- Now declare another Set at the end of
your program named numbers.
Try to declare it as
Set<int>. What error message do you see? How can you
store integers in a Set? Correct the declaration and add two
integer values to the set. Put another comment at the top of your
program explaining wrapper classes.
Question #3. Max Points: 5
Purpose: Gain an understanding of inheritance and abstract classes
Assignment:
- Create an abstract base class named Phone
with one private instance variable of type String named number. Include an abstract
method named with return type void and no parameters named display. Include an accessor
for number and a 1-parameter
constructor.
- Create a subclass of Phone
named CellPhone. Include
one private instance variable of type int named ringTone. Include a
2-parameter constructor (number and ring tone) that calls the parent
class constructor to intialize the phone number. Include a
definition for display which
just displays the phone number and ring tone.
- Include a main method in CellPhone.
First try to create an object of type Phone.
What error message do you see? Put a comment at the type of the
source code to explain abstract classes.
- Now declare a Phone
reference named myPhone and
assign it to a new object of type CellPhone
(using the 2-parameter constructor, you select the
parameter
values).
Submission:
Zip your code and submit on Blackboard to the area named Test1Makeup by midnight on
Thursday. Bring your test to class on Thursday and turn it in
(even if you haven't submitted the zip yet). I will return
the test to you with the revised score. Remember that you
can earn up to half the points you missed on the exam.
NOTE: If you do a question, do it entirely even if you don't need the
maximum points.