Unit 5 - Writing Classes 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
In starting a class,
note the instance variables, constructors, and methods
26
Comments
Ignored by compilers/ interpreters Help make code more readable Prevent execution when testing alternative code
27
Types of comment
``` // single line /* Multi line comment*/ /** Documentation *comment *to create Javadoc */ ```
28
Documentation comments are found in
Javadoc | Tags can be used in Javadoc
29
preconditions
comments for a method | must be TRUE for a method to work -> often a guarantee about a state of parameter
30
postconditions
comments for a method | must be TRUE after the execution of a code -> what is outcome or state of instance variables
31
Code should be written to
Meet the postconditions
32
Non-void methods
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
Accessor methods
Allow safe access to instance variables | Refer to as get methods
34
In order for a different class to access the instance/static variables, you need
accessor methods
35
Conditions of accessor methods
must be PUBLIC return type must MATCH the type of instance variable to be accessed name is often getNameOfVariable no parameters
36
public int getCalories () { return calories; } this is an example of
accessor method
37
toString method
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
If System.out.print or System.out.println is passed an object,
that object's toString method is called and returned String is printed
39
public String toString()
Always returns a string No parameters
40
When System.out.println (object) is called on an object in a different class
the toString method is called | returned String is printed
41
Void method
Does not return a value | Heading contains keyword void before method name
42
mutator method
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
What is necessary if there is any need for a different class to modify the instance variables?
Mutator method
44
Conditions of mutator method
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
public void setName(String n); this is an example of
Mutator methods
46
Accessors and mutator methods can be simplified into
object name.setAge(18); OR object name.getAge(); object name. method(parameters if needed);
47
Methods can only access the private data and methods of a parameter that is a reference to an object when
the parameter is the same type as method's enclosing class
48
non-void methods with parameters receive values
through parameters, use these values, and return a computed value of specified type
49
Class
template that defines the data through instance variables and behaviors through methods of an object
50
Object
Instance of a class
51
Method header
``` Consists of 5 parts Access level Ownership Return type Identifier Parameter list ```
52
Access level
set by access modifier | can be public or private
53
Ownership
Set by whether or not static is needed
54
Return type
Data type of values returned by method | can be primitive, reference, void
55
Identifier
name of method should be meaningful
56
Parameter list
enclose in parentheses, states the data type & identifier for each parameter used in method
57
Parameter
Information needed by method to complete its task
58
If methods does not use parameters,
parentheses are still needed but left empty
59
Instance of a class is considered mutable if the class contains a
mutator method
60
Good practice When actual parameter is a primitive value. formal parameter is
initialized with copy of that value
61
Changes to formal parameter have
no effect on corresponding actual parameter
62
Static methods
``` Associated with class, not objects of class Include keyword static in header before method name ```
63
Static methods are not able to
access or change values of instance variables | Unable to use class' instance variable or call non-static method
64
static variable
``` 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
Local variable
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
When there is a local variable with same name as instance variable, variable name will refer to
local variable instead of instance variable
67
Formal parameters and variables declared in a method and constructor can only
be used within that method or constructor
68
Method decomposition
Programmer breaks down a large problem into smaller sub problems by creating methods to solve each individual sub problem
69
keyword this
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
If a class's data and behavior are unknown, it may not be
appropriate to use this keyword
71
When designing a class, programmers make | decisions about what data to make
accessible | and modifiable from an external class
72
Data can be either
accessible or modifiable, or it can be | both or neither.
73
When a mutable object is a constructor parameter, the instance variable should be
initialized with a copy of the referenced object.
74
In this way, the instance variable is not ... ,and methods are prevented from ...
an alias of the original object modifying the state of the original object.
75
When the return expression is a reference to an object, a copy of that reference is
returned, not a copy of the object
76
return keyword
used to return the flow of control to the point immediately following where the method or constructor was called.
77
When an actual parameter is a reference to an | object, the formal parameter is
initialized with a copy of that reference, not a copy of the object.
78
If the reference is to a mutable object,
the method or constructor can use this reference | to alter the state of the object.
79
Passing a reference parameter results in the | formal parameter and the actual parameter
being aliases. They both refer to the same object