Exercise 2

In this weeks's exercise, we will work with the following problems. The idea is that you are working with the problem with paper and pen; The teacher is on hand to answer questions and help. Some questions may be resolved on the board.

  1. Define a function with signature:
      public static boolean allEqual(int[] a)
    
    that takes an integer array a as a parameter and returns true if all the numbers in the array are equal; otherwise it returns false. Also write a main method to test the function.
  2. In the test program in the previous task, you would have had used the method:
    public static int[] parseAll(String[] args)
    
    that creates and returns an array of integers, built by using Integer.parseInt on each element in the array of arguments. Define this function and the test program.
  3. Define a method with signature:
       public static int maxArray(int[] a)
    
    which returns the largest number found in the parameter array. As usual, you should also write a test program.
  4. Define a method:
    public static double[] reverse (double[] a)
    
    which returns an array with the same elements as in the parameter a, but in reverse order.
  5. Define a method:
      public static void reverse (double[] a)
    
    that changes the contents of the input array, so that the numbers are reordered in reverse order.

    Then write two test programs that use the two different versions of reverse. Also, draw an outline of how the memory looks in the two programs.

  6. The first task on the exam in March 2010:

    Consider the following Java program:

    public class Uppgift1 {
    
      public static void swap(int i, int j, int[] a) {
         int tmp = a[i];
         a[i] = a[j];
         a[j] = tmp;
      }
    
      public static void main(String[] args) {
         int[] a = {3, 7, 9};
         swap(0,2,a);
         for (int i=0; i < a.length; i++)
           System.out.print(a[i] + " ");
         System.out.println();
      }
    }
    
    What printout would you get when the program is run? Explain briefly, preferably with the help of a figure.

Optional

  1. Write a program CurrencyConverter that asks the user to enter today's rate for one Swedish krona in euro. Then the program reads Swedish kronor values and converts each to euro values. Stop when the user enters Q.
  2. The Fibonacci sequence is defined by the following rule. The first two values in the sequence are 1 and 1. Every subsequent value is the sum of the two values preceding it. For example, the third value is 1+1 = 2, the fourth value is 1+2 = 3, and the fifth is 2+3 = 5. If fn denotes the n-th value in the Fibonacci sequence, then
    f1 = 1
    f2 = 1
    fn = fn-1 + fn-2   if n > 2
    
    Write a program that promts the user for n and prints the first n values in the sequence. Hint: There is no need to store all values for fn. You only need the last two values to compute the next one.
  3. Write a program that prints the powers of ten, i.e. 1, 10, 100, 1000, etc. Implement a class PowerGenerator which has the structure:
    public class PowerGenerator {
    
        public PowerGenerator() { .... }
    
        public double nextPower() { ... }
    
    }
    
    then supply a test class PowerGeneratorRunner that calls System.out.println(myGenerator.nextPower()) twelve times.