Skip to content

Commit a4a1664

Browse files
committed
Flow of Control: Loops
1 parent c1ff5e2 commit a4a1664

File tree

47 files changed

+2131
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+2131
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package chapter_04;
2+
3+
import java.util.Scanner;
4+
5+
/**
6+
* 1. Write a fragment of code that will read words from the keyboard until the
7+
* word done is entered. For each word except done, report whether its first
8+
* character is equal to its last character. For the required loop, use a
9+
* a. while statement
10+
* b. do-while statement
11+
*
12+
*
13+
* @author Sharaf Qeshta
14+
* */
15+
public class Exercise_04_01
16+
{
17+
public static void main(String[] args)
18+
{
19+
Scanner scanner = new Scanner(System.in);
20+
21+
System.out.print("Enter a word: ");
22+
String word = scanner.next();
23+
while (!word.equals("done"))
24+
{
25+
if (word.charAt(0) == word.charAt(word.length()-1))
26+
System.out.println("the first and the last characters of " + word + " the same");
27+
else
28+
System.out.println("the first and the last characters of " + word + " is not the same");
29+
System.out.print("Enter a word: ");
30+
word = scanner.next();
31+
}
32+
33+
System.out.print("Enter a word: ");
34+
word = scanner.next();
35+
do
36+
{
37+
if (word.charAt(0) == word.charAt(word.length()-1))
38+
System.out.println("the first and the last characters of " + word + " the same");
39+
else
40+
System.out.println("the first and the last characters of " + word + " is not the same");
41+
42+
System.out.print("Enter a word: ");
43+
word = scanner.next();
44+
}
45+
while (!word.equals("done"));
46+
}
47+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package chapter_04;
2+
3+
/**
4+
* 2. Develop an algorithm for computing the quarterly balance in an equity
5+
* fund. You can make one switch (the increase or decrease in the rate of
6+
* return according to the increase or decrease in the level of risk) in a quarter.
7+
* Interest is added to the fund at the end of each quarter. The quarterly
8+
* interest rate is the yearly percentage rate divided by 4.
9+
*
10+
*
11+
* @author Sharaf Qeshta
12+
* */
13+
public class Exercise_04_02
14+
{
15+
// divide the annual interest rate by 4 and store it in x
16+
// x => annualInterestRate/4
17+
// loop 4 times
18+
// every time equity => equity + (equity * x) + (equity * changeRate)
19+
// and print the current equity
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package chapter_04;
2+
3+
/**
4+
* 3. Develop an algorithm for a simple game of guessing at a secret five-digit
5+
* code. When the user enters a guess at the code, the program returns two
6+
* values: the number of digits in the guess that are in the correct position
7+
* and the sum of those digits. For example, if the secret code is 53840, and
8+
* the user guesses 83241, the digits 3 and 4 are in the correct position. Thus,
9+
* the program should respond with 2 and 7. Allow the user to guess a fixed
10+
* number of times.
11+
*
12+
*
13+
* @author Sharaf Qeshta
14+
* */
15+
public class Exercise_04_03
16+
{
17+
// let numberOfGuesses => 3
18+
// loop numberOfGuesses times
19+
// every time read a guess from the keyboard guess => userGuess
20+
// let rightPositions => 0 and sum => 0
21+
// loop through the characters in the user guess
22+
// if the character in the user guess match the character secret word at
23+
// the same position then increment rightPositions by one and add the
24+
// digit to sum
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package chapter_04;
2+
3+
/**
4+
* 4. Write a fragment of code that will compute the product of the first n
5+
* positive even integers. For example, if n is 6, you should compute 2 × 4 ×
6+
* 6 × 8 × 10 × 12.
7+
*
8+
*
9+
* @author Sharaf Qeshta
10+
* */
11+
public class Exercise_04_04
12+
{
13+
public static void main(String[] args)
14+
{
15+
int n = 6, j = 0, product = 1;
16+
for (int i = 2; j < n; i+=2, j++)
17+
product *= i;
18+
19+
System.out.println(product);
20+
}
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package chapter_04;
2+
3+
/**
4+
* 5. Convert the following code so that it uses nested while statements instead
5+
* of for statements:
6+
* int s = 0;
7+
* int t = 1;
8+
* for (int i = 0; i < 10; i++)
9+
* {
10+
* s = s + i;
11+
* for (int j = i; j > 0; j−−)
12+
* {
13+
* t = t * (j − i);
14+
* }
15+
* s = s * t;
16+
* System.out.println("T is " + t);
17+
* }
18+
* System.out.println("S is " + s);
19+
*
20+
*
21+
* @author Sharaf Qeshta
22+
* */
23+
public class Exercise_04_05
24+
{
25+
public static void main(String[] args)
26+
{
27+
int s = 0;
28+
int t = 1;
29+
int i = 0;
30+
while (i < 10)
31+
{
32+
s = s + i;
33+
int j = i;
34+
while (j > 0)
35+
{
36+
t = t * (j - i);
37+
j--;
38+
}
39+
s = s * t;
40+
System.out.println("T is " + t);
41+
i++;
42+
}
43+
System.out.println("S is " + s);
44+
}
45+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package chapter_04;
2+
3+
/**
4+
* 6. Write a for statement to compute the sum 1 + 1/2 + 1/3 + 1/4 + ... + 1/n
5+
*
6+
*
7+
* @author Sharaf Qeshta
8+
* */
9+
public class Exercise_04_06
10+
{
11+
public static void main(String[] args)
12+
{
13+
int n = 100;
14+
double sum = 0;
15+
for (int i = n; i > 0; i--)
16+
sum += 1.0 / i;
17+
System.out.println("The sum is " + sum);
18+
}
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package chapter_04;
2+
3+
/**
4+
* 7. Repeat the previous question, but use the comma operator and omit the
5+
* for statement’s body
6+
*
7+
*
8+
* @author Sharaf Qeshta
9+
* */
10+
public class Exercise_04_07
11+
{
12+
public static void main(String[] args)
13+
{
14+
int n = 100;
15+
double sum = 0;
16+
for (int i = n; i > 0; sum += 1.0/i--);
17+
System.out.println("The sum is " + sum);
18+
}
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package chapter_04;
2+
3+
/**
4+
* 8. Write a loop that will count the number of blank characters in a given
5+
* string.
6+
*
7+
*
8+
* @author Sharaf Qeshta
9+
* */
10+
public class Exercise_04_08
11+
{
12+
public static void main(String[] args)
13+
{
14+
String word = " Sharaf Qeshta ";
15+
int blanks = 0;
16+
for (int i = 0; i < word.length(); i++)
17+
blanks += ((word.charAt(i) + "").isBlank())? 1 : 0;
18+
19+
System.out.println("The number of blanks in " + word + " is " + blanks);
20+
}
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package chapter_04;
2+
3+
/**
4+
* 9. Write a loop that will create a new string that is the reverse of a given
5+
* string.
6+
*
7+
*
8+
* @author Sharaf Qeshta
9+
* */
10+
public class Exercise_04_09
11+
{
12+
public static void main(String[] args)
13+
{
14+
String word = "Sharaf Qeshta";
15+
String reverse = "";
16+
17+
for (int i = word.length()-1; i > -1; i--)
18+
reverse += word.charAt(i);
19+
20+
System.out.println(reverse); // athseQ farahS
21+
}
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package chapter_04;
2+
3+
import java.util.Scanner;
4+
5+
/**
6+
* 10. Write a program that will compute statistics for eight coin tosses. The user
7+
* will enter either an h for heads or a t for tails for the eight tosses. The program
8+
* will then display the total number and percentages of heads and tails. Use
9+
* the increment operator to count each h and t that is entered. For example, a
10+
* possible sample dialogue between the program and the user might be
11+
* For each coin toss enter either h for heads or t for tails.
12+
* First toss: h
13+
* Second toss: t
14+
* Third toss: t
15+
* Fourth toss: h
16+
* Fifth toss: t
17+
* Sixth toss: h
18+
* Seventh toss: t
19+
* Eighth toss: t
20+
* Number of heads: 3
21+
* Number of tails: 5
22+
* Percent heads: 37.5
23+
* Percent tails: 62.5
24+
*
25+
*
26+
* @author Sharaf Qeshta
27+
* */
28+
public class Exercise_04_10
29+
{
30+
public static void main(String[] args)
31+
{
32+
Scanner scanner = new Scanner(System.in);
33+
System.out.println("For each coin toss enter either h for heads or t for tails.");
34+
int temp = 0, heads = 0, tails = 0;
35+
36+
System.out.print("First toss: ");
37+
temp = (scanner.next().charAt(0) == 'h')? heads++ : tails++;
38+
39+
System.out.print("Second toss: ");
40+
temp = (scanner.next().charAt(0) == 'h')? heads++ : tails++;
41+
42+
System.out.print("Third toss: ");
43+
temp = (scanner.next().charAt(0) == 'h')? heads++ : tails++;
44+
45+
System.out.print("Fourth toss: ");
46+
temp = (scanner.next().charAt(0) == 'h')? heads++ : tails++;
47+
48+
System.out.print("Fifth toss: ");
49+
temp = (scanner.next().charAt(0) == 'h')? heads++ : tails++;
50+
51+
System.out.print("Sixth toss: ");
52+
temp = (scanner.next().charAt(0) == 'h')? heads++ : tails++;
53+
54+
System.out.print("Seventh toss: ");
55+
temp = (scanner.next().charAt(0) == 'h')? heads++ : tails++;
56+
57+
System.out.print("Eighth toss: ");
58+
temp = (scanner.next().charAt(0) == 'h')? heads++ : tails++;
59+
60+
System.out.println("Number of heads: " + heads);
61+
System.out.println("Number of tails: " + tails);
62+
System.out.println("Percent heads: " + (heads/8.0) * 100);
63+
System.out.println("Percent tails: " + (tails/8.0) * 100);
64+
}
65+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package chapter_04;
2+
3+
/**
4+
* 11. Suppose we attend a party. To be sociable, we will shake hands with
5+
* everyone else. Write a fragment of code using a for statement that will
6+
* compute the total number of handshakes that occur. (Hint: Upon
7+
* arrival, each person shakes hands with everyone who is already there.
8+
* Use the loop to find the total number of handshakes as each person
9+
* arrives.)
10+
*
11+
*
12+
* @author Sharaf Qeshta
13+
* */
14+
public class Exercise_04_11
15+
{
16+
public static void main(String[] args)
17+
{
18+
int handShakes = 0;
19+
int numberOfPeople = 5;
20+
for (int i = 1; i <= numberOfPeople; i++)
21+
// everytime we simulating that a new person came
22+
// everyone came will shake hand with everyone in the party
23+
handShakes += (i-1);
24+
25+
// there`s a simpler algorithm to calculate the handshakes
26+
// which is (numberOfPeople * (numberOfPeople - 1)) / 2
27+
28+
System.out.println("If there`s " + numberOfPeople + " people then there`s " + handShakes + " handshakes");
29+
}
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package chapter_04;
2+
3+
import java.util.Scanner;
4+
5+
/**
6+
* 13. Write a fragment of code that computes the final score of a bowling game.
7+
* Use a loop to read the number of pins toppled by three players during five
8+
* turns each. Display the final score afterward.
9+
*
10+
*
11+
* @author Sharaf Qeshta
12+
* */
13+
public class Exercise_04_13
14+
{
15+
public static void main(String[] args)
16+
{
17+
int player1Scores = 0, player2Scores = 0, player3Scores = 0;
18+
Scanner scanner = new Scanner(System.in);
19+
20+
for (int i = 1; i < 4; i++)
21+
{
22+
int playerScores = 0;
23+
for (int j = 1; j < 5; j++)
24+
{
25+
System.out.println("Enter the number of pins toppled by player "
26+
+ i + " at round " + j + ": ");
27+
playerScores += scanner.nextInt();
28+
}
29+
30+
if (i == 1)
31+
player1Scores = playerScores;
32+
else if (i == 2)
33+
player2Scores = playerScores;
34+
else
35+
player3Scores = playerScores;
36+
}
37+
38+
System.out.println("Player 1 scores " + player1Scores);
39+
System.out.println("Player 2 scores " + player2Scores);
40+
System.out.println("Player 3 scores " + player3Scores);
41+
}
42+
}

0 commit comments

Comments
 (0)