Question

In: Computer Science

Problem Description There are bunnies standing in a line, numbered 1, 2, ...n. The odd bunnies...

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.

Solutions

Expert Solution

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));

}
}


Related Solutions

Use C language Level 2 Problem description n (n is odd) people sitting around a round...
Use C language Level 2 Problem description n (n is odd) people sitting around a round table playing a game. In this situation, everyone has a left neighbour and a right neighbour. At the beginning, each of them is holding a whiteboard with an integer number. Now, they are playing a game consisting of several rounds. In each round, everyone will first look at the numbers of his/her left neighbour and right neighbour, then calculate the average of the two...
Using C language Problem description n (n is odd) people sitting around a round table playing...
Using C language Problem description n (n is odd) people sitting around a round table playing a game. In this situation, everyone has a left neighbour and a right neighbour. At the beginning, each of them is holding a whiteboard with an integer number. Now, they are playing a game consisting of several rounds. In each round, everyone will first look at the numbers of his/her left neighbour and right neighbour, then calculate the average of the two numbers, replace...
The β-oxidation of odd-numbered FAs presents a small problem in that once most of the oxidation has occurred
The β-oxidation of odd-numbered FAs presents a small problem in that once most of the oxidation has occurred, the last component is propionyl-CoA rather than acetyl CoA. This has one more carbon and as such cannot be utilized any further. Outline the events that control the metabolism of this unit in their proper order: 1) Methylmalonyl-CoA epimerase converts S-methylmalonyl-CoA to R-methylmalonyl-CoA. 2) Methylmalonyl-CoA mutase demethylates R-methylmalonyl-CoA to succinyl-CoA. 3) Propionyl-CoA carboxylase catalyzes the addition of CO2 to propionyl-CoA. 4) The...
An urn is filled with balls, each numbered n = 0, 1, or 2. The average...
An urn is filled with balls, each numbered n = 0, 1, or 2. The average of n is <n> = 2/7. Calculate the probabilities p0, p1, p2, which yields the maximum uncertainty. find the expectation value, based these probabilities, of <n3> - 2<n>.
Let P(n) := ” If n^3 is odd then n is also odd.” I.e., if ∃k...
Let P(n) := ” If n^3 is odd then n is also odd.” I.e., if ∃k ∈ Z, n3 = 2k + 1, ∃b ∈ Z, n = 2b + 1 a) Prove P(n) by contraposition b) Prove P(n) contradiction c) Prove P(n) using induction
Using the parity bit, port b is to flash in a pattern of Odd Numbered LEDs...
Using the parity bit, port b is to flash in a pattern of Odd Numbered LEDs On and Even Numbered LEDS Off for 1 second, and then Odd Numbered LEDs Off and Even Numbered LEDS On for 1 second. This pattern will repeat indefinitely. Please provide the both the assembly code for MPLAB and a flow chart. Note - consider delay calculations and port B initialization have already been completed.
C++ Primary U.S. interstate highways are numbered 1-99. Odd numbers (like the 5 or 95) go...
C++ Primary U.S. interstate highways are numbered 1-99. Odd numbers (like the 5 or 95) go north/south, and evens (like the 10 or 90) go east/west. Auxiliary highways are numbered 100-999, and service the primary highway indicated by the rightmost two digits. Thus, the 405 services the 5, and the 290 services the 90. Your program will prompt the user to type a highway number ("Please enter a highway number, Ex: 65: "), read in the number typed by the...
Is 2k-1 odd? I get that 2(some int k) + 1 is the property for odd...
Is 2k-1 odd? I get that 2(some int k) + 1 is the property for odd numbers. The main question: I am confused on how 2k-1= 2k-2+1 which is a form of k?
1. Give a direct proof that if n is an odd integers, then n3 is also...
1. Give a direct proof that if n is an odd integers, then n3 is also an odd integer. 2. Give a proof by contradiction that the square of any positive single digit decimal integer cannot have more than two decimal digits.
!!Questions are at the bottom!! This is A&P 1. Please read the numbered line story below....
!!Questions are at the bottom!! This is A&P 1. Please read the numbered line story below. Based upon your knowledge and understanding of anatomy and physiology as presented in this course, please provide comprehensive responses to each question presented. In one terrifying second the lights in the expansive basement file room went dark. The young file clerk was instantly disoriented—unable to even see her hand in front of her face. She waited a few long seconds and she was finally...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT