Index
Module 01
Class 01: Variable
Class 02: User Input
Class 03: Arithmetic
Class 04: If Statement
Class 05: Random Numbers
Class 06: Math Class
Class 07: Printf
Class 08: Nested If statements
Class 09: String Methods
Class 10: SubStrings
Class 11: Ternary operator
Class 12: Enhanced Switches
Class 13: Logical Operators
Class 14: While Loops
Class 15: For Loops; Break and Continue
Class 16: Nested Loops
Module 02
Class 17: Methods
Class 18: Overloaded Methods
Class 19: Variable Scope
Class 20: Arrays
Class 21: Varargs
Class 22: 2D Arrays
Object Oriented Programing
Class 23: Object-Oriented Programming
Class 24: Constructors
Class 25: Overloaded Constructors
Class 26: Array of Objects
Class 27: Static
Class 28: Inheritance
Class 29: Super
Class 30: Method Overriding
Class 31: .toString Method
Class 32: Abstraction
Class 33: Interfaces
Class 34: Polymorphism
Class 35: Runtime Polymorphism
Class 36: Getters and Setters
Extra Class 37: Stack data structure
Extra Class 38: Queue data structure
Object Oriented Programing
Class 39: Aggregation
Class 40: Composition
Class 41: Wrapper Classes
Class 42: ArrayList
Class 43: Exception Handling
Class 44: Write Files
Class 01: Variable
Variable: a reusable container for a value
Camelcase for variables isStudent -> first initial Lowercase and the use Upper case
A variable behaves as if it was the value it contains
Primitive: Simple value stored directly in memory (Stack)-> last in first out
(Its like give a $10 right in cash)
Reference: Memory address (stack) that points to the (Heap)
Heap -> data structure that is used to store other objects. like a tree.
(Its like give a I.O.T of $10) I owe you
Primitive Reference
int-> int age=30; String->String name=”Elviston Duálamo”;
double/float-> double price=3.95; array
char-> char grade= ‘A’; object
boolean->boolean isStudent= true;
(Float is up to 7 decimals digits and Double is up to 15 decimal digits)
Class 02: User Input
To accept the user input in Java we need a help of the Scanner
Scanner->is a class that allows us to accept input
Scanner-> have to be imported from Java Utilities (package)
import [Link];
We need to create a object Scanner Type, create variable that’s going to receive the value
and then close it
Scanner objectname=new Scanner([Link]);
String variable= [Link]();
[Link]();
The nextLine method of the object reads a String of characters, including spaces
“Elviston Duálamo”
The next method doesn’t include space,
“Elviston” Duálamo
To read an Integer we use the method “nextInt”
int age= [Link]();
To read an double we use the method “nextDouble”
int age= [Link]();
To read an boolean we use the method “nextBoolean”
int age= [Link]();
There is a common issue when we accept a int/double then we accept a String
To solve this problem its just to call a nextLine method to clean the input buffer
Exercise: calculate the rectangle area:
Class 03: Arithmetic
Augmented Assignment Operators: We condense the steps of assigning a new value
received from an operation.
So, instead of: x = x+y; We can use: x += y;
Increment and Decrement Operators: If we want to increment and decrement a variable by
one, we can do: x++; or x- -;
A great tools to be used at loops
Order of Operations [P-E-M-D-A-S]:
Parenthesis-Exponents-Multiplication-Division-Adition-Subtraction
() ^ * / + -
Exercise: Make a shopping cart
Class 04: If Statement
Performs a block of code if its condition is true
Class 05: Random Numbers
To create Random Numbers we have to import the Random class:
import [Link];
them we have to create the object:
Random random= new Random();
Doesn’t have to close like the Scanner
Example of use
However it has a huge range from -2Bilion to +2Bilion, but we can create a range
Int number=[Link](1, 10);The first number (1) is inclusive but the last number (10) is
exclusive
By the range (1, 10) we will never have the number “10” only the number 1 to 9
To create a range from 1 to 100, we must use the condition (1, 101)
We can also generate a random double number, which is going to give us a number between
0 and 1
And used to generate a Boolean as well, good for create a flip coin situation
Class 06: Math Class
The Math Class has a lot methods to perform mathematical tasks on numbers
This Class doesn’t have the need of been imported
[Link]
Some main methods of this Class:
Class 07: Printf
Printf() = is a method used to format output
The signal used to format is %
When we use Printf we also has to break the line manually \n
%[flags] [width] [.precision] [specifier-character]
Specifier-character example
.Precision example
Flags example
Width example
Exercise: Compound interest calculator
Class 08: Nested If statements
Structure example
Class 09: String Methods
[Link](“”)-> will return a boolean value
’
Class 10: SubStrings
.substring()= Is a method used to extract a portion of a string; .substring(start, end);
But every email has a different length, so we use a StringMethod
username01 and domain01 are substrings
Enter email:
Exercise: Weight converter
Class 11: Ternary operator
The ternary operator is represented by “?”
? Return 1 of 2 values if a condition is true
Variable= (condition) ? ifTrue : ifFalse;
Temperature Converter Exercise
Class 12: Enhanced Switches
The recommendation is to use enhanced switches over standard switches
Used to replace too many if else statements (Example at class 04)
Works with all Primitive Types, also works with String, Enumerated types
Structure:
switch(Variable){
case “EnterTheCase” ->
}
The default is used in the same way as the else
We can also simplify and improve:
its possible to use comma to unify conditions
and is also possible to write a case with more than one line
Class 13: Logical Operators
Logical operators will analyse and return a logical value
&& = AND -> Both statements must to be true
|| = OR -> At least one statement must to be true
! = NOT -> Will check if the statement is not true
Class 14: While Loops
While loop = repeat some code FOREVER. While some condition remains true
Class 15: For Loops; Break and Continue
For loop = execute some code a CERTAIN amount of times
The structure is:
We have to declare a variable, it's a good practice to name this variable as ‘i’, meaning
index
Set a condition
Set a value change for the variable
for( int i = 0; i < 10; i++) {
}
break = break out of the loop (STOP)
Continue = skip current interaction of a loop (SKIP)
Class 16: Nested Loops
Is a loop inside another loop. Used often with matrices and Data Structure and Algorithms
The index of the first loop its called by “i”
The index of the second loop is called by “j” Good practice of programing
Class 17: Methods
A Method is a block of reusable code that is executed when called by ()
When we call a method from another method that is static (main as exemple), the method
created must be static too.
*The method must be created out of the curly braces from the main method.
*If it is called from a static method, it has to be static too.
The method is unfamiliar with the variables declared within other methods, so we must
create some new variables inside the method that we can associate later.
it is possible to send information from one method to another method.
Anything that is sent to a method is known as ARGUMENTS
It is necessary to match parameters when we send arguments to the class
Parameter refer to the list of variables in a method declaration
The name of the parameters can be different from the name of the arguments
Void is used when the method doesn’t return a value
If the method return a value, the type of value must be listed
Class 18: Overloaded Methods
Overloaded Methods are methods that share the same name, but different parameters
Methods can share the same name but must have different parameters, as known as
signature: signature = name + parameters
two methods can’t share the same signature
If there are two or more methods with the same name, the method with the match
parameters is chosen.
Class 19: Variable Scope
The Variable Scope is the place where the variable is located
Local: for those inside a method
Class: for those made out of a method and inside of a class
Java has a preference to use the local variable first.
Class variables are normally used to create constants, etc… (I’ll find out later)
Class 20: Arrays
Arrays= a collection of values of the same data type
it is like a variable that can store more than one value
If we call an array without put the address the result will be another address because array is
a Reference type not a Primitive
Each value within an array is known as an Element, it needs an index number
It’s totally possible to change an Element value
To create an int array:
Array class methods
Enter user input into an array
Search an array
We cannot do this when we are comparing two strings, because strings are memory
addresses. Is necessary to use the .equals function
Class 21: Varargs
Allow a method to accept a varying number of arguments.
Makes methods more flexible, no need for Overloaded Methods
Java will pack the arguments into an array.
… (ellipsis)
Cass 22: 2D Arrays
An array where each element is an array
Useful for storing a matrix of data
Class 23: Object-Oriented Programming
Object= An entity that holds data (attributes)
and can perform actions (methods)
It is a reference data type
Attributes are things/characteristics that object has
/
Class 24: Constructors
Constructor = A special method to initialize objects
You can pass arguments to a constructor and set up initial values
Class 25: Overloaded Constructors
Allow a class to have multiple constructors with different parameter lists
it allow us to initialize a object in various ways
Class 26: Array of Objects
Class 27: Static
static= Makes a variable or method belong to the class
rather than any specific object
Commonly used for utility methods or shared resources
They will share the same value, even if they have their own copy of the variable
Class 28: Inheritance
One class inherits the attributes and methods from another class
Child <- Parent <-Grandparent
Dog/Cat <- Animal <- Organism
Example: Dogs and cats are types of animals, so Dog and Cat will be it’s children
To inherit it’s necessary to use the command “extends”
If the class Animal inherits the class Organism, so the class Organism it’s grandparent from
the classes Dog and Cat.
Also a class can inherits the class Organism without inherits the Animal class, like a new
class Plant
Organism it’s just a parent to the Plant class
Class 29: Super
Refers to the parent class (subclass <-superclass)
Used in constructors and method overriding
Calls the parent constructor to initialize attributes
Class 30: Method Overriding
When a subclass provides its own
Implementation of a method that is already defined
Allows for code reusability and give specific implementations
It’s a good practice to add a note “@Override”, so we and other Developers will know that
this method is being overridden.
@Override -> The system will check if there is a method being overridden, if there’s not, it
will return an error. It might occur if you misspell the method’s name.
But fish cannot move, so:
Class 31: .toString Method
.toString() = Method inherited from the Object class.
Used to return a string representation of an object
By default, it returns a hash code as a unique identifier
It can be overridden to provide meaningful details
The method that is overridden is:
Class 32: Abstraction
abstract -> it is a keyword = Used to define abstract classes and methods
Abstraction is the process of hiding implementation details and showing only the essential
features
*Abstract classes CAN’T be instantiated directly
Can contain ‘Abstract’ methods (which must be implemented)
Can contain ‘Concrete’ methods (which are inherited)
An error will occur if the children doesn’t implement the abstract method
Class 33: Interfaces
Interface= A blueprint for a class that specifies a set of abstract methods
That implementing classes MUST define
Supports multiple inheritance-like behavior <- key difference from Abstract Classes
Class 34: Polymorphism
Polymorphism -> “Poly” = “Many” / “Morph” = “Shape”
Objects can identify as other objects
Objects can be treated as objects of a common superclass
A Dog can be identified as: an animal, an organism, an object,
Polymorphism can be achieved by using interface
To inherit an interface the command is “implements”
The methods needs an access modifier, like “public
Class 35: Runtime Polymorphism
Runtime Polymorphism = When the method that gets executed is decided at runtime based
on the actual type of the object.
Class 36: Getters and Setters
They help protect object data and add rules for accessing or modifying them.
GETTER = Methods that make a field READABLE. Also is possible to add extra logic
SETTER = Methods that make a field WRITEABLE. Also is possible to add extra logic
Example of class without Getter and Setter methods:
Example of a class with Getter and Setter methods:
Extra Class 37: Stack data structure
Stack = LIFO data structure. Last-In/First-Out
Stores objects into a sort of “Vertical tower”
push() = to add to the top
pop() = to remove from the top
Uses of Stacks:
1. Undo/Re do features in text editors
2. Moving back/forward through browser history
3. Backtracking algorithms (maze, file directories)
4. Calling functions (call stack)
Extra Class 38: Queue data structure
Queue is an interface, to use its technology is necessary to import the LinkedList class and
create a LinkedList instead a Queue
Queue = FIFO data structure. First-In/First-Out (Like a line of people)
A collection designed for holding elements prior to processing
Linear data structure
Can be used:
1. Keyboard Buffer (Letters should appear on the screen in the order they’re pressed
2. Printer Queue (Print jobs should be completed in order)
3. Used in LinkedLists, PriorityQueues, Breadth-first search
offer() = enqueue, used to add an item to the line
poll() = dequeue, used to remove the first item of the line
/
Class 39: Aggregation
Aggregation = Represents a “has-a” relationship between objects.
One object contains another object as part of its structure
but the contained object/s can exist independently
Example:
A Library object and a Book object. The book is contained at the Library object but the book
can exist independently
Class 40: Composition
Represents a “Part-of” relationship between objects.
For example, an Engine is “Part of” a Car
Allows complex objects to be constructed from smaller objects
Class 41: Wrapper Classes
Allow primitive values (int, char, double, boolean) to be used as objects.
“Wrap them in an object”
Generally, don’t wrap primitives unless you need an object.
Allow use of Collections Framework and static Utility Methods
Class 42: ArrayList
A resizeable array that stores objects (Autoboxing)
Array are fixed in size, but ArrayLists can change, they are dynamic
Has to be imported
Exercise
Class 43: Exception Handling
Exception = An event that interrupts the normal flow of a program (Dividing by zero, file not
found, mismatch input type).
Surround any dangerous code with a try{} block
try{}, catch{}, finally{}
Class 44: Write Files