Guest User

Untitled

a guest
Aug 14th, 2018
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.94 KB | None | 0 0
  1. /*-Program opens with a dialog box requesting two integers.
  2.   -Box can be given either two integers or 'stop'
  3.   -If the string given is not stop, the StringTokenizer program
  4.     will break down the string into individual strings.
  5.   -These strings can then be converted to integers.
  6.   -If given two integers, the first determines which math
  7.    problem to use and the second is the value of n
  8.   -The math problem requires a loop to sum the answers from 1 to n.
  9.     (In both directions for problem 23 and just forward for problem 25)
  10.   -A results window will display showing two answers for problem 23, one from
  11.    the begining and one from the end.
  12.   -A results window will display the approximation of Pi for problem 25
  13.   -Clicking okay will restart the program
  14.   -If given 'stop' the program will terminate
  15. */
  16.  
  17. import java.util.StringTokenizer;
  18. import javax.swing.JOptionPane;
  19. import java.text.DecimalFormat;
  20.  
  21. public class n00810678 {
  22.     public static void main(String[] args) {
  23.    
  24.     //Variables
  25.     double RL     = 0.0;
  26.     double LR     = 0.0;
  27.     double a      = 0.0;
  28.     double b      = 0.0;
  29.     double c      = 0.0;
  30.     double result = 0.0;
  31.     String gn     = "go";
  32.     int bn         = 0;
  33.    
  34.     //Input Dialog Box
  35.    
  36.     do {
  37.    
  38.             String sn = JOptionPane.showInputDialog("Enter Problem number and n value");
  39.    
  40.             gn = sn;
  41.    
  42.             if (sn.equals("stop")) 
  43.             System.exit(0);
  44.             else;  
  45.    
  46.     //String Tokenizer
  47.    
  48.             StringTokenizer tn = new StringTokenizer(sn, "= ");
  49.        
  50.             while (tn.hasMoreTokens())
  51.             {
  52.                 String STproblem = st.nextToken();
  53.                 String STnValue  = st.nextToken();
  54.            
  55.             //{System.out.println(tn.nextToken());}
  56.    
  57.     //Convert string to int
  58.  
  59.         int problem = Integer.parseInt(STproblem);
  60.         int nValue  = Integer.parseInt(STnValue);
  61.  
  62.             //int n = bn;
  63.  
  64.             //n = Integer.parseInt(tn);
  65.             System.out.println( nValue + problem);
  66.  
  67. //Problem 23________________________________________________________
  68.    
  69.    
  70.     //Forward Loop
  71.  
  72.     for(int i = 1; i <= n; i++)
  73.        
  74.         {a = 1 / (float)i;
  75.         RL = (RL + a);}
  76.        
  77.     //Reverse Loop
  78.  
  79.     for (int j = n; j >= 1; j--)
  80.        
  81.         {b = 1 / (float)j;
  82.         LR = (LR + b);}
  83.        
  84.     //Output Dialog box for 23
  85.        
  86.     DecimalFormat dec = new DecimalFormat("###.##");
  87.        
  88.     String output = ("Sum of the series from left to right: " + dec.format(LR) +
  89.                        "\nSum of the series from right to left: " + dec.format(RL));
  90.        
  91.     JOptionPane.showMessageDialog(null, output);   
  92.    
  93.     } while (!gn.equals("stop"));
  94. /*
  95. //Problem 25_________________________________________________________________________
  96.    
  97.     String sn = JOptionPane.showInputDialog("Enter Problem number and n");
  98.  
  99.         double n = Double.parseDouble(sn);
  100.        
  101.         for(int i = 0; i <= n; i++)
  102.        
  103.         {c = (4.0 * (Math.pow((-1.0),i) * (1.0 / (2.0 * i + 1.0))));
  104.          result = (result + c);
  105.          System.out.println(c);}
  106.                    
  107.     //Dialog Ouput box for Problem 25
  108.        
  109.     String output = ("Sum of the series: " + result);
  110.        
  111.     JOptionPane.showMessageDialog(null, output);   
  112.  */
  113.  
  114. }
  115. }
Add Comment
Please, Sign In to add comment