| CSC 207 | Algorithms and Object Oriented Design | Spring 2011 |
Summary: In this lab, you will practice more with basic Java types, control structures, and static method calls.
cd somewhere/CSC207/labs/intro
Scanner type, described in Weiss
2.6.2. Its nextLine() method is very convenient.
lastname:firstname:assignment:grade
Doe:John:HW1:78 Doe:Jane:HW1:92 Doe:J:HW1:80
Grader, that prompts the user for the
name of a file, reads one line of the form above from the file,
segments the line into the four parts (last name, first name, name
of homework, and grade) and then prints the data in human-readable
form, such as
John Doe received a 78 on HW1.
indexOf to find the index of the
colon(s) and then substring to extract the
appropriate portion. More directly, you can use
the split method of the String class
with the colon as the (regular expression) string to split upon
matching.
main method) that
contain methods other than main. These are
called static methods (we'll discuss what that means soon). For
now, it suffices to know that they have the same interpretation as the
functions you are used to in C (cf. Weiss 1.6).
MyUtils.java
int variables.
main method that tests your routine by printing
some examples.
main method to test your routine by
printing some eamples.
MyUtils class a static method that
takes a year as an integer parameter,
returning true if it is a leap
year, false
otherwise. (See Leap
year: Algorithm at Wikipedia.)
main method to test your routine by
printing some eamples.
double, computes the square of the square root of 2
and subtracts 2 from the result, printing the result to the screen.
(Henceforth you should use System.out to generate
output for the screen.)
Note that java.lang.Math.sqrt is useful for computing square roots.
double approximates real
numbers. One problem with approximation is that a sequence of
operations can lead to less and less good approximations.
In response to this problem, many programmers write code that
determines whether two values are approximately equal.
MyUtils class that takes two
real numbers and determines whether they are approximately
equal. You may choose the metric for approximate equality,
but it should be a sensible metric.
main method to test your routine by
printing some examples.
main method) that prompts the user for two real
numbers and uses your approximately-equal test of the previous
exercise to report whether they are considered equal.
Note: You refer to a static method of
another class (in the same directory) with the
syntax ClassName.methodName( ... ).
Hint: Java's Scanner class has a useful
method for reading doubles (among other types).
alternatecharacters or sequences of characters are used in place of familiar alphabetics. For example, a plus sign (
+) is used in place of the letter t, a 3 in place of the
letter e, the numeral 1 in place of the letter l, and the numeral 0 in
place of the letter o. In some cases, multiple symbols are used in place
of a single letter, such as a vertical bar and a 3 in place of b or B.
or a vertical bar, a backslash, and a vertical bar in place of n.
Write a main class, DeLeet, which prompts the user for a
phrase in this odd language and attempts to return that phrase to its
English form. Here is a sample run of the program
Whassup? 3@+ |3@|\|@|\|@ I think you said 'eat banana'.
String class. Read the notes below before proceeding.
| is a special character in a regular
expression. Therefore, it must be "escaped" with a
backslash.e.g, \| for a regular expression
interpreter. However, in Java, the backslash is itself an
important character indicating an escape sequence at the String
level. Thus, to search for a "literal" pipe in a regular
expression, one needs not only to escape the pipe (with a backslash) for
the regular expression, but also escape the escaping backslash
with another backslash for the Java compiler,
giving \\|.
Consider also the problem of searching for a literal backslash,
also a special character (an escape sequence!) in a regular
expression. One must escape the backslash at the regular
expression level, but then also escape both of these at the
string level. A search for one backslash therefore requires four
backslashes.
Counter with a main method that takes
three integers as command-line arguments -- a starting value, an
ending value, and an increment -- and then prints all integers
starting with the starting value and ending just before or at
the ending value. For example,
java Counter 5 10 1would produce
Counting...
5 6 7 8 9 10
java Counter 5 12 3
Counting...
5 8 11
java.lang.Integer.parseInt.