• Home
  • Textbooks
  • Introduction to Java Programming
  • Selections

Introduction to Java Programming

Y. Daniel Liang

Chapter 3

Selections - all with Video Answers

Educators


Chapter Questions

07:01

Problem 1

(Algebra: solve quadratic equations) The two roots of a quadratic equation $a x^{2}+b x+c=0$ can be obtained using the following formula:
\[r_{1}=\frac{-b+\sqrt{b^{2}-4 a c}}{2 a} \text { and } r_{2}=\frac{-b-\sqrt{b^{2}-4 a c}}{2 a}\]
$b^{2}-4 a c$ is called the discriminant of the quadratic equation. If it is positive, the equation has two real roots. If it is zero, the equation has one root. If it is negative, the equation has no real roots.
Write a program that prompts the user to enter values for $a, b,$ and $c$ and displays the result based on the discriminant. If the discriminant is positive, display two roots. If the discriminant is $0,$ display one root. Otherwise, display "The equation has no real roots".
Note that you can use Math.pow(x, 0.5) to compute $\sqrt{x}$. Here are some sample runs.

Harriet O'Brien
Harriet O'Brien
Numerade Educator
06:42

Problem 2

(Game: add three numbers) The program in Listing 3.1 generates two integers and prompts the user to enter the sum of these two integers. Revise the program to generate three single-digit integers and prompt the user to enter the sum of these three integers.

Harriet O'Brien
Harriet O'Brien
Numerade Educator
08:31

Problem 3

(Algebra: solve $2 \times 2$ linear equations) You can use Cramer's rule to solve the following $2 \times 2$ system of linear equation:
\[\begin{array}{l}a x+b y=e \\c x+d y=f
\end{array} \quad x=\frac{e d-b f}{a d-b c} \quad y=\frac{a f-e c}{a d-b c}\]
Write a program that prompts the user to enter $a, b, c, d, e,$ and $f$ and displays the result. If $a d-b c$ is $0,$ report that "The equation has no solution".
Enter $a, b, c, d, e, f: 9.04 .03 .0-5.0-6.0-21.0$
$x$ is -2.0 and $y$ is 3.0
Enter $a, b, c, d, e, f: 1.0 \quad 2.02 .04 .04 .05 .0$
The equation has no solution

Harriet O'Brien
Harriet O'Brien
Numerade Educator
06:18

Problem 4

(Game: learn addition) Write a program that generates two integers under 100 and prompts the user to enter the sum of these two integers. The program then reports true if the answer is correct, false otherwise. The program is similar to Listing 3.1.

Harriet O'Brien
Harriet O'Brien
Numerade Educator
06:54

Problem 5

(Find future dates) Write a program that prompts the user to enter an integer for today's day of the week (Sunday is 0, Monday is 1, ...., and Saturday is 6). Also prompt the user to enter the number of days after today for a future day and display the future day of the week. Here is a sample run:
Enter today's day: 1
Enter the number of days elapsed since today: 3
Today is Monday and the future day is Thursday
Enter today"s day: 0
Enter the number of days elapsed since today: 31
Today is Sunday and the future day is Wednesday

Harriet O'Brien
Harriet O'Brien
Numerade Educator
09:39

Problem 6

(Health application: $B M I$ ) Revise Listing $3.5,$ ComputeAndInterpretBMI.java, to let the user enter weight, feet, and inches. For example, if a person is 5 feet and 10 inches, you will enter 5 for feet and 10 for inches. Here is a sample run:
Enter weight in pounds: 140
Enter feet: 5
Enter inches: 10
BMI is 20.087702275404553
Normal

Harriet O'Brien
Harriet O'Brien
Numerade Educator
11:00

Problem 7

(Financial application: monetary units) Modify Listing $2.10,$ ComputeChange.java, to display the nonzero denominations only, using singular words for single units such as 1 dollar and 1 penny, and plural words for more than one unit such as 2 dollars and 3 pennies.

Harriet O'Brien
Harriet O'Brien
Numerade Educator
04:49

Problem 8

(Sort three integers) Write a program that sorts three integers. The integers are cntered from the input dialogs and stored in variables num1, num2, and num3, respectively. The program sorts the numbers so that $n u m 1 \leq m m 2 \leq$ num3.

Harriet O'Brien
Harriet O'Brien
Numerade Educator
07:07

Problem 9

(Business: check ISBN-IO) An ISBN-10 (International Standard Book Number) consists of 10 digits: $d_{1} d_{2} d_{3} d_{4} d_{5} d_{6} d_{7} d_{8} d_{9} d_{10} .$ The last digit, $d_{10}$ is a checksum, which is calculated from the other nine digits using the following formula:
\[\begin{array}{c}\left(d_{1} \times 1+d_{2} \times 2+d_{3} \times 3+d_{4} \times 4+d_{5} \times 5+\right. \\
\left.d_{6} \times 6+d_{7} \times 7+d_{8} \times 8+d_{9} \times 9\right) \% 11\end{array}\]If the checksum is $10,$ the last digit is denoted as $X$ according to the ISBN-10 convention. Write a program that prompts the user to enter the first 9 digits and displays the 10 -digit ISBN (including leading zeros). Your program should read the input as an integer. Here are sample runs:
Enter the first 9 digits of an ISBN as integer: 013601267 The ISBN-10 number is 0136012671
Enter the first 9 digits of an ISBN as integer: 013031997 The ISBN-10 number is $013031997 x$

Brian Ketelobeter
Brian Ketelobeter
Numerade Educator
05:12

Problem 10

(Game: addition quiz) Listing $3.4,$ SubtractionQuiz, java, randomly generates a subtraction question. Revise the program to randomly generate an addition question with two integers less than 100 .

Harriet O'Brien
Harriet O'Brien
Numerade Educator
01:51

Problem 11

(Find the number of days in a month) Write a program that prompts the user to enter the month and year and displays the number of days in the month. For example, if the user entered month 2 and year 2012 , the program should display that February 2012 had 29 days. If the user entered month 3 and year $2015,$ the program should display that March 2015 had 31 days.

Morgan Cheatham
Morgan Cheatham
Numerade Educator
08:23

Problem 12

(Check a number) Write a program that prompts the user to enter an integer and checks whether the number is divisible by both 5 and $6,$ or neither of them, or just one of them. Here are some sample runs for inputs $10,30,$ and 23
10 is divisible by 5 or $6,$ but not both
30 is divisible by both 5 and 6
23 is not divisible by either 5 or 6

Willis James
Willis James
Numerade Educator
02:34

Problem 13

(Financial application: compute taxes) Listing $3.6,$ ComputeTax.java, gives the source code to compute taxes for single filers, Complete Listing 3.6 to give the complete source code.

Morgan Cheatham
Morgan Cheatham
Numerade Educator
09:13

Problem 14

(Game: heads or tails) Write a program that lets the user guess whether the flip of a coin results in heads or tails. The program randomly generates an integer 0 or 1 which represents head or tail. The program prompts the user to enter a guess and reports whether the guess is correct or incorrect.

Harriet O'Brien
Harriet O'Brien
Numerade Educator
04:47

Problem 15

(Game: lottery) Revise Listing $3.9,$ Lottery.java, to generate a lottery of a threedigit number. The program prompts the user to enter a three-digit number and determines whether the user wins according to the following rules:
1. If the user input matches the lottery number in the exact order, the award is $\$ 10,000$
2. If all the digits in the user input match all the digits in the lottery number, the award is $\$ 3,000$
3. If one digit in the user input matches a digit in the lottery number, the award is $\$ 1,000$

Morgan Cheatham
Morgan Cheatham
Numerade Educator
02:21

Problem 16

(Random character) Write a program that displays a random uppercase letter using the Math. random() method.

Harriet O'Brien
Harriet O'Brien
Numerade Educator
08:17

Problem 17

(Game: scissor, rock, paper) Write a program that plays the popular scissor-rockpaper game. (A scissor can cut a paper, a rock can knock a scissor, and a paper can wrap a rock.) The program randomly generates a number $0,1,$ or 2 representing 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 draws. Here are sample runs:
scissor $(0),$ rock $(1),$ paper (2): 1
The computer is scissor. You are rock. You won
scissor $(0),$ rock $(1),$ paper (2): 2
The computer is paper. You are paper too. It is a draw

Harriet O'Brien
Harriet O'Brien
Numerade Educator
06:37

Problem 18

(Use the input dialog box) Rewrite Listing $3.8,$ Leap Year.java, using the input dialog box.

Harriet O'Brien
Harriet O'Brien
Numerade Educator
01:27

Problem 19

(Compute the perimeter of a triangle) Write a program that reads three edges for a triangle and computes the perimeter if the input is valid. Otherwise, display that the input is invalid. The input is valid if the sum of every pair of two edges is greater than the remaining edge.

Morgan Cheatham
Morgan Cheatham
Numerade Educator
02:40

Problem 20

(Science: wind-chill temperature) Programming Exercise 2.17 gives a formula to compute the wind-chill temperature. The formula is valid for temperatures in the range between $-58^{\circ} \mathrm{F}$ and $41^{\circ} \mathrm{F}$ and wind speed greater than or equal to 2 Write a program that prompts the user to enter a temperature and a wind speed. The program displays the wind-chill temperature if the input is valid; otherwise, it displays a message indicating whether the temperature and/or wind speed is invalid.

Morgan Cheatham
Morgan Cheatham
Numerade Educator
02:17

Problem 21

(Science: day of the week) Zeller's congruence is an algorithm developed by Christian Zeller to calculate the day of the week. The formula is
\[h=\left(q+\frac{26(m+1)}{10}+k+\frac{k}{4}+\frac{j}{4}+5 j\right) \% 7\]
where
? h is the day of the week (0: Saturday, 1: Sunday, 2: Monday, 3: Tuesday, 4:
Wednesday, 5: Thursday, 6: Friday).
? q is the day of the month.
? m is the month (3: March, 4: April, . . ., 12: December). January and February
are counted as months 13 and 14 of the previous year.
? j is the century (i.e., ).
? k is the year of the century (i.e., year % 100).
Note that the division in the formula performs an integer division. Write a program that prompts the user to enter a year, month, and day of the month, and
displays the name of the day of the week. Here are some sample runs:
Enter year: $(e . g ., 2012): 2015$
Enter month: $1-12: 1$
Enter the day of the month: $1-31: 25$
Day of the week is Sunday
Enter year: $(e .9 ., 2012): 2012$
Enter month: $1-12: 5$
Enter the day of the month: $1-31: 12$
Day of the week is Saturday
(Hint: January and February are counted as 13 and 14 in the formula, so you need to convert the user input 1 to 13 and 2 to 14 for the month and change the year to the previous year.)

Morgan Cheatham
Morgan Cheatham
Numerade Educator
01:43

Problem 22

(Geometry: point in a circle?) Write a program that prompts the user to enter a point $(x, y)$ and checks whether the point is within the circle centered at (0,0) with radius $10 .$ For example, (4,5) is inside the circle and (9,9) is outside the circle, as shown in Figure 3.9 a.
(Hint: A point is in the circle if its distance to (0,0) is less than or equal to $10 .$ The formula for computing the distance is $\sqrt{\left(x_{2}-x_{1}\right)^{2}+\left(y_{2}-y_{1}\right)^{2}} .$ Test your program to cover all cases.) Two sample runs are shown below.
Enter a point with two coordinates: 45 Point (4.0,5.0) is in the circle
Enter a point with two coordinates: 99 Point (9.0,9.0) is not in the circle

Morgan Cheatham
Morgan Cheatham
Numerade Educator
03:27

Problem 23

(Geometry: point in a rectangle?) Write a program that prompts the user to enter a point $(x, y)$ and checks whether the point is within the rectangle centered at $(0,$
0) with width 10 and height $5 .$ For example, (2,2) is inside the rectangle and $(6,$
4) is outside the rectangle, as shown in Figure 3.9 b. (Hint: A point is in the rectangle if its horizontal distance to (0,0) is less than or equal to $10 / 2$ and its vertical distance to (0,0) is less than or equal to $5.0 / 2 .$ Test your program to cover all cases.) Here are two sample runs.
Enter a point with two coordinates: 22 Point (2.0,2.0) is in the rectangle
Enter a point with two coordinates: 64 Point (6.0,4.0) is not in the rectangle

Akash M
Akash M
Numerade Educator
01:12

Problem 24

(Game: pick a card) Write a program that simulates picking a card from a deck of 52 cards. Your program should display the rank (Ace, $2,3,4,5,6,7,8,9,10,$ Jack, Queen, King) and suit (CTubs, Diamonds, Hearts, Spades) of the card Here is a sample run of the program:
The card you picked is Jack of Hearts

Morgan Cheatham
Morgan Cheatham
Numerade Educator
03:41

Problem 25

