Question
JAVA: When the window is closed, the efficiency values should be computed with the values from 0 to 10, and then written to a file.
JAVA: When the window is closed, the efficiency values should be computed with the values from 0 to 10, and then written to a file. 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. However, my program does not output anything to the output.txt file when the window is closed.The area with the code in question is in bold. There are two files, GUI and Sequence. I only need help with the output to the txt file, it is the only question.
------------------------------------------------------
GUI.java
------------------------------------------------------
package project3;
import javax.swing.*;
import javax.swing.text.BadLocationException;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class GUI extends JFrame
{
/**
*
*/
private static final long serialVersionUID = 1L;
private final Font font = new Font ("Arial", Font.BOLD, 18);
private final static JFrame frame = new JFrame();
private final ButtonGroup userSelection = new ButtonGroup();
private final JLabel enterLabel = new JLabel("Enter a number: ");
private final JLabel resultLabel = new JLabel("Result");
private final JLabel effLabel = new JLabel("Efficiency");
private static final JRadioButton iterativeBtn = new JRadioButton("Iterative");
private final static JRadioButton recursiveBtn = new JRadioButton("Recursive");
private final static JTextField enterField = new JTextField(15);
private final static JTextField resultField = new JTextField();
private final static JTextField effField = new JTextField();
private final JButton calcBtn = new JButton("Compute");
private final JPanel choicePanel = new JPanel();
private final JPanel enterPanel = new JPanel();
private final JPanel buttonPanel = new JPanel();
private final JPanel resultPanel = new JPanel();
private final JPanel mainPanel = new JPanel();
private final static String fileName = "C:\\output.txt";
private static PrintWriter output;
private static FileWriter fw;
private static BufferedWriter bw;
// Constructor for the GUI.
public GUI() {
setFrame();
setPanels();
setAttributes();
addWindowListener(new CloseApp());
}
// Sets the attributes for the frame.
public void setFrame() {
setSize(800, 400);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("Project 3");
add(mainPanel);
}
// Sets the attributes for the components.
public void setAttributes(){
mainPanel.setLayout(new GridLayout(4,1));
resultPanel.setLayout(new GridLayout(2,2));
userSelection.add(iterativeBtn);
userSelection.add(recursiveBtn);
iterativeBtn.setFont(font);
recursiveBtn.setFont(font);
enterLabel.setFont(font);
resultLabel.setFont(font);
effLabel.setFont(font);
calcBtn.setFont(font);
resultField.setEditable(false);
effField.setEditable(false);
iterativeBtn.setSelected(true);
calcBtn.addActionListener(new buttonClick());
}
//Adds the components to the panels and adds the panels to the main Panel
public void setPanels(){
choicePanel.add(iterativeBtn);
choicePanel.add(recursiveBtn);
enterPanel.add(enterLabel);
enterPanel.add(enterField);
buttonPanel.add(calcBtn);
resultPanel.add(resultLabel);
resultPanel.add(resultField);
resultPanel.add(effLabel);
resultPanel.add(effField);
mainPanel.add(choicePanel);
mainPanel.add(enterPanel);
mainPanel.add(buttonPanel);
mainPanel.add(resultPanel);
}
// Method that calls the Sequence class and passes in the integer.
public static void callSequence(int a){
// Iterative method
if(iterativeBtn.isSelected()){
int iterativeValue = Sequence.computeIterative(a);
int itEff = (Sequence.counter + 1);
resultField.setText(Integer.toString(iterativeValue));
effField.setText(Long.toString(itEff));
int recEff = 0;
// Print to the file at C:\\output.txt
iPrintToFile(a,itEff,recEff);
// Reset counter
Sequence.resetCounter(0);
}
// Recursive method
else if (recursiveBtn.isSelected()){
int recursiveValue = Sequence.computeRecursive(a);
int recEff = Sequence.counter;
resultField.setText(Integer.toString(recursiveValue));
effField.setText(Long.toString(recEff));
int itEff = 0;
// Print to the file at C:\\output.txt
rPrintToFile(a,itEff, recEff);
Sequence.resetCounter(0);
}
}
// Test method that parses the text to an Integer and be sure it is not negative.
public static int testMethod(String enterText){
int a = 0;
int b = 0;
try {
a = Integer.parseInt(enterText);
}
catch(NumberFormatException e){
JOptionPane.showMessageDialog(frame, "Please enter a whole number.");
}
if (a >= 0){
b = a;
}
else {
JOptionPane.showMessageDialog(frame, "Please enter a positive whole number.");
}
return b;
}
// Print method that takes in the values and prints them to a file.
public static void iPrintToFile(int a,int itEff, int recEff){
output.println(a + ","+itEff+"," + recEff);
output.close();
}
public static void rPrintToFile(int a,int itEff, int recEff){
output.println(a + "," +itEff+","+ recEff);
output.close();
}
// Inner class that handles the Compute Button, opens the filewriter, the bufferedwriter, and the printwriter.
public class buttonClick implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
int retrievedValue = testMethod(enterField.getText());
try{
fw = new FileWriter(fileName, true);
bw = new BufferedWriter(fw);
output = new PrintWriter(bw);
}
catch(IOException g){
JOptionPane.showMessageDialog(frame, "File was not found.");
}
callSequence(retrievedValue);
}
}
//Inner class that handles the WindowClose Event and closes the filewriter and the bufferedwriter
public static class CloseApp extends WindowAdapter{
public void windowClosing(WindowEvent e, int a)
{
try{
windowClosed(null);
System.exit(0);
fw.close();
bw.close();
}
catch(IOException f){
JOptionPane.showMessageDialog(frame, f);
}
}
public void windowClosed(WindowEvent e, int a, int itEff, int recEff) throws BadLocationException {
for(int i = 0; i < 10; i++) {
int iterativeValue = Sequence.computeIterative(i);
int itEff1 = (Sequence.counter + 1);
resultField.setText(Integer.toString(iterativeValue));
effField.setText(Long.toString(itEff));
// Print to the file at C:\\output.txt
iPrintToFile(a,itEff,recEff);
int recursiveValue = Sequence.computeRecursive(i);
int recEff1 = Sequence.counter;
resultField.setText(Integer.toString(recursiveValue));
effField.setText(Long.toString(recEff));
// Print to the file at C:\\output.txt
rPrintToFile(a,itEff, recEff);
Sequence.resetCounter(0);
int retrievedValue = testMethod(enterField.getText(i, a));
try {
fw = new FileWriter(fileName, true);
bw = new BufferedWriter(fw);
output = new PrintWriter(bw);
}
catch(IOException g) {
JOptionPane.showMessageDialog(frame, "File was not found.");
}
}System.exit(0);
}
}
// Main method
public static void main(String args[]) {
GUI myView = new GUI();
myView.setVisible(true);
}
}
--------------------------------------------
Sequence.java
--------------------------------------------
package project3;
public final class Sequence {
public static int counter;
// Constructor and sets counter.
private Sequence(){
counter = 0;
}
// Computes the sequence values iteratively.
public static int computeIterative(int n){
int previous = 0;
int current = 1;
if(n==0){
return previous;
}
for(int i = 1; i < n; i++){
int result = (2 * current) + previous;
previous = current;
current = result;
getEfficiency();
int effI = counter;
}
return current;
}
// Computes the sequence values recursively.
public static int computeRecursive(int n){
if(n == 0){
int result = 0;
getEfficiency();
return result;
}
if(n == 1){
int result = 1;
getEfficiency();
return result;
}
int num1 = computeRecursive(n-1);
int num2 = computeRecursive(n-2);
int newTerm = ((2 * num1) + num2);
getEfficiency();
int effR = counter;
return newTerm;
}
// Calculates the efficiency for each method.
public static void getEfficiency(){
counter++;
}
/*
* Resets the counter variable
*/
public static void resetCounter(int a){
counter = a;
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started