Object-Oriented Programming in Java

advertisement
Object-Oriented Programming in Java
MISM/MSIT 95-712
Summer 2002
Homework 1
This first homework is intended to help you get comfortable with the edit-compile-run
cycle of Java programming. If you’ve had previous experience programming, writing the
code itself should not be difficult. If writing the actual code (as opposed to learning to
use your programming tools) takes longer than an hour or so, you should consider talking
with the TAs or me.
1. (50 points) Write a program that reads a file line-by-line and prints out each line
that contains a specified “pattern” or string of characters. For example, when
given a file containing
indexOf
public int indexOf(String str)
Returns the index within this string of the first occurrence of the specified
substring. The integer returned is the smallest value k such that:
this.startsWith(str, k)
is true.
Parameters:
str - any string.
Returns:
if the string argument occurs as a substring within this object, then the
index of the first character of the first such substring is returned; if it does
not occur as a substring, -1 is returned.
and the pattern “string”, your program should print the lines
public int indexOf(String str)
Returns the index within this string of the first occurrence of the specified
substring. The integer returned is the smallest value k such that:
str - any string.
if the string argument occurs as a substring within this object, then the
index of the first character of the first such substring is returned; if it does
not occur as a substring, -1 is returned.
Your program should prompt the user for a filename and the pattern to search for.
(The above text was taken from the Java documentation for the String class.)
If you need to, look ahead in Chapter 3 in the textbook to see how the if-else
control structure works in Java.
To submit your homework, hand in
 a printed listing of your Java code, with your name as the first line at the
top
 a 3½” floppy disk containing your .java and .class files. These
should be in a folder named Problem1-1. Your program should run
when a TA sets the current directory to a:\Problem1-1 and types
java FindPattern.class
at the command prompt. Your diskette should contain only your
homework directories, and should be clearly labeled “Java Homework 1
<your name>”.
2. (50 points) Use the Date class illustrated in the HelloDate example program
from the course notes to test how fast you can type the sentence “I type very
quickly”. Your program should print
Your job is to type the sentence "I type very quickly" as fast as you can.
When you are ready, press enter, type the sentence, and press enter again.
Now press enter...
Your program should print the number of milliseconds the user took to type the
sentence. Of course you must check to see if the user typed the sentence
correctly, and print a message if the user input is incorrect. My personal best time
is 6325 milliseconds. See if you can beat me!
To submit your homework, hand in
 a printed listing of your Java code, with your name as the first line at the
top
 your .java and .class files, on the same disk you used for Problem 1
above, in a folder named Problem1-2. Your program should run when
a TA sets the current directory to a:\Problem1-2 and types
java TypeFast.class
at the command prompt.
Download