Add second year

This commit is contained in:
2023-12-07 01:19:12 +00:00
parent 3291e5c79e
commit 3d12031ab8
1168 changed files with 431409 additions and 0 deletions

View File

@ -0,0 +1,194 @@
- #[[CT2106 - Object-Oriented Programming]]
- **Previous Topic:** [[Introduction to Java]]
- **Next Topic:** [[More Java Code]]
- **Relevant Slides:** ![Lecture02.pdf](../assets/Lecture02_1663059993088_0.pdf)
-
- What is the **structure of a class**? #card
card-last-interval:: -1
card-repeats:: 1
card-ease-factor:: 2.5
card-next-schedule:: 2022-09-19T23:00:00.000Z
card-last-reviewed:: 2022-09-19T18:29:57.359Z
card-last-score:: 1
- Every class has the following structure:
- ```java
public class ClassName
{
Fields
Constructors
Methods
}
```
- ## Fields
- What are **Fields**? #card
card-last-interval:: 4
card-repeats:: 2
card-ease-factor:: 2.36
card-next-schedule:: 2022-09-22T14:55:21.827Z
card-last-reviewed:: 2022-09-18T14:55:21.828Z
card-last-score:: 3
- **Fields**, also known as **instance variables**, store values for an object.
- Fields define the state of an object.
- In BlueJ, use *Inspect* to view the state.
- Some values change frequently, others rarely, or not at all.
- ## Encapsulation
- What is **Encapsulation**? #card
card-last-interval:: 4
card-repeats:: 2
card-ease-factor:: 2.22
card-next-schedule:: 2022-09-23T18:22:08.303Z
card-last-reviewed:: 2022-09-19T18:22:08.303Z
card-last-score:: 3
- In **encapsulation**, the ^^variables of a class will be hidden from other classes^^ and can only be accessed through the methods of their current class.
- This is also known as **data hiding**.
- Why use encapsulation? #card
card-last-interval:: 4
card-repeats:: 2
card-ease-factor:: 2.22
card-next-schedule:: 2022-09-23T18:23:15.213Z
card-last-reviewed:: 2022-09-19T18:23:15.213Z
card-last-score:: 3
- In OOP, ^^each object is responsible for its own data.^^
- This allows an object to have greater control over which data is available to be viewed externally, and how external objects can mutate the object's state.
- ### Encapsulation Type: Private
- What is the effect of making a field **private**? #card
card-last-interval:: 4
card-repeats:: 2
card-ease-factor:: 2.46
card-next-schedule:: 2022-09-23T18:25:31.450Z
card-last-reviewed:: 2022-09-19T18:25:31.450Z
card-last-score:: 5
- Making a field **private** encapsulates their values inside their object.
- No external class or object can access a private field.
-
- ## Constructors
- What are **constructors**? #card
card-last-interval:: 3.02
card-repeats:: 2
card-ease-factor:: 2.36
card-next-schedule:: 2022-09-22T18:30:51.722Z
card-last-reviewed:: 2022-09-19T18:30:51.722Z
card-last-score:: 3
- Constructors:
- Initialise an object.
- Have the same name as their class.
- Have a close association with the fields:
- They contain the initial values stored in the fields.
- They contain the parameter values often used for these.
- What is the point of the keyword `this`? #card
card-last-score:: 5
card-repeats:: 2
card-next-schedule:: 2022-09-22T15:21:45.332Z
card-last-interval:: 4
card-ease-factor:: 2.7
card-last-reviewed:: 2022-09-18T15:21:45.332Z
- If the input parameter variables in your constructor have the **same name** as your fields, you must use the `this` keyword to distinguish between the two.
- `this` = "belonging to this object".
- E.g.,
- ```java
public Bicycle(int speed, int gear, int cadence)
{
this.speed = speed;
this.gear = gear;
this.cadence = cadence;
}
```
-
- ## Methods
- What are **methods**? #card
card-last-interval:: 4
card-repeats:: 2
card-ease-factor:: 2.22
card-next-schedule:: 2022-09-23T18:06:15.300Z
card-last-reviewed:: 2022-09-19T18:06:15.300Z
card-last-score:: 3
- **Methods** implement the *behaviour* of an object.
- They have a consistent structure comprised of a *header* and a *body*.
- ### Accessor Methods
- What are **accessor** methods? #card
card-last-interval:: -1
card-repeats:: 1
card-ease-factor:: 2.36
card-next-schedule:: 2022-09-19T23:00:00.000Z
card-last-reviewed:: 2022-09-19T18:22:14.771Z
card-last-score:: 1
- **Accessor** methods provide information about the state of an object.
- An accessor method always returns a type that is **not** `void`.
- An accessor method returns a value (*result*) of the type given in the **header**.
- The method will contain a **return** statement to return the value.
- ### Mutator Methods
- What are **mutator** methods? #card
card-last-interval:: 4
card-repeats:: 2
card-ease-factor:: 2.46
card-next-schedule:: 2022-09-23T18:23:53.666Z
card-last-reviewed:: 2022-09-19T18:23:53.666Z
card-last-score:: 3
- **Mutator** methods alter (*mutate*) the state of an object.
- Achieved through changing the value of one or more fields.
- They typically contain one or more *assignment* statements.
- Often receive parameters.
- ![image.png](../assets/image_1663063179688_0.png)
- ### Mutator Methods: Set
- Each field may have a dedicated **set** mutator method.
- These have a simple, distinctive form:
- **void** return type
- method name related to the field name
- a single formal parameter, with the same type as the type of the field
- a single assignment statement
- A typical "set" method:
- ```java
public void setGear (int number)
{
gear = number;
}
```
- ### Protector Mutators
- A set method does not always have to assign unconditionally to the field.
- The parameter may be checked for validity and rejected if innappropriate.
- Mutators thereby protect fields.
- Mutators also support *encapsulation*.
- #### Protecting a Field
- ```java
public void setGear (int gearing)
{
// this conditional statement prevents innapropriate action.
// if protects the "gear" field from values that are too large or too small.
if (gearing >= 1 && gearing <= 18)
{
gear = gearing;
}
else
{
System.out.println("Exceeds maximum gear ratio. Gear not set");
}
}
```
- ### Method Structure
- The **header**:
- The head tells us:
- the *visibility* of the method to objects of other class.
- whether or not the method *returns a result*.
- the *name* of the method.
- whether or not the method takes *parameters*.
- E.g.,
- ```java
public int getSpeed()
```
- The **body** encloses the method's *statements*.
-
- ## C vs Java
- Unlike C, an OOP program will **not** have a pool of global variables that each method can access.
- Instead, ^^each object has its own data^^, and other objects rely on the *accessor* methods of the object to access the data.
-
- ## Conditional Statements
- Conditional statements in Java have the same format as in C.
- ```java
if (condition) {
do something;
}
else {
do somethingElse;
}
```
- ![image.png](../assets/image_1663063508214_0.png)

