Java Chapter 6
Another term for an object of a class is _____
instance
An object is a(n) A. blueprint B. primitive data type C. variable D. instance of a class
instance of a class
Rectangle object fields
length and width
binding
matching a method call with the correct method- compiler uses the method signature to do this
public
member can be accessed by code inside the class or outside
private
member cannot be accessed by code outside the class, only by methods of the same class
method signature
method's name and data types of the method's parameters NOT the return type ex: add(int, int)
Class objects normally have __________ that perform useful operations on their data, but primitive variables do not.
methods
instance methods
methods that are not declared with a special keyword, static require an object to be created in order to be used
The process of matching a method call with the correct method is known as
binding
A class is analogous to a(n)
blueprint
A class's responsibilities are ______. a. the objects created from the class b. things the class knows c. actions the class performs d. both b and c
both b and c
After the header, the body of the method appears inside a set of _____.
braces {}
This is a method that is automatically called when an instance of a class is created.
constructor
This is automatically provided for a class if you do not write one yourself.
default constructor
no-arg constructor
does not accept arguments default constructor is a no-arg constructor
instance
each object that is created from a class
A class may not have more than one constructor. T or F
false
To find the classes needed for an object-oriented application, you identify all of the verbs in a description of the problem domain. T or F
false
when you write a constructor for a class, it still has the default constructor that Java automatically provides T or F
false
String class constructor shortcut
accepts a String literal as an argument, does not require new keyword --> String name="Michael";
This is a method that gets a value from a class's field, but does not change it.
accessor
accessor vs mutator
accessor: methods that retrieve data of fields mutator: methods that modify data of fields
This is a class member that holds data. a. method b. instance c. field d. constructor
field
A class specifies the ________ and ________ that a particular type of object has.
fields;methods
_____ represents an entity in the real world that can be distinctly identified.
an object
data hiding
an object hides its internal, private fields from code that is outside the class that the object is an instance of --> code outside the class must use the class's public methods to operate on an object's private fields
In a UML diagram to indicate the data type of a variable enter _____.
the variable name followed by a colon and the data type
Two or more methods in a class may have the same name, as long as this is different.
their parameter list
Each instance of a class has its own set of instance fields T or F
true
The new operator creates an instance of a class. T or F
true
method/constructor overloading
two or more methods/constructors can have the same name as long as their parameter lists are different
Rectangle box;
uninitialized local ref variable -must reference an object before it can be used --> box=new Rectangle(7,14);
stale data example
use a method to calculate area because if length or width are changed and the area does not then it is stale
default constructor
when an object is created its constructor is always called If you do not write one then Java provides one when the class is compiled sets everything to 0, false, or null
shadowing
a method may have a local variable with the same name as an instance field the local variable will hide the value of the instance field DISCOURAGED
constructor
a method that is automatically called when an object is created typically initialize instance fields and perform other object initialization
In UML diagrams, this symbol indicates that a member is private.
-
You use the _____ operator to access members of an object.
.
What is the value of times displayed? public class Test { public static void main(String[] args) { Count myCount = new Count(); int times = 0; for (int i=0; i<100; i++) increment(myCount, times); System.out.println("myCount.count = " + myCount.count); System.out.println("times = "+ times);} public static void increment(Count c, int times) { c.count++; times++;}} class Count { int count; Count(int c) { count = c;} Count() { count = 1;}}
0
What is the value of myCount.count displayed? public class Test { public static void main(String[] args) { Count myCount = new Count(); int times = 0; for (int i=0; i<100; i++) increment(myCount, times); System.out.println("myCount.count = " + myCount.count); System.out.println("times = "+ times);} public static void increment(Count c, int times) { c.count++; times++;}} class Count { int count; Count(int c) { count = c;} Count() { count = 1;}}
101
Suppose you declare Date d. d is now called _____ A. a variable that holds an integer value B. a reference variable for an object C. an object D. an object value
B. a reference variable for an objec
The default value for data field of a boolean type, numeric type, object type is _____, respectively. A. true, 1, Null B. false, 0, null C. true, 0, null D. true, 1, null
B. false, 0, null
Suppose you wish to provide an accessor method for a boolean property finished, what signature of the method should be? A. public void getFinished() B. public boolean getFinished() C. public boolean isFinished() D. public void isFinished()
C. public boolean isFinished()
Given the declaration Circle x = new Circle(), which of the following statement is most accurate? A. x contains an int value. B. x contains an object of the Circle type. C. x contains a reference to a Circle object. D. You can assign an int value to x.
C. x contains a reference to a Circle object.
Given the declaration Circle[] x = new Circle[10], which of the following statement is most accurate? A. x contains an array of ten int values. B. x contains an array of ten objects of the Circle type. C. x contains a reference to an array and each element in the array can hold a reference to a Circle object. D. x contains a reference to an array and each element in the array can hold a Circle object.
C. x contains a reference to an array and each element in the array can hold a reference to a Circle object
A UML diagram does not contain _____. A. the class name B. the method names C. the field names D. object names
D. object names
What does the following UML diagram entry mean? + setHeight(h : double) : void A. this is a public attribute named Height and is a double data type B. this is a private method with no parameters and returns a double data type C. this is a private attribute named Height and is a double data type D. this is a public method with a parameter of data type double and does not return a value
D. this is a public method with a parameter of data type double and does not return a value
Data hiding, which means that critical data stored inside the object is protected from code outside the object is accomplished in Java by _____. A. using the public access specifier on the class methods B. using the private access specifier on the class methods C. using the private access specifier on the class definition D. using the private access specifier on the class fields
D. using the private access specifier on the class fields
You can always use the default constructor even though the non-default constructors are defined in the class. T or F
False
access specifier
Java keyword that indicates how a field or method can be accessed public or private
2 general capabilities of objects
can store data in fields can perform operations called methods
A collection of programming statements that specify the fields and methods that a particular type of object may have
class
Variables that are shared by every instances of a class are _____.
class variables
definition of a class
code that describes a particular type of object specifies the data an object can hold (fields) and actions an object can perform (methods) blueprint to create an object
special properties of constructors
have the same name as the class have no return type may not return any values typically public
this is a method that stores a value in a field or in some other way changes the value of a field
mutator
this key word causes an object to be created in memory
new
Rectangle object methods
setLength setWidth getLength getWidth getArea
When a local variable has the same name as a field, the local variable's name does this to the field's name.
shadows
when the value of an item is dependent on other data, and that item i not updated when the other data is changed, what has the value become?
stale