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 = 100;
  20. public static final int SIZE = 3;
  21. public static final int HEIGHT = 1;
  22.  
  23.  
  24. public static void main (String[] args) {
  25.  
  26. harmonic();
  27. sum();
  28. triangle();
  29. }
  30.  
  31. //Method for printing sum of harmonic series
  32. public static void harmonic ()
  33. {
  34.  
  35. //Declare Variable Double 'sum' to hold the harmonic series calculation
  36. double sum=0;
  37. for (int i=1; i<=N; i++) {
  38. // i is used for Denominator of fraction (1/i)
  39. // each run through loop adds the fraction to container 'sum'
  40. sum += 1.0/i;
  41. }
  42. //printing Total value of 'sum' based upon CONSTANT N
  43. System.out.println("Harmonic to " + N + " = " + sum);
  44. System.out.println();
  45. }
  46.  
  47. //method for printing sum [Rules : Odd Integers up to N;
  48. //sum of first N positive odd integers]
  49. public static void sum ()
  50. {
  51. //Declare variables 'sum1' & 'sum2' for
  52. //holding the sum amounts of the required calculations
  53. //'sum 1' : Is for holding the computation of the sum of
  54. // ODD integers up to N.
  55.  
  56. int sum1=0;
  57. int sum2=0;
  58. for (int i=1; i<=N; i+=2)
  59. {
  60. sum1 += i;
  61. }
  62. for (int j=1; j<=(N*2); j+=2)
  63. {
  64. sum2 += j;
  65. }
  66. System.out.println("Sum of odds UP to " + N + ": " + sum1);
  67. System.out.println();
  68. System.out.println("Sum of first " + N + " odds : " + sum2);
  69.  
  70. }
  71.  
  72. //method for printing a triangle of spaces & * using SIZE constant to
  73. //format the output size
  74. public static void triangle ()
  75. {
  76. //Declare variables to hold characters uses for
  77. //creating desired shape
  78. //Chosen to use variables in hopes of easier
  79. //code to read
  80. char star = '*';
  81. char space = ' ';
  82.  
  83. for (int i=0; i<SIZE; i++)
  84. {
  85. for (int j=SIZE-i; j>=1; j--)
  86. {
  87. System.out.print(space);
  88. }
  89. for (int k=1; k<=i; k++)
  90. {
  91. System.out.print(star);
  92. }
  93. //Ends the line and
  94. System.out.println();
  95. }
  96. System.out.println();
  97. }
  98.  
  99. //method for printing a parallelogram of spaces & * using SIZE constant to
  100. //format the output size
  101. public static void parallelogram ()
  102. {
  103.  
  104.  
  105. }
  106.  
  107. //method for printing a diamond shape from '*' characters
  108. //using HEIGHT constant to
  109. public static void diamond ()
  110. {
  111.  
  112. }
  113.  
  114. }
Success #stdin #stdout 0.08s 212416KB
stdin
Standard input is empty
stdout
Harmonic to 100 = 5.187377517639621

Sum of odds UP to 100: 2500

Sum of first 100 odds : 10000
   
  *
 **