write a GUI application that invokes a recursive method, fibonacci, and which relies on a TextArea component
Question:
write a GUI application that invokes a recursive method, fibonacci, and
which relies on a TextArea component to display the count of invocations of the fibonacci
method.
Introduction
In lecture, you've been introduced to recursion, and the fact that a recursive method might
require vastly more memory resources, than a non-recursive version of a solution to the same
problem. That is because each invocation of a recursive method requires the allocation of
memory resources for all of that method's variables. And, you've learned about several GUI
components, including JButton, JLabel. In this lab, you'll implement a recursion method
that calculates the nth Fibonacci number, displays it to the GUI pane, and adds to a JTextBox a
line for each invocation to the recursive method. Four screen shots of the GUI application are
shown in Figure 1.
Figure 1: The zero-th, 20th, 12th, and 13th Fibonacci numbers. Note the drastically more
invocations of the recursive method fibonacci, when the 13th versus the 12th Fibonacci number
is calculated.
I. Structuring a class file for a GUI application
As was discussed in lecture (and as you did in the previous lab), it is good practice to structure a
GUI application java file into several methods, including buildPanel, a constructor, one or more
actionListeners, and a main. You'll do the same here. To complete this part of the lab:
FibonacciRecursionWithGUI.java. It contains the skeleton of the GUI java application
that you'll complete.
2. Edit the file in jGRASP, and declare the following components, as instance fields of your
class:
Access specifier Type Name (initialization)
private JPanel panel
private JButton button1
private JLabel label1
private JLabel label2
private JTextField textField1
private JTextField textField2
private JTextArea display
3. Also in the components section, declare two final variables, for the window width and
height, as well as a counter to keep track of the number of calls to the recursive method
fibonacci:
private int numInvocations = 0;
private final int WINDOW_WIDTH = 350;
private final int WINDOW_HEIGHT = 250;
4. Compile and run your application. Because there's no functionality that you've added (just
declared components), your application should run to completion, and no GUI window
should appear.
II. The buildPanel method
To complete this part of the lab, in the buildPanel method:
1. Instantiate the button1 label as a new JButton, and give it the text "Calculate Fibonacci
Number", and register to the button the ActionListener new
ButtonListener()
2. Instantiate the two labels, labe1 and label2, to display the texts:
label1 = new JLabel("Which Fibonacci Number: ");
label2 = new JLabel("The Fibonacci Number: ");
3. Create the 2 text fields textField1 and textField2. Make the second one non-editable:
textField1 = new JTextField(5); textField2 =
new JTextField(5);
textField2.setEditable(false);
4. A JTextArea is a multi-line area that displays plain text. The component has
capabilities that are not part of JTextBox, in that it permits multiple lines, as well as
adding of scroll bars. The Java 7 API for TextArea:
http://docs.oracle.com/javase/8/docs/api/javax/swing/JTextArea.html
One of the constructors for JTextArea takes
two parameters, both integers, to designate the number of
"rows" (the first argument), and the number of "columns"
(the second argument).
Instantiate the field display, by creating a new JTextArea that has 5 rows and 15
columns:
display = new JTextArea (5, 15);
display.setEditable (false);
5. Because a JTextArea can contain a large number of lines, it is usually good practice
to add scroll bars to the area. To do that, you can use a JScrollPane, yet another
component, which can be configured in many ways. One of the constructors to a
JScrollPane object takes a single argument, which is another Java GUI component,
to which the scroll bar(s) are applied. The JScrollPane object, itself, can be created
by invoking one of the many constructors that are defined in the JScrollPane class.
For this lab, create a JScrollPane for the display field, and designate that a vertical
scroll bar be used at all times, regardless of the number of lines that are currently in the
JTextArea:
JScrollPane scroll = new JScrollPane ( display );
scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
6. You've created all of the components necessary for your GUI. Next, instantiate a new
JPanel:
panel = new JPanel();
And add all the components to the JPanel, in the following order:
• label1
• textField1
• button1
• label2
• textField2
• scroll
7. Compile your class, and fix any syntax errors, that you may have inadvertently
introduced.
III. The Fibonacci recursive method
The Fibonacci sequence:
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ...
is made up of two initial numbers, 0 and 1, and each subsequent number is the sum of the
previous two. In mathematics, the sequence Fn of Fibonacci numbers is defined by the
recurrence relation:
Fn = Fn-1 + Fn-2
where
F0 = 0 and F1 = 1
Thus, the base case is when the input to the fibonacci method is 0 or 1, and the recursive case is
the recurrence relation shown above. Recall that the base case is the simples version of the
problem (which is easily solved), and that if the base case is not met, the recursive case breaks
up the problem in smaller, manageable parts, whose solutions are eventually combined to answer
the larger, more difficult problem.
1. Write the fibonacci method and increment a variable, numInvocations of type int, to
keep track of the number of times that the method is invoked, and use the append method
of the JTextArea class, to add a line to the display field (of type JTextArea):
public int fibonacci(int n){ numInvocations++;
display.append("invocation " + numInvocations + "\n"); if
(n == 0) { return 0;
} else if (n == 1) { return
1;
} else { return fibonacci(n-1) +
fibonacci(n-2); }
}
IV. The ButtonListener inner class
The ButtonListener inner class implements the ActionListener interface, which mandates
that the actionPerformed method be defined. You've already implemented an
ActionListener in lab 6; refer to that lab if you need to review. To complete this part of the
lab:
1. Declare a new variable, fibNum, of type int, and assign it the value that is returned
from the invocation to the recursive method fibonacci, which should receive the
argument that is supplied by the user into the textField1 field. Use the getText()
method from the JTextField class, to retrieve the value input by the user into the
GUI. The value in textField1 is of type String, while fibonacci expects an integer, so use
one of the methods in the wrapper class Integer, to convert the value input by the user
into textField1 into an integer. For example, once you have converted the text input by
the user in textField1, and saved it to a variable userInput, you could write:
int fibNum = fibonacci(userInput);
2. Place the value of fibNum into the textField2. Because fibNum is of type int, use the
method toString in the Integer wrapper class, to convert fibNum to a String.
V. The FibonacciRecursionWithGUI constructor
At this point, you've written the buildPanel method, as well as the ButtonListener inner
class. What's left is to write the constructor for the FibonacciRecursionWithGUI class, which
should:
• invoke the buildPanel method
• add the panel to the window
• set the size of the window
• render the GUI, by issuing the setVisible method
Refer to lab 7 if you've forgotten how to do this.
VI. The main method
Next, add the panel to a GUI window, and write the main method. To complete this step:
1. Invoke the FibonacciRecursionWithGUI constructor, in the main method. The main
method needs to only contain a single line, which creates a new
FibonaccirecursionWithGUI object.
2. Compile and run your program. This time, the GUI should appear, and should be
operational. Make sure that your GUI behaves like those shown in Figure 1.
Starting Out With Java From Control Structures Through Data Structures
ISBN: 9780133957051
6th Edition
Authors: Tony Gaddis