Laboratory: Types and Control in Java
CSC 207 Algorithms and Object Oriented Design Spring 2009

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

Exercises

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 StringTokenizer discussed in Weiss 2.6.2

Exercise 2

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'.

Exercise 3

  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.)

    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 4

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.

Write a program that prompts for and reads in 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.

You may wish to know about the method java.lang.Double.parseDouble.

Exercise 5

You have seen many examples from the reading of class files (most of which are programs since 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 (see 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 a year as an integer parameter, returning true if it is a leap year, false otherwise. (See Leap year: Algorithm at Wikipedia.)
  5. Modify your main method to test your routine by printing some eamples.
  6. Write a static method that takes three strings as parameters and prints them in alphabetical (lexicographic) order (see Weiss 2.3.3) and returns nothing.
  7. Modify your main method to test your routine by printing some eamples.

Exercise 6

Write a main class, Counter, 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.

Portions adopted from Espresso: Laboratory: String Basics, Espresso: Laboratory: Numeric Values in java, Espresso: Laboratory: Loops,