CMPUT 114 More Practise Java Problems

(Adapted by Marie from Java and Software Design, Dale, Weems, Headington)


1. Write a Java application that converts letters of the alphabet into their corresponding digits on the telephone. The program should let the user enter letters repeatedly until a 'Q' or 'Z' is entered. (Q and Z are the two letters that are not on the telephone.) An error message should be printed for any non-alphabetical character that is entered.

ABC = 2
DEF = 3
GHI = 4
JKL = 5
MNO = 6
PRS = 7
TUV = 8
WXY = 9

Hint: you may find this String methods useful: charAt(int) returns a char from a String at position int, and indexOf(char) returns the position in the String of the given character and if the character is not in the String, it returns -1.

Here is a sample run:

Enter a letter on the telephone (Q or Z to quit): P
The letter P corresponds to 7 on the telephone.

Enter a letter on the telephone(Q or Z to quit): A
The letter A corresponds to 2 on the telephone.

Enter a letter on the telephone(Q or Z to quit): 2
Invalid letter.
Enter a letter on the telephone (Q or Z to quit):Q

Have a nice day!
The solution is here
2. A coach of a local baseball team wants to use the computer to keep track of his player's statistics. Create a BaseballPlayer class. Each instance of BaseballPlayer will know the player's name, jersey number, the number of hits, walks and outs he/she has during a game. This class will have methods that return the player's name and other data as well as calculate and return a player's batting average. A batting average is determined by adding the player's total number of hits and dividing by the total number of times at bat. A walk does not count as either a hit or a time at bat when calculating the batting average. Write a test class to test your BaseballPlayer class.
Other example can be found here:
applet
application

Maintained by: Osmar R. Zaïane <zaiane AT cs.ualberta.ca>
Last modified: Sun Nov 19 12:11:30 2000