In the project below i\'ve already written most of the code. The part i need hel
ID: 3766503 • Letter: I
Question
In the project below i've already written most of the code. The part i need help with is the output file indicated in bold italics. So far in my classes we really didn't go over file writing so I have no idea where to start, can anyone help?
The third programming project involves writing a program to calculate the terms of the following sequence of numbers: 0 1 3 7 15 31 ... where each term of the sequence is three times the previous term minus twice 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 set to selected.
When the window is closed, the efficiency values should be computed with values of n from 0 to 10 and 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 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 as your test plan in the Word 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 corresponds element in the sequence using iteration.
The second method computeRecursive should accept a value of n and return the corresponds element in the sequence using recursion. This method with be a helper method because it will need to initialize the efficiency counter before calling the private recursive method that will actually perform the recursive computation.
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 nonnumeric inputs should be properly handled.
Grading rubrics:
25%: requirements (15% for GUI class, 5% for the iterative computation, 5% for recursive computation)
20%: test data and test documentation. Have 2 different sequences of test. Describe the expected output for each sequence in a document, and explain how you computed this expected output, independently of your program; just running your program and pasting the results does not count!
30%: code structure (use of proper Java constructs, no code repetition, variables as local as possible)
25%: code presentation. Fully spelled and meaningful names (variables, classes, functions), header comment with your full name and the assignment name, no useless comments (in particular the automatically generated comments), no useless imports, proper code indentation.
Explanation / Answer
/***JAVA PROGRAM TO COMPUTE NTH TERM OF THE SEQUENCE USING GUI***/
//IMPORT HEADER FILES
import java.io.*;
import java.util.*;
import java.lang.*;
import java.swingx.*;
import java.awt.*;
import java.awt.event.*;
// Sequence UTILITY CLASS
class Sequence
{
static int eff;
/*CONSTRUCTOR*/
public Sequence()
{
eff=0;
}
//COMPUTE NTH TERM USING ITERATIVE METHOD
public static int computeIterative(int n1)
{
eff=0;
if (n1==0)
return 0;
else if(n1==1)
return 1;
else
{
int previousTerm=1;
int currentTerm;
/***LOOP THROUGH TO COMPUTE THE NTH TERM*/
for(int k1=1;k1<n1-1;k1++)
{
currentTerm=2*previousTerm+1;
previousTerm=currentTerm;
eff++;
}
return currentTerm;
}
}
//RECURSIVE METHOD COMPUTTATION
public static int computeRecursive(int n)
{
eff=0;
return recursiveIMP(n);
}
//RETURN NTH TERM
public static int recursiveIMP(int n1)
{
eff++;
if (n1==0)
return 0;
else if(n1==1)
return 1;
else
//RECURSIVE CALL
return 1+2*computeRecursive(n1-1);
}
//RERURN THE EFFICIENCY
public static int getEfficiency()
{
return eff;
}
}
public class GUI extends WindowAdapter
{
JFrame newFrame1;
JPanel newPanel;
JLabel newLabel1;
JLabel newLabel2;
JLabel newLabel3;
JRadioButton iterativeRB;
JRadioButton recursiveRB;
JTextField j1;
JTextField j2;
JTextField j3;
public GUI()
{
newFrame1=new JFrame("Compute Series");
newFrame1.setLayout(new GridLayput());
newFrame1.setVisible(true);
newPanel=new JPanel();
newPanel.setLayout(new FlowLayout());
newLabel1=new JLabel("Enter n");
newLabel2=new JLabel("Result");
newLabel3=new JLabel("Efficiency");
iterativeRB=new JRadioButton("Iteratice",true);
recursiveRB=new JRadioButton("Recursive");
iterativeRB.setActionCommand("ITERATIVE");
recursiveRB.setActionCommand("RECURSIVE");
iterativeRB.addActionListener(this);
recursiveRB.addActionListener(this);
j1=new JTextField(20);
j2=new JTextField(20);
j3=new JTextField(20);
newPanel.add(newLabel1);
newPanel.add(j1);
newPanel.add(iterativeRB);
newPanel.add(recursiveRB);
newPanel.add(newLabel2);
newPanel.add(j2);
newPanel.add(newLabel3);
newPanel.add(j3);
newFrame1.add(newPanel);
//WRITE THE VALUES TO THE FILE ON CLOSING THE WINDOW
newFrame1.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent wte)
{
try
{//WRITE VALUES TO THE FILE
FileWriter f=new FileWriter("result.txt");
BufferedWriter b12=new BufferedWriter(f);
b12.write(j1.getText()+",");
b12.write(j2.getText()+",");
b12.write(j3.getText()+",");
b12.close();
}
catch(Exception x)
{
x.printStackTrace();
}
}
});
}
//OVERRIDDEN METHOD
public void actionPerformed(ActionEvent at)
{
//CHECK IF RADIO BUTTON ITERATIVE IS SELECTED
if("ITERATIVE".equals(at.getActionCommand()))
{
int n1=Integer.parseInt(j1.getText());
j2.setText(Sequence.computeIterative(n1));
j3.setText(Sequence.getEfficiency());
}
//CHECK IF RADIO BUTTON RECURSIVE IS SELECTED
else if("RECURSIVE".equals(at.getActionCommand()))
{
int n=Integer.parseInt(j1.getText());
j2.setText(Sequence.computeRecursive(n));
j3.setText(Sequence.getEfficiency());
}
}
}//END GUI CLASS
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.