HW2 CH 3 and CH 4
HW2 CH 3 and CH 4
Chapter 3 Problems
1. Problem 3.19 from the textbook.
Write a program that will read in three integer values from the console that represent the
sides of a triangle. Display whether the sides form a legal triangle. For a triangle to be
legal, the sum of each pair of sides must be greater than the third side. If your triangle is
“legal”, display the perimeter. If the triangle is “illegal” display a message to the user that
the sides to not form a valid triangle. (for a triangle to be legal s1+ s2 > s3 and s2 + s3 >
s1 and s1 + s3 > s2). Please use if and/or if-else statements in your solution.
3. Problem 3.17 from the textbook, this time with a Switch statement. Rock, Paper,
Scissors. Write a program that plays the popular scissor-rock-paper game. (A scissor can
cut paper, a rock can crush scissor, and a paper can cover rock). The program randomly
generates a number 0, 1, or 2 representing the scissor, rock, and paper. The program
prompts the user to enter a number 0, 1, or 2 and displays a message indicating whether
the user or the computer wins, loses, or ties. See the sample output, as provided in your
textbook. (See section 3.7 for instructions on how to generate a random number.)
Instead of the if…else version in Problem #2, use a Switch statement. You must use a
switch statement to receive credit. No pseudo-code submission is required.
Page 1 of 5
You can have if…else embedded in switch, for example
switch (n){
case 1: if (x == 0){
System.out.println(“x is 0 and n is 1);
}
else{
System.out.println(“x is non-zero and n is 1);
}
break;
case 2: ….. etc.
4. 3.12 with modification for 4-digit input. Read a 4-digit number and determine if the value
entered is a palindrome.
Hint: you will need to add code to isolate the 1000’s, 100’s, 10’s and 1’s digits. You will
need a variable for each place and then compare variables to see if 1000’ equals 1’s and
100’s equals 10’s.
Sample run:
Please enter a 4-digit number: 1221
1221 is a palindrome.
Chapter 4 Problems
5. Write a Java program that will read three integers from the user and display the numbers
in ascending order. Your input and output should be similar to the following.
Page 2 of 5
The international standard letter/number mapping found on the telephone is shown
below:
Write a program that prompts the user to enter a lowercase or uppercase letter and display
its corresponding number. For a non-letter input, display “invalid input”.
(Hint: if the user does not enter ‘A’ –‘Z’ or ‘a’-‘z’, then the input is invalid.)
Enter a letter: r
The corresponding number is 7
Enter a letter: %
Page 3 of 5
% is invalid input
If you need a hint on generating a range of numbers, please refer to RandomNumbers.java that we did
in class:
// Generating a number from any range...
//Let's generate a random number between
// min and max, inclusively
// How about a number between 20 and 100, inclusively.
int max = 100; //biggest value to generate
int min = 20; //smallest value to generate
int range = max - min + 1; //number of values in range
Will display the character represented by rand in the ASCII table. You can generate a random
number in the appropriate range and assign to a variable of type char by casting rand to a char.
8. Write a program the reads a string from the user that represents dollars and cents. The
user will enter the dollar-sign and amount, $XXX.XX. The user will enter the $ sign and
decimal point as part of the string. The output should be how many dollars and how many
cents are represented by the amount entered.
For example:
Please enter an amount: $35.46
The output will be
There are 35 dollars and 46 cents.
Page 4 of 5
Hint: You will want to use the indexOf and substring methods to find, k, the index of the
decimal point in the string.. Next use substring (1,k) and (k+1) to get the dollars portion and
the cents portion.
Page 5 of 5