Chapter 4 Writing Classes Flashcards

1
Q

What is an attribute?

A

An attribute is a data value stored in an object and defines a particular characteristic of that object. For example, one attribute of a Student object might be that student’s current grade point average.
Collectively, the values of an object’s attributes determine that object’s current state.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is an operation?

A

An operation is a function that can be done to or done by an object. For example, one operation of a Student object might be to compute that student’s current grade point average. Collectively, an object’s operations are referred to as the object’s behaviors.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

List some attributes and operations that might be defined for a class called Book that represents a book in a library.

A

Some attributes and operations that might be defined for a class called Book that represents a book in a library are:

Attributes              Operations
idNumber	      checkOut
onShelfStatus	      checkIn
readingLevel	      isAvailable
dueDate	              placeOnHold
                              setStatus
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

True or False? Explain.

a. We should use only classes from the Java standard class library when writing our programs—there is no need to define or use other classes.
b. An operation on an object can change the state of an object.
c. The current state of an object can affect the result of an operation on that object.
d. In Java, the state of an object is represented by its methods.

A

a. False – Identifying classes to help us solve a problem is a key step in object-oriented programming. In addition to identifying classes that already exist, we also identify, design, and implement new classes, as needed.
b. True – We call such operations mutators.
c. True – The result of many operations depends on the current state of the object on which they are operating.
d. False – In Java, the state of an object is represented by its instance data

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is the difference between an object and a class?

A
A class is the blueprint of an object. It defines the variables and methods that will be a part of every object that is instantiated from it. But a class
reserves no memory space for variables. Each object has its own data space and, therefore, its own state
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Describe the instance data of the Die class.

A

The instance data of the Die class are MAX, an integer constant equal to 6 that represents the number of faces on the die and therefore the maximum value of the die, and faceValue, an integer variable that represents the current “up” or face value of the die

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Which of the methods defined for the Die class can change the state of a Die object—that is, which of the methods assign values to the instance data?

A

The methods defined for the Die class that can change the state of a Die object are roll and setFaceValue

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What happens when you pass an object to a print or println method?

A

When you pass an object to a print or println method, the toString method of the object is called automatically to obtain a string description of the object. If no toString method is defined for the object, then a default string is used. Therefore, it is usually a good idea to define a
toString method when defining classes.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is the scope of a variable?

A
The scope of a variable is the area within a program in which the variable can be referenced. An instance variable, declared at the class level, can be referenced in any method of the class. Local variables, including
the formal parameters, declared within a particular method, can be referenced only in that method.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What are UML diagrams designed to do?

A

A UML diagram helps us visualize the entities (classes and objects) in a program as well as the relationships among them. UML diagrams are tools that help us capture the design of a program prior to writing it.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Objects should be self-governing. Explain.

A

A self-governing object is one that controls the values of its own data. Encapsulated objects, which don’t allow an external client to reach in and change its data, are self-governing.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is the interface to an object?

A

An object’s interface is the set of public operations (methods) defined on it. That is, the interface establishes the set of services the object will perform for the rest of the system

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is a modifier?

A

A modifier is a Java reserved word that can be used in the definition of a variable or method and that specifically defines certain characteristics
of its use. For example, by declaring a variable with private visibility, the variable cannot be directly accessed outside of the object in which it is defined.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Why might a constant be given public visibility?

A

A constant might be declared with public visibility, because that would not violate encapsulation. Because the value of a constant cannot be changed, it is not generally a problem for another object to access it
directly.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Describe each of the following:

a. public method
b. private method
c. public variable
d. private variable

A

The modifiers affect the methods and variables in the following ways:

a. A public method is called a service method for an object because it defines a service that the object provides.
b. A private method is called a support method because it cannot be invoked from outside the object and is used to support the activities of other methods in the class.
c. A public variable is a variable that can be directly accessed and modified by a client. This explicitly violates the principle of encapsulation and therefore should be avoided.
d. A private variable is a variable that can be accessed and modified only from within the class. Variables almost always are declared with private visibility

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Why is a method invoked through (or on) a particular object? What is the exception to that rule?

A
Although a method is defined in a class, it is invoked through a particular object to indicate which object of that class is being affected. For example, the Student class may define the operation that computes the
grade point average (GPA) of a student, but the operation is invoked through a particular Student object to compute the GPA for that student. The exception to this rule is the invocation of a static method (see Chapter 3), which is executed through the class name and does not affect any particular object
17
Q

What does it mean for a method to return a value?

A

An invoked method may return a value, which means it computes a value and provides that value to the calling method. The calling method usually uses the invocation and thus its return value, as part of a larger expression

18
Q

What does the return statement do?

A

An explicit return statement is used to specify the value that is returned from a method. The type of the return value must match the return type specified in the method definition.

19
Q

Is a return statement required?

A

A return statement is required in methods that have a return type other than void. A method that does not return a value could use a return statement without an expression, but it is not necessary. Only one return statement should be used in a method.

20
Q

Explain the difference between an actual parameter and a formal parameter.

A

An actual parameter is a value sent to a method when it is invoked. A formal parameter is the corresponding variable in the header of the method declaration; it takes on the value of the actual parameter so that
it can be used inside the method.

21
Q

