Reference no: EM132388422
Methods
Exercise #1: Design and implement a program (name it SumDigits) to compute the sum of all digits in an integer number. The program defines the following method:
Method sumDigits (int number) returns the sum of all digits in number.
In the main method, prompt the user for an integer input and pass the input to method sumDigits() to obtain the sum of its digits. Then display the entered number followed by the sum of its digits as shown below. Document your code and properly label the input prompt and the outputs as shown below.
Sample run 1:
You entered: 12345
Sum of digits: 15
Sample run 2:
You entered: 78501
Sum of digits: 21
Sample run 3:
You entered: 9000
Sum of digits: 9
Exercise #2: Design and implement a program (name it PrintCharacters) to print the characters between a start character and an end character specified by the user. The start character must come before the end character. For example, we can display all characters between character F and character W, but not the other way around. The program defines the following method:
Method printChars (char ch1, char ch2) prints all characters between ch1 and ch2, inclusive.
In the main method, prompt the user to enter the start character and end characters, validate the entered characters are in order, then call method printChars(). Display the character 5 per line. Document your code and properly label the input prompts and the outputs as shown below.
Sample run 1:
Start character: A
End character: L
Output:
A B C D E
F G H I J
K L
Sample run 2:
Start character: W
End character: c
Output:
W X Y Z [
\ ] ^ _ ‘
a b c
Sample run 3
Start character: B
End character: 5
Output: start and end characters are out of order. Try again.
Exercise #3 Guess the Number
Write a "Guess the Number" game that randomly assigns a secret number [1,20] for the user to guess. I recommend hard coding your secret number at first to make testing your code easier. You can write this a number of ways, but try to stick to the instructions outlined here.
Write three methods to make your game work.
• The first method should generate a random number and return the value to main to store in the secret number variable.
• The second method should ask your user what his first guess is. Return this information to main.
• The third method should take in the guess and the secret number via parameters, then evaluate whether or not the quess is correct. Return a string to let the user know if their guess was too high, too low, or correct.
You will need a nested loop in main. You'll want the outer loop to ask if the user wants to play again. You'll also need to think about how to include an inner loop that continues to call the third method until the user guesses correctly.
Attachment:- Programming and Problem Solving.rar