Featuring ... Arrays, ArrayLists
Named location in computer memory.
Stores data for use in application.
Java is statically typed, meaning that variables must be declared, with a data type, before they are used.
int counter;
counter is a variable, able to store the data type referenced as int.
boolean isValid;
isValid is a variable, able to store the data type referenced as boolean.
Variables store data temporarily, and can be visible inside a block of code, or globally thoughout a module.
A descriptive term, indicating what can be done with data, and how much space is required to store it. For instance int and double variables can be divided, added, multiplied. The boolean data type describes variables that can store one of two values, true or false. Basic data types in AP Computer Science curriculum (A) are int, boolean and double.
The int data type takes up 4 bytes of storage and represents whole numbers from -2,147,483,648 to 2,147,483,647.
The double data type takes up 8 bytes of storage and represents fractional numbers. Sufficient for storing 15 decimal digits.
A data type of known storage requirement, like int, boolean, double.
The int data type takes up 4 bytes of storage and represents whole numbers from -2,147,483,648 to 2,147,483,647.
The double data type takes up 8 bytes of storage and represents fractional numbers. Sufficient for storing 15 decimal digits.
The boolean data type only requires 1 bit but in practice may be take up a byte or nybble.
Think of a reference as the starting memory location for storing data of an unpredicted size
A non-null reference requires an allocation of memory, involving some initialization, or the use of the new operator.
Primitive Data type having only values of either true or false. Named for Boolean logic, a branch of mathematics developed by George Boole, an Anglo-Irish mathematician.
A boolean variable can be used to store a value of true or false, this making the boolean data type useful for conditionals.
boolean isSenior = age > 65;
If age is 67, then isValid is true. If age is 45 then isValid is false.
Primitive numeric data Data type.
Occupies 32 bits, or 4 bytes, and can store numbers from -2 31 to 231 - 1. Whole numbers only.
Primitive numeric data Data type.
Occupies 64 bits and can store numbers from -263 to 264 - 1. Fractional parts included.
Data that is passed to a method.
Formal parameters are described in the defintion of a method, whereas actual parameters are passed to the method in calling code.
Below, rate and balance are formal parameters,
public double interestCalculated(double rate, double balance) { ...
}
... and the actual parameters are seen in the call,
double interest = interestCalculated(0.03, 12000);
A symbol used to perform an operation,
| addition | + |
| subtraction | - |
| multiplication | * |
| division | / |
| modulo | % |
| equality | == |
| assignment | = |
A variable, value of which cannot be changed after initialization. Commonly represented in uppercase. Indicated with keyword final.
final int GOAL_HEIGHT = 8;
A template for creating an object or instance.
A data model created from a class.
A way to 'temporarily convert' or represent data as another type. For example,
double height = 34.5;
int val = (int) height; // This is a narrowing conversion
int width = 5;
double side = (double)width; // This is a widening conversion
Note, when an int is typecast to a double, information is not lost. However, when a double is typecast to an int, the fractional part is truncated.
Object Oriented Programming. A software engineering paradigm (technique) in which templates called classes are used to create data models called objects. The application is built around the objects.
The 4 tenets of OOP are
| Abstraction | Pulling out the big ideas and representing these in the OOP framewrk, as classes and objects |
| Polymorphism | Allowing for different objects to specialize and respond in different ways to the same request |
| Inheritance | Reuse of a class code base called the super or parent class, in another class called the sub class or child class |
| Encapsulation | Subsuming the data and logic inside a unit, like a class/object |
A software template from which data models called objects are created.
A class specifies
| Attributes | Features or characteristics of the class or object |
| Methods | Blocks of code that specify what the class or object can do |
A Class is instantiated, or newed up, to create an object.
SoccerPlayer player =
new SoccerPlayer("Harry Kane", 26, "Forward", "Spurs");
Here, the class is SoccerPlayer and the instance (object) is player.
A data model created from a software template called a class. An object (also called instance) is 'instantiated' or 'newed up' from a class.
An object has
| attributes | properties of the object |
| instance methods | blocks of code that can be called on the object, and describe what the object can do |
In order for instance methods to be called, the object has to exist. Therfore it must be newed up before the attributes or methods can be accessed.
Keyword, this refers to the current object.
Below, this is used to set the value of an attribute to the value passed into the block of code.
public class SoccerPlayer {
private int age;
...
public void setAge(int age) {
this.age = age;
}
}
A characteristic of an object or a class. Other terms used include instance variable, class variable, property, data member, instance data, class data.
public class SoccerPlayer {
private int age;
static String club = "Spurs"; ...
public int getAge() {
return this.age;
}
}
age is an instance variable.
SoccerPlayer player =
new SoccerPlayer(....);
int playerAge = player.getAge();
club is a class or static (key word static binds to class) variable. It can be accessed by using the class name and the dot access operator,
String clubName = SoccerPlayer.club;
A block of code, given a label, and bound to an object (instance method), or class (class method).
An instance method is described as something the object can do.
public class SoccerPlayer {
private int age;
...
public void messageCoach() {
...
}
public static String codeOfConduct() {
System.out.println("Practice hours are reserved ...")
}
}
SoccerPlayer player = new SoccerPlayer();
player.messageCoach();
SoccerPlayer.codeOfConduct();
A special method that is responsible for instantiating or 'newing up' an object from a class.
If no constructor is defined, the default constructor can be used. This is the constructor defined at Object level. Once a constructor has been defined in the class, then the default constructor can no longer be used.
public SoccerPlayer(String name, int age,
String position, String club) {
this.name = name;
this.age = age;
this.position = position;
this.club = club;
}
A special method that returns the value of a private attribute.
Attributes are declared private in order to protect them from direct access. Therefore methods that are declared public are necessary to get the value of the attribute.
public class SoccerPlayer {
private String name;
...
public String getName() {
return this.name;
}
The accessor or getter returns the value of the attribute, and the return type mirrors the type or class of the attribute.
A special method that sets the value of a (typically)private attribute.
Attributes are declared private in order to protect them from direct access. Therefore methods that are declared public are necessary to set the value of the attribute.
public class SoccerPlayer {
private String name;
...
public void setName(String newName) {
this.name = newName;
}
The mutator or setter does not have a return the parameter mirrors the type or class of the attribute.
A redeclaration of an existing method, with a different parameter list.
|
public SoccerPlayer() { .... } public SoccerPlayer(String name, int age) { this.name = name; this.age = age; } |
| public void requirements() { .... } public void requirements(SoccerPlayer player) { .... } |
This governs the visibility of a class, a method, an attribute.
private gives access only within the class, whereas public gives access outside of the class. There are other accessibility modifiers, however these are outside the scope of the course.
public class SoccerPlayer {
private String name;
...
public String getName() { return name; }
Client code, or the calling code, can new up an instance of the SoccerPlayer class, but cannot directly access the name attribute because it is private, instead, must call the getName() method, which is public.
String is a special class that Java uses to represent String instances - strings are a bunch of characters, like "Robbie Roe". String is a reference type, used to create an instance. String instances can be created using
the new operator,
String str1 = new String("Have a great day!");
... and by using String literals, String str2 = "Have a great day!";
The String methods you need to know about are: length(), indexOf(..), substring(..), equals(..), compareTo(..)
String instances are immutable, you cannot change them. When you do 'change' an existing String instance, a new String instance is created.
This keyword indicates that an attribute or a method s bound to a class, not an object. As such, no instantiation is required to access the static quantity. For example,
public class SoccerPlayer {
...
static int numberOfPlayers;
}
In the client code,
int playersTotal = SoccerPlayer.numberOfPlayers;
Derived from the creator of Boolean Algebra, George Boole, the term refers to outcomes having values of either true or false, nothing else.
Boolean algebra is the basis of the design of digital computer circuits, and is central to the flow of logic in a computer program.
Logical statement that is either true or false.
Like,
goals > 15
Compound boolean expressions are composed of singleton expressions joined by Boolean operators, like AND(&& in Java), OR (|| in Java).
| boolean isSeniorKeeper = (age > 21) && (position.equals("Keeper"); |
| boolean isValidPlayer = (club.equals("Training") || (club.equals("Spurs"); |
Operators that act on boolean variables.
The 3 basic Boolean operators are
| AND (Java, &&), |
| OR (Java, ||) |
| NOT (Java, !) |
Boolean operators have associated truth tables that map the values of the operands to the result when the Boolean operator is applied.
The AND operation is represented by && in Java. Only true if all operands are true.
Statement that makes a single decision based on boolean expression.
Statement that supports decision based on the value of a boolean expression, and a single alternative.
Statement supporting multiple decisions based on value of multiple boolean expressions.
Two objects are aliases of each other if they refer to the same memory location. In the diagram, player1 and player2 end up as aliases.
An action that is repetetive, happens in a loop.
Iteration in computer programming enables the repeated execution of a block of code. Typically, a loop condition governs execution of the loop, and a sentry variable or loop variable is changed each time through the loop. The loop variable forms a part of the loop condition and determines how many times the loop executes.
If a loop condition is always true, the loop never exits.
Term given to a construct in computer programming in which a block of code is repeated either a set number of times or infinitely.
A loop in computer programming enables the repeated execution of a block of code. Typically, a loop condition governs execution of the loop, and a sentry variable or loop variable is changed each time through the loop. The loop variable forms a part of the loop condition and determines how many times the loop executes.
If a loop condition is always true, the loop never exits.
A specific loop that uses the while keyword, a loop variable (sentry variable) and a boolean expression, to govern how many times the loop is executed. Loop runs while the boolean expression (loop condition) is true.
A specific loop that uses the for keyword, a loop variable or sentry variable and a boolean expression to govern how many times the loop is executed.
The for loop construct supports initialization and allows for specification of an action to be taken each time through the loop.
A loop that that does not have an exit point, the loop condition is always true - this results in a loop that cycles infinitely.
A software template in Object Oriented Programming.
Software models called objects (or instances) are made from a class, in the same way an achitectural blueprint can be used to build multiple houses.
A class is a building block of Object Oriented Programming, OOP.
The 4 tenets of OOP are
| Abstraction - Pulling out the big ideas and forming classes |
| Polymorphism - Different objects specializing different responses to the same request |
| Inheritance - Reuse of a class as the basis for another class |
| Encapsulation - Subsuming the logic and data inside a unit, class or object |
A software model made from the instructions in a class. Also called an instance. We say the object has been instatiated, or 'newed up' based on the class.
SoccerPlayer player = new SoccerPlayer();
SoccerPlayer is the class, and player represents the object.
An instance may have instance variables and instance methods. Instance variables may be protected by an accessibility modifier and may only be viewed or changed by using methods called getters and setters.
public class SoccerPlayer {
private String name;
...
public String getName() {
return this.name;
}
An object or instance is a reference to an area of memory, the starting memory address at which the content of the object is stored.
A characteristic or feature of a class or object. Attributes belonging to the instance are also termed
| instance variable |
| data member |
| instance data |
| property |
By convention, attributes are protected with the private accessibility modifier. Methods called Getters and Setters get and set attribute values from outside the class.
Below, name is an instance variable. Keyword static indicates that division is a class variable.
public class SoccerPlayer {
private String name;
public static String division = "Premier League"; ...
public String getName() {
return this.name;
}
This is a block of code tied to an object or a class. It is given a signature describing it's visibility (like public or private), the return type and the name.
public class SoccerPlayer {
private String name;
...
public String getName() {
return this.name;
}
public static associatedSport() {
return "Soccer aka European Football";
}
getName is an instance method, called on the instance. associatedSport is a class method, called on the class.
SoccerPlayer player = new SoccerPlayer();
...
String playerName = player.getName();
String sportCategory = SoccerPlayer.associatedSport();
A constructor is a special method that has responsibility for instantiating a new object based on the instructions in a class. A constructor is described as an empty constructor if it has no parameters, and a parameterized constructor if it has a parameter list.
public class SoccerPlayer {
private String name;
....
public SoccerPlayer() {
}
public SoccerPlayer(String name, int age) {
this.name = name;
this.age = age;
}
These constructors are called when the object is newed up from the class,
SoccerPlayer player = new SoccerPlayer();
SoccerPlayer player2 = new SoccerPlayer("Harry Kane", 27);
These constructors are overloaded.
Feature of programming languages, supports storing and processing data, showing relationships.
Examples of data structures include arrays, Arraylist objects, 2 Dimensional arrays.
| item #1 | item #2 | item #3 |
| 00 | 01 | 02 |
| 10 | 11 | 12 |
| 20 | 21 | 22 |
These data structures extend the ability of a programming language, and facilitate data manipulation and processing.
Data structure that stores a collection of elements, each identified by an array index.
Arrays are declared with the data type or class, and must be newed up with the new operator, or initialized with an initialization list or initializer list.
String[] values = {"item #1", "item #2", "item #3"};
or
String[] values = new String[3];
values[0] = "item #1";
values[1] = "item #2";
values[2] = "item #3";
Arrays are used to store multiple quantities, representing a programming convenience and alternative to declaring and managing multiple variables.
A whole number valid in range 0 to 1 less than the length of the array. Identifies array element.
| item #1 | item #2 | item #3 |
| index 0 - | index 1 - | index 2 |
Condition, index less than 0 or greater than or equal to number of array elements.
| item #1 | item #2 | item #3 |
| index 0 - | index 1 - | index 2 - | index 3 !!! |
Use the length property of Array to ensure that only valid index values are used.
0 <= index < array.length
Entity (primitive or object) array contains, identified by array index.
int[] nums = {1,222,23};
| 1 | 222 | 23 |
| numbers[0] is 1, the array element at index 0 is 1 |
| numbers[1] is 222 |
| numbers[2] is 23 |
Sequencing through the elements of array, typically using a loop.
| while index within bounds use index to access array element change value of index |
|
|
|
int[] numbers = new int[100]; for (int index = 0; index < numbers.length; index++) { System.out.print(numbers[index]); } |
|
int[] numbers = new int[100]; int index = 0; while (index <numbers.length ) { System.out.print(numbers[index]); index++; } |
Loop, sequences through a collection, an array, referring to the items in the array in sequence.
for (Item item : items)
process(item)
Note that Item represents either a primitive data type or a Class name, corresponding to the data type or object contained by the array.
Add elements to the array.
SoccerPlayer[] team = new SoccerPlayer[3];
| null | null | null |
| index 0 - | index 1 - | index 2 |
The array can be populated by direct assignments,
team[index] = new SoccerPlayer("Harry Kane", 26, "Forward","Spurs");
... or by using an initializer list when the array is declared, after newing up instances of the class,
SoccerPlayer[] team = {player1, player2, player3};
Search algorithm, sequences through array, comparing each element to target.
| item #1 | item #2 | item #3 | item #4 | ..... |
start at index 0
while (index in bounds) {
check if element at index equals target
if it does, take action,
else increase index
}
Search Algorithm, assumes sorted array. Splits array in half repetitively, to find target.
Looking for 55:
| 10 | 11 | 22 | 33 | 44 | 55 | 77 |
| 10 | 11 | 22 | 33 | 44 | 55 | 77 |
| 10 | 11 | 22 | 33 | 44 | 55 | 77 |
Sorting algorithm, relies on inserting subsequent items into sorted part of list. O(n2), Ω(n).
| 3 | -1 | 9 | 2 | 1 | 4 | 7 |
| 3 | -1 | 9 | 2 | 1 |
| -1 | 3 | 9 | 2 | 1 |
| -1 | 3 | 9 | 2 | 1 |
| -1 | 2 | 3 | 9 | 1 |
| -1 | 1 | 2 | 3 | 9 |
Sorting algorithm, swaps elements to sort list. O(n2), Ω(n2).
| 3 | -1 | 9 | 2 | 1 |
| -1 | 3 | 9 | 2 | 1 |
| -1 | 1 | 9 | 2 | 3 |
| -1 | 1 | 3 | 2 | 9 |
| -1 | 1 | 3 | 2 | 9 |
| -1 | 1 | 2 | 3 | 9 |
Big O.
Symbol describing worst case scenario in terms of swapping/comparing values in sort/search process.
| Linear Search | O(n) |
| Selection Sort | O(n2) |
| Insert Search | O(n2) |
| Binary Search | O(log n) |
Big Ω.
Symbol describing best case scenario in terms of swapping/comparing values in sort process.
| Linear Search | Ω(1) |
| Selection Sort | Ω(n2) |
| Insert Search | Ω(n) |
| Binary Search | Ω(1) |