Title: Events
1Events
- An event is an object that represents some
activity to which we may want to respond - Java makes it easy to write an "event driven
program" when we want our program to perform
some action when the following occurs - the mouse is moved or dragged
- a mouse button is clicked
- a graphical button is clicked
- a keyboard key is pressed
- a timer expires
2Events
- when we import java.awt.event. we get to use
the Java event library - Certain objects, such as applets, keys and
graphical buttons, generate (fire) an event when
they occur - Other objects, called listeners, respond to
events - We can write our own listener objects to do
whatever we want when an event occurs
3- import java.awt.
- import java.awt.event.
- import java.applet.
- public class SimplestButtonExampleEver
- extends Applet implements ActionListener
-
- Button aButton new Button("Click Me!")
- public void init()
-
- add(aButton)
- aButton.addActionListener(this)
-
- public void paint(Graphics g)
-
- g.drawString("You rolled a "
- (int)(Math.random()61), 20, 20)
-
- public void actionPerformed(ActionEvent ae)
4this means the class I'm inside of
- import java.awt.
- import java.awt.event.
- import java.applet.
- public class SimplestButtonExampleEver
- extends Applet implements ActionListener
-
- Button aButton new Button("Click Me!")
- public void init()
-
- add(aButton)
- aButton.addActionListener(this)
-
- public void paint(Graphics g)
-
- g.drawString("You rolled a "
- (int)(Math.random()61), 20, 20)
-
- public void actionPerformed(ActionEvent ae)
5A Simple Button Applet
6setLayout(null)setBounds()
- Java will automatically size and place the button
unless you tell it not to - public void init()
-
- //Turn Layout manager off
- setLayout(null)
- //Position Button(X, Y, Width, Height)
- Button1.setBounds(10,20,50,20)
7Multiple buttons
- If you have more than one button, you can find
out which one was pressed with e.getSource() - Button first, second
- //lots of java not shown
- public void actionPerformed(ActionEvent e)
-
- if (e.getSource() first)
- //do something
- else if (e.getSource() second)
- //do something else
8Practice Quiz Question What number will be
displayed after buttons b1, b2 and b3 are pressed
in that order?
- import java.awt.
- import java.awt.event.
- import java.applet.
- public class ButtonQuizAP extends Applet
implements ActionListener -
- Button b1, b2, b3
- int nValue
- public void init()
-
- nValue 4
- b1 new Button("One")
- b2 new Button("Two")
- b3 new Button("Three")
- add(b1)
- add(b2)
- add(b3)
- b1.addActionListener(this)
- b2.addActionListener(this)
9Nested loops
- "Nested" means "one inside the other"
- Let's say we wanted to make a 10x10 times table
- 1 2 3 4 5 6 7 8 9 10
- 2 4 6 8 10 12 14 16 18 20
- 3 6 9 12 15 18 21 24 27 30
- 4 8 12 16 20 24 28 32 36 40
- 5 10 15 20 25 30 35 40 45 50
- 6 12 18 24 30 36 42 48 54 60
- 7 14 21 28 35 42 49 56 63 70
- 8 16 24 32 40 48 56 64 72 80
- 9 18 27 36 45 54 63 72 81 90
- 10 20 30 40 50 60 70 80 90 100
10Nested loops
- Let's start with the first row
- 1 2 3 4 5 6 7 8 9 10
- 2 4 6 8 10 12 14 16 18 20
- 3 6 9 12 15 18 21 24 27 30
- 4 8 12 16 20 24 28 32 36 40
- 5 10 15 20 25 30 35 40 45 50
- 6 12 18 24 30 36 42 48 54 60
- 7 14 21 28 35 42 49 56 63 70
- 8 16 24 32 40 48 56 64 72 80
- 9 18 27 36 45 54 63 72 81 90
- 10 20 30 40 50 60 70 80 90 100
-
- It would be easy enough to write a loop that
makes the numbers 1 through 10
11Nested loops
- Let's start with the first row
- 1 2 3 4 5 6 7 8 9 10
- 2 4 6 8 10 12 14 16 18 20 x2
- 3 6 9 12 15 18 21 24 27 30 x3
- 4 8 12 16 20 24 28 32 36 40 x4
- 5 10 15 20 25 30 35 40 45 50 x5
- 6 12 18 24 30 36 42 48 54 60 x6
- 7 14 21 28 35 42 49 56 63 70 x7
- 8 16 24 32 40 48 56 64 72 80 x8
- 9 18 27 36 45 54 63 72 81 90 x9
- 10 20 30 40 50 60 70 80 90 100 x10
-
- We'd put that loop inside another loop, that
"multiplies" it
12Nested loops
- for(int nJ 1 nJ lt 10 nJ)
- System.out.print(nJ '\t')
- //inside loop counts columns 1 to 10
- 1 2 3 4 5 6 7 8 9 10
- 2 4 6 8 10 12 14 16 18 20 x2
- 3 6 9 12 15 18 21 24 27 30 x3
- 4 8 12 16 20 24 28 32 36 40 x4
- 5 10 15 20 25 30 35 40 45 50 x5
- 6 12 18 24 30 36 42 48 54 60 x6
- 7 14 21 28 35 42 49 56 63 70 x7
- 8 16 24 32 40 48 56 64 72 80 x8
- 9 18 27 36 45 54 63 72 81 90 x9
- 10 20 30 40 50 60 70 80 90 100 x10
13Nested loops
- for(int nI 1 nI lt 10 nI)
-
- for(int nJ 1 nJ lt 10 nJ)
- System.out.print( ?? '\t')
- ??
-
- //outside loop counts rows 1 to 10
- //(inside loop column)outside row
- 1 2 3 4 5 6 7 8 9 10
- 2 4 6 8 10 12 14 16 18 20 x2
- 3 6 9 12 15 18 21 24 27 30 x3
- 4 8 12 16 20 24 28 32 36 40 x4
- 5 10 15 20 25 30 35 40 45 50 x5
- 6 12 18 24 30 36 42 48 54 60 x6
- 7 14 21 28 35 42 49 56 63 70 x7
- 8 16 24 32 40 48 56 64 72 80 x8
- 9 18 27 36 45 54 63 72 81 90 x9
- 10 20 30 40 50 60 70 80 90 100 x10
14In Class
- Write an application that generates a 10x10 times
table using nested loops and displays it using
System.out.print() - Hints
- Use "\t" to space numbers
- Use an empty System.out.println() to end the line
15In Class
- Next, Modify your application so that it produces
the following output - (1,1) (1,2) (1,3) (1,4) (1,5) (1,6) (1,7) (1,8) (1
,9) (1,10) - (2,1) (2,2) (2,3) (2,4) (2,5) (2,6) (2,7) (2,8) (2
,9) (2,10) - (3,1) (3,2) (3,3) (3,4) (3,5) (3,6) (3,7) (3,8) (3
,9) (3,10) - (4,1) (4,2) (4,3) (4,4) (4,5) (4,6) (4,7) (4,8) (4
,9) (4,10) - (5,1) (5,2) (5,3) (5,4) (5,5) (5,6) (5,7) (5,8) (5
,9) (5,10) - (6,1) (6,2) (6,3) (6,4) (6,5) (6,6) (6,7) (6,8) (6
,9) (6,10) - (7,1) (7,2) (7,3) (7,4) (7,5) (7,6) (7,7) (7,8) (7
,9) (7,10) - (8,1) (8,2) (8,3) (8,4) (8,5) (8,6) (8,7) (8,8) (8
,9) (8,10) - (9,1) (9,2) (9,3) (9,4) (9,5) (9,6) (9,7) (9,8) (9
,9) (9,10) - (10,1) (10,2) (10,3) (10,4) (10,5) (10,6) (10,7) (
10,8) (10,9) (10,10) - Which loop gives the row? Which loop gives the
column? Change your variable names so that they
match.
16What is the output?
- for(char cChar 'a' cChar lt 'c' cChar)
-
- for(int nI 1 nI lt 4 nI)
-
- System.out.print(cChar ", " nI '\t')
-
- System.out.println()
-
172 dimensional arrays
- Can be thought of as a grid with rows and
columns
column 0 1 2
-3
-1
5
row 0
7
12
13
row 1
182 dimensional arrays
- int naNums -3, -1, 5,
- 7, 12, 13
- /In this form, Java automatically sizes the
array to 2 rows and 3 columns - Note rows first, then columns /
- System.out.println(naNums12)
- //displays 13
- System.out.println(naNums21)
- //Out of bounds exception!
column 0 1 2
-3
-1
5
row 0
7
12
13
row 1
192 dimensional arrays
- int naNums
- /In Java, arrays are objects. If we declare and
initialize the array seperately, we need to use
"new" / - naNums new int23
202 dimensional arrays
- int naNums
- naNums new int23
- Typically, nested loops are used to initialize a
2d array. The outside loop controls the row, and
the inside the column - for(int nRow 0 nRow lt 2 nRow)
- for(int nCol 0 nCol lt 3 nCol)
- naNumsnRownCol nCol(3nRow)
column 0 1 2
0
1
2
row 0
3
4
5
row 1
21A one dimensional array of buttons
- import java.awt.
- import java.awt.event.
- import java.applet.
- public class Buttons extends Applet implements
ActionListener -
- Button buttonArray
- int nRows 3
- int nColumns 4
- int nTotalButtons nRows nColumns
- public void init()
-
- //declare a grid layout with no space
between buttons - setLayout(new GridLayout(nRows, nColumns,
0, 0)) -
- //declare a new array of buttons
- buttonArray new ButtonnTotalButtons
-
- //initialize each of the buttons in the
array
22A one dimensional array of buttons
- import java.awt.
- import java.awt.event.
- import java.applet.
- public class Buttons extends Applet
- implements ActionListener
-
- Button buttonArray
- int nRows 3
- int nColumns 4
- int nTotalButtons nRows nColumns
- public void init()
-
- //declare a grid layout with no space
between buttons - setLayout(new GridLayout(nRows, nColumns,
0, 0)) -
- //declare a new array of buttons
- buttonArray new ButtonnTotalButtons
-
23A one dimensional array of buttons
- import java.awt.
- import java.awt.event.
- import java.applet.
- public class Buttons extends Applet
- implements ActionListener
-
- Button buttonArray
- int nRows 3
- int nColumns 4
- int nTotalButtons nRows nColumns
- public void init()
-
- //grid layout with no space between buttons
- setLayout(new GridLayout(nRows,nColumns,0,0))
- //declare a new array of buttons
- buttonArray new ButtonnTotalButtons
- //initialize each of the buttons in the array
- //with an empty label
24A one dimensional array of buttons
- import java.awt.
- import java.awt.event.
- import java.applet.
- public class Buttons extends Applet
- implements ActionListener
-
- Button buttonArray
- int nRows 3
- int nColumns 4
- int nTotalButtons nRows nColumns
- public void init()
-
- //grid layout with no space between buttons
- setLayout(new GridLayout(nRows,nColumns,0,0))
- //declare a new array of buttons
- buttonArray new ButtonnTotalButtons
- //initialize each of the buttons in the array
- //with an empty label
25A one dimensional array of buttons
- public void actionPerformed(ActionEvent e)
-
- //find out which button is the source
- int nButtonNumber 0
- for(int i 0 i lt buttonArray.length
i) - if(e.getSource() buttonArrayi)
- nButtonNumber i
-
- //display button Number at bottom of
screen - showStatus("Button number "
nButtonNumber) -
- //change background of buttons
- if(buttonArraynButtonNumber.getBackgrou
nd() - !
Color.red) - buttonArraynButtonNumber.setBackground
( -
Color.red) - else
- buttonArraynButtonNumber.setBackground(
-
Color.blue)
26How would we modify the example for minesweeper?
- When a button is pressed, we need to know two
numbers the row and column - We also need to know if the button is hiding a
mine
27extending the Button class
- class Cell extends Button
-
- private boolean bIsBomb
- private int myRow, myCol
- . . .
28extending the Button class
- class Cell extends Button
-
- private boolean bIsBomb
- private int myRow, myCol
- public Cell(??)
-
- ??
-
29extending the Button class
- class Cell extends Button
-
- private boolean bIsBomb
- private int myRow, myCol
- public Cell(??)
-
- ??
-
- public int getRow()return myRow
30In actionPerformed
- To find out the row (and column) of the button
that was pressed - public void actionPerformed(ActionEvent e)
-
- int nRow ((Cell)e.getSource()).getRow()
31In actionPerformed
- To find out the row (and column) of the button
that was pressed - //Get the object that generated the event
- e.getSource()
- //Cast it as a Cell
- (Cell)e.getSource()
- //Then ask it what row it came from
- ((Cell)e.getSource()).getRow()
32How to get started on Minesweeper
- Start a new Applet Project. Copy the sample
button applet code into your project - Change nRows and nCols to constants public
final static int nROWS 10 - Modify the array to be a 2 dimensional array.
Button buttonArray will become
Button buttonArray and so on - You can delete the code in actionPerformed. Were
going to use a different technique. - Write a new class Cell that extends Button
- Change the applet from an array of Buttons to an
array of Cells.
33How to get started on Minesweeper
- AB students should use a Set to make sure that
the correct number of bombs is placed - Don't forget import java.util.
34What will be displayed?
- public class Recursion extends Applet
- public void init()
- mystery()
-
- public void mystery()
-
- System.out.println(Math.random())
-
-
35What will be displayed now?
- public class Recursion extends Applet
- public void init()
- mystery()
-
- public void mystery()
-
- System.out.println(Math.random())
- mystery()
-
36The program will repeatedly display random
numbersproblem is it won't stop!
37Recursion A method that calls itself
- public void mystery()
-
- System.out.println(
- Math.random())
- mystery()
-
- Recursion is another way of making a loop
- Recursion is hard to controlit's very easy to
create an infinite loop that never stops
38In recursion, the "stopping point" is called the
base case
- public void mystery(int nTimes)
-
- if(nTimes 0)
- System.out.println("Stop!")
- else
-
- System.out.println(
- Math.random())
- mystery(nTimes-1)
-
-
- The base case stops the recursive calls
39- public class Recursion extends Applet
- public void init()
- mystery(10)
-
- public void mystery(int nTimes)
-
- if(nTimes 0)
- System.out.println("Stop!")
- else
-
- System.out.println(
- Math.random())
- mystery(nTimes-1)
-
-
40Recursion
- There are two basic ways to make things happen
"over and over again" in programming - Loops
- Recursion
- In theory, anything you can do with loops, you
can do with recursion ( vice versa)
41A loop that "counts" from 1 to 10
- public class Recursion extends Applet
- public void init()
- for(int nI 1 nI lt 10 nI)
- System.out.println(nI)
-
42A Recursive Function that "counts" from 1 to 10
- public class Recursion extends Applet
- public void init()
- count(1)
-
- public void count(int nTimes)
-
- if(nTimes 10)
- System.out.println(nTimes " Stop!")
- else
-
- System.out.println(nTimes)
- count(nTimes1)
-
-
43Just like a loop, it has a starting point, a
stopping point, and a way to get from one to the
other
- public class Recursion extends Applet
- public void init()
- count(1)
-
- public void count(int nTimes)
-
- if(nTimes 10)
- System.out.println(nTimes " Stop!")
- else
-
- System.out.println(nTimes)
- count(nTimes1)
-
-
44Find the output
- import java.awt.
- import java.applet.
- public class Recursion extends Applet
-
- public void init()
- mystery(2)
-
- public void mystery(int nNum)
-
- if(nNum gt 5)
- System.out.println("Base Case")
- else
-
- System.out.print(nNum ", ")
- mystery(nNum 1)
-
-
45Recursive Methods that return values
- A Recursive Method is a method that calls itself
- public int Mystery(int nNum)
-
- if(nNum 0)
- return 0
- else
- return 2 Mystery(nNum - 1)
-
46Recursive Methods
- A Recursive Method is a method that calls itself
- public int Mystery(int nNum)
-
- if(nNum 0)
- return 0
- else
- return 2 Mystery(nNum - 1)
-
Recursion
47Recursive Methods
- What would be returned by the call Mystery(0)?
- public int Mystery(int nNum)
-
- if(nNum 0)
- return 0
- else
- return 2 Mystery(nNum - 1)
-
0
0
48Recursive Methods
- What would be returned by the call Mystery(1)?
- public int Mystery(int nNum)
-
- if(nNum 0)
- return 0
- else
- return 2 Mystery(nNum - 1)
-
1
1
49Recursive Methods
- Mystery(1) 2 Mystery(0) 20
- public int Mystery(int nNum)
-
- if(nNum 0)
- return 0
- else
- return 2 Mystery(nNum - 1)
-
1
1
50Recursive Methods
- What would be returned by the call Mystery(2)?
- public int Mystery(int nNum)
-
- if(nNum 0)
- return 0
- else
- return 2 Mystery(nNum - 1)
-
2
2
51Recursive Methods
- Mystery(2) 2 Mystery(1)
- 2 2 Mystery(0) 2 2 0
- public int Mystery(int nNum)
-
- if(nNum 0)
- return 0
- else
- return 2 Mystery(nNum - 1)
-
2
2
52Recursive Methods
- What would be returned by the call Mystery(10)?
53Recursive Methods
- What would be returned by the call Mystery(10)?
- Mystery(10) 2 Mystery(9)
- 22Mystery(8)
- 222Mystery(7)
- 2222Mystery(6)
- 22222Mystery(5)
- 222222Mystery(4)
- 2222222Mystery(3)
- 22222222Mystery(2)
- 222222222Mystery(1)
- 2222222222Mystery(0)20
54Recursive Methods
- Let's remove some code from Mystery
- Now what would be returned by the call
Mystery(10)? - public int Mystery(int nNum)
-
- if(nNum 0) //delete 3 lines
- return 0
- else
- return 2 Mystery(nNum - 1)
-
55Recursive Methods
- Mystery(10) 2 Mystery(9)
- 22Mystery(8)
- 222Mystery(7)
- 2222Mystery(6)
- 22222Mystery(5)
- 222222Mystery(4)
- 2222222Mystery(3)
- 22222222Mystery(2)
- 222222222Mystery(1)
- 2222222222Mystery(0)
- 22222222222Mystery(-1)
- 222222222222Mystery(-2)
- and so on, and so on
- Infinite Recursion! Stack Overflow!
- CRASH!
56The Base Case
- Every recursive method must have a base case
- The base case is where the recursion stops
- public int Mystery(int nNum)
-
- if(nNum 0) //base case
- return 0
- else
- return 2 Mystery(nNum - 1)
57Preventing Stack Overflow
- What would happen with the following call?
- Mystery(-1)
- How could the method be improved?
-
- public int Mystery(int nNum)
-
- if(nNum 1)
- return 1
- else
- return 3 Mystery(nNum - 1)
58Practice Quiz Question
- Find the return values of the calls to the
Mystery method shown below - Mystery(1)
- Mystery(2)
- Mystery(5)
-
-
- public int Mystery(int nNum)
-
- if(nNum 1)
- return 1
- else
- return 3 Mystery(nNum - 1)
-
59Javabat Bunny Ears
- http//www.javabat.com/prob?idRecur1.bunnyEars
- We have a number of bunnies and each bunny has
two big floppy ears. We want to compute the total
number of ears across all the bunnies recursively
(without loops or multiplication).
60Problem Create a Fibonacci function
- The first two Fibonacci numbers are 1 1
- The next Fibonacci numbers can be found by adding
the previous two - 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89. . .
61Problem Create a Fibonacci function
- We could describe the Fibonacci numbers like this
62- public int fibonacci(int nNum)
-
- ???
63- public int fibonacci(int nNum)
-
- if(nNum 0 nNum 1)
- ???
64- public int fibonacci(int nNum)
-
- if(nNum 0 nNum 1)
- return 1
- else
- ???
65- public int fibonacci(int nNum)
-
- if(nNum 0 nNum 1)
- return 1
- else
- return
- fibonacci(nNum-2)
- fibonacci(nNum-1)
66Problem Create a non-recursive Fibonacci function
- public int fibonacci(int nNum)
-
- ???
67Problem Create a non-recursive Fibonacci function
- public int fibonacci(int nNum)
-
- int nPos 0
- int nFibonacci1 1
- int nFibonacci2 1
- while(nPos lt nNum)
-
- int nTemp nFibonacci2
- nFibonacci2
- nFibonacci2 nFibonacci1
- nFibonacci1 nTemp
- nPos
-
- return nFibonacci1
68Practice Quiz Question
- Write a simple applet that
- finds the factorial of an integer
- 3! 3 2 1
- Write a recursive factorial method
- Start with an Applet like
- import java.awt.
- import java.applet.
- public class FactorialPractice extends Applet
- public int factorial(int nNum)
- ??? //your 4 lines of code here
-
- public void paint(Graphics g)
- System.out.println(factorial(3))
- System.out.println(factorial(1))
- System.out.println(factorial(10))
- System.out.println(factorial(0))
- System.out.println(factorial(-3))
-
69Head vs. Tail Recursion
- The position of the recursive call in a recursive
method can have a BIG effect on the way the
method works - If the recursive call is at the end of the
method, it's called tail recursion - If the recursive call is at the beginning of the
method, it's called head recursion
70Tail Recursion
- public void tailRecursion(int nNum)
-
- if(nNum gt 10)
- System.out.println("base case")
- else
-
- System.out.print(nNum ", ")
- tailRecursion(nNum 1)
-
-
71Head Recursion
- public void headRecursion(int nNum)
-
- if(nNum gt 10)
- System.out.println("base case")
- else
-
- headRecursion(nNum 1)
- System.out.print(nNum ", ")
-
-
72Tail Recursion
- What would the following calls display?
- tailRecursion(10)
- tailRecursion(9)
- tailRecursion(1)
- public void tailRecursion(int nNum)
-
- if(nNum gt 10)
- System.out.println("base case")
- else
-
- System.out.print(nNum ", ")
- tailRecursion(nNum 1)
-
-
73Tail Recursion
- What would the following calls display?
- tailRecursion(10)
- tailRecursion(9)
- tailRecursion(1)
- base case
- 9, base case
- 1, 2, 3, 4, 5, 6, 7, 8, 9, base case
74Head Recursion
- What would the following calls display?
- headRecursion(10)
- headRecursion(9)
- headRecursion(1)
- public void headRecursion(int nNum)
-
- if(nNum gt 10)
- System.out.println("base case")
- else
-
- headRecursion(nNum 1)
- System.out.print(nNum ", ")
-
-
75Head Recursion
- What would the following calls display?
- headRecursion(10)
- headRecursion(9)
- headRecursion(1)
- base case
- base case
- 9,
- base case
- 9, 8, 7, 6, 5, 4, 3, 2, 1,
76Head vs. Tail Recursion
- Tail recursion works like you would expect, it
goes "forward" - Head recursion "stacks up" and then "unwinds", it
goes backwards!
77Javabat Bunny Ears 2
- http//www.javabat.com/prob?idRecur1.bunnyEars2
- Hint 3 possibilites base case, odd bunny, even
bunny
78Javabat Triangle
- http//www.javabat.com/prob?idRecur1.triangle
- 1,3,6,10,. . . .
79Javabat sumDigits
- http//www.javabat.com/prob?idRecur1.sumDigits
- 123,456 can be split into two parts
- 123,456 10 6 (the ones digit)
- 123,456 / 10 12,345 (the rest of the digits)
- The sum of all the digits the one's digit plus
the sum of the rest of the digits - What's the base case?
80Recursion in 2d arrays
- Problem Write a "Remove Blob" method. A Blob
is a collection of one or more dark adjacent
cells.
- There are 4 Blobs on the right
- Clicking on any dark cell should remove the
entire Blob
81A BlobCell class
- class BlobCell extends Button
-
- private int myRow, myCol
- public BlobCell(int nR, int nC)
-
- myRow nR
- myCol nC
-
- public int getRow()return myRow
- public int getCol()return myCol
82The "Applet" class
- public class RemoveBlobDemo extends Applet
- implements ActionListener
-
- BlobCell buttonArray
- final static int nROWS 9
- final static int nCOLUMNS 9
- // init() not shown
- public void actionPerformed(ActionEvent e)
-
- int nRow
- ((BlobCell)e.getSource()).getRow()
- int nCol
- ((BlobCell)e.getSource()).getCol()
- removeBlob(nRow,nCol)
-
-
83The removeBlob method
- public class RemoveBlobDemo extends Applet
- implements ActionListener
-
- . . .
- public void removeBlob(int nRow,int nCol)
-
- if(buttonArraynRownCol.getBackground()
- .equals(Color.black))
-
- buttonArraynRownCol.
- setBackground(Color.white)
- . . . .
-
84The removeBlob method
- public class RemoveBlobDemo extends Applet
- implements ActionListener
-
- . . .
- public void removeBlob(int nRow,int nCol)
-
- if(buttonArraynRownCol.getBackground()
- .equals(Color.black))
-
- buttonArraynRownCol.
- setBackground(Color.white)
- //Check 8 neighbors
- if(nRow gt 0 nCol gt 0 )
- removeBlob(nRow-1,nCol-1)
-
85Practice Quiz Questions
- Which of the following calls to this method
create infinite recursion? - public void mystery(int nNum1, int nNum2)
-
- if(nNum1 ! nNum2)
- mystery(nNum1 1, nNum2 - 1)
-
- mystery(2,3)
- mystery(3,3)
- mystery(2,4)
- none of the above
86substring() one argument
- returns the rest of the String beginning at that
index - Examples
- "unhappy".substring(2) returns "happy"
- "Harbison".substring(3) returns "bison"
- "emptiness".substring(9) returns "" (empty
String)
87substring() two arguments
- returns part of the String beginning at that
first index, and ending one position before the
second index - Examples
- "hamburger".substring(4, 8) returns "urge"
- "smiles".substring(1, 5) returns "mile"
88JavaBat practice problemRecur1 gt countX
- Given a string, compute recursively (no loops)
the number of lowercase 'x' chars in the string. - countX("xxhixx") ? 4
- countX("xhixhix") ? 3
- countX("hi") ? 0
89Practice Quiz Questions
- Complete the following method so that clicking on
a black button changes it's background to white
AND the background of all the adjacent black
buttons to the right
public void removeRowRight(int nRow,int nCol)
if(buttonArraynRownCol.getBackground(
).equals(Color.black))
buttonArraynRownCol.setBackground(Color.white)
if(__________________________)
_________________________________________
90- A class of 30 students rated their cs teacher on
a scale of 1 to 10. The response array is a 30
element integer array of the student reponses.
An 11 element array will count the number of
occurrences of each response. For example
freq6 will count the number of students who
responded 6. The quantity freq0 will not be
used. Here is a program that counts the
students' responses and outputs the results - public class StudentEvaluations
-
- public static void main(String args)
-
- int responses 6,6,7,8,10,1,5,4,6,7,5,4,
3,4 - ,4,9,8,6,7,10,6,7,8,8,9,6,7,8,9
,2 - int freq new int11
- for(int i0iltresponses.lengthi)
- freqresponsesI
-
-
- Suppose the last entry in the initializer list
for the responses array was incorrectly typed as
12 instead of 2. What would be the result of
running the program? - A rating of 12 would be listed with a frequency
of 1 - A rating of 1 would be liste with a frequency of
12 - An ArrayIndexOutOfBoundsException would be thrown
- A StringIndexOutOfBoundsException would be thrown
- A NullPointerException would be thrown