View File

@ -0,0 +1,196 @@
- #[[CT2106 - Object-Oriented Programming]]
- **Previous Topic:** [[Introduction to Java]]
- **Next Topic:** [[More Java Code]]
- **Relevant Slides:** ![Lecture02.pdf](../assets/Lecture02_1663059993088_0.pdf)
-
- What is the **structure of a class**? #card
card-last-interval:: 2.3
card-repeats:: 2
card-ease-factor:: 2.36
card-next-schedule:: 2022-10-02T16:26:57.406Z
card-last-reviewed:: 2022-09-30T09:26:57.407Z
card-last-score:: 3
- Every class has the following structure:
- ```java
public class ClassName
{
Fields
Constructors
Methods
}
```
- ## Fields
- What are **Fields**? #card
card-last-interval:: 8.76
card-repeats:: 3
card-ease-factor:: 2.46
card-next-schedule:: 2022-10-09T06:15:08.025Z
card-last-reviewed:: 2022-09-30T12:15:08.025Z
card-last-score:: 5
- **Fields**, also known as **instance variables**, store values for an object.
- Fields define the state of an object.
- In BlueJ, use *Inspect* to view the state.
- Some values change frequently, others rarely, or not at all.
- ## Encapsulation
- What is **Encapsulation**? #card
card-last-interval:: -1
card-repeats:: 1
card-ease-factor:: 2.22
card-next-schedule:: 2022-10-03T23:00:00.000Z
card-last-reviewed:: 2022-10-03T11:44:39.961Z
card-last-score:: 1
- In **encapsulation**, the ^^variables of a class will be hidden from other classes^^ and can only be accessed through the methods of their current class.
- This is also known as **data hiding**.
- Why use encapsulation? #card
card-last-interval:: 8.32
card-repeats:: 3
card-ease-factor:: 2.08
card-next-schedule:: 2022-10-08T15:30:57.356Z
card-last-reviewed:: 2022-09-30T08:30:57.357Z
card-last-score:: 3
- In OOP, ^^each object is responsible for its own data.^^
- This allows an object to have greater control over which data is available to be viewed externally, and how external objects can mutate the object's state.
- ### Encapsulation Type: Private
- What is the effect of making a field **private**? #card
card-last-interval:: 10.24
card-repeats:: 3
card-ease-factor:: 2.56
card-next-schedule:: 2022-10-10T13:30:49.649Z
card-last-reviewed:: 2022-09-30T08:30:49.650Z
card-last-score:: 5
- Making a field **private** encapsulates their values inside their object.
- No external class or object can access a private field.
-
- ## Constructors
- What are **constructors**? #card
card-last-interval:: 10.56
card-repeats:: 3
card-ease-factor:: 2.46
card-next-schedule:: 2022-10-14T00:41:02.783Z
card-last-reviewed:: 2022-10-03T11:41:02.784Z
card-last-score:: 5
- Constructors:
- Initialise an object.
- Have the same name as their class.
- Have a close association with the fields:
- They contain the initial values stored in the fields.
- They contain the parameter values often used for these.
- What is the point of the keyword `this`? #card
card-last-score:: 5
card-repeats:: 3
card-next-schedule:: 2022-10-14T15:39:43.016Z
card-last-interval:: 11.2
card-ease-factor:: 2.8
card-last-reviewed:: 2022-10-03T11:39:43.018Z
- The `this` keyword refers to the current object in a method or constructor.
- The most common use of `this` is to distinguish between class attributes & parameters of the same name.
- If the input parameter variables in your constructor have the **same name** as your fields, you must use the `this` keyword to distinguish between the two.
- `this` = "belonging to this object".
- E.g.,
- ```java
public Bicycle(int speed, int gear, int cadence)
{
this.speed = speed;
this.gear = gear;
this.cadence = cadence;
}
```
-
- ## Methods
- What are **qmethods**? #card
card-last-score:: 3
card-repeats:: 3
card-next-schedule:: 2022-10-08T19:10:46.142Z
card-last-interval:: 8.32
card-ease-factor:: 2.08
card-last-reviewed:: 2022-09-30T12:10:46.142Z
- **Methods** implement the *behaviour* of an object.
- They have a consistent structure comprised of a *header* and a *body*.
- ### Accessor Methods
- What are **accessor** methods? #card
card-last-interval:: 4
card-repeats:: 2
card-ease-factor:: 2.46
card-next-schedule:: 2022-10-04T09:21:53.359Z
card-last-reviewed:: 2022-09-30T09:21:53.359Z
card-last-score:: 5
- **Accessor** methods provide information about the state of an object.
- An accessor method always returns a type that is **not** `void`.
- An accessor method returns a value (*result*) of the type given in the **header**.
- The method will contain a **return** statement to return the value.
- ### Mutator Methods
- What are **mutator** methods? #card
card-last-interval:: 10.24
card-repeats:: 3
card-ease-factor:: 2.56
card-next-schedule:: 2022-10-13T19:27:22.160Z
card-last-reviewed:: 2022-10-03T14:27:22.161Z
card-last-score:: 5
- **Mutator** methods alter (*mutate*) the state of an object.
- Achieved through changing the value of one or more fields.
- They typically contain one or more *assignment* statements.
- Often receive parameters.
- ![image.png](../assets/image_1663063179688_0.png)
- ### Mutator Methods: Set
- Each field may have a dedicated **set** mutator method.
- These have a simple, distinctive form:
- **void** return type
- method name related to the field name
- a single formal parameter, with the same type as the type of the field
- a single assignment statement
- A typical "set" method:
- ```java
public void setGear (int number)
{
gear = number;
}
```
- ### Protector Mutators
- A set method does not always have to assign unconditionally to the field.
- The parameter may be checked for validity and rejected if innappropriate.
- Mutators thereby protect fields.
- Mutators also support *encapsulation*.
- #### Protecting a Field
- ```java
public void setGear (int gearing)
{
// this conditional statement prevents innapropriate action.
// if protects the "gear" field from values that are too large or too small.
if (gearing >= 1 && gearing <= 18)
{
gear = gearing;
}
else
{
System.out.println("Exceeds maximum gear ratio. Gear not set");
}
}
```
- ### Method Structure
- The **header**:
- The head tells us:
- the *visibility* of the method to objects of other class.
- whether or not the method *returns a result*.
- the *name* of the method.
- whether or not the method takes *parameters*.
- E.g.,
- ```java
public int getSpeed()
```
- The **body** encloses the method's *statements*.
-
- ## C vs Java
- Unlike C, an OOP program will **not** have a pool of global variables that each method can access.
- Instead, ^^each object has its own data^^, and other objects rely on the *accessor* methods of the object to access the data.
-
- ## Conditional Statements
- Conditional statements in Java have the same format as in C.
- ```java
if (condition) {
do something;
}
else {
do somethingElse;
}
```
- ![image.png](../assets/image_1663063508214_0.png)

