fork download
  1. /*
  2. CSC110 Assignment:_Lab04.java__
  3. Programmer: Phillip Sherman
  4. Due Date: 2/13/12
  5. Points: 35
  6.  
  7. Description of Application: Four methods (harmonic, sum, triangle, parellelogram)
  8.  
  9. Description of Inputs: Class constants N, SIZE, HEIGHT
  10.  
  11. Description of Outputs: '*', ' ', or intergers
  12. */
  13.  
  14.  
  15. class Lab04 {
  16.  
  17. //Class constants required for modifying output of various methods
  18. // N is used
  19. public static final int N = 5;
  20. public static final int SIZE = 1;
  21. public static final int HEIGHT = 1;
  22.  
  23.  
  24. public static void main (String[] args) {
  25.  
  26. harmonic();
  27. sum();
  28. }
  29.  
  30. //Method for printing sum of harmonic series
  31. public static void harmonic () {
  32.  
  33. //Declare Variable Double 'sum' to hold the harmonic series calculation
  34. double sum=0;
  35. for (int i=1; i<=N; i++) {
  36. // i is used for Denominator of fraction (1/i)
  37. // each run through loop adds the fraction to container 'sum'
  38. sum += 1.0/i;
  39. }
  40. //printing Total value of 'sum' based upon CONSTANT N
  41. System.out.println("Harmonic to " + N + "= " + sum);
  42. }
  43.  
  44. //method for printing sum [Rules : Odd Integers up to N;
  45. //sum of first N positive odd integers]
  46. public static void sum () {
  47. //Declare variables 'sum1' & 'sum2' for
  48. //holding the sum amounts of the required calculations
  49. //'sum 1' : Is for holding the computation of the sum of
  50. // ODD integers up to N.
  51.  
  52. int sum1=0;
  53. int sum2=0;
  54. for (int i=1; i<=N; i+=2)
  55. {
  56. sum1 += i;
  57. }
  58. for (int j=1; j<=N; j++)
  59. {
  60. for (int k=1; k<=N; k+=2)
  61. {
  62. sum2 += k;
  63. }
  64. }
  65. System.out.println(sum1);
  66. System.out.println();
  67. System.out.println(sum2);
  68.  
  69. }
  70.  
  71. //method for printing a triangle of spaces & * using SIZE constant to
  72. //format the output size
  73. public static void triangle () {
  74.  
  75.  
  76. }
  77.  
  78. //method for printing a parallelogram of spaces & * using SIZE constant to
  79. //format the output size
  80. public static void parallelogram () {
  81.  
  82.  
  83. }
  84.  
  85. }
Success #stdin #stdout 0.09s 212416KB
stdin
Standard input is empty
stdout
Harmonic to 5= 2.283333333333333
9

45