unit 2 Flashcards
Difference between a parameter and an argument (formal parameter vs. actual parameter)
What kind of operation does an object method use?
dot operation
can you use == and < or > on string types?
no, you need to use equals or compareTo
Fields are assigned _____ if they are not EXPLICITLY INITIALIZED in a constructor
the default value for their type
int holds one number, array holds many, universal data structure
yes
What information type do math class methods such as Math.sqrt(); or Math.random() yield?
double
accessor methods do not have parameters
(true/false)
TRUEEEEEEE SO TRUE SO TRUE
private double m;
public double getSlope(double m)
{
(IMPLEMENTATION NOT SHOWN)
}
————————-
work? why/why not?
you cannot pass what you dont have initialized as a value. we don’t know PARAMETER m, even if we know FIELD m. thus, we cannot pass parameter m (because parameters within METHODS must be known)
Consider the following description of the Thing class which includes two constructors.
public Thing() – Constructs a Thing object that uses a default value to represent a color
public Thing(String setColor) – Constructs a Thing object that uses setColor to represent a color
Which of the following code segments, when appearing in a class other than Thing, will create an instance variable of a Thing object with a value of null ?
A
private Thing someThing = new Thing(“Green”);
B
private Thing someThing = new Thing(“”);
C
private Thing someThing = new Thing();
D
private Thing someThing;
E
private Thing(“Green”);
D, as all other options would result in a successfully declared and initialized object except E (which would just result in a full compilation error, so someThing would be neither declared or initialized in this case). we want something to be declared, but not initialized, thus returning the default for the type, or a null value.
Consider the following description of the VetRecord class which includes two constructors.
public VetRecord(String name, int age, int weight, boolean needsVac) – Constructs a VetRecord object that represents a vet record for a pet with name name, age age, weight weight, and whether they need to be vaccinated needsVac.
public VetRecord(String name, int age, int weight) – Constructs a VetRecord object that represents a vet record for a pet with name name, age age, and weight weight.
A new constructor is to be added to the VetRecord class. Which of the following is NOT a possible header for the new constructor?
A
VetRecord(int age, int weight)
B
VetRecord(int age, boolean needsVac)
C
VetRecord(String name, int age)
D
VetRecord(String name, boolean needsVac)
E
VetRecord(String name, int weight, int age)
E, as there is already a constructor with parameters in order of: string, int, int. thus, if this were added then it would cause a “compile-time” (syntax) error
Consider the following description of the MagicNumber class which includes one constructor and two methods.
public MagicNumber()– Constructs a MagicNumber object that represents a number
public void displayNumber()– Displays the number to the screen
public void add_2()– Increases the number by 2
When located in a method in a class other than MagicNumber, which of the following code segments will compile without error?
- MagicNumber.add_2();
MagicNumber.displayNumber(); - MagicNumber n1 = new MagicNumber();
n1.add_2();
n1.displayNumber(); - n2.add_2();
n2.displayNumber();
A
I only
B
II only
C
III only
D
II and III only
E
None of the code segments will compile.
B, II only, as though option 1 calls a nonstatic method successfully through dot notation, it does not actually construct an object of the MagicNumber class to do anything with. 3 does the same, using dot notation on a nonexistent object. Option 2, however, successfully declares an object variable to be manipulated.
What does the dot operator do?
“invokes a nonstatic method of a class onto an object of that same class,” or uses the method that a class creates that isn’t static (shared by all objects, belonging to the class and not a specific instance of that class(instance of a class = object))
in the methods:
public void methodA(int arg) – Calls methodB with the value of arg * 10
public void methodB(int arg) – Displays the value of arg + 10
if methodA(4) were called, what would happen?
methodA would execute with arg value of 4, which would THEN call methodB with the result and continue on
Why would you use a wrapper?
to give a primitive type to a method that wants an object
How do you call a method?
.method for methods OUTSIDE of the class/being called in the main method
method() for methods within the same class