Unit 5 - Writing Classes Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

Access modifiers

A

Affect the access of classes, data, constructors, and methods

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

keyword private

A

Restricts access to declaring class

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

keyword public

A

Allows access from classes outside the declaring class

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

In APCSA, classes and constructors are designated as

A

public

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

In APCSA, instance variables are designated as

A

private

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

In APCSA, methods can be designated as either

A

private or public

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

Computer science creates models of

A

Things that exist in the real world
Blueprints -> class
Using the class to create instances -> objects
Attributes of objects -> instance variables
Behaviors of objects -> methods

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q
public Snack () {
      name = "";
      calories = 0;
}

This is classified as a

A

Default constructor

No values in parameter list

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q
public Snack (String n, int c) {
      name = n;
      calories = c;
}
This is classified as a
A

Overloaded constructor

Values in parameter list

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

A class can have what features

A

Private instance variables
Public constructors - default, overloaded
Methods - accessor, mutator

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

Object in another class can call a public method in another class but can’t call a

A

PRIVATE method in another class

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

Encapusulation

A

Wrapping the data (variables & code) that acts on dat (methods) in one unit (class)

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

We perform encapusulation by

A
Writing a class
Declaring instance variables as private
Providing access & modifier methods
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Why make instance variables private?

A

Restrict access to read-only

Option to provide validation checks

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

Object’s state

A

Its attributes & their values at a given time
Defined by instance variables belonging to object
Creates a “has-a” relationship between object and its instance variables

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

Constructors

A

Used to set initial state of object which should include initial values for all instance variables

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

Constructor used to set state depends on the way

A

That the object is instantiated

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

Only one constructor can be used to set

A

Initial states of instance variables

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

Sport tbh = new Sports();

This requires which type of constructor?

A

Default constructor

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

Sport wp = new Sport (“Water Polo”, 14);

This requires which type of constructor?

A

Overloaded constructor

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

Does all instance variables need to be set by parameters?

A

No

In some constructors, a portion of instance variables can be set in code body while the rest in the parameter list.

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

Parameters in constructors are

A

Local variables only to constructor to which they are sent

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

If no constructor is provided, Java provides a

A

default constructor in which all instance variables are set to default values

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

The default constructor provided by Java set each data type to what default values

A

int - 0
double - 0.0
Strings/ other - null

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

In starting a class,

A

note the instance variables, constructors, and methods

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

Comments

A

Ignored by compilers/ interpreters
Help make code more readable
Prevent execution when testing alternative code

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

Types of comment

A
// single line
/* Multi line
comment*/
/** Documentation
   *comment
   *to create Javadoc
   */
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
28
Q

Documentation comments are found in

A

Javadoc

Tags can be used in Javadoc

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

preconditions

A

comments for a method

must be TRUE for a method to work -> often a guarantee about a state of parameter

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

postconditions

A

comments for a method

must be TRUE after the execution of a code -> what is outcome or state of instance variables

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

Code should be written to

A

Meet the postconditions

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

Non-void methods

A

Returns a single value
Header includes the return type in place of keyword void
Return expression compatible with return type is evaluated & copy of that value is returned

33
Q

Accessor methods

A

Allow safe access to instance variables

Refer to as get methods

34
Q

In order for a different class to access the instance/static variables, you need

A

accessor methods

35
Q

Conditions of accessor methods

A

must be PUBLIC
return type must MATCH the type of instance variable to be accessed
name is often getNameOfVariable
no parameters

36
Q

public int getCalories () {
return calories;
}
this is an example of

A

accessor method

37
Q

toString method

A

Overridden methods that is included in classes to provide a description of a specific object

Generally includes what values are stored in instance data of object

38
Q

If System.out.print or System.out.println is passed an object,

A

that object’s toString method is called and returned String is printed

39
Q

public String toString()

A

Always returns a string

No parameters

40
Q

When System.out.println (object) is called on an object in a different class

A

the toString method is called

returned String is printed

41
Q

Void method

