In: Computer Science
Problem Description There are bunnies standing in a line, numbered 1, 2, ...n. The odd bunnies (1, 3, ..) have the normal 2 ears. The even bunnies (2, 4, ..) are genetic mutations and have 3 ears. Write a GUI program which uses a recursive method, earCount(), to calculate the number of "ears" in the bunny line. The user will supply the number of bunnies in the line. Specifications Your program will consist of 2 Textboxes and a Button, each should be labeled appropriately. The user will enter an integer in one Textbox, press the Button, and the number of ears will be shown in the other Textbox. The second Textbox should be readonly. Do not use loops or multiplication in your program. Error Checking Inputs User input should be an integer. Give feedback if it is not and ask them to reenter. Use a Try-Catch blocks where you deem appropriate. Commenting Use the generated flower box in BlueJ to provide a description of the Class and your name. Example: A single line comment followed by statements to create an instance of JTextField, and call setEditable method is fine. Significant variables descriptions. No “loop” variables. a, b, c, x, y, z, etc.
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class GUIwithRecursion extends Applet implements
ActionListener
{
public static TextField numberTF = new TextField ();
public static TextField fibTF = new TextField();
int result = fib(numberN);
public void init()
{
setBackground(Color.magenta);
Label numberLB = new Label("n= ");
Button calcBN = new Button("Calculate");
Label fibLB = new Label("fib(n)= ");
setLayout(null);
numberLB.setBounds(20, 40, 200, 40);
numberTF.setBounds(10, 60, 200, 40);
numberTF.setBackground(Color.yellow);
fibLB.setBounds(30, 90, 100, 20);
fibTF.setBounds(30, 80, 100, 20);
fibTF.setBackground(Color.red);
calcBN.setBounds(30, 120, 100, 20);
add(numberLB);
add(numberTF);
add(fibLB);
add(fibTF);
add(calcBN);
calcBN.addActionListener(this);
}
public static int fib(int numberN)
{
if (numberN<=1)
{return 1;}
else
{return fib(numberN-1)+fib(numberN-2);}
}
public void actionPerformed(ActionEvent e)
{
int result = fib(numberN);
fibTF.setText(Integer.toString(result));
}
}