星期六, 5月 21, 2005

Lab 5/27

Lab Object Variables

Lab Sorting

Study Display 6.1, and then write a program that can sort 5 numbers in ascending order. The 5 numbers are input by the user.

Lab Object Variables

Study Display 5.9 and then write a program such that
1. an object variable aObject of class ToyClass holds the content of ("Mrs. Smith",1)
2. another object variable bObject of class ToyClass also holds the content of ("Mrs. Smith",1)
3. the other object variable cObject that have the same reference as aObject
4. Test whether aObject == bObject, meaning the two have the same reference.
5. Test whether aObject.equals(bOject), meaning the two hold the same content.
6. Test whether aObject == cObject
7. Test whether aObject.equals(cOject)

Answer to 5/20 Lab (Fibonacci Class)

public class StaticDemo
{
public static void main(String[] args)
{
for (int i = 3; i < 15; i++)
{
System.out.print("Fib( " + i + " )=" );
System.out.println(Fibonacci.next());
}
}
}


public class Fibonacci
{
private static int i=1;
private static int j=1;
private static int k;

public static int next()
{
k = i + j;
j = k;
i = j;
return k;
}
}

星期五, 5月 20, 2005

5/20 Lab (Fibonacci Class)

Study Display 5.2.
Using static variables and static methods to implement static class Fibonacci such that
the first call to Fibonacci.next()
returns 2, the second returns 3, and then 5, and so on.