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

Everything works flawlessly, except when the computation is done, the efficiency

ID: 670518 • Letter: E

Question

Everything works flawlessly, except when the computation is done, the efficiency results starts with "1" and always increases when pressing "compute" for either Iterative or Recursive. The efficiency results is not computing correctly.

Please keep the GUI intact much as possible.

Source:


import java.util.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

//GUI class implementing ActionListener and
//contains inner class WindowAdapter
public class SequenceGUI extends JFrame implements ActionListener {
     //declare the required varaibles
     JRadioButton jrb1, jrb2;
     JButton jb;
     JLabel jlb1, jlb2, jlb3;
     JTextField jtf1, jtf2, jtf3;
     JPanel jpan;
     int iterCount = 0, recCount = 0;

    //Constructor
     public SequenceGUI() {
         jpan = new JPanel(new GridLayout(6, 2, 5, 20));
         jrb1 = new JRadioButton("Iterative");
         jrb2 = new JRadioButton("Recursive");
         ButtonGroup bg = new ButtonGroup();
         bg.add(jrb1);
         bg.add(jrb2);
         jpan.add(new JLabel());
         jpan.add(jrb1);
         jpan.add(new JLabel());
         jpan.add(jrb2);

        jlb1 = new JLabel("Enter n: ");
         jtf1 = new JTextField();
         jpan.add(jlb1);
         jpan.add(jtf1);
         jb = new JButton("Compute");

        jpan.add(new JLabel());
         jpan.add(jb);

        jlb2 = new JLabel("Result: ");
         jtf2 = new JTextField();
         jtf2.setEnabled(false);
         jpan.add(jlb2);
         jpan.add(jtf2);

        jlb3 = new JLabel("Efficiency: ");
         jtf3 = new JTextField();
         jtf3.setEnabled(false);
         jpan.add(jlb3);
         jpan.add(jtf3);

        add(jpan);

        //add action listener to the button
         jb.addActionListener(this);

        //create an object to the WindowAdpaterImplementation class
         WindowAdapterImplementations wai = new WindowAdapterImplementations();

        //invoke the add
         addWindowListener(wai);
     }

    //main method
     public static void main(String args[]) {
         //create an object to the SequenceGUI
         SequenceGUI seqGUI = new SequenceGUI();
         seqGUI.setVisible(true);
         seqGUI.setSize(300, 300);
         seqGUI.setTitle("Sequence numbers generator");
         seqGUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     }

    //actionPerformed method that executes the
     //operations on button click
     public void actionPerformed(ActionEvent ae) {
         if (jb == ae.getSource()) {
             if (jrb1.isSelected()) {
                 int itValue = Integer.parseInt(jtf1.getText());
                 int itVal = SequenceUtilityClass.computeIterative(itValue);
                 jtf2.setText(itVal + "");
                 SequenceUtilityClass.getEfficiency(iterCount);
                 iterCount++;
                 jtf3.setText(iterCount + "");

            } else if (jrb2.isSelected()) {
                 int recValue = Integer.parseInt(jtf1.getText());
                 int recVal = SequenceUtilityClass
                     .computeIterative(recValue);
                 jtf2.setText(recVal + "");
                 SequenceUtilityClass.getEfficiency(recCount);
                 recCount++;
                 jtf3.setText(recCount + "");

            }
         }
     }
  
     //WindowAdapter class that writes the data into the .csv file
     class WindowAdapterImplementations extends WindowAdapter {
         public void windowClosing(WindowEvent e) {
             try {
                 File f = new File("SequenceEfficiency.csv");
                 FileWriter fw = new FileWriter(f);
                 for (int i = 0; i < 10; i++) {
                     fw.write(String.valueOf(i + 1));
                     fw.write(",");
                     fw.write(String.valueOf(SequenceUtilityClass.computeIterative(i+1)));
                     fw.write(",");
                    fw.write(String.valueOf(SequenceUtilityClass.computeRecursive(i+1)));
                     fw.write(" ");
                 }
                 fw.flush();
                 fw.close();
                 System.out.println("SequenceEfficiency.csv file created successfully");
             } catch (Exception ae) {
                 System.err.println("Unable to write to the file"+ae);
                 System.exit(0);
             }
         }
     }
}

// Sequence Class

//SequenceUtilityClass.java will contain all static methods
public class SequenceUtilityClass
{
public static int computeIterative(int n)
{
   int value[] = new int[n + 1];
   value[0] = 0;
   value[1] = 1;
   for (int i = 2; i <= n; i++)
    value[i] = 2 * value[i - 1] + value[i - 2];
   return value[n];
}

public static int computeRecursive(int n)
{
   if (n == 0)
    return 0;
   else if (n == 1)
    return 1;
   else
   {
    return 2 * computeRecursive(n - 1) + computeRecursive(n - 2);
   
   }
}

public static int getEfficiency(int eff)
{
   return eff;
}
}