View File

@ -0,0 +1,196 @@
- #[[CT2106 - Object-Oriented Programming]]
- **Previous Topic:** [[Introduction to Java]]
- **Next Topic:** [[More Java Code]]
- **Relevant Slides:** ![Lecture02.pdf](../assets/Lecture02_1663059993088_0.pdf)
-
- What is the **structure of a class**? #card
card-last-interval:: 9.84
card-repeats:: 3
card-ease-factor:: 2.46
card-next-schedule:: 2022-10-16T13:11:17.984Z
card-last-reviewed:: 2022-10-06T17:11:17.985Z
card-last-score:: 5
- Every class has the following structure:
- ```java
public class ClassName
{
Fields
Constructors
Methods
}
```
- ## Fields
- What are **Fields**? #card
card-last-interval:: 8.76
card-repeats:: 3
card-ease-factor:: 2.46
card-next-schedule:: 2022-10-09T06:15:08.025Z
card-last-reviewed:: 2022-09-30T12:15:08.025Z
card-last-score:: 5
- **Fields**, also known as **instance variables**, store values for an object.
- Fields define the state of an object.
- In BlueJ, use *Inspect* to view the state.
- Some values change frequently, others rarely, or not at all.
- ## Encapsulation
- What is **Encapsulation**? #card
card-last-interval:: 4
card-repeats:: 2
card-ease-factor:: 2.32
card-next-schedule:: 2022-10-11T10:23:51.445Z
card-last-reviewed:: 2022-10-07T10:23:51.446Z
card-last-score:: 5
- In **encapsulation**, the ^^variables of a class will be hidden from other classes^^ and can only be accessed through the methods of their current class.
- This is also known as **data hiding**.
- Why use encapsulation? #card
card-last-interval:: -1
card-repeats:: 1
card-ease-factor:: 2.08
card-next-schedule:: 2022-10-09T23:00:00.000Z
card-last-reviewed:: 2022-10-09T08:51:16.259Z
card-last-score:: 1
- In OOP, ^^each object is responsible for its own data.^^
- This allows an object to have greater control over which data is available to be viewed externally, and how external objects can mutate the object's state.
- ### Encapsulation Type: Private
- What is the effect of making a field **private**? #card
card-last-interval:: 10.24
card-repeats:: 3
card-ease-factor:: 2.56
card-next-schedule:: 2022-10-10T13:30:49.649Z
card-last-reviewed:: 2022-09-30T08:30:49.650Z
card-last-score:: 5
- Making a field **private** encapsulates their values inside their object.
- No external class or object can access a private field.
-
- ## Constructors
- What are **constructors**? #card
card-last-interval:: 10.56
card-repeats:: 3
card-ease-factor:: 2.46
card-next-schedule:: 2022-10-14T00:41:02.783Z
card-last-reviewed:: 2022-10-03T11:41:02.784Z
card-last-score:: 5
- Constructors:
- Initialise an object.
- Have the same name as their class.
- Have a close association with the fields:
- They contain the initial values stored in the fields.
- They contain the parameter values often used for these.
- What is the point of the keyword `this`? #card
card-last-score:: 5
card-repeats:: 3
card-next-schedule:: 2022-10-14T15:39:43.016Z
card-last-interval:: 11.2
card-ease-factor:: 2.8
card-last-reviewed:: 2022-10-03T11:39:43.018Z
- The `this` keyword refers to the current object in a method or constructor.
- The most common use of `this` is to distinguish between class attributes & parameters of the same name.
- If the input parameter variables in your constructor have the **same name** as your fields, you must use the `this` keyword to distinguish between the two.
- `this` = "belonging to this object".
- E.g.,
- ```java
public Bicycle(int speed, int gear, int cadence)
{
this.speed = speed;
this.gear = gear;
this.cadence = cadence;
}
```
-
- ## Methods
- What are **methods**? #card
card-last-score:: 3
card-repeats:: 4
card-next-schedule:: 2022-10-24T09:51:35.296Z
card-last-interval:: 15.05
card-ease-factor:: 1.94
card-last-reviewed:: 2022-10-09T08:51:35.296Z
- **Methods** implement the *behaviour* of an object.
- They have a consistent structure comprised of a *header* and a *body*.
- ### Accessor Methods
- What are **accessor** methods? #card
card-last-interval:: 10.24
card-repeats:: 3
card-ease-factor:: 2.56
card-next-schedule:: 2022-10-17T15:19:14.037Z
card-last-reviewed:: 2022-10-07T10:19:14.038Z
card-last-score:: 5
- **Accessor** methods provide information about the state of an object.
- An accessor method always returns a type that is **not** `void`.
- An accessor method returns a value (*result*) of the type given in the **header**.
- The method will contain a **return** statement to return the value.
- ### Mutator Methods
- What are **mutator** methods? #card
card-last-interval:: 10.24
card-repeats:: 3
card-ease-factor:: 2.56
card-next-schedule:: 2022-10-13T19:27:22.160Z
card-last-reviewed:: 2022-10-03T14:27:22.161Z
card-last-score:: 5
- **Mutator** methods alter (*mutate*) the state of an object.
- Achieved through changing the value of one or more fields.
- They typically contain one or more *assignment* statements.
- Often receive parameters.
- ![image.png](../assets/image_1663063179688_0.png)
- ### Mutator Methods: Set
- Each field may have a dedicated **set** mutator method.
- These have a simple, distinctive form:
- **void** return type
- method name related to the field name
- a single formal parameter, with the same type as the type of the field
- a single assignment statement
- A typical "set" method:
- ```java
public void setGear (int number)
{
gear = number;
}
```
- ### Protector Mutators
- A set method does not always have to assign unconditionally to the field.
- The parameter may be checked for validity and rejected if innappropriate.
- Mutators thereby protect fields.
- Mutators also support *encapsulation*.
- #### Protecting a Field
- ```java
public void setGear (int gearing)
{
// this conditional statement prevents innapropriate action.
// if protects the "gear" field from values that are too large or too small.
if (gearing >= 1 && gearing <= 18)
{
gear = gearing;
}
else
{
System.out.println("Exceeds maximum gear ratio. Gear not set");
}
}
```
- ### Method Structure
- The **header**:
- The head tells us:
- the *visibility* of the method to objects of other class.
- whether or not the method *returns a result*.
- the *name* of the method.
- whether or not the method takes *parameters*.
- E.g.,
- ```java
public int getSpeed()
```
- The **body** encloses the method's *statements*.
-
- ## C vs Java
- Unlike C, an OOP program will **not** have a pool of global variables that each method can access.
- Instead, ^^each object has its own data^^, and other objects rely on the *accessor* methods of the object to access the data.
-
- ## Conditional Statements
- Conditional statements in Java have the same format as in C.
- ```java
if (condition) {
do something;
}
else {
do somethingElse;
}
```
- ![image.png](../assets/image_1663063508214_0.png)

View File

@ -0,0 +1,196 @@
- #[[CT2106 - Object-Oriented Programming]]
- **Previous Topic:** [[Introduction to Java]]
- **Next Topic:** [[More Java Code]]
- **Relevant Slides:** ![Lecture02.pdf](../assets/Lecture02_1663059993088_0.pdf)
-
- What is the **structure of a class**? #card
card-last-interval:: 9.84
card-repeats:: 3
card-ease-factor:: 2.46
card-next-schedule:: 2022-10-16T13:11:17.984Z
card-last-reviewed:: 2022-10-06T17:11:17.985Z
card-last-score:: 5
- Every class has the following structure:
- ```java
public class ClassName
{
Fields
Constructors
Methods
}
```
- ## Fields
- What are **Fields**? #card
card-last-interval:: 27.13
card-repeats:: 4
card-ease-factor:: 2.56
card-next-schedule:: 2022-11-06T14:43:03.193Z
card-last-reviewed:: 2022-10-10T11:43:03.194Z
card-last-score:: 5
- **Fields**, also known as **instance variables**, store values for an object.
- Fields define the state of an object.
- In BlueJ, use *Inspect* to view the state.
- Some values change frequently, others rarely, or not at all.
- ## Encapsulation
- What is **Encapsulation**? #card
card-last-interval:: 4
card-repeats:: 2
card-ease-factor:: 2.32
card-next-schedule:: 2022-10-11T10:23:51.445Z
card-last-reviewed:: 2022-10-07T10:23:51.446Z
card-last-score:: 5
- In **encapsulation**, the ^^variables of a class will be hidden from other classes^^ and can only be accessed through the methods of their current class.
- This is also known as **data hiding**.
- Why use encapsulation? #card
card-last-interval:: -1
card-repeats:: 1
card-ease-factor:: 2.08
card-next-schedule:: 2022-10-09T23:00:00.000Z
card-last-reviewed:: 2022-10-09T08:51:16.259Z
card-last-score:: 1
- In OOP, ^^each object is responsible for its own data.^^
- This allows an object to have greater control over which data is available to be viewed externally, and how external objects can mutate the object's state.
- ### Encapsulation Type: Private
- What is the effect of making a field **private**? #card
card-last-interval:: 10.24
card-repeats:: 3
card-ease-factor:: 2.56
card-next-schedule:: 2022-10-10T13:30:49.649Z
card-last-reviewed:: 2022-09-30T08:30:49.650Z
card-last-score:: 5
- Making a field **private** encapsulates their values inside their object.
- No external class or object can access a private field.
-
- ## Constructors
- What are **constructors**? #card
card-last-interval:: 10.56
card-repeats:: 3
card-ease-factor:: 2.46
card-next-schedule:: 2022-10-14T00:41:02.783Z
card-last-reviewed:: 2022-10-03T11:41:02.784Z
card-last-score:: 5
- Constructors:
- Initialise an object.
- Have the same name as their class.
- Have a close association with the fields:
- They contain the initial values stored in the fields.
- They contain the parameter values often used for these.
- What is the point of the keyword `this`? #card
card-last-score:: 5
card-repeats:: 3
card-next-schedule:: 2022-10-14T15:39:43.016Z
card-last-interval:: 11.2
card-ease-factor:: 2.8
card-last-reviewed:: 2022-10-03T11:39:43.018Z
- The `this` keyword refers to the current object in a method or constructor.
- The most common use of `this` is to distinguish between class attributes & parameters of the same name.
- If the input parameter variables in your constructor have the **same name** as your fields, you must use the `this` keyword to distinguish between the two.
- `this` = "belonging to this object".
- E.g.,
- ```java
public Bicycle(int speed, int gear, int cadence)
{
this.speed = speed;
this.gear = gear;
this.cadence = cadence;
}
```
-
- ## Methods
- What are **methods**? #card
card-last-score:: 3
card-repeats:: 4
card-next-schedule:: 2022-10-24T09:51:35.296Z
card-last-interval:: 15.05
card-ease-factor:: 1.94
card-last-reviewed:: 2022-10-09T08:51:35.296Z
- **Methods** implement the *behaviour* of an object.
- They have a consistent structure comprised of a *header* and a *body*.
- ### Accessor Methods
- What are **accessor** methods? #card
card-last-interval:: 10.24
card-repeats:: 3
card-ease-factor:: 2.56
card-next-schedule:: 2022-10-17T15:19:14.037Z
card-last-reviewed:: 2022-10-07T10:19:14.038Z
card-last-score:: 5
- **Accessor** methods provide information about the state of an object.
- An accessor method always returns a type that is **not** `void`.
- An accessor method returns a value (*result*) of the type given in the **header**.
- The method will contain a **return** statement to return the value.
- ### Mutator Methods
- What are **mutator** methods? #card
card-last-interval:: 10.24
card-repeats:: 3
card-ease-factor:: 2.56
card-next-schedule:: 2022-10-13T19:27:22.160Z
card-last-reviewed:: 2022-10-03T14:27:22.161Z
card-last-score:: 5
- **Mutator** methods alter (*mutate*) the state of an object.
- Achieved through changing the value of one or more fields.
- They typically contain one or more *assignment* statements.
- Often receive parameters.
- ![image.png](../assets/image_1663063179688_0.png)
- ### Mutator Methods: Set
- Each field may have a dedicated **set** mutator method.
- These have a simple, distinctive form:
- **void** return type
- method name related to the field name
- a single formal parameter, with the same type as the type of the field
- a single assignment statement
- A typical "set" method:
- ```java
public void setGear (int number)
{
gear = number;
}
```
- ### Protector Mutators
- A set method does not always have to assign unconditionally to the field.
- The parameter may be checked for validity and rejected if innappropriate.
- Mutators thereby protect fields.
- Mutators also support *encapsulation*.
- #### Protecting a Field
- ```java
public void setGear (int gearing)
{
// this conditional statement prevents innapropriate action.
// if protects the "gear" field from values that are too large or too small.
if (gearing >= 1 && gearing <= 18)
{
gear = gearing;
}
else
{
System.out.println("Exceeds maximum gear ratio. Gear not set");
}
}
```
- ### Method Structure
- The **header**:
- The head tells us:
- the *visibility* of the method to objects of other class.
- whether or not the method *returns a result*.
- the *name* of the method.
- whether or not the method takes *parameters*.
- E.g.,
- ```java
public int getSpeed()
```
- The **body** encloses the method's *statements*.
-
- ## C vs Java
- Unlike C, an OOP program will **not** have a pool of global variables that each method can access.
- Instead, ^^each object has its own data^^, and other objects rely on the *accessor* methods of the object to access the data.
-
- ## Conditional Statements
- Conditional statements in Java have the same format as in C.
- ```java
if (condition) {
do something;
}
else {
do somethingElse;
}
```
- ![image.png](../assets/image_1663063508214_0.png)

View File

@ -0,0 +1,196 @@
- #[[CT2106 - Object-Oriented Programming]]
- **Previous Topic:** [[Introduction to Java]]
- **Next Topic:** [[More Java Code]]
- **Relevant Slides:** ![Lecture02.pdf](../assets/Lecture02_1663059993088_0.pdf)
-
- What is the **structure of a class**? #card
card-last-interval:: 9.84
card-repeats:: 3
card-ease-factor:: 2.46
card-next-schedule:: 2022-10-16T13:11:17.984Z
card-last-reviewed:: 2022-10-06T17:11:17.985Z
card-last-score:: 5
- Every class has the following structure:
- ```java
public class ClassName
{
Fields
Constructors
Methods
}
```
- ## Fields
- What are **Fields**? #card
card-last-interval:: 27.13
card-repeats:: 4
card-ease-factor:: 2.56
card-next-schedule:: 2022-11-06T14:43:03.193Z
card-last-reviewed:: 2022-10-10T11:43:03.194Z
card-last-score:: 5
- **Fields**, also known as **instance variables**, store values for an object.
- Fields define the state of an object.
- In BlueJ, use *Inspect* to view the state.
- Some values change frequently, others rarely, or not at all.
- ## Encapsulation
- What is **Encapsulation**? #card
card-last-interval:: 4
card-repeats:: 2
card-ease-factor:: 2.32
card-next-schedule:: 2022-10-11T10:23:51.445Z
card-last-reviewed:: 2022-10-07T10:23:51.446Z
card-last-score:: 5
- In **encapsulation**, the ^^variables of a class will be hidden from other classes^^ and can only be accessed through the methods of their current class.
- This is also known as **data hiding**.
- Why use encapsulation? #card
card-last-interval:: 9.12
card-repeats:: 3
card-ease-factor:: 2.28
card-next-schedule:: 2022-10-29T10:28:34.075Z
card-last-reviewed:: 2022-10-20T08:28:34.075Z
card-last-score:: 5
- In OOP, ^^each object is responsible for its own data.^^
- This allows an object to have greater control over which data is available to be viewed externally, and how external objects can mutate the object's state.
- ### Encapsulation Type: Private
- What is the effect of making a field **private**? #card
card-last-interval:: 28.3
card-repeats:: 4
card-ease-factor:: 2.66
card-next-schedule:: 2022-11-17T15:33:20.443Z
card-last-reviewed:: 2022-10-20T08:33:20.443Z
card-last-score:: 5
- Making a field **private** encapsulates their values inside their object.
- No external class or object can access a private field.
-
- ## Constructors
- What are **constructors**? #card
card-last-interval:: 10.56
card-repeats:: 3
card-ease-factor:: 2.46
card-next-schedule:: 2022-10-14T00:41:02.783Z
card-last-reviewed:: 2022-10-03T11:41:02.784Z
card-last-score:: 5
- Constructors:
- Initialise an object.
- Have the same name as their class.
- Have a close association with the fields:
- They contain the initial values stored in the fields.
- They contain the parameter values often used for these.
- What is the point of the keyword `this`? #card
card-last-score:: 5
card-repeats:: 3
card-next-schedule:: 2022-10-14T15:39:43.016Z
card-last-interval:: 11.2
card-ease-factor:: 2.8
card-last-reviewed:: 2022-10-03T11:39:43.018Z
- The `this` keyword refers to the current object in a method or constructor.
- The most common use of `this` is to distinguish between class attributes & parameters of the same name.
- If the input parameter variables in your constructor have the **same name** as your fields, you must use the `this` keyword to distinguish between the two.
- `this` = "belonging to this object".
- E.g.,
- ```java
public Bicycle(int speed, int gear, int cadence)
{
this.speed = speed;
this.gear = gear;
this.cadence = cadence;
}
```
-
- ## Methods
- What are **methods**? #card
card-last-score:: 3
card-repeats:: 4
card-next-schedule:: 2022-10-24T09:51:35.296Z
card-last-interval:: 15.05
card-ease-factor:: 1.94
card-last-reviewed:: 2022-10-09T08:51:35.296Z
- **Methods** implement the *behaviour* of an object.
- They have a consistent structure comprised of a *header* and a *body*.
- ### Accessor Methods
- What are **accessor** methods? #card
card-last-interval:: 10.24
card-repeats:: 3
card-ease-factor:: 2.56
card-next-schedule:: 2022-10-17T15:19:14.037Z
card-last-reviewed:: 2022-10-07T10:19:14.038Z
card-last-score:: 5
- **Accessor** methods provide information about the state of an object.
- An accessor method always returns a type that is **not** `void`.
- An accessor method returns a value (*result*) of the type given in the **header**.
- The method will contain a **return** statement to return the value.
- ### Mutator Methods
- What are **mutator** methods? #card
card-last-interval:: 10.24
card-repeats:: 3
card-ease-factor:: 2.56
card-next-schedule:: 2022-10-13T19:27:22.160Z
card-last-reviewed:: 2022-10-03T14:27:22.161Z
card-last-score:: 5
- **Mutator** methods alter (*mutate*) the state of an object.
- Achieved through changing the value of one or more fields.
- They typically contain one or more *assignment* statements.
- Often receive parameters.
- ![image.png](../assets/image_1663063179688_0.png)
- ### Mutator Methods: Set
- Each field may have a dedicated **set** mutator method.
- These have a simple, distinctive form:
- **void** return type
- method name related to the field name
- a single formal parameter, with the same type as the type of the field
- a single assignment statement
- A typical "set" method:
- ```java
public void setGear (int number)
{
gear = number;
}
```
- ### Protector Mutators
- A set method does not always have to assign unconditionally to the field.
- The parameter may be checked for validity and rejected if innappropriate.
- Mutators thereby protect fields.
- Mutators also support *encapsulation*.
- #### Protecting a Field
- ```java
public void setGear (int gearing)
{
// this conditional statement prevents innapropriate action.
// if protects the "gear" field from values that are too large or too small.
if (gearing >= 1 && gearing <= 18)
{
gear = gearing;
}
else
{
System.out.println("Exceeds maximum gear ratio. Gear not set");
}
}
```
- ### Method Structure
- The **header**:
- The head tells us:
- the *visibility* of the method to objects of other class.
- whether or not the method *returns a result*.
- the *name* of the method.
- whether or not the method takes *parameters*.
- E.g.,
- ```java
public int getSpeed()
```
- The **body** encloses the method's *statements*.
-
- ## C vs Java
- Unlike C, an OOP program will **not** have a pool of global variables that each method can access.
- Instead, ^^each object has its own data^^, and other objects rely on the *accessor* methods of the object to access the data.
-
- ## Conditional Statements
- Conditional statements in Java have the same format as in C.
- ```java
if (condition) {
do something;
}
else {
do somethingElse;
}
```
- ![image.png](../assets/image_1663063508214_0.png)

View File

@ -0,0 +1,196 @@
- #[[CT2106 - Object-Oriented Programming]]
- **Previous Topic:** [[Introduction to Java]]
- **Next Topic:** [[More Java Code]]
- **Relevant Slides:** ![Lecture02.pdf](../assets/Lecture02_1663059993088_0.pdf)
-
- What is the **structure of a class**? #card
card-last-interval:: 21.53
card-repeats:: 4
card-ease-factor:: 2.32
card-next-schedule:: 2022-12-01T00:46:31.046Z
card-last-reviewed:: 2022-11-09T12:46:31.046Z
card-last-score:: 3
- Every class has the following structure:
- ```java
public class ClassName
{
Fields
Constructors
Methods
}
```
- ## Fields
- What are **Fields**? #card
card-last-interval:: 27.13
card-repeats:: 4
card-ease-factor:: 2.56
card-next-schedule:: 2022-11-06T14:43:03.193Z
card-last-reviewed:: 2022-10-10T11:43:03.194Z
card-last-score:: 5
- **Fields**, also known as **instance variables**, store values for an object.
- Fields define the state of an object.
- In BlueJ, use *Inspect* to view the state.
- Some values change frequently, others rarely, or not at all.
- ## Encapsulation
- What is **Encapsulation**? #card
card-last-interval:: 4
card-repeats:: 2
card-ease-factor:: 2.32
card-next-schedule:: 2022-10-11T10:23:51.445Z
card-last-reviewed:: 2022-10-07T10:23:51.446Z
card-last-score:: 5
- In **encapsulation**, the ^^variables of a class will be hidden from other classes^^ and can only be accessed through the methods of their current class.
- This is also known as **data hiding**.
- Why use encapsulation? #card
card-last-interval:: 9.12
card-repeats:: 3
card-ease-factor:: 2.28
card-next-schedule:: 2022-10-29T10:28:34.075Z
card-last-reviewed:: 2022-10-20T08:28:34.075Z
card-last-score:: 5
- In OOP, ^^each object is responsible for its own data.^^
- This allows an object to have greater control over which data is available to be viewed externally, and how external objects can mutate the object's state.
- ### Encapsulation Type: Private
- What is the effect of making a field **private**? #card
card-last-interval:: 28.3
card-repeats:: 4
card-ease-factor:: 2.66
card-next-schedule:: 2022-11-17T15:33:20.443Z
card-last-reviewed:: 2022-10-20T08:33:20.443Z
card-last-score:: 5
- Making a field **private** encapsulates their values inside their object.
- No external class or object can access a private field.
-
- ## Constructors
- What are **constructors**? #card
card-last-interval:: 10.56
card-repeats:: 3
card-ease-factor:: 2.46
card-next-schedule:: 2022-10-14T00:41:02.783Z
card-last-reviewed:: 2022-10-03T11:41:02.784Z
card-last-score:: 5
- Constructors:
- Initialise an object.
- Have the same name as their class.
- Have a close association with the fields:
- They contain the initial values stored in the fields.
- They contain the parameter values often used for these.
- What is the point of the keyword `this`? #card
card-last-score:: 5
card-repeats:: 3
card-next-schedule:: 2022-10-14T15:39:43.016Z
card-last-interval:: 11.2
card-ease-factor:: 2.8
card-last-reviewed:: 2022-10-03T11:39:43.018Z
- The `this` keyword refers to the current object in a method or constructor.
- The most common use of `this` is to distinguish between class attributes & parameters of the same name.
- If the input parameter variables in your constructor have the **same name** as your fields, you must use the `this` keyword to distinguish between the two.
- `this` = "belonging to this object".
- E.g.,
- ```java
public Bicycle(int speed, int gear, int cadence)
{
this.speed = speed;
this.gear = gear;
this.cadence = cadence;
}
```
-
- ## Methods
- What are **methods**? #card
card-last-score:: 3
card-repeats:: 4
card-next-schedule:: 2022-10-24T09:51:35.296Z
card-last-interval:: 15.05
card-ease-factor:: 1.94
card-last-reviewed:: 2022-10-09T08:51:35.296Z
- **Methods** implement the *behaviour* of an object.
- They have a consistent structure comprised of a *header* and a *body*.
- ### Accessor Methods
- What are **accessor** methods? #card
card-last-interval:: 10.24
card-repeats:: 3
card-ease-factor:: 2.56
card-next-schedule:: 2022-10-17T15:19:14.037Z
card-last-reviewed:: 2022-10-07T10:19:14.038Z
card-last-score:: 5
- **Accessor** methods provide information about the state of an object.
- An accessor method always returns a type that is **not** `void`.
- An accessor method returns a value (*result*) of the type given in the **header**.
- The method will contain a **return** statement to return the value.
- ### Mutator Methods
- What are **mutator** methods? #card
card-last-interval:: 10.24
card-repeats:: 3
card-ease-factor:: 2.56
card-next-schedule:: 2022-10-13T19:27:22.160Z
card-last-reviewed:: 2022-10-03T14:27:22.161Z
card-last-score:: 5
- **Mutator** methods alter (*mutate*) the state of an object.
- Achieved through changing the value of one or more fields.
- They typically contain one or more *assignment* statements.
- Often receive parameters.
- ![image.png](../assets/image_1663063179688_0.png)
- ### Mutator Methods: Set
- Each field may have a dedicated **set** mutator method.
- These have a simple, distinctive form:
- **void** return type
- method name related to the field name
- a single formal parameter, with the same type as the type of the field
- a single assignment statement
- A typical "set" method:
- ```java
public void setGear (int number)
{
gear = number;
}
```
- ### Protector Mutators
- A set method does not always have to assign unconditionally to the field.
- The parameter may be checked for validity and rejected if innappropriate.
- Mutators thereby protect fields.
- Mutators also support *encapsulation*.
- #### Protecting a Field
- ```java
public void setGear (int gearing)
{
// this conditional statement prevents innapropriate action.
// if protects the "gear" field from values that are too large or too small.
if (gearing >= 1 && gearing <= 18)
{
gear = gearing;
}
else
{
System.out.println("Exceeds maximum gear ratio. Gear not set");
}
}
```
- ### Method Structure
- The **header**:
- The head tells us:
- the *visibility* of the method to objects of other class.
- whether or not the method *returns a result*.
- the *name* of the method.
- whether or not the method takes *parameters*.
- E.g.,
- ```java
public int getSpeed()
```
- The **body** encloses the method's *statements*.
-
- ## C vs Java
- Unlike C, an OOP program will **not** have a pool of global variables that each method can access.
- Instead, ^^each object has its own data^^, and other objects rely on the *accessor* methods of the object to access the data.
-
- ## Conditional Statements
- Conditional statements in Java have the same format as in C.
- ```java
if (condition) {
do something;
}
else {
do somethingElse;
}
```
- ![image.png](../assets/image_1663063508214_0.png)