Espresso: A Concentrated Introduction to Java


Laboratory: Static Methods

Summary: In this laboratory, you will build a number of static utility methods.

Contents

Exercises

Exercise 0: Preparation

a. Create a project named Code. It is likely that you will use this project for the remainder of the semester.

b. Within the project, create a package named username.util.

c. Start Eclipse and load the project.

Exercise 1: Reflection: Static vs. Object Methods

Make a list of a dozen or so methods that you have already used and, for each, indicate whether it is a static method or an object method. Remember: If you call it with ClassName.methodName, it is a static method, and if you call it with objectName.methodName, it is an object method.

Exercise 2: Reading Integers

a. Create a new utility class, IO, and, within that class, write a method, readInt(PrintWriter pw, BufferedReader br, String prompt), that prompts the user for an integer and returns that integer. You may want to refer to the reading for ideas about the structure of readInt.

b. Create a new main class, TestIO, that tests that method. The main method in your new class should resemble the following

  public static void main(String[] args)
    throws Exception
  {
    PrintWriter pen = new PrintWriter(System.out, true);
    BufferedReader eyes = new BufferedReader(new InputStreamReader(System.in));
    int i = IO.readInt(pen, eyes, "Enter an integer and I will square it: ");
    pen.println(i + "*" + i + " = " + (i*i));
  } // main(String[])

c. Update readInt so that it only prints the prompt if the prompt is not null. Your test should look something like

  if (prompt != null) {
    ...
  }
  else {
    ...
  }

d. Test the revised readInt by reading two numbers from a file and multiplying them. Note that you should not prompt for those numbers. For example,

  BufferedReader fileReader = new BufferedReader(new FileReader(new File("foo")));
  int x = IO.readInt(pen, fileReader, null);
  int y = IO.readInt(pen, fileReader, null);
  pen.println(x + "+" + y + "=" + (x+y));

Exercise 3: Other Numeric Input

a. Add readDouble to your IO class.

b. Test readDouble.

c. Add readBigInteger to your IO class.

d. Test readBigInteger.

Exercise 4: Mathematical Utilities

a. Create a new utility class, MyMath, and, within that class, create a method, boolean equiv(double x, double y), that returns true if x and y are nearly equal and false otherwise.

b. Write a main class, TestEquiv, that verifies that the MyMath.equiv method works correctly.

c. Add an expt(BigInteger x, int n) method, similar to that of the reading, to your class.

d. Write a main class, TestExpt that permits you to verify that the MyMath.expt method works correctly.

Exercise 5: Square Roots

You are fortunate that Java provides a built-in square-root method, Math.sqrt. But what would happen if that method were not available? You would have to write your own procedure and put it in a utility library. Let's try doing so.

Fortunately, there is a well-known algorithm, the Newton-Raphson method, for computing square roots. The algorithm starts with some approximation of the square root and repeatedly improves that approximation until the approximation is good enough (that is, the absolute value of the difference between n and the square of the approximation is less than some specified epsilon).

Suppose we are computing the square root of a number n, and our current approximation is a. How do we improve a? We take the average of a and n/a. Note that this is a very sensible way to improve approximations because (a) if the approximation is already correct, a and n/a will be the same, so the approximation will remain correct; (b) if a is too small, n/a will be too large, so the average will be closer to the root; (c) similarly, if a is too large, n/a will be too small.

What approximation should we start with? 1.0 seems to be a decent starting point.

a. Write and test a sqrt(double n, double epsilon) method for the MyMath class.

b. Write and test a variant of the same method that takes a PrintWriter as a third parameter and prints the approximation at every step.

History

Thursday, 9 February 2006 [Samuel A. Rebelsky]


This page was generated by Siteweaver on Thu Mar 30 15:24:43 2006.
The source to the page was last modified on Thu Feb 9 13:31:31 2006.
This page may be found at http://www.cs.grinnell.edu/~rebelsky/Espresso/Labs/static-methods.html.

You may wish to validate this page's HTML ; Valid CSS! ; Check with Bobby

Samuel A. Rebelsky
rebelsky@grinnell.edu