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
Q

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

A

describe the String method
split()

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

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.

A

What are
constants in Java

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

syntax:

booleanCondition ? expressionIfTrue : expressionIfFalse

A

what is the syntax for the
conditional/ternary operator

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

What is the
interface in a class?

A

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.

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

What are two
limitations of class methods in Java

A

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.

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

describe in 2 points the
conditional/ternary operator

A

points include:
1. acts as a more consice way of writing an if else statement
2. is the only java operator with three operands

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

how is
weak or loose coupling
achieved

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
32
Q

this is a collection whee each entry is made of a key:value pair

A

describe a
map

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

describe the Random method
nextInt(intbound)

A

will return a random number between 0 (inclusive) and the bound (exclusive)

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

describe the
class method interface

A

this would be the header of the method and arguably its documentation

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

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.

A

What is abstraction?

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

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

A

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

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

Can instance methods access both instance and static data in Java

A

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.

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

what is
javadoc

A

this is a documentation generator that is included with java

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

create a
class constant

A

code example:
Private static final int GRAVITY = 3;

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

describe the HashMap method
keySet()

A

this HashMap method will return all keys of the hashmap as a set

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

this is the process of encapsulating or wrapping a primitive type into its associated wrapper class

A

describe
boxing

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

this ArrayList and HashSet method allows us to add all the objects from one collection into a new collection

A

describe the following method of ArrayList and HashSet
addAll()

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

is a map
an ordered or unordered collection

A

this is an unordered collection. this means that on access the order that the items are in cannot be guaranteed

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

describe the String method
trim()

A

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

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

describe a
set

A

this is a collectionthat does not allow duplicateelementsto be stored inside of it

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

how would we read the following statement

HashMap<String, HashSet<String>> animalMap

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
47
Q

a limitation of this is that its second and third operands must be expressions and not statements such as “System.out.println()”

A

what is a limitation of the
conditional/ternary operator

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

What is the
implementation in a class?

A

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.

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

How do we change the value of static variables in Java, and why is this considered good practice?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
50
Q

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

A

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

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

this is a class that can represent a primitive type value

A

describe a
wrapper class

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

what is the syntax of the
switch statement

A

syntax:

Switch(expression/variable) {
Case “Y”: isValid();
                 Break;
Case “N”: isValid();
                 Break;
Case “M”: isValid();
                 Break;
Default:
    “please enter Y, N, M”
    break
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
53
Q

what is the statement to import the
HashMap class

A

statement:

Import java.util.HashMap;

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

why must the String method equals() be used when comparing String values

A

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.

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

describe the class Random

A

by importing this class and creating an instance we can use the instance to generate a stream of pseudorandom numbers

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

this Integer static method is used to parse a string representation of an integer into an integer value

A

describe the Integer static method
parseInt()

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

describe why we might use the HashMap method
**getOrDefault() **

A

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

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

this HashMap method will return all keys of the hashmap as a set

A

describe the HashMap method
keySet()

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

this includes Words such as “public” and “private” and define the visibility of a field, constructor, or method.

A

describe the term
Access modifiers

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

describe a
hashmap

A

this is a particular implementation of a map.
it is a parameratized type which requires two type parameters in order to use it

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

write a
class/static method

A

code example:
~~~
Public static int getNumberOfDaysThisMonth()
{

}
~~~

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

code example:
~~~
Public static int getNumberOfDaysThisMonth()
{

}
~~~

A

write a
class/static method

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

write the code that will
Produce and store a random int between 0 and 2 inclusive inside the variable named myRanInt

A

code:

import java.util.Random;
Random ran = new Random();
int myRanInt = ran.nextInt(3);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
64
Q

describe the String method
toUpperCase()

A

this String method will return the given string but with all characters in lower case

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

statement:

Import java.util.HashMap;

A

what is the statement to import the
HashMap class

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

what is the syntax for the
conditional/ternary operator

A

syntax:

booleanCondition ? expressionIfTrue : expressionIfFalse

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

this is a particular implementation of a map.
it is a parameratized type which requires two type parameters in order to use it

A

describe a
hashmap

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

ignore

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

points include:
1.allows no duplicate elements
2. is Unordered
3.Is a parameratized type collection

A

give 4 points regarding a
HashSet

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

this Allows an element to be accessed only from within the class in which it is defined

A

describe the term
Private access modifier

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

Why are there limitations in place for class methods in Java?

A

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.

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

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.

A

describe the HashMap method
put()

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

What are
constants in Java

A

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.

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

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

A

What are
class variables

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

describe the syntax and param of the String method
equals()

A

syntax:

equals(ObjectanObject)

param:
anObject a string object to compare this string to

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

describe
autoboxing

A

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

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

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.

A

When is it
appropriate to use class variables

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

describe the HashMap method
get()

A

this HashMap method is used to retrieve values from the hashmap.
Returns the value to which the specified key is mapped, ornullif this map contains no mapping for the key.

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

code:

for (String aKey : myMap.keySet()) {
    System.out.println("The key " + aKey 
    \+ " is mapped to the value " + myMap.get(aKey)); 
}
A

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”

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

What is
weak or loose coupling

A

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

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

name 2 scenarios where the following will occur
unboxing

A

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

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

ignore

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

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.

A

What is the
interface in a class?

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

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

A

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

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

This is a Paramaterised type collection and is an implementation of a set

A

describe a
HashSet

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

What is
modularization

A

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.

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

ignore

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

give 5 pieces of
information that would be conveyed by a class interface

A

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

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

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.

A

why must the String method equals() be used when comparing String values

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

describe the String method
substring()

A

this String method will return a specfied substring of the string

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

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

A

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

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

describe a
HashSet

A

This is a Paramaterised type collection and is an implementation of a set

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

this HashMap method Returns the value to which the specified key is mapped, ora defaultValueif this map contains no mapping for the key.

A

describe the HashMap method
getOrDefault()

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

name 2 key differences between
HashSet vs ArrayList

A

differences include:
1. AHashSetcannot contain duplicate elements, but anArrayListcan.
2. AHashSetis unordered whereas anArrayListis ordered by an index.

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

this String method will return a specfied substring of the string

A

describe the String method
substring()

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

in this case and in regards to maps the the old value associated with that key is overwritten by the new value.

A

what is the result if we
add a value to a map using a key that already holds a value

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

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)

A

describe the access modifier
public

98
Q

this HashSet method Removes all of the elements from this set. The set will be empty after this call returns

A

describe the HashSet method
clear()

99
Q

Syntax for HashSet method:
Contains(element)

@param element element whose presence in this set is to be tested

A

what is the Syntax and params of the HashSet method
contains()

100
Q

code example:
Private static final int GRAVITY = 3;

A

create a
class constant

101
Q

write a
1. declaration
2. initialisation
3. declaration/initialisation

for the following:
a HashSet of String

A

Declaration:
HashSet<String> mySet;

Initialise:
mySet = new HashSet<>();

Equivalent:
HashSet<String> mySet = new HashSet<>()

102
Q

this String method will return the given string but with all characters in lower case

A

describe the String method
toLowerCase()

103
Q

syntax:

equals(ObjectanObject)

param:
anObject a string object to compare this string to

A

describe the syntax and param of the String method
equals()

104
Q

what 2 ways can the following be read as

HashMap<String, String>

A

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
Q

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”

A

code:

String toPrint = value == 1 ? "hello" : "world";
System.out.print(toPrint);
106
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 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

A

describe
autoboxing

107
Q

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.

A

Why should fields in a class be private?

108
Q

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

A

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
Q

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

A

give 5 pieces of
information that would be conveyed by a class interface

110
Q

String objects are immutable meaning they cannot be destroyed
describe what this means if we wanted to change a Strings value

A

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
A

ignore

112
Q

code:

import java.util.Random;
Random ran = new Random();
int myRanInt = ran.nextInt(3);
A

write the code that will
Produce and store a random int between 0 and 2 inclusive inside the variable named myRanInt

113
Q

Syntax for the HashSet method:
Add(element)

@param element element to be added to this set

A

what is the syntax and params of the HashSet method
add()

114
Q

When is it
appropriate to use class variables

A

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
Q

What is a
helper method

A

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
Q

describe
class/static methods

A

these are conceptually the same as class variables. they belong to the class and will be shared by all instances

117
Q

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.

A

What is the
implementation in a class?

118
Q

this HashMap method returns a boolean value stating whether a key exists within the hashmap

A

describe the HashMap method
containsKey()

119
Q

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

A

name 3 scenarios where the following will occur
autoboxing

120
Q

describe the
java standard class library

A

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
Q

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

A

Syntax:
containsKey(key)

@param key The key whose presence in this map is to be tested

122
Q

describe the following method of ArrayList and HashSet
addAll()

A

this ArrayList and HashSet method allows us to add all the objects from one collection into a new collection

123
Q

write out the java statement for the following

“a hash map from keys of type string, to values of type hash set of string”

A

how would we read the following statement

HashMap<String, HashSet<String>> animalMap

124
Q

give 4 points regarding a
HashSet

A

points include:
1.allows no duplicate elements
2. is Unordered
3.Is a parameratized type collection

125
Q

describe the String method
equals()

A

this String method will compare the string value to a given string value. If the values match it will return true

126
Q

statement:
Import java.util.HashSet;

A

what is the statement to
import the class HashSet

127
Q

syntax:

startsWith(Stringprefix)

param:
prefix a prefix we would like to know exists at the start of the string

A

describe the syntax and params of the String method
startsWith()

128
Q

code:

String toPrint = value == 1 ? "hello" : "world";
System.out.print(toPrint);
A

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
Q

differences include:
1. AHashSetcannot contain duplicate elements, but anArrayListcan.
2. AHashSetis unordered whereas anArrayListis ordered by an index.

A

name 2 key differences between
HashSet vs ArrayList

130
Q

why should we maintain the information hiding principle
not being allowed to know

A

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
Q

write the code that will
add an int to an ArrayList

A

code:

ArrayList<Integer> aIntList = new ArrayList<>();
aIntList.add(22)
132
Q

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

A

describe the
switch statement

133
Q

this is a set of packages included as standard with java
each package contains related classes that can be used within a java program

A

describe the
java standard class library

134
Q

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”

A

code:

for (String aKey : myMap.keySet()) {
    System.out.println("The key " + aKey 
    \+ " is mapped to the value " + myMap.get(aKey)); 
}
135
Q

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”

A

what 2 ways can the following be read as

HashMap<String, String>

136
Q

describe the String method
startsWith()

A

this String method will return true if the given string starts with a given prefix string

137
Q

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
A

name 4
common javadoc annotations and their purpose

138
Q

describe the
class implementation

A

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
Q

this is a collectionthat does not allow duplicateelementsto be stored inside of it

A

describe a
set

140
Q

by importing this class and creating an instance we can use the instance to generate a stream of pseudorandom numbers

A

describe the class Random

141
Q

what is the following also known as
conditional operator

A

what is the following also known as
terneray operator

142
Q

describe
boxing

A

this is the process of encapsulating or wrapping a primitive type into its associated wrapper class

143
A

ignore

144
Q

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.

A

What is
modularization

145
Q

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

A

describe why we might use the HashMap method
**getOrDefault() **

146
Q

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

A

name 4 pieces of
information that should be included in the class docuentation

147
Q

this HashSet method returns boolean stating if the given element is in the set

A

describe the HashSet method
contains()

148
Q

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

A

why might we use a primitive types wrapper class

149
Q

describe the HashSet method
add()

A

this HashSet method adds an element to the set. If element already exists the set is unchanged

150
Q

these are conceptually the same as class variables. they belong to the class and will be shared by all instances

A

describe
class/static methods

151
Q

this String method will compare the string value to a given string value. If the values match it will return true

A

describe the String method
equals()

152
Q

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

A

describe the
class implementation

153
Q

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.

A

Can instance methods access both instance and static data in Java

154
Q

What is abstraction?

A

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
Q

what is a limitation of the
conditional/ternary operator

A

a limitation of this is that its second and third operands must be expressions and not statements such as “System.out.println()”

156
Q

what is the Syntax and params of the HashSet method
contains()

A

Syntax for HashSet method:
Contains(element)

@param element element whose presence in this set is to be tested

157
Q

describe the
class interface

A

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
Q

What is
information hiding

A

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
Q

this is an unordered collection. this means that on access the order that the items are in cannot be guaranteed

A

is a map
an ordered or unordered collection

160
Q

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.

A

Why are there limitations in place for class methods in Java?

161
Q

this String method will return true if the given string starts with a given prefix string

A

describe the String method
startsWith()

162
Q

describe the term
Private access modifier

A

this Allows an element to be accessed only from within the class in which it is defined

163
Q

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

A

How do we define implementation and interface in a class?

164
Q

this HashMap method is used to retrieve values from the hashmap.
Returns the value to which the specified key is mapped, ornullif this map contains no mapping for the key.

A

describe the HashMap method
get()

165
Q

this is the processs of unwrapping objects back into there associated primtive types

A

describe
unboxing

166
Q

declare:
HashMap<String, HashSet<String>> 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");

A

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
Q

will return a random number between 0 (inclusive) and the bound (exclusive)

A

describe the Random method
nextInt(intbound)

168
Q

in this case and in regards to maps the value null will be returned

A

what happens if we
use a maps get method to access a key that does not exists

169
Q

this is used to define class/static variables.

A

What is the
**static keyword **
used for

170
Q

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

A

what is the syntax and params of the integer static method
parseInt

171
Q

describe the HashMap method
put()

A

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
Q

how does abstraction and modularization maintain the
information hiding principle “not need know”

A

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
Q

what is the syntax and params of the integer static method
parseInt

A

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
Q

what is the statement to
import the class HashSet

A

statement:
Import java.util.HashSet;

175
Q

describe the Random method
nextInt()

A

this Random method will return a random number between -2^32 and 2^32

176
Q

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.

A

how is
weak or loose coupling
achieved

177
Q

this HashMap method Returns the number of key-value mappings in this map.

A

describe the HashMap method
size()

178
Q

Syntax:
containsKey(key)

@param key The key whose presence in this map is to be tested

A

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

179
Q

this would be the header of the method and arguably its documentation

A

describe the
class method interface

180
Q

describe the HashMap method
getOrDefault()

A

this HashMap method Returns the value to which the specified key is mapped, ora defaultValueif this map contains no mapping for the key.

181
Q

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

A

name 2 scenarios where the following will occur
unboxing

182
Q

how do we import the class Random

A

this class can be imported with the following statement

import java.util.Random;

183
Q

describe
autoboxing

A

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
Q

create an
instance constant

A

code example:
Private final int GRAVITY = 3;

185
Q

Syntax:
Get(key)

@param key the key whose associated value is to be returned

A

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

186
Q

describe the HashMap method
size()

A

this HashMap method Returns the number of key-value mappings in this map.

187
Q

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

A

describe the
java.lang package

188
Q

name 3 scenarios where the following will occur
autoboxing

A

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
Q

name 4
common javadoc annotations and their purpose

A

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
Q

describe the HashSet method
contains()

A

this HashSet method returns boolean stating if the given element is in the set

191
Q

name 2 key similarities between
HashSet vs ArrayList

A

similarities include:
1. Both aHashSetand anArrayListcan grow dynamically.
2. Both aHashSetand anArrayListcan contain only objects.

192
Q

code example:
Private final int GRAVITY = 3;

A

create an
instance constant

193
Q

describe
unboxing

A

this is the processs of unwrapping objects back into there associated primtive types

194
Q

describe the
switch statement

A

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
Q

points include:
1. acts as a more consice way of writing an if else statement
2. is the only java operator with three operands

A

describe in 2 points the
conditional/ternary operator

196
Q

ignore

197
Q

what is the result if we
add a value to a map using a key that already holds a value

A

in this case and in regards to maps the the old value associated with that key is overwritten by the new value.

198
Q

ignore

199
Q

Why is it important for a class to maintain full control over its private fields?

A

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
Q

describe a
map

A

this is a collection whee each entry is made of a key:value pair

201
Q

code:

ArrayList<Integer> aIntList = new ArrayList<>();
aIntList.add(22)
A

write the code that will
add an int to an ArrayList

202
Q

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

A

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

203
Q

Declaration:
HashSet<String> mySet;

Initialise:
mySet = new HashSet<>();

Equivalent:
HashSet<String> mySet = new HashSet<>()

A

write a
1. declaration
2. initialisation
3. declaration/initialisation

for the following:
a HashSet of String

204
Q

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

A

syntax:

substring(int beginIndex, int endIndex)

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

205
Q

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

A

Syntax:
Get(key)

@param key the key whose associated value is to be returned

206
Q

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

A

String objects are immutable meaning they cannot be destroyed
describe what this means if we wanted to change a Strings value

207
Q

this HashSet method adds an element to the set. If element already exists the set is unchanged

A

describe the HashSet method
add()

208
Q

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.

A

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

209
Q

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.

A

What are the
two principles of information hiding

210
Q

describe the HashSet method
clear()

A

this HashSet method Removes all of the elements from this set. The set will be empty after this call returns

211
Q

what
information is conveyed by the class method interfcae

A

information includes:
1.The access modifier
2.The return type
3.The method name
4.The parameters (if any)

212
Q

information includes:
1.The access modifier
2.The return type
3.The method name
4.The parameters (if any)

A

what
information is conveyed by the class method interfcae

213
Q

this Random method will return a random number between -2^32 and 2^32

A

describe the Random method
nextInt()

214
Q

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

A

when might we witness the following exception
StringIndexOutOfBoundsException

215
Q

describe the term
Access modifiers

A

this includes Words such as “public” and “private” and define the visibility of a field, constructor, or method.

216
Q

this String method will return the given string but with all characters in lower case

A

describe the String method
toUpperCase()

217
Q

What are the
two principles of information hiding

A

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
Q

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.

A

Why is it important for a class to maintain full control over its private fields?

219
Q

when might we witness the following exception
StringIndexOutOfBoundsException

A

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
Q

Why should fields in a class be private?

A

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
Q

write a
1. declaration
2. initialisation
3. declaration/initialisation

for the following:
a hashmap of key type String to value type String

A

Declaration:
HashMap<String, String> stringsMap;

Initialisation:
stringsMap = new HashMap<>();

Equivalent:
HashMap<String, String> stringsMap = new HashMap<>();

222
Q

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.

A

how does abstraction and modularization maintain the
information hiding principle “not need know”

223
Q

what happens if we
use a maps get method to access a key that does not exists

A

in this case and in regards to maps the value null will be returned

224
Q

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

A

why should we maintain the information hiding principle
not being allowed to know

225
Q

name 4 pieces of
information that should be included in the class docuentation

A

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
Q

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”

A

declare:
HashMap<String, HashSet<String>> 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
Q

Declaration:
HashMap<String, String> stringsMap;

Initialisation:
stringsMap = new HashMap<>();

Equivalent:
HashMap<String, String> stringsMap = new HashMap<>();

A

write a
1. declaration
2. initialisation
3. declaration/initialisation

for the following:
a hashmap of key type String to value type String

228
Q

describe the HashMap method
containsKey()

A

this HashMap method returns a boolean value stating whether a key exists within the hashmap

229
Q

describe the Integer static method
parseInt()

A

this Integer static method is used to parse a string representation of an integer into an integer value

230
Q

describe the syntax and params of the String method
startsWith()

A

syntax:

startsWith(Stringprefix)

param:
prefix a prefix we would like to know exists at the start of the string

231
Q

describe a
wrapper class

A

this is a class that can represent a primitive type value

232
Q

similarities include:
1. Both aHashSetand anArrayListcan grow dynamically.
2. Both aHashSetand anArrayListcan contain only objects.

A

name 2 key similarities between
HashSet vs ArrayList

233
Q

Syntax for String method:
split(value)

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

A

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

234
Q

syntax:

Switch(expression/variable) {
Case “Y”: isValid();
                 Break;
Case “N”: isValid();
                 Break;
Case “M”: isValid();
                 Break;
Default:
    “please enter Y, N, M”
    break
}
A

what is the syntax of the
switch statement

235
Q

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

A

How do we change the value of static variables in Java, and why is this considered good practice?

236
Q

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.

A

What is
information hiding

237
Q

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

A

code:

Int anInt = 22
Integer aWrapper = new Integer(anInt);
238
Q

describe the String method
toLowerCase()

A

this String method will return the given string but with all characters in lower case

239
Q

what is the syntax and params of the HashSet method
add()

A

Syntax for the HashSet method:
Add(element)

@param element element to be added to this set

240
Q

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.

A

What are two
limitations of class methods in Java

241
A

ignore

242
Q

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

A

describe the
class interface