Spring Semester 2002


Iffy Practice Problems: This is textbook chapter 5. Programming problems.
1. Write a program (called One) that prints all real solutions to the quadratic equation
a x2 + b x + c = 0
Read in a, b, c and use the quadratic formula. If the discriminant
b2 - 4ac
is negative, display a message stating that there are no real solutions.
Here's how your program should behave:
frilled.cs.indiana.edu%java One
Please enter the value of a then press Enter : 1.0
Please enter the value of b then press Enter : -2.0
Please enter the value of c then press Enter : 1.0
2.0 2.0
frilled.cs.indiana.edu%java One
Please enter the value of a then press Enter : 1
Please enter the value of b then press Enter : 0
Please enter the value of c then press Enter : 1
There are no solutions.
frilled.cs.indiana.edu%java One
Please enter the value of a then press Enter : 1
Please enter the value of b then press Enter : 2
Please enter the value of c then press Enter : 3
There are no solutions.
frilled.cs.indiana.edu%java One
Please enter the value of a then press Enter : 1
Please enter the value of b then press Enter : -3
Please enter the value of c then press Enter : 1
1.881966011250105 4.118033988749895
frilled.cs.indiana.edu%java One
Please enter the value of a then press Enter : 0
Please enter the value of b then press Enter : 0
Please enter the value of c then press Enter : 3
3.0 is not zero
frilled.cs.indiana.edu%java One
Please enter the value of a then press Enter : 1
Please enter the value of b then press Enter : 0
Please enter the value of c then press Enter : 0
-0.0 0.0
frilled.cs.indiana.edu%java One
Please enter the value of a then press Enter : 0
Please enter the value of b then press Enter : 0
Please enter the value of c then press Enter : 0
Identity: zero == zero.
frilled.cs.indiana.edu%

2. Write a program (called Two) that takes user input describing a playing card in the shorthand notation described below, and then prints the full description of the card.
A Ace
2..10 Card values
J Jack
Q Queen
K King
D Diamonds
H Hearts
S Spades
C Clubs
Here's a sample run of such a program:
frilled.cs.indiana.edu%java Two
Enter the card notation: AS
Ace of spades.
frilled.cs.indiana.edu%java Two
Enter the card notation: 2H
Two of hearts.
frilled.cs.indiana.edu%java Two
Enter the card notation: CJ
Unknown denomination of unknown colour.
frilled.cs.indiana.edu%java Two
Enter the card notation: JC
Jack of clubs.
frilled.cs.indiana.edu%java Two
Enter the card notation: QK
Queen of unknown colour.
frilled.cs.indiana.edu%java Two
Enter the card notation: QH
Queen of hearts.
frilled.cs.indiana.edu%java Two
Enter the card notation:  A D
Unknown denomination of unknown colour.
frilled.cs.indiana.edu%java Two
Enter the card notation: A D
Ace of unknown colour.
frilled.cs.indiana.edu%java Two
Enter the card notation: AD
Ace of diamonds.
frilled.cs.indiana.edu%

3. Write a program (called Three) that
  • reads in three floating-point numbers and
  • prints the largest of the three inputs
Here are (essentially) all possible runs of your program:
frilled.cs.indiana.edu%java Three
Please enter the value of a then press Enter : 1
Please enter the value of b then press Enter : 2
Please enter the value of c then press Enter : 3
The largest number is: 3.0
frilled.cs.indiana.edu%java Three
Please enter the value of a then press Enter : 1
Please enter the value of b then press Enter : 3
Please enter the value of c then press Enter : 2
The largest number is: 3.0
frilled.cs.indiana.edu%java Three
Please enter the value of a then press Enter : 2
Please enter the value of b then press Enter : 1
Please enter the value of c then press Enter : 3
The largest number is: 3.0
frilled.cs.indiana.edu%java Three
Please enter the value of a then press Enter : 2
Please enter the value of b then press Enter : 3
Please enter the value of c then press Enter : 1
The largest number is: 3.0
frilled.cs.indiana.edu%java Three
Please enter the value of a then press Enter : 3.0
Please enter the value of b then press Enter : 1.0
Please enter the value of c then press Enter : 2.0
The largest number is: 3.0
frilled.cs.indiana.edu%java Three
Please enter the value of a then press Enter : 3.0
Please enter the value of b then press Enter : 2.0
Please enter the value of c then press Enter : 1.0
The largest number is: 3.0
frilled.cs.indiana.edu%

4. Write a program (called Four) that creates a circle with radius 100 and center (110, 120). Ask the user to specify the x and y coordinates of a point. If the point lies inside the circle, then show a message "Congratulations!" Otherwise, show a message "You missed." Feel free to use, and enhance with a method
public boolean contains(double xPoint, double yPoint){ ... }
the Circle class that we developed in the second set of problems.

Here's a sample run of such a program:
frilled.cs.indiana.edu%java Four
Welcome. Circle created. 
Center is at: (110.0, 120.0)
Radius is: 100.0
You will be asked to specify a point.
First enter x, then enter y.
Please enter x now: 110
Please enter y now: 120
Congratulations.
frilled.cs.indiana.edu%java Four
Welcome. Circle created. 
Center is at: (110.0, 120.0)
Radius is: 100.0
You will be asked to specify a point.
First enter x, then enter y.
Please enter x now: -30 
Please enter y now: 120
You missed.
frilled.cs.indiana.edu%java Four
Welcome. Circle created. 
Center is at: (110.0, 120.0)
Radius is: 100.0
You will be asked to specify a point.
First enter x, then enter y.
Please enter x now: 170.98
Please enter y now: 190.05
Congratulations.
frilled.cs.indiana.edu%

5. Write a program (called Five) that asks the user to specify the radii of two circles. The first circle has center (100, 200) and the second circle has center (200, 100). Check to see if the circles intersect. If they intersect, then display a message "Circles intersect." Otherwise, display "Circles don't intersect." Hint: Compute the distance between the centers and compare it to the radii.
Here's a sample run of such a program:
frilled.cs.indiana.edu%java Five
Welcome. Please specify the radius of the first circle: 10
Great. Now please specify the radius of the second circle: 10
Circles don't intersect.
frilled.cs.indiana.edu%java Five
Welcome. Please specify the radius of the first circle: 100
Great. Now please specify the radius of the second circle: 100
Circles intersect.
frilled.cs.indiana.edu%java Five
Welcome. Please specify the radius of the first circle: 70
Great. Now please specify the radius of the second circle: 71.42
Circles don't intersect.
frilled.cs.indiana.edu%java Five
Welcome. Please specify the radius of the first circle: 70
Great. Now please specify the radius of the second circle: 71.43
Circles intersect.
frilled.cs.indiana.edu%java Five
Welcome. Please specify the radius of the first circle: 140
Great. Now please specify the radius of the second circle: 1.414299
Circles don't intersect.
frilled.cs.indiana.edu%java Five
Welcome. Please specify the radius of the first circle: 140
Great. Now please specify the radius of the second circle: 1.4299
Circles intersect.
frilled.cs.indiana.edu%

6. Write a program (called Six) that prints the question
Do you want to continue?
and reads the user input. If the user input is any of the following:
  • Y
  • Yes
  • OK
  • Sure
  • Why not?
print out
  • OK
If the user input is
  • N
  • No
then print out
  • Terminating
Otherwise, print
  • Bad input
The case of the user input should not matter. For example,
  • y
  • yes
are also valid inputs. Hint: Convert the user input to lowercase and then compare.
Here's a sample run of such a program:
frilled.cs.indiana.edu%java Six
Do you want to continue? Y
OK
frilled.cs.indiana.edu%java Six
Do you want to continue? Yes
OK
frilled.cs.indiana.edu%java Six
Do you want to continue? OK
OK
frilled.cs.indiana.edu%java Six
Do you want to continue? suRe
OK
frilled.cs.indiana.edu%java Six
Do you want to continue? Sure
OK
frilled.cs.indiana.edu%java Six
Do you want to continue? whY nOt?
OK
frilled.cs.indiana.edu%java Six
Do you want to continue? why not ?
Bad input
frilled.cs.indiana.edu%java Six
Do you want to continue? N
Terminating
frilled.cs.indiana.edu%java Six
Do you want to continue? NO
Terminating
frilled.cs.indiana.edu%java Six
Do you want to continue? nO
Terminating
frilled.cs.indiana.edu%java Six
Do you want to continue? No!
Bad input
frilled.cs.indiana.edu%

7. Write a program (called Seven) that translates a letter grade into a number grade. Letter grades are A, B, C, D, F possibly followed by + or -. Their numeric values are 4, 3, 2, 1, and 0. There is no F+ or F-. A + increases the numeric value by 0.3, a - decreases it by 0.3. However an A+ has a value of 4.0.
Here's a sample run of such a program:
frilled.cs.indiana.edu%java Seven
Enter a letter grade: A+
The numeric value is: 4.0
frilled.cs.indiana.edu%java Seven
Enter a letter grade: A +
Bad input.
frilled.cs.indiana.edu%java Seven
Enter a letter grade: U-
Bad input.
frilled.cs.indiana.edu%java Seven
Enter a letter grade: B-
The numeric value is: 2.7
frilled.cs.indiana.edu%java Seven
Enter a letter grade: C
The numeric value is: 2.0
frilled.cs.indiana.edu%java Seven
Enter a letter grade: F
The numeric value is: 0.0
frilled.cs.indiana.edu%java Seven
Enter a letter grade: D-
The numeric value is: 0.7
frilled.cs.indiana.edu%java Seven
Enter a letter grade: --
Bad input.
frilled.cs.indiana.edu%

8. Write a program (called Eight) that translates a number between 0 and 4 into the closest letter grade. For example, the number 2.8 (which might have been the average of several grades) would be converted to B-. Break ties in favor of the better grade; for example 2.85 should be a B.
Here's a sample run of such a program:
frilled.cs.indiana.edu%java Eight
Enter numeric score then press Enter : 3.85
A
frilled.cs.indiana.edu%java Eight
Enter numeric score then press Enter : 3.84
A-
frilled.cs.indiana.edu%java Eight
Enter numeric score then press Enter : 3.5
A-
frilled.cs.indiana.edu%java Eight
Enter numeric score then press Enter : 3.49
B+
frilled.cs.indiana.edu%java Eight
Enter numeric score then press Enter : 3.1
B
frilled.cs.indiana.edu%java Eight
Enter numeric score then press Enter : 10
A+
frilled.cs.indiana.edu%java Eight
Enter numeric score then press Enter : -2

frilled.cs.indiana.edu%java Eight
Enter numeric score then press Enter : -0.5

frilled.cs.indiana.edu%

9. Write a program (called Nine) that reads in three strings and sorts them lexicographically.
Here's a sample run of such a program:
frilled.cs.indiana.edu%java Nine
Enter three strings: 
Alpha
Beta
Gamma
Alpha
Beta
Gamma
frilled.cs.indiana.edu%java Nine
Enter three strings: 
Alpha
Gamma
Beta
Alpha
Beta
Gamma
frilled.cs.indiana.edu%java Nine
Enter three strings: 
Username
User
Use
Use
User
Username
frilled.cs.indiana.edu%java Nine
Enter three strings: 
Alpha
Alpha
Alpha
Alpha
Alpha
Alpha
frilled.cs.indiana.edu%java Nine
Enter three strings: 
Gamma
gamma
gama
Gamma
gama
gamma
frilled.cs.indiana.edu%java Nine
Enter three strings: 
10
1
2
1
10
2
frilled.cs.indiana.edu%

10. A year with 366 days is called a leap year. A year is a leap year if it is divisible by 4 (for example, the year 1980), except it is not a leap year if it is divisible by 100 (for example, the year 1900); however, it is a leap year if it is divisible by 400 (for example, the year 2000). There were no exceptions before the introduction of the Gregorian calendar on October 15, 1582 (for example, the year 1500 was a leap year). Write a program (called Ten) that asks the user for a year and computes whether that year is a leap year or not.
Here's a sample run of such a program:
frilled.cs.indiana.edu%java Ten
Please enter the year then press Enter : 1500
Leap year: 1500
frilled.cs.indiana.edu%java Ten
Please enter the year then press Enter : 1900
1900 not a leap year!
frilled.cs.indiana.edu%java Ten
Please enter the year then press Enter : 1996
Leap year: 1996
frilled.cs.indiana.edu%java Ten
Please enter the year then press Enter : 1997
1997 not a leap year!
frilled.cs.indiana.edu%java Ten
Please enter the year then press Enter : 2000
Leap year: 2000
frilled.cs.indiana.edu%

11. Write a program (called Eleven) that asks the user to enter a month
  • 1 = January
  • 2 = February
  • ... and so on
and then prints the number of days of the month.

For February, print

28 or 29 days
Here's a sample run of such a program:
frilled.cs.indiana.edu%java Eleven
Enter a month : 1
31 days
frilled.cs.indiana.edu%java Eleven
Enter a month : 2
28 or 29 days
frilled.cs.indiana.edu%java Eleven
Enter a month : 3
31 days
frilled.cs.indiana.edu%java Eleven
Enter a month : 4
30 days
frilled.cs.indiana.edu%java Eleven
Enter a month : 5
31 days
frilled.cs.indiana.edu%java Eleven
Enter a month : 12
31 days
frilled.cs.indiana.edu%java Eleven
Enter a month : 13
Bad input
frilled.cs.indiana.edu%java Eleven
Enter a month : 0
Bad input
frilled.cs.indiana.edu%java Eleven
Enter a month : 1.2
Exception in thread "main" java.lang.NumberFormatException: 1.2
	at java.lang.Integer.parseInt(Integer.java:418)
	at java.lang.Integer.parseInt(Integer.java:458)
	at ConsoleReader.readInt(Eleven.java:58)
	at Eleven.main(Eleven.java:7)
frilled.cs.indiana.edu%

12. Write a program (called Twelve) that reads in two floating-point numbers and tests BOTH
  • whether they are the same up to two decimal places, and
  • whether they are within 0.01 of each other
Here's a sample run of such a program:
frilled.cs.indiana.edu%java Twelve
Comparing floating-point numbers.
Enter the first number : 1.3456
Enter the second number: 1.3402
1.3456 and 1.3402 are the same up to two decimal places.
1.3456 and 1.3402 are within 0.01 of one another.
Thanks for asking.
frilled.cs.indiana.edu%java Twelve
Comparing floating-point numbers.
Enter the first number : 2.003
Enter the second number: 1.998
2.003 and 1.998 are NOT the same up to two decimal places.
2.003 and 1.998 are within 0.01 of one another.
Thanks for asking.
frilled.cs.indiana.edu%java Twelve
Comparing floating-point numbers.
Enter the first number : 1.998
Enter the second number: 1.990
1.998 and 1.99 are the same up to two decimal places.
1.998 and 1.99 are within 0.01 of one another.
Thanks for asking.
frilled.cs.indiana.edu%java Twelve
Comparing floating-point numbers.
Enter the first number : 1.0000001
Enter the second number: 0.9999999
1.0000001 and 0.9999999 are NOT the same up to two decimal places.
1.0000001 and 0.9999999 are within 0.01 of one another.
Thanks for asking.
frilled.cs.indiana.edu%

13. Enhance the BankAccount class by

  1. rejecting negative amounts in the deposit and withdraw methods

  2. rejecting withdrawals that would result in a negative balance

Write a program (called Thirteen) that illustrates the behaviour of your new BankAccount.

Here's a sample run of such a program:
frilled.cs.indiana.edu%java Thirteen
Hello, and welcome to JavaOne Bank.
An account will be created for you.
What will the initial balance be?
Type it now: 
20
The current balance in your account is: 20.0
You now want to make a deposit. How much?
Type the amount here: 
30
The current balance in your account is: 50.0
You now want to make a withdrawal. How much?
Type it now: 
100
Sorry, you cannot do that.
The current balance in your account is: 50.0
Thanks for using class BankAccount. Good-bye!
frilled.cs.indiana.edu%java Thirteen
Hello, and welcome to JavaOne Bank.
An account will be created for you.
What will the initial balance be?
Type it now: 
-20
The current balance in your account is: -20.0
You now want to make a deposit. How much?
Type the amount here: 
10
The current balance in your account is: -10.0
You now want to make a withdrawal. How much?
Type it now: 
10
Sorry, you cannot do that.
The current balance in your account is: -10.0
Thanks for using class BankAccount. Good-bye!
frilled.cs.indiana.edu%java Thirteen
Hello, and welcome to JavaOne Bank.
An account will be created for you.
What will the initial balance be?
Type it now: 
-30
The current balance in your account is: -30.0
You now want to make a deposit. How much?
Type the amount here: 
-20
Sorry, you cannot do that.
The current balance in your account is: -30.0
You now want to make a withdrawal. How much?
Type it now: 
-40
The current balance in your account is: 10.0
Thanks for using class BankAccount. Good-bye!
frilled.cs.indiana.edu%
Notice that you are free to allow negative initial balances (resembling a loan) but the two methods that implement deposit and withdraw should behave as stated in the text of the problem (rejecting negative arguments and rejecting operations that result in a negative balance other than the initial creation of the account). If you want you can disallow initial negative balances as well.

14. Write a program that reads in the name and hourly wage of an employee. Then ask how many hours the employee worked in the past week. Be sure to accept fractional hours. Compute the pay. Any overtime work (over 40 hours per week) is paid at 150 percent of the regular wage. Print a paycheck for the employee.
Here's a sample run of such a program:
frilled.cs.indiana.edu%java Fourteen
Please enter employee's name then press Enter : Larry Bird
Please enter hourly wage then press Enter : 12.50
Please enter hours worked then press Enter: 10
 Paycheck for employee Larry Bird

 Hours worked: 10.0
 Hourly wage: 12.5

 Total payment: 125.0
frilled.cs.indiana.edu%java Fourteen
Please enter employee's name then press Enter : Michael Jordan
Please enter hourly wage then press Enter : 10
Please enter hours worked then press Enter: 50
 Paycheck for employee Michael Jordan

 Hours worked: 50.0
 Hourly wage: 10.0

 Overtime hours: 10.0
 Overtime hourly wage: 15.0

 Total payment: 550.0
frilled.cs.indiana.edu%java Fourteen
Please enter employee's name then press Enter : Dennis Rodman
Please enter hourly wage then press Enter : 2
Please enter hours worked then press Enter: -4
 Paycheck for employee Dennis Rodman

 Hours worked: -4.0
 Hourly wage: 2.0

 Total payment: -8.0
frilled.cs.indiana.edu%
Notice it's up to you what you do when the input is out of range. Also, you don't need to use the class Employee developed in the previous chapter, the emphasis is here on decisions.


Last updated: Feb 5, 2002 by Adrian German for A201