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

Please make this code as simple and easy to follow as possible. Comments in the

ID: 3693236 • Letter: P

Question

Please make this code as simple and easy to follow as possible. Comments in the code would be most apperciated. Also please use eclipse to make this java program.

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 and the 1st term of the sequence is 1. 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 selected.

When the window is closed, the efficiency values should be computed with values of n from 0 to 10 and written to a text file outData.txt. Each line of the file should contain the value of n, the efficiency of the iterative method for that value of n and the efficiency of the recursive method. The values should be separated by commas so the file can be opened with Excel and used to graph the value of the efficiencies for both the iterative and recursive options along the y axis with the value of n along the x-axis. The graph should be included in the solution description document that accompanies this project and should also contain a brief explanation of the observed results.

The program should consist of two classes. The first class should define the GUI and should be hand-coded and not generated by a GUI generator. In addition to the main method and a constructor to build the GUI, an event handler will be needed to handle the Compute button click and another handler will be needed to produce the file described above when the window is closed. The latter handler should be an object of an inner class that extends the WindowAdapter class.

The other class should be named Sequence. It should be a utility class meaning that all its methods must be class (static) methods and no objects should be able to be generated for that class. It should contain three public methods:

The first method computeIterative should accept a value of n and return the corresponding element in the sequence using iteration.

The second method computeRecursive should accept a value of n and return the corresponding element in the sequence using recursion.

The third method getEfficiency will return the efficiency counter left behind by the previous call to either of the above two methods.

Be sure that all instance and class variables are declared as private. Also any exceptions thrown by non-numeric inputs should be properly handled.

Explanation / Answer

Given below are the with comments to expain the working of the code:

import java.awt.*;

import java.awt.event.*;

import java.awt.event.*;

import java.io.File;

import java.io.SequenceInputStream;

import java.io.FileWriter;

import java.io.IOException;

import javax.swing.*;

import javax.swing.JOptionPane;

public class Project01 extends JProj01 implements ActionListener {

private final int TWICE = 2;

private JLabel jLabel1 = new JLabel(), jLabel2 = new JLabel(), jLabel3 = new JLabel(),

jLabel4 = new JLabel(), jLabel5 = new JLabel(), jLabel6 = new JLabel();

private ButtonGroup radioButtons = new ButtonGroup();

private JRadioButton rbIterative = new JRadioButton(), rbRecursive = new JRadioButton();

private JTextField enterN = new JTextField(16), textOutput = new JTextField(16),

textEfficiency = new JTextField(16);

private JButton computeBut = new JButton();

private int efficiency;

//class project//

public Project01() {

super("Project01 of Numbers, Iterative, Recursive!");

setSize(300, 200);

setDefaultCloseOperation(JProj01.EXIT_ON_CLOSE);

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

getContentPane().add(jLabel4);

radioButtons.add(rbIterative);

rbIterative.setSelected(true);

rbIterative.setText("Iterative");

getContentPane().add(rbIterative);

getContentPane().add(jLabel5);

radioButtons.add(rbRecursive);

rbRecursive.setText("Recursive");

getContentPane().add(rbRecursive);

jLabel1.setText("Enter n: ");

getContentPane().add(jLabel1);

getContentPane().add(enterN);

getContentPane().add(jLabel6);

computeBut.setText("Compute");

computeBut.addActionListener(this);

getContentPane().add(computeBut);

jLabel2.setText("Output: ");

getContentPane().add(jLabel2);

getContentPane().add(textOutput);

textOutput.setEditable(false);

jLabel3.setText("Efficiency: ");

getContentPane().add(jLabel3);

getContentPane().add(textEfficiency);

textEfficiency.setEditable(false);

pack();

}

public void actionPerformed(ActionEvent event) {

int output;

efficiency = 0;

if (rbIterative.isSelected()) {

output = Iteration(Integer.parseInt(enterN.getText()));

} else {

output = Recursion(Integer.parseInt(enterN.getText()));

}

textOutput.setText(Integer.toString(output));

textEfficiency.setText(Integer.toString(efficiency));

}

//for iteration//

private int Iteration(int n) {

int output = 0;

if (n == 0) {

output = 0;

} else if (n == 1) {

output = 1;

} else {

int secondPrev = 0;

int prev = 1;

for (int i = TWICE; i <= n; i++) {

efficiency++;

output = TWICE * prev + secondPrev;

secondPrev = prev;

prev = output;

}

}

return output;

}

//for recursion//

private int Recursion(int n) {

int output;

efficiency++;

if (n == 0) {

output = 0;

} else if (n == 1) {

output = 1;

} else {

output = TWICE * Recursion(n - 1) + Recursion(n - 2);

}

return output;

}

//main function//

public static void main(String[] args) {

Project01 proj01 = new Project01();

proj01.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