Laboratory: Types and Control in Java
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.

Preparation

  1. Go to your directory from the previous lab.
      cd somewhere/CSC207/labs/intro
  2. Open the Java API reference in a new tab. (There is a link on the course home page. You may want to add to your bookmarks bar. I do!)

Exercises

In this lab, we will gather input using Java's Scanner type, described in Weiss 2.6.2. Its nextLine() method is very convenient.

Exercise 1

Programmers often find it convenient to store compound data in a text file with one line per entry. To separate the components of the entry, they use some designated symbol, such as a colon. For example, we might store student grades as
lastname:firstname:assignment:grade
with sample entries of
Doe:John:HW1:78
Doe:Jane:HW1:92
Doe:J:HW1:80
Write a main class, 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.
You may find it easiest to first ensure that you can print the last name, then to add the first name, and so on and so forth.

There are (at least) two ways to approach this problem. First, you could use 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.

Exercise 2

You have seen many examples from the reading of class files (most of which are programs because they contain a 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).
  1. Create a new file called MyUtils.java
  2. Write a static method that returns the maximum of three int variables.
  3. Add a main method that tests your routine by printing some examples.
  4. Write a static method that takes three strings as parameters and prints them in alphabetical (lexicographic) order (cf. Weiss 2.3.3) and returns nothing.
  5. Modify your main method to test your routine by printing some eamples.

Exercise 3

  1. Add to your 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.)
  2. Modify your main method to test your routine by printing some eamples.

Exercise 4

  1. Write a simple program that, using the Java primitive type 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.

  2. Ideally, the difference should be 0; why isn't it? How big is the difference?

Exercise 5

As should now know, the type 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.

  1. Add a method to 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.
  2. Update your main method to test your routine by printing some examples.

Exercise 6

Write another program (i.e., create new class file with a 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).

For Those with Extra Time

Exercise 7

You may be familiar with a juvenile form of writing, known as 133+ or leet, in which alternate characters 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'.
If you haven't already, you may wish to explore the methods available in the String class. Read the notes below before proceeding.

Important Notes

A pipe | 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.

Exercise 8

Write a class 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 1
would produce
Counting...
5 6 7 8 9 10
      
Similarly,
  java Counter 5 12 3
Counting...
5 8 11
      

You may wish to recall the method java.lang.Integer.parseInt.

Created: Samuel A Rebelsky, 31 January 2005
Modified: Jerod Weinman, 17 January 2011
Modified: Jerod Weinman, 27 January 2011
Portions adopted from Espresso: Laboratory: String Basics, Espresso: Laboratory: Numeric Values in java, Espresso: Laboratory: Loops,