Week 2 Flashcards

1
Q

What is the purpose of the main method and what is it’s syntax?

A

The main method is the point of insertion for the program and where we will instantiate classes.

Syntax:

public static void main(String args[]) {

};

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

What is the essential class structure?

A
  1. Constants
  2. Instance variables (fields)
  3. Constructors
  4. Accessors and mutators (getters and setters)
  5. Methods (behaviours, arranged logically)
  6. Equals method
  7. Hashcode
  8. toString
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What are the basic properties of a constructor?

A
  • method name is the same as the class name
  • no return type NOT EVEN VOID!
  • initializes an object by assigning values to all the instance variables
  • uses arguments passed as external parameter values

remember, instance variables are declared in the class not in the constructor.

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

What are the important properties of instance variables?

A
  • they are declared at the top of a class after the CONSTANTS
  • initial values for instance variables are assigned in the constructor
  • the scope of instance variables is the entire class, all constructors and methods have access to them
  • instance variables are private knowledge unless you would like other classes to be able to use them (use “private” visibility modifier)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Key differences between local variables and instance variables?

A

local:
- declared inside methods
- compatible values must be assigned to them before using them

instance:
- declared inside the class (after CONSTANTS) and outside methods
- assigned values in the constructor
- default values are assigned if the constructor does not explicitly assign values to them

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

What are the responsibilities of the constructor?

A

the constructor is responsible for…
- ensuring instance variables are set to valid and logical initial values

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

What happens if we exclude a constructor from our class? what if we include multiple?

A

if no constructor…
- Java compiler will generate a default constructor and add it to the .class bytecode file
- the auto-generated default constructor will set all instance variables to their default values

if multiple…
- no default constructor is added
- constructors can call upon each other using the “this” keyword

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

What are some key properties of a Java object?

A
  • is an instance of a class
  • occupies a location in memory
  • maintains a state stored in its instance variables
  • can be accessed through a reference (object that stores the object’s address)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Briefly summarize the key points of encapsulation and information hiding

A

encapsulation:
- each class is responsible for exposing and managing its own state
- we put data with the methods that manage it

information hiding:
- wrap objects in a privacy barrier
- users only need to know how to use an object but not how the objects is able to do what it does

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

What are the visibility modifiers? Explain the scope of each.

A
  1. public
    - public members can be referenced anywhere within the same package
  2. private
    - private members can only be referenced within the same class
  3. protected (will learn later)
  4. package-default (will learn later)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What are the basic rules for using visibility modifiers with a class?

A
  1. INSTANCE VARIABLES MUST ALWAYS BE PRIVATE
  2. classes and (most) constructors should be PUBLIC
  3. methods that are part of the public interface (what the user will interact with) should be public, aka, what the class offers as a service/functionality
  4. methods that are part of private implementation (what the user doesn’t need to know about) should be private
  5. CONSTANTS may be private or public, depending on context
  6. static members may be private or public, depending on context
  7. local variables are implicitly private therefore no visibility modifier is needed for them
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

When should we use accessors and what is the syntax for accessors (getters)?

A

when to use…
- access private instance variables, we may write accessors for SOME instance variables

syntax…

public int getValue() {
return this.aValue;
}

usually come before mutators (setters)

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

When should we use mutators and what is the syntax for mutators (setters)?

A

when to use…
- change data stored in an object’s instance variables (change the state of an object), we may write mutators for SOME instance variables

syntax…

public void setInstanceVariableTo(<data> newValue) {
oldValue = newValue;
}</data>

usually come after accessors (getters)

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

What are some important facts to remember about using mutators?

A
  • use of mutators must be restricted
  • mutators must contain logic so the values of variables can be set to valid values
  • it is correct for mutators to do nothing if incorrect values are passed to it
  • mutator methods should only change the implicit argument
  • mutators should limit side effects (IO, modifying the params passed to it, modifying static variables, modifying non-local variable)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is the use of the toString method? What are some of its properties?

A
  • for developers not users
  • describe the state of an object
  • should contain all the values of instance variables
  • a call to println that passes a reference to an object automatically calls that object’s toString method
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Why do we use the keyword “this”?

A

we use “this” to remedy overshadowing

  • “this” means the object whose method is currently executing
  • we use “this” in methods to clarify that a variable or member belongs to the object whose code is executing
  • we can use “this” to invoke an alternative constructor
  • we use “this” when parameters and instance variables have the same name
  • we use “this” to improve readability
17
Q

What are some properties of a class’ interface and implementation?

A

Interface…
- a class’ public interface is composed of it’s public members
- we call a class’ public interface it’s application programming interface (API)
- we say that only a class’ API is visible and that we expose the interface
- class instances communicate with the system and each other through the API’s

Implementation…
- the system and other objects are clueless to the other object’s “inner workings”
- a well designed class hides how it achieves its functionality
- nobody needs to know how a class gets its job done

18
Q

What are the benefits of information hiding?

A
  • decouples components
  • system components can be developed and tested in isolation
  • we can develop in parallel as long as a interface is agreed upon before starting
  • maintenance becomes easier because classes are less tightly interconnected
19
Q

What are some rules we should follow to minimize scope, mutability, and accessability?

A
  1. minimize scope of local variables (don’t declare them until they are needed)
  2. make all instance variables private
  3. make all (or most) instance variables final and assign their (final!) values in the constructor
  4. don’t provide mutators for instance variables unless it is necessary
  5. mutator should only change the implicit argument
  6. don’t provide accessors to a mutable member because then it can be modified
  7. accessors should not have side effects
20
Q

What is responsibility-driven design (RDD)?

A

follows Abbot’s Heuristic…
- classes employ self governance: each class must be responsible for manipulating its own data
- RDD leads to high coherence (MORE connections WITHIN a class) and low coupling (LESS connections BETWEEN classes)
- localize change: when change is needed, as few classes as possible should be affected

  • each module of a system (class) must have only one responsibility (think of atomic functions from Python)
  • each module must perform that responsibility with minimal reliance on other modules (we call this coupling, we want MINIMAL COUPLING)

coupling increases when…
- A has an instance variables that refers to (is of type) B
- A uses object B
- A has a method that references B (via return type or parameter)
- A is a subclass of (or implements) class B

21
Q

What does UML stand for?

A

UML = unified modelling language

22
Q

What is a UML class diagram? What are the three key components?

A

similar to an entity relationship diagram it represents different classes (entities), their properties (instance variables), and their behaviours (constructors and methods)

three key components…
1. class name
2. attributes (+ for public and - for private)
3. methods (+ for public and - for private)

23
Q

What are functions in Java called?

A

methods

24
Q

What are some key details of methods? What is the syntax for a method header?

A
  • every method is inside a class (in Java nothing exists outside of a class)
  • a method is an instruction, verb, action, or function
  • Java methods accept input (parameters) and generate output (return value)
  • the name of a parameter in the method declaration is called a formal parameter

syntax (for method header):

public int methodName (final int firstIntParam, final int secondIntParam)

  • we include final in the parameters to remind ourselves that we must not modify the values passed as parameters to our method
25
Q

What are overloaded methods?

A

methods that have the same header signature but accept different parameters

26
Q

What is the syntax for variable length parameter lists?

A

public void aFunction (Integer… integers) {
for (Integer someNumber : integers) {
System.out.println(someNumber);
}
}

27
Q

What are some rules to follow when using variable length parameter lists?

A
  • type can be primitive or object
  • COME LAST in the formal arguments
  • a method cannot accept two sets of variable length parameter lists
  • constructors can also accept a variable length parameter list
28
Q
A