Explanation / Answer

Modified Program:


import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.FileWriter;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;


public class NewJFrame extends javax.swing.JFrame implements ActionListener{
JRadioButton jrb1, jrb2;
JButton jb;
JLabel jlb1, jlb2, jlb3;
JTextField jtf1, jtf2, jtf3;
JPanel jpan;
int iterCount = 0, recCount = 0;
public NewJFrame() {
// initComponents();
jpan = new JPanel(new GridLayout(6, 2, 5, 20));
jrb1 = new JRadioButton("Iterative");
jrb2 = new JRadioButton("Recursive");
ButtonGroup bg = new ButtonGroup();
bg.add(jrb1);
bg.add(jrb2);
jpan.add(new JLabel());
jpan.add(jrb1);
jpan.add(new JLabel());
jpan.add(jrb2);
jlb1 = new JLabel("Enter n: ");
jtf1 = new JTextField();
jpan.add(jlb1);
jpan.add(jtf1);
jb = new JButton("Compute");
jpan.add(new JLabel());
jpan.add(jb);
jlb2 = new JLabel("Result: ");
jtf2 = new JTextField();
jtf2.setEnabled(false);
jpan.add(jlb2);
jpan.add(jtf2);
jlb3 = new JLabel("Efficiency: ");
jtf3 = new JTextField();
jtf3.setEnabled(false);
jpan.add(jlb3);
jpan.add(jtf3);
add(jpan);
//add action listener to the button
jb.addActionListener(this);
//create an object to the WindowAdpaterImplementation class
WindowAdapterImplementations wai = new WindowAdapterImplementations();
//invoke the add
addWindowListener(wai);
}

@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 400, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 300, Short.MAX_VALUE)
);

pack();
}// </editor-fold>

  
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
NewJFrame seqGUI = new NewJFrame();
seqGUI.setVisible(true);
seqGUI.setSize(300, 300);
seqGUI.setTitle("Sequence numbers generator");
seqGUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
/* Create and display the form */
// java.awt.EventQueue.invokeLater(new Runnable() {
// public void run() {
// new NewJFrame().setVisible(true);
// }
//});
}
@Override
public void actionPerformed(ActionEvent ae) {
if (jb == ae.getSource()) {
if (jrb1.isSelected()) {
int itValue = Integer.parseInt(jtf1.getText());
int itVal = SequenceUtilityClass.computeIterative(itValue);
jtf2.setText(itVal + "");
iterCount=SequenceUtilityClass.getIterativeEfficiency(itValue);
jtf3.setText(iterCount + "");
} else if (jrb2.isSelected()) {
int recValue = Integer.parseInt(jtf1.getText());
int recVal = SequenceUtilityClass
.computeRecursive(recValue);
jtf2.setText(recVal + "");
recCount=SequenceUtilityClass.getRecursiveEfficiency(recValue);
jtf3.setText(recCount + "");
}
}
}
// Variables declaration - do not modify   
// End of variables declaration   
}
//WindowAdapter class that writes the data into the .csv file
class WindowAdapterImplementations extends WindowAdapter {
@Override
public void windowClosing(WindowEvent e) {
try {
File f = new File("SequenceEfficiency.csv");
FileWriter fw = new FileWriter(f);
for (int i = 0; i < 10; i++) {
fw.write(String.valueOf(i + 1));
fw.write(",");
fw.write(String.valueOf(SequenceUtilityClass.computeIterative(i+1)));
fw.write(",");
fw.write(String.valueOf(SequenceUtilityClass.computeRecursive(i+1)));
fw.write(" ");
}
fw.flush();
fw.close();
System.out.println("SequenceEfficiency.csv file created successfully");
} catch (Exception ae) {
System.err.println("Unable to write to the file"+ae);
System.exit(0);
}
}
}

// Sequence Class
//SequenceUtilityClass.java will contain all static methods
class SequenceUtilityClass
{
static int eff;
public static int computeIterative(int n)
{
int value[] = new int[n + 1];
value[0] = 0;
value[1] = 1;
for (int i = 2; i <= n; i++)
value[i] = 2 * value[i - 1] + value[i - 2];
return value[n];
}
public static int computeRecursive(int n)
{
if (n == 0)
return 0;
else if (n == 1)
return 1;
else
return 2 * computeRecursive(n - 1) + computeRecursive(n - 2);
}
public static int getIterativeEfficiency(int n)
{
eff=n*n;
return eff;
}
public static int getRecursiveEfficiency(int n)
{
eff=2*n;
return eff;
}
}

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