Exercise 3

In this weeks's exercise, we will work with the following problems.

For this exercise, the file counterPrograms.zip contains three subdirectories, each with its incomplete programs. The programs are described below. Each program can be updated and tested later.

The main task during this practice session is to write several different classes all of which implements the interface Counter.

  1. (Rehearsal; should go fast) Define a function
    public static boolean contains(int elem, int[] a);
    
    such that contains(elem, a) determines if the number elem can be found in the array a.
  2. The picture below shows a counter which can be used for counting a large number of people, passing by, when there is a risk of misscounting.

    Such counter stores an integer value. As a user, you can do three things with it:

    This is reflected in the interface:
    public interface Counter {
        public void count();
        public void reset();
        public int getValue();
    }
    
    Define a class SimpleCounter, which implements this interface, i.e.: The physical counter is shipped from the factory with a value set to 0; in analogy with this, Java counters should have constructors setting the value to 0.
  3. What printout is obtained when running the following programs? Provide a brief explanation for each line:
    public class Question3 {
    
      public static void changeValues(int n,
                                      SimpleCounter c1,
                                      SimpleCounter c2) {
        n++;
        c1.count();
        c2 = new SimpleCounter();
        c2.count();
      }
    
      public static void main(String[] args) {
        int k=5;
        SimpleCounter a = new SimpleCounter();
        SimpleCounter b = new SimpleCounter();
        changeValues(k,a,b);
        System.out.println("k="+k);
        System.out.println("a="+a.getValue());
        System.out.println("b="+b.getValue());
      }
    }
    
  4. This task requires access to a computer and can be done later.

    The four files in the subdirectory simpleCounter, together with class SimpleCounter.java from exercise 2 is a graphical programs that can replace the physical counter; a picture of the program during execution is given below:

    Add your SimpleCounter.java to the directory, compile and test the program.

  5. Modify the class SimpleCounter by adding another constructor that takes a parameter int init. A counter created with this constructor has the starting value init instead of zero. A class may thus have more than one constructor, if they differ in the type and/or number of parameters.

    Now, change the main method in the program so that you instead create a counter with initial value 95. Run the program and click a few times until the counter value passes 100. Apparently, the graphical program is poorly prepared for this; the window accommodates only two digits.

    One can imagine two different measures:

  6. Define a class BoundedCounter which also implements Counter, but that is different from Counter in two ways: Then replace the class SimpleCounter with BoundedCounter in the program from task 4 and test. Note that in addition to adding the file BoundedCounter.java you need to change the code in Main.java; class CounterPanel does not know with what kind of counters it is working.
  7. In clockCounter there is a program that acts as a digital clock. This is a screenshot:

    Note that the CounterPanel in this catalog is the same as in the previous      programs. Here we have three objects of this class, so we have three separate counters.

    The program starts by giving the current time on the command line; for example:

    > java Main 8 42 20
    

    Unfortunately, there is no class ChainedCounter.java. You must write it. This class also implements the interface Counter, but it differs from the BoundedCounter as follows:

  8. In the reflexCounter folder there is an additional program which uses a ChainedCounter, to test the user's reaction:

    Press Start and wait until the counter begins to count; press the Stop as soon as possible.

Optional

  1. Modify the class RectangleComponent from the example in the lecture to show a square of size 50 by 50 and change the color of the rectangle to red. After you have drawn the first square, move and rescale the box object by using its methods translate and grow. Draw the modified box in black. The combined picture must look like this:

    Consult the Java API for the method Graphics.setColor. It will let you to change the current color.

  2. In addition to the method draw, the Graphics2D class has also the method fill which fills in some shape with the current color. Draw the Swedish flag by using one big blue rectangle and two smaller yellow rectangles over it. Experiment with the size and the coordinates of the rectangles. Consult the Java API for the methods Graphics.setColor and Graphics2D.fill.