0% found this document useful (0 votes)
244 views5 pages

HW2 CH 3 and CH 4

This document provides 8 programming problems related to chapters 3 and 4 of a textbook. The problems cover topics like: - Validating if triangle sides form a legal triangle - Rock paper scissors game using if/else and switch statements - Checking if a number is a palindrome - Ordering integers in ascending order - Mapping letters to phone numbers - Generating vehicle license plates with random letters and numbers - Parsing a dollar amount string into dollars and cents portions The problems are to be completed and submitted by September 1st, 2021 for a total of 30 points.

Uploaded by

Nischal Lg
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
244 views5 pages

HW2 CH 3 and CH 4

This document provides 8 programming problems related to chapters 3 and 4 of a textbook. The problems cover topics like: - Validating if triangle sides form a legal triangle - Rock paper scissors game using if/else and switch statements - Checking if a number is a palindrome - Ordering integers in ascending order - Mapping letters to phone numbers - Generating vehicle license plates with random letters and numbers - Parsing a dollar amount string into dollars and cents portions The problems are to be completed and submitted by September 1st, 2021 for a total of 30 points.

Uploaded by

Nischal Lg
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

CS 4150 Chapter 3 & 4 Due Date: 9/1/2021, 11:59 pm Total points: 30 points

(3.75 points each problem)

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.

Please enter 3 sides of a triangle: 1 2 3

The sides 1 2 3 make an illegal triangle.

Please enter the 3 sides of a triangle: 3 4 5


The perimeter is 12

2. Problem 3.17 from the textbook, with modification.


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.) Use if…else statements for this 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.

Examples of palindromes include: 1221 3443

Examples that are not a palindrome 1231 3453

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.

Please enter 3 integers: 55 33 44

The numbers in order are: 33 44 55

6. Problem 4.15 from the textbook.

Page 2 of 5
The international standard letter/number mapping found on the telephone is shown
below:

image from https://www.dcode.fr/phone-keypad-cipher

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.)

To read a single character -


String str = input.nextLine();
char firstCharacter = str.charAt(0);

This is a great problem to practice the switch-statement.


Sample from three different runs:
Enter a letter: T
The corresponding number is 8

Enter a letter: r
The corresponding number is 7

Enter a letter: %

Page 3 of 5
% is invalid input

7. Problem 4.25 from the textbook.


(Generate vehicle plate numbers) Assume that a vehicle plate number consists of three
uppercase letters followed by four digits. Write a program to generate a plate number.
Hint: to generate a random capital letter – generate a number between 65 and 90, inclusively. To
generate the code for a number, generate a number between 48 and 57, inclusively. You will need to
convert the random number to a displayable ASCII character. See Section 4.3.1 and 4.3.3 in the text
for details on how to covert between the code for a character and the displayed character.
Examples of license plate: ABC1234 ZBG5387

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

int rand = (int)(Math.random() * range) + min;


System.out.println("rand is " + rand);

Also, recall that


int rand = (int)(Math.random() * range) + min; //min is code for A and range is 26
char c = (char) rand;
System.out.println(c);

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

You might also like