Java GUI program - Recursion Specifications Write a program using recursion to c
ID: 3795697 • Letter: J
Question
Java GUI program - Recursion
Specifications
Write a program using recursion to count the total number of ears in a line of dogs numbered 1, 2, 3, …..etc. Here’s the twist, the even numbered dogs are genetic mutations that have 4 ears instead of 2. The odd numbered dogs (1, 3, 5, …. etc.) are normal and have 2 ears.
1.A GUI program consisting of 2 textboxes and a button.
2. Use recursive method, numOfEars(),
3. One for the input and the other for the output. The results in the second box should be un-editable.
4. On the first textbox, the user will be asked to enter the number(Integer) of dogs and the second box will display the number of ears based on the input from the user.
5. Appropriate try catch blocks should be used where needed.
Explanation / Answer
/**
* The java program Recursion that prompts user to enter number
* of dogs then display the number of ears using
* recursive calling of method numEars method
* */
//Recursion.java
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class Recursion extends JFrame{
//Set application width and height
private final int APP_WIDTH=300;
private final int APP_HEIGHT=300;
private JLabel label=new JLabel("Enter number of dogs :");
private JTextField txtField=new JTextField(10);
private JLabel earsLabel=new JLabel("Number of ears :");
private JButton btnCalculate=new JButton("Calculate");
private JTextField outtxtField=new JTextField(10);
//main method
public static void main(String[] args) {
//instatiate the class
new Recursion();
}
//constructor to set default settings
public Recursion() {
//set size
setSize(APP_WIDTH, APP_HEIGHT);
//set title
setTitle("Recursion");
//set grid layout
//1 2
//3 4
//5 6
setLayout(new GridLayout(3, 2));
//set text field of outtxtfield as false
outtxtField.setEditable(false);
//add controls to the frame
add(label);add(txtField);
add(earsLabel);add(outtxtField);
add(new JLabel());add(btnCalculate);
//add addactionlistener to the btnCalculate
btnCalculate.addActionListener(new ButtonListener());
pack();
//set visible true for frame
setVisible(true);
}
public class ButtonListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
//get text and convert to integer
int numeras=Integer.parseInt(txtField.getText());
//call method numOfEars method
int totalears=numOfEars(numeras);
//set outtxtField value
outtxtField.setText(String.valueOf(totalears));
}
}
/*
* The method numOfEars that takes n value and returns
* number of ears as output
* */
public int numOfEars(int n)
{
if(n==0)
return 0;
//for even number add 4 ears
else if(n%2==0)
return 4+numOfEars(n-1);
//for odd number add 2 ears
else
return 2+numOfEars(n-1);
}
}
Sample output:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.