import java.text.DecimalFormat; // for formatting doubles import uwcse.io.*; /** * Practicing using loops */ public class Loops { /** * Check that the sum of the cubes is the square of the sum *
*
* i.e. (^ means exponentiation (not available in java))
* 1^3 + 2^3 + 3^3 + ... + n^3 = (1 + 2 + 3 + ... + n)^2
* Use a loop to allow the user to test the formula with several values of
* n. Exit the loop if n is not a positive integer.
*/
public void sum3vsSum2() {
int n;
Input input = new Input();
System.out.println("Sum of the cubes / square of the sum");
do { // Input n until n<=0
n = input.readInt("Enter an integer (<=0 to quit): ");
int sum = 0; // sum
int sumCubes = 0; // sum of the cubes
// Compute sum and sumCubes
// use a for loop
for (int i = 1; i <= n; i++) {
sum += i;
sumCubes += (int) (Math.pow(i, 3));
}
if (n > 0)
System.out.println("Sum of the cubes: " + sumCubes
+ "\nSquare of the sum: " + sum * sum);
} while (n > 0);
}
/**
* Display shapes of stars
*
* In a console window, display the following shapes using for loops * *
* * * * * ** ** * *** *** * **** **** * ** * Allow the user to select the number of lines. Repeat the process until * the user inputs a non positive value for the number of lines. */ public void figuresOfStars() { int n; Input input = new Input(); System.out.println("Display of stars"); do { n = input.readInt("Enter the number of lines (<=0 to quit): "); for (int i = 1; i <= n; i++) { // print the line for the left triangle for (int j = 1; j <= i; j++) System.out.print("*"); // gap between the two triangles for (int j = 1; j <= 2 * (n - i) + 2; j++) System.out.print(" "); // print the line for the right triangle for (int j = 1; j <= i; j++) System.out.print("*"); // Go to the next line System.out.println(); } } while (n > 0); } /** * Sum the digits of a positive integer *
* Let the user enter an integer.
* If the integer is positive, sum all of the digits of the integer. Repeat
* the process until you get a single digit answer:
* for instance, 123456 gives 21 which gives 3
* Repeat the process until the user inputs a non positive value for the
* integer.
*/
public void sumDigits() {
int n;
Input input = new Input();
System.out.println("Sum the digits");
int nSave;
do {
n = input.readInt("Enter an integer (<=0 to quit): ");
int sum; // sum of the digits of n.
nSave = n; // n will be modified. Save its initial value.
do {
sum = 0;
while (n > 0) {
sum += n % 10; // add to sum the rightmost digit of n
n /= 10; // shift all of the digits by one to the right.
// the rightmost digit is dropped.
}
n = sum;
} while (n >= 10);
if (sum > 0)
System.out.println(nSave + " yields " + sum);
} while (nSave > 0);
}
/**
* Rolling a die
*
* Let the user enter an integer n. Roll a dice n times and display how many
* times each face comes out.
* Repeat the process until the user enters a non positive value for n.
*/
public void rollADie() {
int n;
Input input = new Input();
System.out.println("Roll a die");
do {
// To format the percentages
DecimalFormat twoDigits = new DecimalFormat("0.00");
n = input.readInt("Enter an integer (<=0 to quit): ");
// Count the numbers of ones, twos, threes... that come out.
int one = 0, two = 0, three = 0, four = 0, five = 0, six = 0;
// Roll the die n times
for (int i = 1; i <= n; i++) {
switch ((int) (Math.random() * 6) + 1)
// generate a random integer between 1 and 6
// and count how many times each value comes out.
{
case 1:
one++;
break;
case 2:
two++;
break;
case 3:
three++;
break;
case 4:
four++;
break;
case 5:
five++;
break;
case 6:
six++;
break;
}
}
if (n > 0)
System.out.println("Face 1: " + one + " or "
+ twoDigits.format(100. * one / n) + "%\n" + "Face 2: "
+ two + " or " + twoDigits.format(100. * two / n)
+ "%\n" + "Face 3: " + three + " or "
+ twoDigits.format(100. * three / n) + "%\n"
+ "Face 4: " + four + " or "
+ twoDigits.format(100. * four / n) + "%\n"
+ "Face 5: " + five + " or "
+ twoDigits.format(100. * five / n) + "%\n"
+ "Face 6: " + six + " or "
+ twoDigits.format(100. * six / n) + "%\n");
} while (n > 0);
}
/**
* Starts the application
*/
public static void main(String[] args) {
Loops lp = new Loops();
lp.sum3vsSum2();
lp.figuresOfStars();
lp.sumDigits();
lp.rollADie();
}
}