(Geometry: intersecting point) Two points on line 1 are given as $(x 1, y 1)$ and $(x 2,$
$y 2)$ and on line 2 as $(x 3, y 3)$ and $(x 4, y 4),$ as shown in Figure $3.10 a-b$
The intersecting point of the two lines can be found by solving the following linear equation:
\[\begin{array}{l}\left(y_{1}-y_{2}\right) x-\left(x_{1}-x_{2}\right) y=\left(y_{1}-y_{2}\right) x_{1}-\left(x_{1}-x_{2}\right) y_{1} \\\left(y_{3}-y_{4}\right) x-\left(x_{3}-x_{4}\right) y=\left(y_{3}-y_{4}\right) x_{3}-\left(x_{3}-x_{4}\right) y_{3}\end{array}\]
This linear equation can be solved using Cramer's rule (see Exercise 3.3 ). If the equation has no solutions, the two lines are parallel (Figure $3.10 \mathrm{c}$ ). Write a program that prompts the user to enter four points and displays the intersecting point. Here are sample runs:
Enter $x 1, \quad y 1, \quad x 2, y 2, x 3, y 3, x 4, y 4: 225-1.04 .0 \quad 2.0-1.0-2.0$
The intersecting point is at (2.88889,1.1111)
Enter $x 1, y 1, x 2, y 2, x 3, y 3, x 4, y 4: 2276.04 .02 .0-1.0-2.0$
The two 1 ines are paralle?

Morgan Cheatham
Morgan Cheatham
Numerade Educator
08:23

Problem 26

(Use the 88 , || and \wedge operators) Write a program that prompts the user to enter an integer and determines whether it is divisible by 5 and $6,$ whether it is divisible by 5 or $6,$ and whether it is divisible by 5 or $6,$ but not both. Here is a sample run of this program:
Enter an integer: 10
Is 10 divisible by 5 and $6 ?$ false Is 10 divisible by 5 or $6 ?$ true Is 10 divisible by 5 or $6,$ but not both? true

Willis James
Willis James
Numerade Educator
02:53

Problem 27

(Geometry: points in triangle?) Suppose a right triangle is placed in a plane as shown below. The right-angle point is placed at $(0,0),$ and the other two points are placed at $(200,0),$ and $(0,100) .$ Write a program that prompts the user to enter a point with $x-$ and $y$ -coordinates and determines whether the point is inside the triangle. Here are the sample runs:
Enter a point's $x-$ and $y-$ coordinates: 100.525 .5 The point is in the triangle
Enter a point's $x-$ and $y-$ coordinates: 100.550 .5 The point is not in the triangle

Morgan Cheatham
Morgan Cheatham
Numerade Educator
03:59

Problem 28

(Geometry: two rectangles) Write a program that prompts the user to enter the center $x-, y$ -coordinates, width, and height of two rectangles and determines whether the second rectangle is inside the first or overlaps with the first, as shown in Figure $3.11 .$ Test your program to cover all cases.
Here are the sample runs:
Enter r1's center $x-, y$ -coordinates, width, and height: 2.542 .543 Enter $r 2^{\prime}$ s center $x-, y$ -coordinates, width, and height: 1.550 .53 $r^{2}$ is inside $r 1$
Enter r1's center $x-, y$ -coordinates, width, and height: 1235.5 Enter $r 2^{\prime}$ s center $x-, y$ -coordinates, width, and height: 344.55
$\mathrm{r} 2$ over 7 aps $\mathrm{r} 1$
Enter r1's center $x-, y$ -coordinates, width, and height: 1233 Enter $r 2^{\prime}$ s center $x-, y-$ coordinates, width, and height: 404532
r2 does not over'lap r1

Morgan Cheatham
Morgan Cheatham
Numerade Educator
03:03

Problem 29

(Geometry: two circles) Write a program that prompts the user to enter the center coordinates and radii of two circles and determines whether the second circle is inside the first or overlaps with the first, as shown in Figure $3.12 .$ (Hint; circle 2 is inside circle 1 if the distance between the two centers $<=|r 1-r 2|$ and circle 2 overlaps circle 1 if the distance between the two centers $<=r 1+r 2 .$ Test your program to cover all cases.)
Here are the sample runs:
Enter circle1's center $x-, y$ -coordinates, and radius: 0.55 .113 Enter circle2's center $x-, y-$ coordinates, and radius: 11.74 .5 circle2 is inside circle1
Enter circlel's center $\mathrm{x}$ -, y-coordinates, and radius: 3.45 .75 .5
Enter circle2's center $x-, y$ -coordinates, and radius: 6.73 .53 circle 2 over laps circlel
Enter circlel's center $x-, y-$ coordinates, and radius: 3.45 .51
Enter circle2's center $x-, y$ -coordinates, and radius: 5.57 .21 circle2 does not overlap circlel

Morgan Cheatham
Morgan Cheatham
Numerade Educator
01:21

Problem 30

(Current time) Revise Programming Exercise 2.8 to display the hour using a 12 hour clock. Here is a sample run:
Enter the time zone offset to GMT: -5 The current time is 4: 50: 34 AM

Morgan Cheatham
Morgan Cheatham
Numerade Educator
03:05

Problem 31

(Financials: currency exchange) Write a program that prompts the user to enter the exchange rate from currency in U.S. dollars to Chinese RMB. Prompt the user to enter 0 to convert from U.S. dollars to Chinese $\mathrm{RMB}$ and 1 to convert from Chinese $\mathrm{RMB}$ and U.S. dollars. Prompt the user to enter the amount in U.S. dollars or Chinese RMB to convert it to Chinese RMB or U.S. dollars, respectively. Here are the sample runs:
Enter the exchange rate from dollars to RMB: 6.81
Enter 0 to convert dollars to $\mathrm{RMB}$ and 1 vice versa: 0
Enter the dollar amount: 100
$\$ 100.0$ is 681.0 yuan
Enter the exchange rate from dollars to RMB: 6.81
Enter 0 to convert dollars to RMB and 1 vice versa: 1
Enter the RMB amount: 10000
10000.0 yuan is $\$ 1468.43$
Enter the exchange rate from dollars to RMB: 6.81
Enter 0 to convert dollars to $\mathrm{RMB}$ and 1 vice versa: 5
Incorrect input

Morgan Cheatham
Morgan Cheatham
Numerade Educator
02:43

Problem 32

(Geometry: point position) Given a directed line from point $\mathrm{p} 0(\mathrm{x} 0, \mathrm{y} 0)$ to $\mathrm{pl}(\mathrm{x} 1,$ y $1),$ you can use the following condition to decide whether a point $\mathrm{p} 2(\mathrm{x} 2, \mathrm{y} 2)$ is on the left of the line, on the right, or on the same line (see Figure 3.13 ):
$(x 1-x 0)^{*}(y 2-y 0)-(x 2-x 0)^{*}(y 1-y 0)\left\{\begin{array}{l}>0 p 2 \text { is on the left side of the line } \\ =0 p 2 \text { is on the same line } \\ <0 p 2 \text { is on the right side of the line }\end{array}\right.$
Write a program that prompts the user to enter the three points for $\mathrm{p} 0, \mathrm{p} 1,$ and $\mathrm{p} 2$ and displays whether $\mathrm{p} 2$ is on the left of the line from $\mathrm{p} 0$ to $\mathrm{p} 1$, to the right, or on the same line. Here are some sample runs:
Enter three points for $p 0, p 1,$ and $p 2: 4.426 .59 .5-54$ $p^{2}$ is on the left side of the line
Enter three points for $p 0, p 1,$ and $p 2: 115522$ $p 2$ is on the same line
Enter three points for $p 0, p 1,$ and $p 2: 3.426 .59 .552 .5$ $p 2$ is on the right side of the line

Morgan Cheatham
Morgan Cheatham
Numerade Educator
02:05

Problem 33

(Financial: compare costs) Suppose you shop for rice in two different packages. You would like to write a program to compare the cost. The program prompts the user to enter the weight and price of the each package and displays the one with the better price. Here is a sample run:
Enter weight and price for package $1: 50 \quad 24.59$ Enter weight and price for package 2: 2511.99 Package 1 has a better price.

Morgan Cheatham
Morgan Cheatham
Numerade Educator
02:48

Problem 34

(Geometry: point on line segment) Exercise 3.32 shows how to test whether a point is on an unbounded line. Revise Exercise 3.32 to test whether a point is on a line segment. Write a program that prompts the user to enter the three points for $\mathrm{p} 0, \mathrm{p} 1,$ and $\mathrm{p} 2$ and displays whether $\mathrm{p} 2$ is on the line segment from $\mathrm{p} 0$ to $\mathrm{p} 1 .$ Here are some sample runs:
Enter three points for $p 0, p 1,$ and $p 2: 112.52 .51 .51 .5$
(1.5,1.5) is on the line segment from (1.0,1.0) to (2.5,2.5)
Enter three points for $p 0, p 1,$ and $p 2: 11223.53 .5$
(3.5,3.5) is not on the line segment from (1.0,1.0) to (2.0,2.0)

Morgan Cheatham
Morgan Cheatham
Numerade Educator
01:05

Problem 35

(Decimal to hex) Write a program that prompts the user to enter an integer between 0 and 15 and displays its corresponding hex number. Here are some sample runs:
Enter a decimal value $(0 \text { to } 15): 11$ The hex value is $B$
Enter a decimal value $(0 \text { to } 15 \text { ): } 5$ The hex value is 5
Enter a decimal value $(0 \text { to } 15): 31$ Invalid input

Morgan Cheatham
Morgan Cheatham
Numerade Educator