Don't like ads? PRO users don't see any ads ;-)
Guest

n00810678 a2

By: DrivenWanderer on May 23rd, 2012  |  syntax: Java  |  size: 3.25 KB  |  hits: 20  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  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 a      = 0.0;
  26.          double b      = 0.0;
  27.          double c      = 0.0;
  28.          String gn     = "go";
  29.      
  30.       //Input Dialog Box
  31.      
  32.          do {
  33.          
  34.             String sn = JOptionPane.showInputDialog("Enter problem number and n value");
  35.          
  36.             gn = sn;
  37.          
  38.             if (sn.equals("stop"))     
  39.                System.exit(0);
  40.             else;      
  41.          
  42.          //String Tokenizer
  43.          
  44.             StringTokenizer st = new StringTokenizer(sn, "= ");
  45.            
  46.                int p = Integer.parseInt(st.nextToken());
  47.                int n  = Integer.parseInt(st.nextToken());
  48.                
  49.          //Problem 23________________________________________________________
  50.                        
  51.             if (p == 23)
  52.             {
  53.                 double RL     = 0.0;
  54.                         double LR     = 0.0;
  55.  
  56.             //Forward Loop
  57.            
  58.                for(int i = 1; i <= n; i++)
  59.                
  60.                {a = 1 / (float)i;
  61.                   RL = (RL + a);}
  62.            
  63.             //Reverse Loop
  64.            
  65.                for (int j = n; j >= 1; j--)
  66.                
  67.                {b = 1 / (float)j;
  68.                   LR = (LR + b);}
  69.            
  70.             //Output Dialog box for 23
  71.            
  72.                String output = ("Sum of the series from left to right: " + LR +
  73.                     "\nSum of the series from right to left: " + RL);
  74.            
  75.                JOptionPane.showMessageDialog(null, output);    
  76.            
  77.            
  78.             }
  79.             else
  80.            
  81.             //Problem 25_________________________________________________________________________
  82.            
  83.                                 { if (p == 25)  
  84.                                         { double result = 0.0;
  85.                                                
  86.                for(int i = 0; i <= n; i++)
  87.                
  88.                {c = (4.0 * (Math.pow((-1.0),i) * (1.0 / (2.0 * i + 1.0))));
  89.                   result = (result + c);}
  90.                
  91.             //Dialog Ouput box for Problem 25
  92.            
  93.                String output = ("Sum of the series: " + result);
  94.            
  95.                JOptionPane.showMessageDialog(null, output);    
  96.                                 }
  97.                                         else;
  98.                                 }        
  99.          } while (!gn.equals("stop"));
  100.      
  101.       }
  102.    
  103.    }