Chapter 4 Writing Classes Flashcards
What is an attribute?
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.
What is an operation?
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.
List some attributes and operations that might be defined for a class called Book that represents a book in a library.
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
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. 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
What is the difference between an object and a class?
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
Describe the instance data of the Die class.
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
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?
The methods defined for the Die class that can change the state of a Die object are roll and setFaceValue
What happens when you pass an object to a print or println method?
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.
What is the scope of a variable?
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.
What are UML diagrams designed to do?
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.
Objects should be self-governing. Explain.
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.
What is the interface to an object?
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
What is a modifier?
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.
Why might a constant be given public visibility?
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.
Describe each of the following:
a. public method
b. private method
c. public variable
d. private variable
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