Title: Counter Practical whats going on
1Counter Practicalwhats going on?
- David Corsar
- dcorsar_at_csd.abdn.ac.uk
2Last week
- Last week we looked at how to
- Use rectangle as a supplier class for displaying
a rectangle and getting it to change colour - Use 2 supplier classes (MovingSun and Oval) in
the Sun Picture practical, and combine with a
simple controller class (SunPicture) which tells
MovingSun what to do
3Counter Practical
- We also started the Counter practical, today we
will look at how it works
4Counter practical Classes
Makes the 3 digit counter, tells each digit to
increment, decrement, makeZero where appropriate
Displays a Digit of the counter
Controller class provides window for Counter to
display and tells it what to do depending on
button press
5What happens when it runs
- Load BlueJ, create new CounterWindow
- BlueJ creates a new instance of CounterWindow,
calling its Constructor
public CounterWindow() window new
ThreeButtonFrame("Counter") counter new
Counter(window)
6What happens when it runs
- Load BlueJ, create new CounterWindow
- BlueJ creates a new instance of CounterWindow,
calling its Constructor - CounterWindow initialises counter to a new
Counter instance
public CounterWindow() window new
ThreeButtonFrame("Counter") counter new
Counter(window)
7What happens when it runs
- Load BlueJ, create new CounterWindow
- BlueJ creates a new instance of CounterWindow,
calling its Constructor - CounterWindow initialises counter to a new
Counter instance - Counter then initialises its 3 Digits
public Counter(JFrame frame) units new
Digit(..., frame) tens new Digit(...,
frame) hundreds new Digit(..., frame)
8What happens when it runs
- Load BlueJ, create new CounterWindow
- BlueJ creates a new instance of CounterWindow,
calling its Constructor - CounterWindow initialises counter to a new
Counter instance - Counter then initialises its 3 Digits
public Counter(JFrame frame) units new
Digit(..., frame) tens new Digit(...,
frame) hundreds new Digit(..., frame)
9What happens when it runs
- Load BlueJ, create new CounterWindow
- BlueJ creates a new instance of CounterWindow,
calling its Constructor - CounterWindow initialises counter to a new
Counter instance - Counter then initialises its 3 Digits
public Counter(JFrame frame) units new
Digit(..., frame) tens new Digit(...,
frame) hundreds new Digit(..., frame)
10What happens when it runs
- Load BlueJ, create new CounterWindow
- BlueJ creates a new instance of CounterWindow,
calling its Constructor - CounterWindow initialises counter to a new
Counter instance - Counter then initialises its 3 Digits
public Counter(JFrame frame) units new
Digit(..., frame) tens new Digit(...,
frame) hundreds new Digit(..., frame)
11What happens when it runs
- Load BlueJ, create new CounterWindow
- BlueJ creates a new instance of CounterWindow,
calling its Constructor - CounterWindow initialises counter to a new
Counter instance - Counter then initialises its 3 Digits
public Counter(JFrame frame) units new
Digit(..., frame) tens new Digit(...,
frame) hundreds new Digit(..., frame)
12What happens when it runs
- Load BlueJ, create new CounterWindow
- BlueJ creates a new instance of CounterWindow,
calling its Constructor - CounterWindow initialises counter to a new
Counter instance - Counter then initialises its 3 Digits
public Counter(JFrame frame) units new
Digit(..., frame) tens new Digit(...,
frame) hundreds new Digit(..., frame)
13What happens when it runs
- Load BlueJ, create new CounterWindow
- BlueJ creates a new instance of CounterWindow,
calling its Constructor - CounterWindow initialises counter to a new
Counter instance - Counter then initialises its 3 Digits
public Counter(JFrame frame) units new
Digit(..., frame) tens new Digit(...,
frame) hundreds new Digit(..., frame)
14What happens when it runs
- Load BlueJ, create new CounterWindow
- BlueJ creates a new instance of CounterWindow,
calling its Constructor - CounterWindow initialises counter to a new
Counter instance - Counter then initialises its 3 Digits
- Everything is set up ready to be used
public CounterWindow() window new
ThreeButtonFrame("Counter") counter new
Counter(window)
15What happens when it runs
- User clicks right button, calls rightAction on
CounterWindow
public void rightAction()
counter.increment()
16What happens when it runs
- User clicks right button, calls rightAction on
CounterWindow
public void rightAction()
counter.increment()
17What happens when it runs
- Increment tells the relevant digits to increment
- Lets say units 9, tens 9, hundreds 0,
public void increment() units.increment() if
(units.getValue() 10) units.makeZero() tens.i
ncrement() if (tens.getValue()
10) tens.makeZero() hundreds.increment() if
(hundreds.getValue() 10) makeZero()
18What happens when it runs
- Increment tells the relevant digits to increment
- now units 10, tens 9, hundreds 0,
public void increment() units.increment() if
(units.getValue() 10) units.makeZero() tens.i
ncrement() if (tens.getValue()
10) tens.makeZero() hundreds.increment() if
(hundreds.getValue() 10) makeZero()
19What happens when it runs
- Increment tells the relevant digits to increment
- now units 10, tens 9, hundreds 0,
public void increment() units.increment() if
(units.getValue() 10) units.makeZero() tens.i
ncrement() if (tens.getValue()
10) tens.makeZero() hundreds.increment() if
(hundreds.getValue() 10) makeZero()
20What happens when it runs
- Increment tells the relevant digits to increment
- now units 0, tens 9, hundreds 0,
public void increment() units.increment() if
(units.getValue() 10) units.makeZero() tens.i
ncrement() if (tens.getValue()
10) tens.makeZero() hundreds.increment() if
(hundreds.getValue() 10) makeZero()
21What happens when it runs
- Increment tells the relevant digits to increment
- now units 0, tens 10, hundreds 0,
public void increment() units.increment() if
(units.getValue() 10) units.makeZero() tens.i
ncrement() if (tens.getValue()
10) tens.makeZero() hundreds.increment() if
(hundreds.getValue() 10) makeZero()
22What happens when it runs
- Increment tells the relevant digits to increment
- now units 0, tens 10, hundreds 0,
public void increment() units.increment() if
(units.getValue() 10) units.makeZero() tens.i
ncrement() if (tens.getValue()
10) tens.makeZero() hundreds.increment() if
(hundreds.getValue() 10) makeZero()
23What happens when it runs
- Increment tells the relevant digits to increment
- now units 0, tens 0, hundreds 0,
public void increment() units.increment() if
(units.getValue() 10) units.makeZero() tens.i
ncrement() if (tens.getValue()
10) tens.makeZero() hundreds.increment() if
(hundreds.getValue() 10) makeZero()
24What happens when it runs
- Increment tells the relevant digits to increment
- now units 0, tens 0, hundreds 1,
public void increment() units.increment() if
(units.getValue() 10) units.makeZero() tens.i
ncrement() if (tens.getValue()
10) tens.makeZero() hundreds.increment() if
(hundreds.getValue() 10) makeZero()
25What happens when it runs
- Increment tells the relevant digits to increment
- now units 0, tens 0, hundreds 1
- Now finished with increment and rightAction
- Waiting for next click