/*-Program opens with a dialog box requesting two integers.
-Box can be given either two integers or 'stop'
-If the string given is not stop, the StringTokenizer program
will break down the string into individual strings.
-These strings can then be converted to integers.
-If given two integers, the first determines which math
problem to use and the second is the value of n
-The math problem requires a loop to sum the answers from 1 to n.
(In both directions for problem 23 and just forward for problem 25)
-A results window will display showing two answers for problem 23, one from
the begining and one from the end.
-A results window will display the approximation of Pi for problem 25
-Clicking okay will restart the program
-If given 'stop' the program will terminate
*/
import java.util.StringTokenizer;
import javax.swing.JOptionPane;
import java.text.DecimalFormat;
public class n00810678 {
public static void main(String[] args) {
//Variables
double a = 0.0;
double b = 0.0;
double c = 0.0;
String gn = "go";
//Input Dialog Box
do {
String sn = JOptionPane.showInputDialog("Enter problem number and n value");
gn = sn;
if (sn.equals("stop"))
System.exit(0);
else;
//String Tokenizer
StringTokenizer st = new StringTokenizer(sn, "= ");
int p = Integer.parseInt(st.nextToken());
int n = Integer.parseInt(st.nextToken());
//Problem 23________________________________________________________
if (p == 23)
{
double RL = 0.0;
double LR = 0.0;
//Forward Loop
for(int i = 1; i <= n; i++)
{a = 1 / (float)i;
RL = (RL + a);}
//Reverse Loop
for (int j = n; j >= 1; j--)
{b = 1 / (float)j;
LR = (LR + b);}
//Output Dialog box for 23
String output = ("Sum of the series from left to right: " + LR +
"\nSum of the series from right to left: " + RL);
JOptionPane.showMessageDialog(null, output);
}
else
//Problem 25_________________________________________________________________________
{ if (p == 25)
{ double result = 0.0;
for(int i = 0; i <= n; i++)
{c = (4.0 * (Math.pow((-1.0),i) * (1.0 / (2.0 * i + 1.0))));
result = (result + c);}
//Dialog Ouput box for Problem 25
String output = ("Sum of the series: " + result);
JOptionPane.showMessageDialog(null, output);
}
else;
}
} while (!gn.equals("stop"));
}
}