Homework - Interfaces, Exceptions, Stream
I/O
-
Define an interface called Collection with the following methods:
- a method named isFull (with no parameters) - if the collection is full
then it returns true; otherwise it returns false.
- a method named add (with one parameter of type Object) - if the
collection is not full then it adds the given object to the collection
and returns true; otherwise it returns false.
- a method named isIn (with one parameter of type Object) - if the
given Object is in the collection then it returns true, otherwise it
returns false.
-
Write a class called myCollect that contains an array of 100 Objects
and which satisfies the Collection interface. Use the array to hold the
Objects added to the Collection. You can add other fields to myCollect
if you need them. (Don't forget to write a constructor for your
class - a default constructor is sufficient.)
- Suppose there are also classes Set and List that implement the
Collection interface. Write a method which has two parameters -- an Object,
and a Collection (some object that implements Collection). The method
should add the Object to the Collection. If there is no room for the Object
then the method should print an error message to the screen.
-
Consider the following code:
public class TestClass
{
public static void main(String[] args)
{
PrintWriter out = new PrintWriter(System.out, true);
String words[] = new String[4];
words[0] = "at";
words[1] = "second";
words[2] = "third";
words[3] = "fourth";
try
{
methodA(words, out);
out.println("Done with A");
}
catch(ArithmeticException e)
{
out.println("Arithmetic Error");
}
catch(Exception e)
{
out.println("Error occurred:" + e);
}
}
static void methodA(String[] words, PrintWriter out)
{
int r;
try
{
// get random num for value of r
r = (int) Math.round(10 * Math.random());
words[r] = "new";
char c = words[0].charAt(r);
r = r / r;
}
catch(ArrayIndexOutOfBoundsException e)
{
out.println("array error");
}
catch(StringIndexOutOfBoundsException e)
{
out.println("string error");
}
try
{
// get random num for value of s
int s = (int) Math.round(10 * Math.random());
int inum = Integer.parseInt(words[s]);
}
catch(NumberFormatException e)
{
out.println("Error in number format");
}
}
}
What will be printed if the program is executed using the following
values the random numbers in methodA. (It's possible that the statements
to generate s might not be reached in some cases.)
- 1st execution: r is 0, s is 4
- 2nd execution: r is 3, s is 0
- 3rd execution: r is 1, s is 2
- 4th execution: r is 1, s is 5
- 5th execution: r is 8, s is 1
Write a static method to read a file that contains integers and
write the integers to another file. The integers are separated by blanks,
and the number of integers on each line of the file varies. The output
file should have one integer on each line. The names of the input and
output files are passed to your method as String parameters.
Write a Java application to read doubles from a file and print
them out on the screen, one number on each line. Assume that there is
one double on each line of the file. The filename is passed to the
program as a command line parameter. Print an error message if the
file does not
exist. If any value in the file is not a double, print an error message
for that value, and then continue reading the rest of the values.
Write a Java application that reads the words contained in a
file named words.in. There may be
more than one word on each line of the file. After reading the words,
the program should print all words that contain the letter b
(uppercase or lowercase), then find all words that end with an s, and
print those words with the s removed.
Email Me |
Office Hours |
My Home Page |
Department Home |
MCC Home Page