Exercises


[Page 400 (continued)]

Note

For programming exercises, first draw a UML class diagram describing all classes and their inheritance relationships and/or associations.


Exercise 8.1

Fill in the blanks in each of the following sentences:

  1. A method that lacks a body is an _____ method.

  2. An ______ is like a class except that it contains only instance methods, no instance variables.

  3. Two ways for a class to inherit something in Java are to _____ a class and _____ an interface.

  4. Instance variables and instance methods that are declared _____ or _____ are inherited by the subclasses.


  5. [Page 401]
  6. An object can refer to itself by using the _____ keyword.

  7. If an applet intends to handle ActionEvents, it must implement the _____ interface.

  8. A _____ method is one that does different things depending upon the object that invokes it.

Exercise 8.2

Explain the difference between the following pairs of concepts:

  1. Class and interface.

  2. Stub method and abstract method.

  3. Extending a class and instantiating an object.

  4. Defining a method and implementing a method.

  5. A protected method and a public method.

  6. A protected method and a private method.

Exercise 8.3

Draw a hierarchy to represent the following situation. There are lots of languages in the world. English, French, Chinese, and Korean are examples of natural languages. Java, C, and C++ are examples of formal languages. French and Italian are considered romance languages, while Greek and Latin are considered classical languages.

Exercise 8.4

Look up the documentation for the JButton class on Sun's Web site:

http://java.sun.com/j2se/docs/

List the names of all the methods that would be inherited by the ToggleButton subclass that we defined in this chapter.

Exercise 8.5

Design and write a toString() method for the ToggleButton class defined in this chapter. The toString() method should return the ToggleButton's current label.

Exercise 8.6

Design a class hierarchy rooted in the class Employee that includes subclasses for HourlyEmployee and SalaryEmployee. The attributes shared in common by these classes include the name and job title of the employee, plus the assessor and mutator methods needed by the attributes. The salaried employees need an attribute for weekly salary, and the corresponding methods for accessing and changing this variable. The hourly employees should have a pay rate and an hours-worked variable. There should be an abstract method called calculateWeeklyPay(), defined abstractly in the superclass and implemented in the subclasses. The salaried worker's pay is just the weekly salary. Pay for an hourly employee is simply hours worked times pay rate.

Exercise 8.7

Design and write a subclass of JTextField called IntegerField that is used for inputting integers but behaves in all other respects like a JTextField. Give the subclass a public method called getInteger().


[Page 402]
Exercise 8.8

Implement a method that uses the following variation of the Caesar cipher. The method should take two parameters, a String and an int N. The result should be a String in which the first letter is shifted by N, the second by N +1, the third by N +2, and so on. For example, given the string "Hello", and an initial shift of 1, your method should return "Igopt". Write a method that converts its String parameter so that letters are written in blocks five characters long.

Exercise 8.9

Design and implement an applet that lets the user type a document into a TextArea and then provides the following analysis of the document: the number of words in the document, the number of characters in the document, and the percentage of words that have more than six letters.

Exercise 8.10

Design and implement a Cipher subclass to implement the following substitution cipher: Each letter in the alphabet is replaced with a letter from the opposite end of the alphabet: a is replaced with z, b with y, and so forth.

Exercise 8.11

One way to design a substitution alphabet for a cipher is to use a keyword to construct the alphabet. For example, suppose the keyword is "zebra". You place the keyword at the beginning of the alphabet, and then fill out the other 21 slots with the remaining letters, giving the following alphabet:

Cipher alphabet:    zebracdfghijklmnopqstuvwxy Plain alphabet:     abcdefghijklmnopqrstuvwxyz 


Design and implement an Alphabet class for constructing substitution alphabets. It should have a single public method that takes a keyword String as an argument and returns an alphabet string. Note that an alphabet cannot contain duplicate letters, so repeated letters in a keyword like "xylophone" would have to be removed.

Exercise 8.12

Design and write a Cipher subclass for a substitution cipher that uses an alphabet from the Alphabet class created in the preceding exercise.

Exercise 8.13

Challenge: Find a partner and concoct your own encryption scheme. Then work separately, with one partner writing encode() and the other writing decode(). Test to see that a message can be encoded and then decoded to yield the original message.

Exercise 8.14

Design a TwoPlayerGame subclass called MultiplicationGame. The rules of this game are that the game generates a random multiplication problem using numbers between 1 and 10, and the players, taking turns, try to provide the answer to the problem. The game ends when a wrong answer is given. The winner is the player who did not give the wrong answer.

Exercise 8.15

Design a class called MultiplicationPlayer that plays the multiplication game described in the preceding exercise. This class should implement the IPlayer interface.


[Page 403]
Exercise 8.16

Design a TwoPlayerGame subclass called RockPaperScissors. The rules of this game are that both players simultaneously make a pick: either a rock, a paper, or a scissors. For each round, the rock beats the scissors, the scissors beats the paper, and the paper beats the rock. Ties are allowed. The game is won in a best out of three fashion when one of the players wins two rounds.

Exercise 8.17

Design a class called RockPaperScissorsPlayer that plays the game described in the preceding exercise. This class should implement the IPlayer interface.




Java, Java, Java(c) Object-Orienting Problem Solving
Java, Java, Java, Object-Oriented Problem Solving (3rd Edition)
ISBN: 0131474340
EAN: 2147483647
Year: 2005
Pages: 275

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net