A

Does not return a value

Heading contains keyword void before method name

42
Q

mutator method

A

Void method that changes the values of instance variables or static variables

Referred to as set methods

Allow the change of values for instance variables outside of class

43
Q

What is necessary if there is any need for a different class to modify the instance variables?

A

Mutator method

44
Q

Conditions of mutator method

A

Must be PUBLIC
return type must be VOID
name is often setNameOfVariable
parameter type must match the type of instance variable to be modified

45
Q

public void setName(String n);

this is an example of

A

Mutator methods

46
Q

Accessors and mutator methods can be simplified into

A

object name.setAge(18);
OR
object name.getAge();

object name. method(parameters if needed);

47
Q

Methods can only access the private data and methods of a parameter that is a reference to an object when

A

the parameter is the same type as method’s enclosing class

48
Q

non-void methods with parameters receive values

A

through parameters, use these values, and return a computed value of specified type

49
Q

Class

A

template that defines the data through instance variables and behaviors through methods of an object

50
Q

Object

A

Instance of a class

51
Q

Method header

A
Consists of 5 parts
Access level
Ownership
Return type
Identifier
Parameter list
52
Q

Access level

A

set by access modifier

can be public or private

53
Q

Ownership

A

Set by whether or not static is needed

54
Q

Return type

A

Data type of values returned by method

can be primitive, reference, void

55
Q

Identifier

A

name of method should be meaningful

56
Q

Parameter list

A

enclose in parentheses, states the data type & identifier for each parameter used in method

57
Q

Parameter

A

Information needed by method to complete its task

58
Q

If methods does not use parameters,

A

parentheses are still needed but left empty

59
Q

Instance of a class is considered mutable if the class contains a

A

mutator method

60
Q

Good practice When actual parameter is a primitive value. formal parameter is

A

initialized with copy of that value

61
Q

Changes to formal parameter have

A

no effect on corresponding actual parameter

62
Q

Static methods

A
Associated with class, not objects of class
Include keyword static in header before method name
63
Q

Static methods are not able to

A

access or change values of instance variables

Unable to use class’ instance variable or call non-static method

64
Q

static variable

A
Belong to class with all objects of a class sharing a single static variable
Can be designated as either public or private
Designated with static before method name
Used with class name & dot operator
65
Q

Local variable

A

Can be declared in body of constructors & methods
May only be used within constructor and method
Can’t be declared to be public or private

66
Q

When there is a local variable with same name as instance variable, variable name will refer to

A

local variable instead of instance variable

67
Q

Formal parameters and variables declared in a method and constructor can only

A

be used within that method or constructor

68
Q

Method decomposition

A

Programmer breaks down a large problem into smaller sub problems by creating methods to solve each individual sub problem

69
Q

keyword this

A

Within a non-static method or constructor, a reference to current object whose method/ constructor is being called
Can be used to pass current object as an actual parameter in a method call

70
Q

If a class’s data and behavior are unknown, it may not be

A

appropriate to use this keyword

71
Q

When designing a class, programmers make

decisions about what data to make

A

accessible

and modifiable from an external class

72
Q

Data can be either

A

accessible or modifiable, or it can be

both or neither.

73
Q

When a mutable object is a constructor parameter, the instance variable should be

A

initialized with a copy of the referenced object.

74
Q

In this way, the instance variable is not …
,and methods are
prevented from …

A

an alias of the original object

modifying the state of the
original object.

75
Q

When the return expression is a reference to an object, a copy of that reference is

A

returned, not a copy of the object

76
Q

return keyword

A

used to return the flow
of control to the point immediately following
where the method or constructor was called.

77
Q

When an actual parameter is a reference to an

object, the formal parameter is

A

initialized with a copy of that reference, not a copy of the object.

78
Q

If the reference is to a mutable object,

A

the method or constructor can use this reference

to alter the state of the object.

79
Q

Passing a reference parameter results in the

formal parameter and the actual parameter

A

being aliases. They both refer to the same object