Chapter 2 Flashcards

1
Q

Inner part of class consist of____

A
  1. Fields- store date persistently w/i an object
  2. Constructors- ensures that an object is set up properly when it is first created
  3. Methods- implement the behavior of an object; provide its functionality
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is another name for fields?

A

instance variables

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Field pattern

A
  • usually start with reserved word: private
  • they include a type name (such as int, String, Person, etc.)
  • include a user-chosen name for the field variable
  • end with semicolon
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Fields

A
  • Store values for an object
  • Define state of object
  • Use inspect to view state
  • some values change often
  • some change raraely
  • always define fields to be private
    ie:
    private int price;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

constructors

A
  • initialize the fields
  • have same name as class
  • close association w/fields
  • store initial values into the fields
  • external parameters values for this
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

For these constructors, why isn’t price set to 0?
{
price = cost;
balance = 0;
total = 0;
}

A

Because we don’t know what the ticket price will be for this project since it’s entered externally.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Passing data via parameter

A
  • parameters are another sort of variable
  • used to hold data
  • temporary messenger carrying data originating externally
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is formal parameter vs actual parameter

A

formal is parameter names such as cost in
TicketMachine(int cost)
while actual is the user supplied value like 500 if user entered that

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Assignment

A
  • values are stored into fields & other variables via assignment statements
    ie:
    variable = expression;
    price = cost;
  • a variable stores a single value, so any previous value is lost
  • types must match
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Methods

A
  • yellow boxes
  • 2 parts: header & body
  • always has parenthesis and no semicolons at end
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

method header

A
  • provides method’s signature
  • tells us:
    method name
    parameters it takes
    whether it returns a result
    it’s visibility to objects of other classes
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Method body

A
  • Encloses the method’s statements with curly brackets
  • contain:
    declarations- used to create additional, temp variable space
    statements- describe actions of the method
  • a set of declarations and statements b/t curly brackets is a block
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Accessor (get) Method

A
  • always has non void return type
  • returns value (result) of the type given in header
  • contains return statement
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

T or F: returning is the same as printing

A

F: returning is not printing!

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Returning a value

A
  • some info passed internally b/t 2 different parts of a program
  • 1 part requested info from object via method call & return value is the way the object has of passing the info back to caller
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Mutator method

A
  • Changes state of object- mutate
  • achieved through changing the value of 1 or more fields
    typically contain assignment statements
    often receive parameters
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

What kind of method is this code:
Public int getPrice()
{
return price;
}

A

Accessor

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

What kind of method is this:
public void insertMoney (int amount)
{
balance = balance + amount;
}

A

mutator

19
Q

set mutator methods

A
  • most basic
  • fields often have dedicated set mutator methods
  • simple distinctive form:
    void return type
    method name related to field name
    single parameter, w/same type as the type of the field
    single assignment statement
20
Q

Local variables

A
  • similar to field variables
  • never have “private” or public”
  • lifetime is at method execution- created when method is called & destroyed when method finishes
21
Q

Local variable are defined within a method

A
  • short lived like parameters
  • the method sets their values- unlike parameters they do not receive external values
  • used for temp calc and storage
  • only accessible w/i the method
22
Q

What is this code doing?
int amountToRefund = balance;

A

declaring the variable amountToRefund and giving it an initial value
- it is common to initialize local variables W/I their declaration

23
Q

What is this?
int amountToRefund;

A

local variable

24
Q

What is the difference in scope between a local variable and field?

A
  • A scope of a local variable is the block in which its declared while scope of fields is its whole class
  • lifetime of LV is time of execution of the block in which its declared while lifetime of field if the lifetime of its containing object
25
Q

What are the different kinds of variables

A

fields
parameters
local variables

26
Q

T or F: Java keywords never contain uppercase letters

A

True

27
Q

Field pattern

A
  • usually start with reserved word: private
  • they include a type name (such as int, String, Person, etc.)
  • include a user-chosen name for the field variable
  • end with semicolon
28
Q

T or F: all fields are automatically initialized to a default value if they are not explicitly initialized

A

True
so integers are automatically set to 0

29
Q

Constructor space of the object (or method space for methods)

A
  • Used to provide space to store the values for the constructor’s parameters.
  • Created only when constructor executes
30
Q

What is scope?

A
  • The section of source code from which the variable can be accessed
  • scope of parameter is restricted to the body of the constructor/method in which it is declared
  • scope of field is the whole class definition so it can be accessed from anywhere in the same class
31
Q

lifetime

A
  • describes how long the variable continues to exist before it is destroyed
  • lifetime of parameter is limited to a single call of a constructor/method
  • lifetime of field is the same as lifetime of the object to which it belongs
32
Q

Effect of mutator

A

an object will often exhibit slightly different behavior before and after it is called

33
Q

What compound assignment operator is used when adding an amount to the value in a variable?

A

+=
balance + = amount;

34
Q

what is the basic form of a call to println?

A

System.out.println(something-we-want-to-print);

35
Q

What are the 3 components in this parameter?
System.out.println(“# “ + price + “ cents.”);

A
  • the string literal: “# “ (note the space after the hash)
  • the value of the price field (note that there are no quotes around the field name because we want the field’s value, not its name)
    -the string literal: “ cents.” ( note the space character before the word)
36
Q

_________ “+” is a string-concatenation operator

A
  • when used between a string and anything else
  • ie., it concatenate/joins strings together to create a new string
37
Q

Non-void vs void return type

A

-non-void: method will return some data to the place it was called from- and that data will be used in the caller for further calcs or program manipulations
-void: return nothing

38
Q

conditional statement

A

-Takes 1 of 2 possible actions based upon the result of a test.
-aka if-statements
-general form:
if(perform some test that gives a t/f result) {
Do the statements here if the test gave a true result
}
else {
Do the statements here if the test gave a false result
}

39
Q

Scope highlighting

A

colored boxes around some elements, such as methods and if-statements.

40
Q

T or F: Fields are used to store data that persist throughout the life of a method. As such, they maintain the current state of an object. They have a lifetime that lasts as long as their method lasts.

A

False: Fields are used to store data that persist throughout the life of an object. As such, they maintain the current state of an object. They have a lifetime that lasts as long as their object lasts.

41
Q

T or F: Formal parameters have a scope that is limited to their defining constructor or method.

A

True: Formal parameters have a scope that is limited to their defining constructor or method.

42
Q

T or F: Fields are defined inside constructors and methods.

A

False: Fields are defined outside constructors and methods.

43
Q

T or F: Java requires that every variable be given a type before it can be used.

A

True: Java requires that every variable be given a type before it can be used.