Level 89 Level 91
Level 90

[Quiz] Methods III

96 words 0 ignored

Ready to learn       Ready to review

Ignore words

Check the boxes below to ignore/unignore words, then click save at the bottom. Ignored words will never appear in any learning session.

All None

Great Internet Mersenne Prime Search project
Control Mechanisms (5)
Calls to methods, Iterative statements (loops), Conditional statmentes (if-else), switch statements, exceptions
Instruction Pointer
special register with address of instructions, incremented automatically, makes the program execute consecutive instructions
How many relationap operators?
it compares reference/addresses not values
What does useing == or != for objects do
String's equals method used for
comparing addresses of a word and a document
and, or
Binary Logical Operators
!(p && q)
!p || !q
!(p || q)
!p && !q
Which have higher precedence unary or binary operators?
binary arithmetic
Higher rank: binary arithmetic or relational
Higher rank: binary logical or arithmetic/relational
! ++ -- * / % + - < <= > >= == != && ||
List order of importance: && ! * + || < > == ++ -- / % <= =>
create fractions
What do constructors in the Fraction class do?
do things with fractions (ex. add, multiply)
What do methods in the Fraction class do?
a program component that uses a class is called a client of the class
Person who needs to know precisely how different features of the class are available and how to use them.
Person interested in which features of the class are available and how to use them.
public or private
Every field, constructor, and method in Fraction class is either:
What do "public" and "private" tell the compiler about the code?
if code in other classes is allowed to access a field or call a constructor/ method directly
within the class's own code
Private features of a class can be directly accessed where?
client classes (with an appropriate name-dot prefix)
Public features of a class can be accessed in?
What is "information hiding"?
providing as little information to clients as possible
Why make some features of a class private?
to control access to its fields by the class's clients and to hide its implementation details (makes maintenance easier)
If necessary, the developer provides a special _______ method that returns the value of a particular private field.
What are modifiers/ mutators?
methods that update private fields
that the class can be completely described to outsiders by its constructors and public methods
Making all of a class's fields private and making the helper methods private ensures what?
the class as a whole
Do public and private apply to the class as a whole or to individual objects of the class?
Why might one make a constructor private?
other constructors of the same class can call it
What are constructors?
procedures for creating objects of a class
same name as the class
A constructor always has what name?
Unlike methods, constructors...
do not return any values
no, not even void
Do constructors have a return type?
the class definition
All constructors are defined inside what?
What is the main task of a constructor?
to initialize all or some of the new object's fields
default values
Fields that are not explicitly initialized are set to what?
zero; false; null
What are the default values for numbers, booleans, and objects?
the number or types of their parameters
If a class has > 1 constructor, they must differ in:
What does this do:
takes no parameters and creates fraction 0/1
number, types, and orders
The numbers, types, and orders of parameters passed to the new operator must match the [3 things] expected by one of the constructors.
What happens if a class has no constructors?
the compiler supplies a default no-args constructor which allocates memory for the object and initializes fields to default values
Constructors should prevent what?
programs from creating invalid objects (ex. throwing IllegalArgumentException if denom = 0)
What happens if IllegalArgumentException is thrown?
program is aborted, Java interpreter displays error message
Keyword used in the body of a method definition when you need to refer to the object to which the method belongs.
the first statement
If a constructor calls this(...), the call must be where in the constructor's body?
yes (potential source of bugs)
Does Java let you use the class name for a method name?
a reference to an object of the corresponding type (its address in memory)
When you declare a variable of a class type (ex. private JButton go;), what does the variable hold?
currently the variable does not refer to a valid object
If the value of a variable is null, what does it indicate?
T/F: It is crucial to initialize a variable before using it.
true (ex. through the new operator or by setting the variable to a reference returned from a method)
literal strings and initialized arrays
In which 2 cases are objects not created with new?
What happens if you call a method for a reference that is equal to null? (private String errorMsg;)
System.out.println(new Fraction(12, 20));
making a temporary fraction 12/20
the value of the reference (not the object itself)
When the assignment operator (=) is applied to references, what does it copy?
After these statements:
the same Fraction object as f1
What do these statements do?
creates new Fraction object, a copy of f1
What is a method?
segment of code that implements a certain task/ calculation
Can a method take some parameters?
yes (ex. if a method is supposed to calculate the GCF of 2 integers, it needs to know their values)
return a value, complete a task
A method does either, or both, of these 2 things:
where the method can fetch them (ex. system stack)
When a method is called, where does the caller place parameters?
a place accessible to the method (ex. system stack)
Where is the return address saved in a method?
What happens when a method has finished?
returns control to place in the program from which it was called
What happens if a method returns a value?
it places that value into a location where the caller can retrieve it (ex. a particular CPU register)
When you define a method, what do you need to do?
give it a name, specify the types of its parameters, and assign names so you can refer to them in the method's code
The header of a method is called its signature. It provides information needed to invoke that method.
Examples of returntype?
any primitive data type: int, double, boolean, char, etc.; or any object defined in Java/ program: ex. String, Color, RollingDie, Fraction, etc.
Can returntype be void?
yes (method then performs task but doesn't return any value)
T/F: All methods have parameters.
false (some have only empty parenthesis)
What is common Java style for naming methods?
name that sounds like a verb; starts with lowercase letter with subsequent words capitalized
get... (ex. getWidth() )
If a method returns the value of a field, its name usually starts with:
set... (ex. setText (...) )
If a method sets the value of a field, its name usually starts with:
private int countToDegrees(int count, int total)
What is wrong with this:
What is the method's code called?
its body; placed within braces after the header
true (act pretty much as local variables)
T/F: The names of parameters matter only inside the method body.
keyword static is in its header
How is a static method designated?
the class as a whole; no
What does a static method belong to, and does it deal with instance variables?
How can a static method be called?
use the class's name and a dot as a prefix to the method's name (ex. x = Math.random(); )
(ex. obj.itsMethod(<parameters>);)
T/F: When we call a non-static (instance) method, we call it for:
implicit parameter
In effect, the object for which the non-static method is called becomes an _______ passed to the call.
How is a non-static field of another object (of the same class) accessed?
adding the object's name and a dot as a prefix (ex. int newNum= num * other.num;)
invokes Timer's constructor with two parameters
What does clock= new Timer(delay, this); do?
literal or symbolic constant, variable, expression that uses arithmetic operators/ casts/ calls to other methods
When a method is called or a constructor is invoked with a parameter of a primitive data type (int, double, etc.) what types can the parameter be?
Parameters of primitive data types are always passed how?
by value (changes to the copy will not change the original)
When you pass an object to a method or a constructor, what is passed?
a copy of the reference to the original method (method can reach/ manipulate the original object through that reference)
What does it mean for an object to be immutable?
no public methods can change the instance variables of the object (once created, the object can not change)
How is the add method in Fraction immutable?
it creates a new fraction equal to the sum of this and other, and returns a reference to it
returns something to the caller
A method that is not void...
format of a return statement
return <expression>; (expression must be of the method's specified return type or something that can be converted into it)
int, double, boolean, etc.
Examples of primitive type
a value of that type
If the return type is a primitive type, then what does the method return?
The return expression in a boolean method can include what?
a constant (true or false), a boolean variable, or a boolean expression
the method returns a reference to an object of that class
What does it mean that a method can also have a return type of some class?
What does a return statement do?
tells the method what value to return to the caller, quits the method code, and passes control back to the caller
If a method has several return statements, all but one of them must be where?
inside a conditional statement (or inside a case in switch) (otherwise there will be unreachable code after the unconditional return)
Can a void method have a return?
yes, but without any value (just as a command to quit the method)
T/F: Methods in different classes may have the same names.
true (a method is always called for a particular object or class so there is no confusion)
T/F: Methods of the same class may have the same name.
true (as long as the number/ types of their parameters are different)