Exercises:
Exercises:
1. Write a fragment of code that will read words from the keyboard until the word done is entered. For each word except done, report whether its first character is equal to its last character. For the required loop, use a
a. while statement
b. do-while statement
|Solution: |
| |
|Scanner reader = new Scanner(System.in); |
|String word = ""; |
| |
|System.out.println("Please enter words ending with done."); |
| |
|word = reader.next(); |
|while(!word.equals("done")){ |
|if(word.charAt(0) == word.charAt(word.length()-1)){ |
|System.out.println("The word " + word |
|+ " has first and last characters that are the same."); |
|} |
|word = reader.next(); |
|} |
| |
| |
|System.out.println("Please enter words ending with done."); |
| |
|boolean finished = false; |
|do{ |
|word = reader.next(); |
|if(word.equals("done")) |
|finished = true; |
|else |
| |
|if(word.charAt(0) == word.charAt(word.length()-1)){ |
|System.out.println("The word " + word |
|+ " has first and last characters that are the same."); |
|} |
|} while (!finished); |
| |
|This code is in Fragments.java. |
2. Develop an algorithm for computing the month-by-month balance in your savings account. You can make one transaction—a deposit or a withdrawal—each month. Interest is added to the account at the beginning of each month. The monthly interest rate is the yearly percentage rate divided by 12.
|Solution: |
| |
|For month goes from 1 to 12 |
|Compute the monthly interest |
|Compute the interest and add to the balance |
|Ask if the user is making a deposit or withdrawal. |
|Get the amount from the user |
|If making a deposit |
|Add the amount to the balance |
|Else |
|Subtract the amount from the balance |
|Display the current balance |
3. Develop an algorithm for a simple game of guessing at a secret five-digit code. When the user enters a guess at the code, the program returns two values: the number of digits in the guess that are in the correct position and the sum of those digits. For example, if the secret code is 53840, and the user guesses 83241, the digits 3 and 4 are in the correct position. Thus, the program should respond with 2 and 7.
Allow the user to guess a fixed number of times.
|Solution: |
| |
|Generate a secret code |
|For guess is 1 to max |
|Get the users guess for the 5 digit code |
|Let correct be zero |
|Let sum be zero |
|For each digit |
|If the digit in the guess matches the users guess |
|Add 1 to correct |
|Add the value of the digit to sum |
|If correct is 5 |
|Congratulate the user on guessing the code |
|Exit the program |
|Else |
|Display correct and sum |
|Ask if the user is making a deposit or withdrawal. |
|Display a message telling the user they did not guess the code within the maximum number of allowed guesses. |
4. Write a fragment of code that will compute the sum of the first n positive odd integers. For example, if n is 5, you should compute 1 + 3 + 5 + 7 + 9.
|Solution: |
| |
|int sum = 0; |
|int odd = 1; |
|for(int i=0; i 0; j--)
{
t = t * (j - i);
}
s = s * t;
System.out.println(“T is “ + t);
}
System.out.println(“S is “ + s);
|Solution: |
| |
|int s = 0; |
|int t = 1; |
|int i = 0; |
|while(i0){ |
|t = t * (j - i); |
|j--; |
|} |
|s = s * t; |
|System.out.println("T is " + t); |
|i++; |
|} |
|System.out.println("S is " + s); |
| |
|This code is in Fragments.java. |
6. Write a for statement to compute the sum 1 + 22 + 32 + 42 + 52 + ... + n2.
|Solution: |
| |
|sum = 0; |
|for( i=1; i ................
................
In order to avoid copyright disputes, this page is only a partial summary.
To fulfill the demand for quickly locating and searching documents.
It is intelligent file search solution for home and business.
Related searches
- writing exercises for 5th graders
- printable english exercises for beginners
- writing exercises for 4th graders
- writing exercises for 7th graders
- writing exercises for 7th grade
- writing exercises for 6th graders
- reading comprehension exercises for adults
- english exercises for beginners pdf
- creative writing exercises for kids
- english grammar exercises free printables
- english grammar exercises free online
- vocabulary exercises for adults