1.
A method that lacks a body is an method.
ActionEvent
s, it must implement the interface.JButton
class on Sun’s Web site: http://java.sun.com/j2se/1.5.0/docs/api/. List the names of all the methods that would be inherited by the ToggleButton
subclass that we defined in this chapter.toString()
method for the ToggleButton
class defined in this chapter. The toString()
method should return the ToggleButton
’s current label.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 accessor and mutator methods needed by those 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.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()
.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\text{,}\) the third by \(N+2\text{,}\) 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.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.Cipher alphabet: zebracdfghijklmnopqstuvwxy
Plain alphabet: abcdefghijklmnopqrstuvwxyz
Alphabet
class for constructing these kinds of 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.Cipher
subclass for a substitution cipher that uses an alphabet from the Alphabet
class created in the previous exercise. 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.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 a wrong answer.MultiplicationPlayer
that plays the multiplication game described in the previous exercise. This class should implement the IPlayer
interface.TwoPlayerGame
subclass called RockPaperScissors
. The rules of this game are that each player, at the same time, picks 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.RockPaperScissorsPlayer
that plays the the game described in the previous exercise. This class should implement the IPlayer
interface.public class Animal ...
public class DomesticAnimal extends Animal ...
public class FarmAnimal extends DomesticAnimal...
public class HousePet extends DomesticAnimal...
public class Cow extends FarmAnimal ...
public class Goat extends FarmAnimal ...
public class DairyCow extends Cow ...
DairyCow dc = new FarmAnimal();
FarmAnimal fa = new Goat();
Cow c1 = new DomesticAnimal();
Cow c2 = new DairyCow();
DomesticAnimal dom = new HousePet();
SomeFrame()
constructor. The error will cause the actionPerformed()
method never to display “Clicked” even though the user clicks the button in the JFrame. Why? (Hint: Think scope!)public class SomeFrame extends JFrame
implements ActionListener
{
// Declare instance variables
private JButton button;
public JFrame()
{
// Instantiate the instance variable
JButton button = new JButton("Click me");
add(button);
button.addActionListener(this);
} // init()
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == button)
System.out.println("Clicked");
} // actionPerformed()
} // SomeFrame
public class SomeFrame2 extends JFrame
{
// Declare instance variables
private JButton button;
private JTextField field;
public SomeFrame()
{
// Instantiate instance variables
button = new JButton("Click me");
add(button);
field = new JTextField("Field me");
add(field);
System.out.println(field.getText() + button.getText());
} // init()
public static void main(String[] args) {
SomeFrame2 frame = new SomeFrame2();
frame.setSize(400,400);
frame.setVisible(true);
}
} // SomeFrame2
JButton
, a JTextField
, and a JLabel
and then uses the toString()
method to display each object’s string representation.JButton
class inherits a setText(String s)
from its AbstractButton()
superclass. Using that method, design and implement a GUI that has a single button labeled initially, “The Doctor is out.” Each time the button is clicked, it should toggle its label to, “The Doctor is in.” and vice versa.