1/86
C&P II Midterm
Name | Mastery | Learn | Test | Matching | Spaced | Call with Kai |
|---|
No analytics yet
Send a link to your students to track their progress
What is true object-oriented programming based on?
Defining classes that represent objects with well-defined characteristics and functionality
Can you only have 1 constructor?
No; you can have multiple, BUT you need to identify parameters
What does every object have?
State and behavior
Dice analogy for state and behavior
State can be defined as which face is showing
Behavior that it can be rolled
What can a class contain?
Data declarations and method declarations
What do the values of the data define in a class?
The state of an object created from the class
What do the functionality of the methods define in a class?
The behaviors of the object
What does final mean?
The value CANNOT be updated
What does private mean?
Must go through method?
What is the difference between public class Die and public Die()?
public class Die → class
public Die() → method
What are two data values of the Die class
A constant MAX that represents the maximum face value
An integer faceValue that represents the current face value
What does the roll method do?
Uses the random method of the Math class to determine a new face value
What is the toString method?
Method returns a character string that represents the object in some way
When is toString called?
Called automatically when an object is concatenated to a string OR when it is passed to the println method
What is the toString method convenient for?
Debugging problems
What is a constructor?
Used to set up an object when it is initially created
What do the constructor and class have in common?
A constructor has the same name as the class
What is the data scope?
The scope of data is the area in a program in which that data can be referenced (used)
What is important to note about data declared at the class level?
It can be referenced by all methods in that class
What is important to note about data declared within a method?
That data can only be used in that method
What is local data?
Data declared with a method
What is instance data?
Variable declared at the class level
What is an instance?
A specific object created from a class
What is important to note about each instance?
Each instance (object) has its own instance variable
Does a class reserve memory space for data?
No; a class declares the type of data, but does not reserve memory space for it
Do objects share the method definitions?
Yes; objects of a class share the method definitions
Do objects share data space?
No; each object has its own data space
What are the only two ways two objects can have different states?
The objects of a class share the method definitions but each object has its own data space
What does UML stand for?
Unified Modeling Language
What do UML diagrams show?
Shows relationships among classes and objects
What does a UML class diagram consist of and what does it contain?
One or more classes, each with sections for the class name, attributes (data) and operations (methods)
What do lines between classes represent in UML diagrams?
Associations
What do dotted arrows show in UML diagrams?
Shows that one classes uses the other (aka calls its methods)
What does it mean when an arrow points to something in a UML diagram?
The place where the arrow originates from is DEPENDENT
What is the relationship between a class and an object?
Class is the definition/pattern/blueprint of an object; it defines data that will be managed by an object, but doesn’t reserve memory space for it
Multiple objects can be created from a class, and each object has its own copy of the instance data.
Where is instance data declared?
At the class level
What is the scope of instance data?
It can be referenced in any method of the class
What is local data?
Local data is declared within a method, and is only accessible in that method
What are the two views of an object?
Internal and external
What is internal view?
The details of the variables and methods of the class that defines it
What is the external view?
The services that an object provides and how the object interacts with the rest of the system
What is known about the object from the external view?
An objected is an encapsulated entity, providing a set of specific services
What do the services define in an object?
They define the interface to the object
What is encapsulation and how does it work?
An object where the inner workings are hidden form the client; the client invokes the interface methods and they manage the instance data
What does it mean for an object to be self-governing?
When an object (client) uses another object for the services it provides, the client of an object can request its services, but it should not be aware of how those services are accomplished
Any changes to the object’s state (variables) should be made by that object’s methods; it should be difficult/impossible for a client to access an object’s variables directly
What are visibility modifiers?
A Java reserved word that specifies particular characteristics of a method or data
What are the three visibility modifiers?
Public, Protected, Private
What is the difference between public, protected, and private
Public - for everyone
Protected - somewhat restricted, belongs to some members
Private - restricted
What visibility do members declared without a visibility modifier have?
Default visibility; can be referenced by any class in the same package
Why do public variables violate encapsulation?
Because they allow the client to modify the values directly
Should instance variables be declared with public visibility?
NO
Do public constants violate encapsulation?
No; although the client can access it, its value cannot be changed
What visibility modifier do methods that provide the object’s services have?
Declared with public visibility so that they can be invoked by clients
What is another word for public methods?
Service methods
What is a support method?
Method created simply to assist a service method
What visibility modifier should support methods not be declared with?
Public visibility, since support methods are not intended to be called by a client
What is an accessor method?
Returns the current value of a variable
What is a mutator method?
Changes the value of a variable
What is the format of an accessor method?
getX
What is the format of the mutator method?
setX
What does the use of mutators give the class desginer?
the ability to restrict a client’s options to modify an object’s state
How is a mutator often designed?
So that the values of variables can be set only within particular limits
Why was the faceValue variable declares as private in the Die class?
By making it private, each Die object controls its own data and allows it to be modified only by the well-defined operations it provides
Why is it okay to declare MAX as public in the Die class?
MAX is a constant. Its value cannot be changed. Therefore, there is no violation of encapsulation
What is a method declaration?
Specifies the code that will be executed when the method is invoked
What happens when a method is invoked?
the flow of control jumps to the method and executes its code
What does a method declaration begin with?
A method header
What does the parameter list specify?
The type and name of each parameter
What is a formal parameter?
The name of a parameter in the method declaration
What is the method header followed by?
The method body
What type of data is sum and result?
Local data
When is sum and data created?
Each time the method is called, and are destroyed when it finishes executing
What must be consistent with the return type?
The return expression
What does the return type of a method indicate?
The type of value that the method sends back to the calling location?
What is a void return type?
A method that does not return a value
What does a return statement specify?
The value that will be returned; its expression must conform to the return type
What occurs to the parameters when a method is called?
The actual parameters in the invocation are copied into the formal parameters in the method head
What do the formal parameters of a method create?
Automatic local variables
What occurs when the method finishes?
All local variables are destroyed, including formal parameters
What level are instance variables declared at?
The class variables
What happens to local variables after the method finishes?
They are destroyed, including formal parameters
What is a driver program?
Drives the use of other, more interesting parts of a program
What are driver programs used for?
To test other parts of the software
Does a constructor have a return type?
NO; it has no return type specified in the method header, not even void
What is a common error with constructors?
Putting a return type on a constructor, which makes it a “regular” method that happens to have the same name as the class
Does the programmer have to define a constructor for a class?
No
What is important to note about classes and constructors?
Each class has a default constructor that accepts no parameters