CS110 Programming Assignment #6

Instructor: Trish Cornez

for statements and using Java methods






PROGRAM 1
Write an interactive program that prompts the user for a positive integer n .
use a for loop to calculate the following sum (a real number):
     sum = 1 + 1/2 + 1/3 . . . . + 1/n

For accuracy, use the long double data type. Use methods to perform input, calculations, and output.

1.  Print the the source code for the program.
2.  Print a single output window that performs the following:
  • Execution 1: Let n be equal to any negative number.
  • Execution 2: Input n as zero.
  • Execution 3: Let n be equal to any positive number.

3.  use functions where needed : Input, Calculation, Display.
The Input function should employ a forever loop to ensure that a positive integer is entered.



PROGRAM 2
Write an interactive program that prompts the user for anytwo integers X and Y - positive or negative. Calculate X * Y without using the multiplication operator. Use methods to perform input, calculations, and output.

1.  Print the the source code for the program.
2.  Print a single output window with the following:
  • Let X and/or Y be equal to any negative number.
  • Input X or Y as zero.
  • Let X and Y both be equal to any positive number greater than 2.
  • Let X and Y both be equal to any negative number less than -5.
  • Let X be any positive greater than 10 and Y any negative number less than -10.
  • Let y be any positive greater than 10 and x any negative number less than -10.

3.  use separate functions for Input, Calculation, and Display.



PROGRAM 3
Write an interactive program that prompts the user for a positive integer n - use a function that utilizes a forever loop to ensure that a positive integer is entered - then convert the value of n to binary notation.

Guidelines:
  1. Allow the user to perform repeated conversions in a single execution.
  2. Print only one execution window with the following values of n.
  3. Use methods to perform input, calculations, and output.
  • n = 3 , the binary value is 11.
  • n = 16, the binary value is 10000.
  • n = 201, the binary value is 11001001.
  • n = -4, Invalid value for n. n must be positive.
  • n = 0, binary value is 0.
For conversions use repeated division by 2 on the n until it reaches 0.
For example, if n = 10:
10 /2 = 5         remainder #1 is 0     Multiply this remainder by 1.
5 / 2 = 2         remainder #2 is 1     Multiply this remainder by 10.
2 / 2 = 1         remainder #3 is 0     Multiply this remainder by 100.
1 / 2 = 0         remainder #3 is 1     Multiply this remainder by 1000.