Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*-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 RL = 0.0;
- double LR = 0.0;
- double a = 0.0;
- double b = 0.0;
- double c = 0.0;
- double result = 0.0;
- String gn = "go";
- int bn = 0;
- //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 tn = new StringTokenizer(sn, "= ");
- while (tn.hasMoreTokens())
- {
- String STproblem = st.nextToken();
- String STnValue = st.nextToken();
- //{System.out.println(tn.nextToken());}
- //Convert string to int
- int problem = Integer.parseInt(STproblem);
- int nValue = Integer.parseInt(STnValue);
- //int n = bn;
- //n = Integer.parseInt(tn);
- System.out.println( nValue + problem);
- //Problem 23________________________________________________________
- //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
- DecimalFormat dec = new DecimalFormat("###.##");
- String output = ("Sum of the series from left to right: " + dec.format(LR) +
- "\nSum of the series from right to left: " + dec.format(RL));
- JOptionPane.showMessageDialog(null, output);
- } while (!gn.equals("stop"));
- /*
- //Problem 25_________________________________________________________________________
- String sn = JOptionPane.showInputDialog("Enter Problem number and n");
- double n = Double.parseDouble(sn);
- 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);
- System.out.println(c);}
- //Dialog Ouput box for Problem 25
- String output = ("Sum of the series: " + result);
- JOptionPane.showMessageDialog(null, output);
- */
- }
- }
Add Comment
Please, Sign In to add comment