Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The third programming project involves writing a program to calculate the terms

ID: 3545205 • Letter: T

Question

The third programming project involves writing a program to calculate the terms of the following sequence of numbers: 0 1 2 5 12 29 ... where each term of the sequence is twice the previous term plus the second previous term. The 0th term of the sequence is 0. The interface to the program should be a GUI that looks similar to the following:


The pair of radio buttons allows the user to choose whether an iterative or recursive method is used to compute the term of the sequence. When the user enters a value for n and then clicks the Compute button, the nth term of the sequence should be displayed in the Result field. The Efficiency field should contain the number of calls to the recursive method when the recursive option is chosen and the number of iterations of the loop when the iterative option is selected.

The Iterative radio button should be initially set to selected.You should run your program with values for n from 0 to 10 and record and graph the value of the Efficiency field for both the iterative and recursive options. The graph should be contained in a Word document that also contains a brief explanation of the observed results. That document must also be submitted with the code for this project.

The third programming project involves writing a program to calculate the terms of the following sequence of numbers: 0 1 2 5 12 29 ... where each term of the sequence is twice the previous term plus the second previous term. The 0th term of the sequence is 0. The interface to the program should be a GUI that looks similar to the following: The pair of radio buttons allows the user to choose whether an iterative or recursive method is used to compute the term of the sequence. When the user enters a value for n and then clicks the Compute button, the nth term of the sequence should be displayed in the Result field. The Efficiency field should contain the number of calls to the recursive method when the recursive option is chosen and the number of iterations of the loop when the iterative option is selected. The Iterative radio button should be initially set to selected. You should run your program with values for n from 0 to 10 and record and graph the value of the Efficiency field for both the iterative and recursive options. The graph should be contained in a Word document that also contains a brief explanation of the observed results. That document must also be submitted with the code for this project.

Explanation / Answer

import java.awt.Container;

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;


@SuppressWarnings("serial")

public class CalculateTerms extends JFrame


{

private JTextField enterNthField = new JTextField(10);

private JTextField resultField = new JTextField(10);

private JTextField efficiencyField = new JTextField(10);

private JButton computeButton = new JButton("Compute");

private JRadioButton iterativeRadioButton = new JRadioButton("Iterative",

true), recursiveRadioButton = new JRadioButton("Recursive", false);

int efficiencyTotal;


public CalculateTerms() {

super("Project 3");

setLayout(new FlowLayout());

setSize(380, 280);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

Container contentPane = getContentPane();

contentPane.setLayout(new GridLayout(6, 2));


ButtonGroup group = new ButtonGroup();

group.add(iterativeRadioButton);

group.add(recursiveRadioButton);


add(new JLabel(""));

add(iterativeRadioButton);


add(new JLabel(""));

add(recursiveRadioButton);


add(new JLabel("Enter n: "));

add(enterNthField);


add(new JLabel(""));

add(computeButton);


add(new JLabel("Result: "));

add(resultField);


add(new JLabel("Efficiency: "));

add(efficiencyField);


thehandler handler = new thehandler();

computeButton.addActionListener(handler);

}


int efficiencyTotalIterative = 0;


private int performIterativeMethod(int n)


{

int theNthTerm = 0;


if (n == 0) {

return 0;

} else if (n == 1) {

return 1;

} else {

int secondPreviousTermNumber = 0;

int twicePreviousTermNumber = 1;


for (int i = 2; i <= n; i++)


{

efficiencyTotalIterative++;

theNthTerm = 2 * twicePreviousTermNumber

+ secondPreviousTermNumber;

secondPreviousTermNumber = twicePreviousTermNumber;

twicePreviousTermNumber = theNthTerm;

}

}

return theNthTerm;


}


int efficiencyTotalRecursive = 0;


private int performRecursionMethod(int n)


{

int theNthTerm;

efficiencyTotalRecursive++;


if (n == 0) {

theNthTerm = 0;

} else if (n == 1) {

theNthTerm = 1;

} else {

theNthTerm = 2 * performRecursionMethod(n - 1)

+ performRecursionMethod(n - 2);

}

return theNthTerm;


}


private class thehandler implements ActionListener {


public void actionPerformed(ActionEvent event) {

int theNthTerm;

efficiencyTotalRecursive = 0;

efficiencyTotalIterative = 1;


if (iterativeRadioButton.isSelected()) {

theNthTerm = performIterativeMethod(Integer

.parseInt(enterNthField.getText()));

efficiencyField.setText(Integer

.toString(efficiencyTotalIterative));

} else {

theNthTerm = performRecursionMethod(Integer

.parseInt(enterNthField.getText()));

efficiencyField.setText(Integer

.toString(efficiencyTotalRecursive));

}

resultField.setText(Integer.toString(theNthTerm));


}

}


public static void main(String[] args) {

CalculateTerms frame = new CalculateTerms();

frame.setVisible(true);

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote