Chapter 6 - More-Sophisticated Behaviour Flashcards

1
Q

What is the
**static keyword **
used for

A

this is used to define class/static variables.

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

this is a java compiler feature that will automatically box(wrap) and unbox(unwrap) primitive types to/from there wrapper class when the context calls for it (such as adding a primitive type to a collection)

NOTE: we can do this explicitly but java will handle it for us

A

describe
autoboxing

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

How do we
call a static method in Java

A

We can call a static method in Java without creating an instance of the class. Instead, we can simply use the name of the class followed by the name of the method

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

in 3 points
How do constant declarations differ from field declarations in Java?

A

these differ in that
1. they include the final keyword before the type and name
2. must be initialized with a value at the point of declaration
3. are usually capitalized by convention.

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

what is the syntax and params of the String method
split()

A

Syntax for String method:
split(value)

@param value a character or regex that defines where to separate a string

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

why might we use a primitive types wrapper class

A

one reason inludes:
collections may only hold objects, if we tried to place a primitive type into a collection it would not be accepted

in this case we must create an object representing the primitive type using its associated wrapper class

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

this is the idea that different parts of a system are not strongly connected, and can be changed without affecting each other.

A

What is
weak or loose coupling

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

describe the String method
split()

A

this String method can be used to split a string by a given character or regex. The separated strings are then returned in an array of strings

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

Syntax for the ArrayList and HashSet method addAll():
Collection1.addAll(collection2)

A

what is the syntax of the ArrayList and HashSet method
addAll()

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

this String method will return a new string from the given string but with any leading and trailing whitespace removed

A

describe the String method
trim()

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

what is the following also known as
terneray operator

A

what is the following also known as
conditional operator

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

syntax:

substring(int beginIndex, int endIndex)

param:
beginIndex the beginning index, inclusive.
endIndex (optional) the ending index, exclusive.

A

give the syntax and params of the String method
substring()

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

information includes:
1.The name of the method
2.Purpose of the function or method
3.@param annotation - the name and type and description of a parameter
4.@return annotation - the type and a description of what is returned

A

name 4 pieces of
information that should be included in the Constructor and method documentation

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

describe the access modifier
public

A

this access modifier allows a member (e.g., a field, method, or class) to be accessed from anywhere. (e.g., outside the class it was defined)

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

Syntax:
put(key, value)

@param key- key with which the specified value is to be associated
@param value- value to be associated with the specified key

A

what is the syntax and params of the HashMap method
put()

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

What are
class variables

A

also known as static variables, are variables that only have one copy, no matter how many instances of the class are created.

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

code:

Int anInt = 22
Integer aWrapper = new Integer(anInt);
A

write the code that would explicitly wrap a an int using its associated wrapper class

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

How do we define implementation and interface in a class?

A

We define the implementation using the private keyword, and the interface using the public keyword.

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

this class can be imported with the following statement

import java.util.Random;

A

how do we import the class Random

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

this is a documentation generator that is included with java

A

what is
javadoc

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

describe the
java.lang package

A

this package is automatically imported into every java program and contains commonly used classes such as
1. wrapper classes - Integer, Boolean, etc
2. String class
3. Math class

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

this is a method that is not for public use but helps to achieve publicly accessible operations.

It should be declared as private because it is part of the implementation of a class, and is not intended to be used by other classes or users.

A

What is a
helper method

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

We can call a static method in Java without creating an instance of the class. Instead, we can simply use the name of the class followed by the name of the method

A

How do we
call a static method in Java

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

the wrapper class has the same name as its associated primitive type except
1. we include a capital letter at the beggining
2. int uses wrapper class Integer
3. char uses wrapper class Character

A

what are the names of the wrapper classes for the primitive types

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
this String method can be used to split a string by a given character or regex. The separated strings are then returned in an array of strings
describe the String method **split()**
26
in Java these are defined using the modifier keyword final. If any attempt is made to change this, a compile-time error message will occur.
What are **constants in Java**
27
syntax: `booleanCondition ? expressionIfTrue : expressionIfFalse`
what is the syntax for the **conditional/ternary operator**
28
What is the **interface in a class?**
this is the public information of a class. It includes any data or operation that we would like the class to share with other classes.
29
What are two **limitations of class methods in Java**
these include: 1. Class methods may not access any instance fields defined in the class. They may only access class variables of the same class. 2. A class method may not call an instance method defined in the class. They may only call other class methods of the same class.
30
describe in 2 points the **conditional/ternary operator**
points include: 1. acts as a more consice way of writing an if else statement 2. is the only java operator with three operands
31
how is **weak or loose coupling** achieved
this is achieved by ensuring that one class does not depend on the implementation of another class, and by allowing the implementation of a class to be changed without breaking other classes that rely on a specific implementation. this is important for writing code that is more flexible and adaptable.
32
this is a collection whee each entry is made of a key:value pair
describe a **map**
33
describe the Random method **nextInt(int bound)**
will return a random number between 0 (inclusive) and the bound (exclusive)
34
describe the **class method interface**
this would be the header of the method and arguably its documentation
35
this is the process of focusing on the essential aspects of an object and ignoring the details of its implementation. It allows us to use an object without needing to know how it works internally.
What is abstraction?
36
name 4 pieces of **information that should be included in the Constructor and method documentation**
information includes: 1.The name of the method 2.Purpose of the function or method 3.@param annotation - the name and type and description of a parameter 4.@return annotation - the type and a description of what is returned
37
Can instance methods access both instance and static data in Java
Yes, these can access both instance and static data in Java. these have access to the instance variables and instance methods of their own instance, as well as to any class variables or class methods that are defined in their class.
38
what is **javadoc**
this is a documentation generator that is included with java
39
create a **class constant**
code example: `Private static final int GRAVITY = 3;`
40
describe the HashMap method **keySet()**
this HashMap method will return all keys of the hashmap as a set
41
this is the process of encapsulating or wrapping a primitive type into its associated wrapper class
describe **boxing**
42
this ArrayList and HashSet method allows us to add all the objects from one collection into a new collection
describe the following method of ArrayList and HashSet **addAll()**
43
is a map **an ordered or unordered collection**
this is an unordered collection. this means that on access the order that the items are in cannot be guaranteed
44
describe the String method **trim()**
this String method will return a new string from the given string but with any leading and trailing whitespace removed
45
describe a **set**
this is a collection that does not allow duplicate elements to be stored inside of it
46
how would we read the following statement `HashMap> animalMap`
write out the java statement for the following “a hash map from keys of type string, to values of type hash set of string”
47
a limitation of this is that its second and third operands must be expressions and not statements such as "System.out.println()"
what is a limitation of the **conditional/ternary operator**
48
What is the **implementation in a class?**
this is the private information of a class. It includes any operation or data that helps in providing the class's overall objective, and is only accessible within the class.
49
How do we change the value of static variables in Java, and why is this considered good practice?
To change the value of this in Java, we use dot notation with the class name and the variable name, like this: `Employee.employeeCount++;` This style of referencing the static variable using its class name is considered good practice because it helps clarify that the variable belongs to the class, rather than to a specific instance of the class
50
what is the syntax of the ArrayList and HashSet method **addAll()**
Syntax for the ArrayList and HashSet method addAll(): Collection1.addAll(collection2)
51
this is a class that can represent a primitive type value
describe a **wrapper class**
52
what is the syntax of the **switch statement**
syntax: ``` Switch(expression/variable) { Case “Y”: isValid(); Break; Case “N”: isValid(); Break; Case “M”: isValid(); Break; Default: “please enter Y, N, M” break } ```
53
what is the statement to import the **HashMap class**
statement: `Import java.util.HashMap;`
54
why must the String method equals() be used when comparing String values
This is because the == operator will compare whether its two operands refer to the same object. The problem is that we can have two different string objects that have the same value but using the == operator would evaluate to false sometimes though the operator == will work and this is because string objects that are not explicitly created and have already been created are just re referenced.
55
describe the class Random
by importing this class and creating an instance we can use the instance to generate a stream of pseudorandom numbers
56
this Integer static method is used to parse a string representation of an integer into an integer value
describe the Integer static method **parseInt()**
57
describe why we might use the HashMap method **getOrDefault() **
the HashMap method get() would return null if the key did not exist. If we still wanted to produce a value even if get() returned null, using this we could do so without checking if null was returned
58
this HashMap method will return all keys of the hashmap as a set
describe the HashMap method **keySet()**
59
this includes Words such as "public" and "private" and define the visibility of a field, constructor, or method.
describe the term **Access modifiers**
60
describe a **hashmap**
this is a particular implementation of a map. it is a parameratized type which requires two type parameters in order to use it
61
write a **class/static method**
code example: ``` Public static int getNumberOfDaysThisMonth() { … } ```
62
code example: ``` Public static int getNumberOfDaysThisMonth() { … } ```
write a **class/static method**
63
write the code that will **Produce and store a random int between 0 and 2 inclusive inside the variable named myRanInt**
code: ``` import java.util.Random; Random ran = new Random(); int myRanInt = ran.nextInt(3); ```
64
describe the String method **toUpperCase()**
this String method will return the given string but with all characters in lower case
65
statement: `Import java.util.HashMap;`
what is the statement to import the **HashMap class**
66
what is the syntax for the **conditional/ternary operator**
syntax: `booleanCondition ? expressionIfTrue : expressionIfFalse`
67
this is a particular implementation of a map. it is a parameratized type which requires two type parameters in order to use it
describe a **hashmap**
68
ignore
[Chapter 6 Quiz 1](https://learn2.open.ac.uk/mod/quiz/view.php?id=2014910)
69
points include: 1.allows no duplicate elements 2. is Unordered 3.Is a parameratized type collection
give 4 points regarding a **HashSet**
70
this Allows an element to be accessed only from within the class in which it is defined
describe the term **Private access modifier**
71
Why are there limitations in place for class methods in Java?
These limitations are in place for class variables in Java because class methods and class variables are intended to be used for tasks that are related to the class as a whole, rather than to a specific instance of the class. Therefore, they do not have access to instance-specific data or behavior, as they are not associated with a specific instance of the class. This helps maintain the modularity and independence of the class, and promotes encapsulation and information hiding.
72
this Hashmap method places a new key/value pair in the hashmap. If the map previously contained a mapping for the key, the old value is replaced.
describe the HashMap method **put()**
73
What are **constants in Java**
in Java these are defined using the modifier keyword final. If any attempt is made to change this, a compile-time error message will occur.
74
also known as static variables, are variables that only have one copy, no matter how many instances of the class are created.
What are **class variables**
75
describe the syntax and param of the String method **equals()**
syntax: `equals(Object anObject)` param: **anObject** a string object to compare this string to
76
describe **autoboxing**
this is a java compiler feature that will automatically box(wrap) and unbox(unwrap) primitive types to/from there wrapper class when the context calls for it (such as adding a primitive type to a collection) NOTE: we can do this explicitly but java will handle it for us
77
these are typically used when a value should be the same for all instances of a class. For example, if you have a class that represents a bank account, you might use a class variable to store the interest rate for all accounts. This ensures that all accounts have the same interest rate, regardless of which instance of the class is used to access it.
When is it **appropriate to use class variables**
78
describe the HashMap method **get()**
this HashMap method is used to retrieve values from the hashmap. Returns the value to which the specified key is mapped, or null if this map contains no mapping for the key.
79
code: ``` for (String aKey : myMap.keySet()) { System.out.println("The key " + aKey + " is mapped to the value " + myMap.get(aKey)); } ```
write the code that will **print out every key/value pair in a HashMap using the following format** the key "HashMapKey" is mapped to the value "HashMapValue"
80
What is **weak or loose coupling**
this is the idea that different parts of a system are not strongly connected, and can be changed without affecting each other.
81
name 2 scenarios where the following will occur **unboxing**
this occurs in the following scenarios: 1.When a wrapper type variable is passed to the parameter of a method that expects a primitive type 2.When a wrapper type is stored in a primitive type variable
82
ignore
[Chapter 6 Quiz 3](https://learn2.open.ac.uk/mod/quiz/view.php?id=2014914)
83
this is the public information of a class. It includes any data or operation that we would like the class to share with other classes.
What is the **interface in a class?**
84
what is the syntax and params of the HashMap method **getOrDefault()**
Syntax of the HashMap method: getOrDefault(key, defaultvalue) Param: Key - the key of the hashmap collection we would like the value for Defaultvalue - a default value to return if key does not exist
85
This is a Paramaterised type collection and is an implementation of a set
describe a **HashSet**
86
What is **modularization**
this is the process of breaking a complex system into smaller, more manageable pieces. It allows us to write code that is easier to understand, maintain, and reuse. Together with abstraction, modularization helps us use classes and objects without needing to know the internals of how they work.
87
[Chapter 6 glossary quiz](https://learn2.open.ac.uk/mod/quiz/view.php?id=2014915)
ignore
88
give 5 pieces of **information that would be conveyed by a class interface**
information includes: 1.The name of the class 2. A general description of the classes purpose 3. A list of the classes constructors and methods 4. The parameters and return types for each constructor and method 5. A description of the purpose of each constructor and method
89
This is because the == operator will compare whether its two operands refer to the same object. The problem is that we can have two different string objects that have the same value but using the == operator would evaluate to false sometimes though the operator == will work and this is because string objects that are not explicitly created and have already been created are just re referenced.
why must the String method equals() be used when comparing String values
90
describe the String method **substring()**
this String method will return a specfied substring of the string
91
what are the names of the wrapper classes for the primitive types
the wrapper class has the same name as its associated primitive type except 1. we include a capital letter at the beggining 2. int uses wrapper class Integer 3. char uses wrapper class Character
92
describe a **HashSet**
This is a Paramaterised type collection and is an implementation of a set
93
this HashMap method Returns the value to which the specified key is mapped, or a defaultValue if this map contains no mapping for the key.
describe the HashMap method **getOrDefault()**
94
name 2 key differences between **HashSet vs ArrayList**
differences include: 1. A HashSet cannot contain duplicate elements, but an ArrayList can. 2. A HashSet is unordered whereas an ArrayList is ordered by an index.
95
this String method will return a specfied substring of the string
describe the String method **substring()**
96
in this case and in regards to maps the the old value associated with that key is overwritten by the new value.
what is the result if we **add a value to a map using a key that already holds a value**
97
this access modifier allows a member (e.g., a field, method, or class) to be accessed from anywhere. (e.g., outside the class it was defined)
describe the access modifier **public**
98
this HashSet method Removes all of the elements from this set. The set will be empty after this call returns
describe the HashSet method **clear()**
99
Syntax for HashSet method: Contains(element) @param element element whose presence in this set is to be tested
what is the Syntax and params of the HashSet method **contains()**
100
code example: `Private static final int GRAVITY = 3;`
create a **class constant**
101
write a 1. declaration 2. initialisation 3. declaration/initialisation for the following: **a HashSet of String**
Declaration: `HashSet mySet;` Initialise: `mySet = new HashSet<>();` Equivalent: `HashSet mySet = new HashSet<>()`
102
this String method will return the given string but with all characters in lower case
describe the String method **toLowerCase()**
103
syntax: `equals(Object anObject)` param: **anObject** a string object to compare this string to
describe the syntax and param of the String method **equals()**
104
what 2 ways can the following be read as **`HashMap`**
this can be read as 1. "a hash map from keys of type string to values of type string" 2. “a map from String to String”
105
using a conditional/ternary operator write the code that will print "hello" if a variable named value is equal to 1 else it prints "world"
code: ``` String toPrint = value == 1 ? "hello" : "world"; System.out.print(toPrint); ```
106
this is a java compiler feature that will automatically box(wrap) and unbox(unwrap) primitive types to/from there wrapper class when the context calls for it (such as adding a primitive type to a collection) NOTE 1: we can do this explicitly but java will handle it for us NOTE 2: although we do not need to explicitly wrap we do need to ensure that when this occurs the recieving collection or other class is expecting the wrapper class
describe **autoboxing**
107
these should be private because they are mostly a part of the class's implementation, and declaring them public breaks the principle of information hiding.
Why should fields in a class be private?
108
what is the syntax and params of the HashMap method **put()**
Syntax: `put(key, value)` @param key - key with which the specified value is to be associated @param value - value to be associated with the specified key
109
information includes: 1.The name of the class 2. A general description of the classes purpose 3. A list of the classes constructors and methods 4. The parameters and return types for each constructor and method 5. A description of the purpose of each constructor and method
give 5 pieces of **information that would be conveyed by a class interface**
110
String objects are immutable meaning they cannot be destroyed describe what this means if we wanted to change a Strings value
if we wanted to change a strings value then we must create a reference to a new string object either by creating a new variable or overwriting the existing variable
111
[Chapter 6 Quiz 2](https://learn2.open.ac.uk/mod/quiz/view.php?id=2014911)
ignore
112
code: ``` import java.util.Random; Random ran = new Random(); int myRanInt = ran.nextInt(3); ```
write the code that will **Produce and store a random int between 0 and 2 inclusive inside the variable named myRanInt**
113
Syntax for the HashSet method: Add(element) @param element element to be added to this set
what is the syntax and params of the HashSet method **add()**
114
When is it **appropriate to use class variables**
these are typically used when a value should be the same for all instances of a class. For example, if you have a class that represents a bank account, you might use a class variable to store the interest rate for all accounts. This ensures that all accounts have the same interest rate, regardless of which instance of the class is used to access it.
115
What is a **helper method**
this is a method that is not for public use but helps to achieve publicly accessible operations. It should be declared as private because it is part of the implementation of a class, and is not intended to be used by other classes or users.
116
describe **class/static methods**
these are conceptually the same as class variables. they belong to the class and will be shared by all instances
117
this is the private information of a class. It includes any operation or data that helps in providing the class's overall objective, and is only accessible within the class.
What is the **implementation in a class?**
118
this HashMap method returns a boolean value stating whether a key exists within the hashmap
describe the HashMap method **containsKey()**
119
this occurs in the following scenarios: 1.When a primitive type is passed as a parameter to a method that expects a wrapper type 2.When a primitive type is stored in a wrapper type variable 3.When a primitive type is stored in a collection and expects its wrapper type
name 3 scenarios where the following will occur **autoboxing**
120
describe the **java standard class library**
this is a set of packages included as standard with java each package contains related classes that can be used within a java program
121
what is the syntax and params of the HashMap method **containsKey()**
Syntax: containsKey(key) @param key The key whose presence in this map is to be tested
122
describe the following method of ArrayList and HashSet **addAll()**
this ArrayList and HashSet method allows us to add all the objects from one collection into a new collection
123
write out the java statement for the following “a hash map from keys of type string, to values of type hash set of string”
how would we read the following statement `HashMap> animalMap`
124
give 4 points regarding a **HashSet**
points include: 1.allows no duplicate elements 2. is Unordered 3.Is a parameratized type collection
125
describe the String method **equals()**
this String method will compare the string value to a given string value. If the values match it will return true
126
statement: `Import java.util.HashSet;`
what is the statement to **import the class HashSet**
127
syntax: `startsWith(String prefix)` param: **prefix** a prefix we would like to know exists at the start of the string
describe the syntax and params of the String method **startsWith()**
128
code: ``` String toPrint = value == 1 ? "hello" : "world"; System.out.print(toPrint); ```
using a conditional/ternary operator write the code that will print "hello" if a variable named value is equal to 1 else it prints "world"
129
differences include: 1. A HashSet cannot contain duplicate elements, but an ArrayList can. 2. A HashSet is unordered whereas an ArrayList is ordered by an index.
name 2 key differences between **HashSet vs ArrayList**
130
why should we maintain the information hiding principle **not being allowed to know**
this information hiding principle should be maintained as it means another class will not depend on another classes implementation this means we are free to change the implemenation without affecting other classes this is as long as the classes interfcae remains unchanged this is known as weak or loose coupling
131
write the code that will **add an int to an ArrayList**
code: ``` ArrayList aIntList = new ArrayList<>(); aIntList.add(22) ```
132
this is a statement that can be used in place of if...elseif...else where we may have many elseif. it works by matching a given expression or variable value against a set of different cases
describe the **switch statement**
133
this is a set of packages included as standard with java each package contains related classes that can be used within a java program
describe the **java standard class library**
134
write the code that will **print out every key/value pair in a HashMap using the following format** the key "HashMapKey" is mapped to the value "HashMapValue"
code: ``` for (String aKey : myMap.keySet()) { System.out.println("The key " + aKey + " is mapped to the value " + myMap.get(aKey)); } ```
135
this can be read as 1. "a hash map from keys of type string to values of type string" 2. “a map from String to String”
what 2 ways can the following be read as **`HashMap`**
136
describe the String method **startsWith()**
this String method will return true if the given string starts with a given prefix string
137
these include: 1. @version - used to spcify version 2. @author - used to specify author 3. @param - used to specify parameter information for methods 4. @return - used to provide return information for methods
name 4 **common javadoc annotations and their purpose**
138
describe the **class implementation**
this is any data or methods that enable or aid the interface to work. this is kept hidden in the source code and we usually do not want this information to be public
139
this is a collection that does not allow duplicate elements to be stored inside of it
describe a **set**
140
by importing this class and creating an instance we can use the instance to generate a stream of pseudorandom numbers
describe the class Random
141
what is the following also known as **conditional operator**
what is the following also known as **terneray operator**
142
describe **boxing**
this is the process of encapsulating or wrapping a primitive type into its associated wrapper class
143
[Chapter 6 Quiz 3](https://learn2.open.ac.uk/mod/quiz/view.php?id=2014914)
ignore
144
this is the process of breaking a complex system into smaller, more manageable pieces. It allows us to write code that is easier to understand, maintain, and reuse. Together with abstraction, modularization helps us use classes and objects without needing to know the internals of how they work.
What is **modularization**
145
the HashMap method get() would return null if the key did not exist. If we still wanted to produce a value even if get() returned null, using this we could do so without checking if null was returned
describe why we might use the HashMap method **getOrDefault() **
146
information includes: 1.Class name 2.A description of what a class does and how it can be used 3.@author annotation 4.@version annotation
name 4 pieces of **information that should be included in the class docuentation**
147
this HashSet method returns boolean stating if the given element is in the set
describe the HashSet method **contains()**
148
one reason inludes: collections may only hold objects, if we tried to place a primitive type into a collection it would not be accepted in this case we must create an object representing the primitive type using its associated wrapper class
why might we use a primitive types wrapper class
149
describe the HashSet method **add()**
this HashSet method adds an element to the set. If element already exists the set is unchanged
150
these are conceptually the same as class variables. they belong to the class and will be shared by all instances
describe **class/static methods**
151
this String method will compare the string value to a given string value. If the values match it will return true
describe the String method **equals()**
152
this is any data or methods that enable or aid the interface to work. this is kept hidden in the source code and we usually do not want this information to be public
describe the **class implementation**
153
Yes, these can access both instance and static data in Java. these have access to the instance variables and instance methods of their own instance, as well as to any class variables or class methods that are defined in their class.
Can instance methods access both instance and static data in Java
154
What is abstraction?
this is the process of focusing on the essential aspects of an object and ignoring the details of its implementation. It allows us to use an object without needing to know how it works internally.
155
what is a limitation of the **conditional/ternary operator**
a limitation of this is that its second and third operands must be expressions and not statements such as "System.out.println()"
156
what is the Syntax and params of the HashSet method **contains()**
Syntax for HashSet method: Contains(element) @param element element whose presence in this set is to be tested
157
describe the **class interface**
this is public information about a class that can be used by programmers to understand what the class does and how it can be used
158
What is **information hiding**
this is a principle that states that the inner workings of a class (implementation) should be hidden from other classes and users. This results in better modularization.
159
this is an unordered collection. this means that on access the order that the items are in cannot be guaranteed
is a map **an ordered or unordered collection**
160
These limitations are in place for class variables in Java because class methods and class variables are intended to be used for tasks that are related to the class as a whole, rather than to a specific instance of the class. Therefore, they do not have access to instance-specific data or behavior, as they are not associated with a specific instance of the class. This helps maintain the modularity and independence of the class, and promotes encapsulation and information hiding.
Why are there limitations in place for class methods in Java?
161
this String method will return true if the given string starts with a given prefix string
describe the String method **startsWith()**
162
describe the term **Private access modifier**
this Allows an element to be accessed only from within the class in which it is defined
163
We define the implementation using the private keyword, and the interface using the public keyword.
How do we define implementation and interface in a class?
164
this HashMap method is used to retrieve values from the hashmap. Returns the value to which the specified key is mapped, or null if this map contains no mapping for the key.
describe the HashMap method **get()**
165
this is the processs of unwrapping objects back into there associated primtive types
describe **unboxing**
166
declare: `HashMap> animalMap;` initialise: `animalMap = new HashMap<>();` place key/value pair in hashmap: `animalMap.put("Reptiles", new HashSet<>());` place element in the set that is a value within the hashmap: `animalMap.get("repile").add("snake");`
declare and initialise the following “a hash map from keys of type string to values of type hash set of string” then 1. put the key "reptile" and value "empty set" into the hashmap 2. add the element "snake" into the empty set that is held by the key "reptile"
167
will return a random number between 0 (inclusive) and the bound (exclusive)
describe the Random method **nextInt(int bound)**
168
in this case and in regards to maps the value null will be returned
what happens if we **use a maps get method to access a key that does not exists**
169
this is used to define class/static variables.
What is the **static keyword ** used for
170
syntax of the Integer static method parseInt parseInt(aString) aString - a string representing an int that we would like to be parsed into an int
what is the syntax and params of the integer static method **parseInt**
171
describe the HashMap method **put()**
this Hashmap method places a new key/value pair in the hashmap. If the map previously contained a mapping for the key, the old value is replaced.
172
how does abstraction and modularization maintain the **information hiding principle "not need know"**
Abstraction and modularization help maintain this information hiding principle by allowing us to use objects and classes without needing to know the details of their implementation.
173
what is the syntax and params of the integer static method **parseInt**
syntax of the Integer static method parseInt parseInt(aString) aString - a string representing an int that we would like to be parsed into an int
174
what is the statement to **import the class HashSet**
statement: `Import java.util.HashSet;`
175
describe the Random method **nextInt()**
this Random method will return a random number between -2^32 and 2^32
176
this is achieved by ensuring that one class does not depend on the implementation of another class, and by allowing the implementation of a class to be changed without breaking other classes that rely on a specific implementation. this is important for writing code that is more flexible and adaptable.
how is **weak or loose coupling** achieved
177
this HashMap method Returns the number of key-value mappings in this map.
describe the HashMap method **size()**
178
Syntax: containsKey(key) @param key The key whose presence in this map is to be tested
what is the syntax and params of the HashMap method **containsKey()**
179
this would be the header of the method and arguably its documentation
describe the **class method interface**
180
describe the HashMap method **getOrDefault()**
this HashMap method Returns the value to which the specified key is mapped, or a defaultValue if this map contains no mapping for the key.
181
this occurs in the following scenarios: 1.When a wrapper type variable is passed to the parameter of a method that expects a primitive type 2.When a wrapper type is stored in a primitive type variable
name 2 scenarios where the following will occur **unboxing**
182
how do we import the class Random
this class can be imported with the following statement `import java.util.Random;`
183
describe **autoboxing**
this is a java compiler feature that will automatically box(wrap) and unbox(unwrap) primitive types to/from there wrapper class when the context calls for it (such as adding a primitive type to a collection) NOTE 1: we can do this explicitly but java will handle it for us NOTE 2: although we do not need to explicitly wrap we do need to ensure that when this occurs the recieving collection or other class is expecting the wrapper class
184
create an **instance constant**
code example: `Private final int GRAVITY = 3;`
185
Syntax: Get(key) @param key the key whose associated value is to be returned
what is the syntax and params of the HashMap method **get()**
186
describe the HashMap method **size()**
this HashMap method Returns the number of key-value mappings in this map.
187
this package is automatically imported into every java program and contains commonly used classes such as 1. wrapper classes - Integer, Boolean, etc 2. String class 3. Math class
describe the **java.lang package**
188
name 3 scenarios where the following will occur **autoboxing**
this occurs in the following scenarios: 1.When a primitive type is passed as a parameter to a method that expects a wrapper type 2.When a primitive type is stored in a wrapper type variable 3.When a primitive type is stored in a collection and expects its wrapper type
189
name 4 **common javadoc annotations and their purpose**
these include: 1. @version - used to spcify version 2. @author - used to specify author 3. @param - used to specify parameter information for methods 4. @return - used to provide return information for methods
190
describe the HashSet method **contains()**
this HashSet method returns boolean stating if the given element is in the set
191
name 2 key similarities between **HashSet vs ArrayList**
similarities include: 1. Both a HashSet and an ArrayList can grow dynamically. 2. Both a HashSet and an ArrayList can contain only objects.
192
code example: `Private final int GRAVITY = 3;`
create an **instance constant**
193
describe **unboxing**
this is the processs of unwrapping objects back into there associated primtive types
194
describe the **switch statement**
this is a statement that can be used in place of if...elseif...else where we may have many elseif. it works by matching a given expression or variable value against a set of different cases
195
points include: 1. acts as a more consice way of writing an if else statement 2. is the only java operator with three operands
describe in 2 points the **conditional/ternary operator**
196
ignore
[Chapter 6 glossary quiz](https://learn2.open.ac.uk/mod/quiz/view.php?id=2014915)
197
what is the result if we **add a value to a map using a key that already holds a value**
in this case and in regards to maps the the old value associated with that key is overwritten by the new value.
198
ignore
[Chapter 6 Quiz 2](https://learn2.open.ac.uk/mod/quiz/view.php?id=2014911)
199
Why is it important for a class to maintain full control over its private fields?
this is important because it allows the class to ensure that the fields are not filled with invalid data. If fields were public, another class could enter data there as it sees fit, which could cause unknown errors. By maintaining control over the fields, the class can validate the data that is entered and ensure that it is correct. This helps maintain the integrity of the class's data and behavior.
200
describe a **map**
this is a collection whee each entry is made of a key:value pair
201
code: ``` ArrayList aIntList = new ArrayList<>(); aIntList.add(22) ```
write the code that will **add an int to an ArrayList**
202
Syntax of the HashMap method: getOrDefault(key, defaultvalue) Param: Key - the key of the hashmap collection we would like the value for Defaultvalue - a default value to return if key does not exist
what is the syntax and params of the HashMap method **getOrDefault()**
203
Declaration: `HashSet mySet;` Initialise: `mySet = new HashSet<>();` Equivalent: `HashSet mySet = new HashSet<>()`
write a 1. declaration 2. initialisation 3. declaration/initialisation for the following: **a HashSet of String**
204
give the syntax and params of the String method **substring()**
syntax: `substring(int beginIndex, int endIndex)` param: **beginIndex** the beginning index, inclusive. **endIndex** (optional) the ending index, exclusive.
205
what is the syntax and params of the HashMap method **get()**
Syntax: Get(key) @param key the key whose associated value is to be returned
206
if we wanted to change a strings value then we must create a reference to a new string object either by creating a new variable or overwriting the existing variable
String objects are immutable meaning they cannot be destroyed describe what this means if we wanted to change a Strings value
207
this HashSet method adds an element to the set. If element already exists the set is unchanged
describe the HashSet method **add()**
208
these differ in that 1. they include the final keyword before the type and name 2. must be initialized with a value at the point of declaration 3. are usually capitalized by convention.
in 3 points **How do constant declarations differ from field declarations in Java?**
209
The two principles of this are: 1) A programmer should not need to know the internals of a class 2) A user should not be allowed to know the internals of a class.
What are the **two principles of information hiding**
210
describe the HashSet method **clear()**
this HashSet method Removes all of the elements from this set. The set will be empty after this call returns
211
what **information is conveyed by the class method interfcae**
information includes: 1.The access modifier 2.The return type 3.The method name 4.The parameters (if any)
212
information includes: 1.The access modifier 2.The return type 3.The method name 4.The parameters (if any)
what **information is conveyed by the class method interfcae**
213
this Random method will return a random number between -2^32 and 2^32
describe the Random method **nextInt()**
214
this exception may be seen when we try and access a character of a string that is larger than the string itself such as when using the charAt() method. If we were looking at each character of the string using a loop it is wise to have a check such as `while (i < string.length())` this ensure we cannot access an index larger than the string and avoid such an error at runtime
when might we witness the following exception **StringIndexOutOfBoundsException**
215
describe the term **Access modifiers**
this includes Words such as "public" and "private" and define the visibility of a field, constructor, or method.
216
this String method will return the given string but with all characters in lower case
describe the String method **toUpperCase()**
217
What are the **two principles of information hiding**
The two principles of this are: 1) A programmer should not need to know the internals of a class 2) A user should not be allowed to know the internals of a class.
218
this is important because it allows the class to ensure that the fields are not filled with invalid data. If fields were public, another class could enter data there as it sees fit, which could cause unknown errors. By maintaining control over the fields, the class can validate the data that is entered and ensure that it is correct. This helps maintain the integrity of the class's data and behavior.
Why is it important for a class to maintain full control over its private fields?
219
when might we witness the following exception **StringIndexOutOfBoundsException**
this exception may be seen when we try and access a character of a string that is larger than the string itself such as when using the charAt() method. If we were looking at each character of the string using a loop it is wise to have a check such as `while (i < string.length())` this ensure we cannot access an index larger than the string and avoid such an error at runtime
220
Why should fields in a class be private?
these should be private because they are mostly a part of the class's implementation, and declaring them public breaks the principle of information hiding.
221
write a 1. declaration 2. initialisation 3. declaration/initialisation for the following: **a hashmap of key type String to value type String**
Declaration: `HashMap stringsMap;` Initialisation: `stringsMap = new HashMap<>();` Equivalent: `HashMap stringsMap = new HashMap<>();`
222
Abstraction and modularization help maintain this information hiding principle by allowing us to use objects and classes without needing to know the details of their implementation.
how does abstraction and modularization maintain the **information hiding principle "not need know"**
223
what happens if we **use a maps get method to access a key that does not exists**
in this case and in regards to maps the value null will be returned
224
this information hiding principle should be maintained as it means another class will not depend on another classes implementation this means we are free to change the implemenation without affecting other classes this is as long as the classes interfcae remains unchanged this is known as weak or loose coupling
why should we maintain the information hiding principle **not being allowed to know**
225
name 4 pieces of **information that should be included in the class docuentation**
information includes: 1.Class name 2.A description of what a class does and how it can be used 3.@author annotation 4.@version annotation
226
declare and initialise the following “a hash map from keys of type string to values of type hash set of string” then 1. put the key "reptile" and value "empty set" into the hashmap 2. add the element "snake" into the empty set that is held by the key "reptile"
declare: `HashMap> animalMap;` initialise: `animalMap = new HashMap<>();` place key/value pair in hashmap: `animalMap.put("Reptiles", new HashSet<>());` place element in the set that is a value within the hashmap: `animalMap.get("repile").add("snake");`
227
Declaration: `HashMap stringsMap;` Initialisation: `stringsMap = new HashMap<>();` Equivalent: `HashMap stringsMap = new HashMap<>();`
write a 1. declaration 2. initialisation 3. declaration/initialisation for the following: **a hashmap of key type String to value type String**
228
describe the HashMap method **containsKey()**
this HashMap method returns a boolean value stating whether a key exists within the hashmap
229
describe the Integer static method **parseInt()**
this Integer static method is used to parse a string representation of an integer into an integer value
230
describe the syntax and params of the String method **startsWith()**
syntax: `startsWith(String prefix)` param: **prefix** a prefix we would like to know exists at the start of the string
231
describe a **wrapper class**
this is a class that can represent a primitive type value
232
similarities include: 1. Both a HashSet and an ArrayList can grow dynamically. 2. Both a HashSet and an ArrayList can contain only objects.
name 2 key similarities between **HashSet vs ArrayList**
233
Syntax for String method: split(value) @param value a character or regex that defines where to separate a string
what is the syntax and params of the String method **split()**
234
syntax: ``` Switch(expression/variable) { Case “Y”: isValid(); Break; Case “N”: isValid(); Break; Case “M”: isValid(); Break; Default: “please enter Y, N, M” break } ```
what is the syntax of the **switch statement**
235
To change the value of this in Java, we use dot notation with the class name and the variable name, like this: `Employee.employeeCount++;` This style of referencing the static variable using its class name is considered good practice because it helps clarify that the variable belongs to the class, rather than to a specific instance of the class
How do we change the value of static variables in Java, and why is this considered good practice?
236
this is a principle that states that the inner workings of a class (implementation) should be hidden from other classes and users. This results in better modularization.
What is **information hiding**
237
write the code that would explicitly wrap a an int using its associated wrapper class
code: ``` Int anInt = 22 Integer aWrapper = new Integer(anInt); ```
238
describe the String method **toLowerCase()**
this String method will return the given string but with all characters in lower case
239
what is the syntax and params of the HashSet method **add()**
Syntax for the HashSet method: Add(element) @param element element to be added to this set
240
these include: 1. Class methods may not access any instance fields defined in the class. They may only access class variables of the same class. 2. A class method may not call an instance method defined in the class. They may only call other class methods of the same class.
What are two **limitations of class methods in Java**
241
[Chapter 6 Quiz 1](https://learn2.open.ac.uk/mod/quiz/view.php?id=2014910)
ignore
242
this is public information about a class that can be used by programmers to understand what the class does and how it can be used
describe the **class interface**