Write a method called getFaceDown for the Die class that returns the current “face down” value of the die. Hint: On a standard die, the sum of any two opposite faces is seven.

A

The following code implements the requested getFaceDown method.:

//---------------------------------------------------
// Face down value accessor.
//---------------------------------------------------
public int getFaceDown()
{
return (MAX + 1) - faceValue;
}
22
Q

In the Transactions program:

a. How many Account objects are created?
b. How many arguments (actual parameters) are passed to the
withdraw method when it is invoked on the acct2 object?
c. How many arguments (actual parameters) are passed to the
addInterest method when it is invoked on the acct3 object?

A

In the Transactions program:

a. Three Account objects are created.
b. Two arguments (actual parameters) are passed to the withdraw
method when it is invoked on the acct2 object.
c. No arguments (actual parameters) are passed to the addInterest
method when it is invoked on the acct3 object

23
Q

Which of the Account class methods would you classify as accessor methods? As mutator methods? As service methods?

A

The method getBalance is a classic accessor method. One can also classify the toString method as an accessor, since it returns information about the
object. The deposit, withdraw, and addInterest methods all provide both mutator and accessor capabilities, because they can be used to change the
account balance and also return the value of the balance after the change is made. All of the methods mentioned above are service methods––they all
have public visibility and provide a service to the client.

24
Q

What are constructors used for?

A

Constructors are special methods in an object that are used to initialize the object when it is instantiated.

25
Q

How are constructors defined?

A

A constructor has the same name as its class, and it does not return a value.

26
Q

Where is the “content” of the panel created in the SmilingFace program defined?

A

The “content” of the panel created in the SmilingFace program is defined in the SmilingFacePanel class.

27
Q

In the SmilingFace program, when and how is the paintComponent method of the panel object invoked?

A

In the SmilingFace program, the paintComponent method of the panel object is invoked automatically when the panel object SmilingFacePanel is instantiated.

28
Q

Write code that will add a pair of eyeglasses to the smiling face.

A

There are many ways to add a pair of eyeglasses to the smiling face. The following code, inserted after the code that draws the nose and mouth, is one approach.

page. drawOval(BASEX+17, BASEY+23, 21, 21); // glasses
page. drawOval(BASEX+42, BASEY+23, 21, 21);
page. drawLine(BASEX+3, BASEY+27, BASEX+17, BASEY+27);
page. drawLine(BASEX+62, BASEY+27, BASEX+76, BASEY+27);
page. drawLine(BASEX+39, BASEY+29, BASEX+42, BASEY+29);

29
Q
Rewrite the constructor for the Circle class so that it generates a circle with a random diameter that is between 20 and 200 inclusive for the Circle object it sets up. The other attributes of the circle should
be provided as parameters to the constructor.
A
The following code implements the requested constructor:
//---------------------------------------------------
// Constructor: Sets up this circle with the
// specified values.
//---------------------------------------------------
public Circle(Color shade, int upperX, int upperY)
{
diameter = (int) (Math.random() * 180) + 20;
color = shade;
x = upperX;
y = upperY;
}

It might be better to define and use constants of 180 and 20 in the Circle class, or perhaps pass those values to the constructor from the client as arguments.

30
Q

What is the relationship between an event and a listener?

A

Events usually represent user actions. A listener object is set up to listen for a certain event to be generated from a particular component.

31
Q

Can we add any kind of listener to any component? Explain.

A

No, we cannot add any listener to any component. Each component generates a certain set of events, and only listeners of those types can be added to the component.

32
Q

What type of event does a push button (a JButton object) generate?

A

A JButton object generates an action event when the button is pushed. When that occurs, the actionPerformed method of the action listener associated with that button is invoked.

33
Q

How would you change the PushCounterPanel class so that instead of displaying a count of how many times the button was pushed, it displays a count “trail”? After one push, it displays “01”; after two pushes, it displays “012”; after five pushes, it displays “012345”; and so on

A

To change the PushCounterPanel class so that instead of displaying a count of how many times the button was pushed it displays a count “trail,” you can define a new instance variable of the PushCounterPanel class as follows:

private String display = “0”;

Then change the code in the actionPerformed method to be:
count++;
display = display + count;
label.setText(“Pushes: “ + display);

34
Q

Describe what happens within the Fahrenheit program when a user types a number into the text box and presses the Enter key.

A
In the Fahrenheit program, when a user types a number into the text box of the interface and presses the Enter (or Return) key, the text field component generates an action event. The TempListener class that is listening for that event reacts by getting the text from the text
box, transforming the text into an integer that represents the given Fahrenheit temperature, calculating the corresponding Celsius temperature and saving it to the resultLabel. The contents of the resultLabel then appear on the screen.
35
Q

Change the FahrenheitPanel class so that when the user enters a number in the text field a statement appears in the form “X degrees Fahrenheit = Y degrees Celsius” below the text field. For example, if the user enters 223, the statement “223 degrees Fahrenheit = 106 degrees Celsius” appears instead of “Temperature in Celsius: 106”.

A

To make the change to the FahrenheitPanel class, first remove the outputLabel from the class since it is no longer needed. Then, change the code that sets the result label to:

String hold = text + “ degrees Fahrenheit = “;
hold += celsiusTemp + “ degrees Celsius”;
resultLabel.setText(hold);