Notes and code taken during watching O'Reilly The Complete Java Course
22nd March 2023
I am not a fan of Java... it's needed for my work projects though
When watching the second section of the course there's first difficult question:
- Which version of which Java should I install? - The answer come, as usual, when I was away from the computer:
install the version that is used in the course - jdk-14.0.1 - maybe just take the last version of jdk-14.
Once done with the course one would need to start jumping through the release notes of Oracle to learn what's new on upper versions. Or find some documentation on how to do stuff on earlier versions of Java for already existing projects.
Let's see if my patience grow up to such a point. I already feel a scratch of annoyance in my brain for learning this language.
Ok, so Java 14 is already on Oracle archive...
...and I need to create an account...
JDK 14 Documentation link
Java 14 API Documentation link
But it's done, I got similar output on slightly higher version of JDK:
This was exhausting: nap time. - Primitive vs Reference Types - There are 8 primitive types in Java: byte, short, int, long, float, double, char, boolean - they hold the value of interest directly. String is the only reference data type we saw so far - it holds the memory address of the object of interest
23rd March 2023
- Comments - single line vs. multiline comments
// some comment here - single line
class Number {
int number = 37;
/*
multiple lines
of comments
*/
}
- Arithmetic Operators available in Java
- Relational Operators available in Java
- Logical Operators available in Java
24th March 2023
- User Input - here we are touching for the first time JAVA packages: java.lang & java.util.Scanner
import java.util.Scanner;
public class KeyboardInput {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
String name;
System.out.println("What's your name?");
name = keyboard.nextLine();
System.out.println("Hello, " + name);
}//end main
}
java.lang package is the default package - it is imported automatically in the beginning of each Java File, once we create .java file.
- number reading functions are leaving EOL char in the stream causing your following nextLine() function to skip getting user input
- floating number inputs are taking the ./, from the system, so I needed to input "3,5" instead of "3.5", however the program output was:
25th March 2023
I am sick of Java and of this world. Here's my version of your learning example John p. Baugh:
There's once was a stupid boy named John who was a fat programmer in the Kingdom of Earth.
He loved to wear glasses and to masochism.
He wanted to marry the wise layer named Lucy but his mother, Queen Lucia forbid him from seeing her.
Why does coding classes include this sick theme of males forbidding females of doing something? I need to throw up... Go choke on it John! Fuck this shit!
Control flow - the order in which instructions are executed within a program
There are 3 Categories of Control Statements in Java:
- Sequential (Sequence)
- Selection (Decision)
- Repetition (Loops)
Selection Control Statements
- The if statement
- The if-else statement
- the switch statement
Repetition Control Statements
- The while loop
- The do-while loop
- The for loop
- The for-each loop (syntactic sugar)
Continue and Break
- statements to be used with loops
- continue used inside a loop sends the execution to loop's beginning while
break will exit the loop entirely:
Have a read on Structured Programming Paradigm in your free moment
I will skip the mathematical discussion on why machines are not able to generate truly random numbers
Very first data structures - collections of elements
- elements must be type homogenous
You can find the Java SE 14 documentation for the java.util.Arrays class, which provides utility methods for working with arrays, including sorting, searching, and filling arrays, at the following link:
Additionally, you can find the Java SE 14 documentation for the java.lang.reflect.Array class, which provides static methods for creating and manipulating arrays using reflection, at the following link:
Both of these classes are part of the Java Standard Library and are included in the JDK (Java Development Kit) distribution.
Creating and manipulating arrays using reflection is a way of programmatically creating and modifying arrays at runtime, rather than at compile time. This can be useful in certain situations, such as when you don't know the size of the array until runtime or when you need to work with arrays of different types.
- ArrayList can only hold Reference types, not the primitive types.
- We don't need to declare the size of Array List - as we can add and remove elements from it on the go.
- ArrayList are type of entity known as generic
- Generics can only work with reference types
- For handling the primitives we have 8 Wrapper Classes
Those classes wrap a value of primitive type variable in an object. An object of Wrapper Class contains a single field whose type is its corresponding primitive.
We are covering String Class in this module and its descendants.
STRINGS ARE IMMUTABLE: the string object cannot be changed
StringBuilder Class STRINGBUILDERS objects are MUTABLE:
- the object can be modified (mutated)
- have many of the same methods as the String class (e.g. charAt(), indexOf(), length())
- have many methods the String class doesn't have
- Discussion on pass-by-value vs. pass-by-reference
Pass-by-value | Pass-by-reference | |
---|---|---|
What is passed in? | a copy of the variable data is made and being passed in | the memory address of the variable is passed in |
Is the original data affected? | no | yes |
Java only supports pass-by-value
- Methods overloading - SIGNATURE - combination of method's name and parameter list
- RECURSION - when a method calls itself - For a method to be recursive, it needs:
- At least one base case (no recursion in it)
- At least one recursive case(this is where recursion occurs)
- Open CMD in the file yours Java main method file is stored
- Run in CMD:
java nameOfYourFile.java arg1 arg2
Tensors and more specifics Matrices which are 2-dimensional Tensors
There's instruction document provided. I will create it in this repo for the time being. After completing this course I can move this project to the separate repo and work out missing bits:
- divide into separate files
- add tests for the methods
- publish on your GitLab
Paper and pen quick game of 9 fields, for two players, X and O Objective is to have 3 symbols in a row If no one gets 3 in a row, it's called a 'cat's game'
- X wins with 3 in a column
- X wins with 3 in a row
- X wins in a diagonal
- X wins in a reverse diagonal
In this section of the document, I'll describe the way I went about solving this project. This does not mean you have to do it exactly the way I did it, or at all. However, I do expect you to use good modularization, and divide some complexity of the project across multiple methods.
The methods I implemented(in addition to main, of course):
runGame()
- the game loop- initialize the winner as a string that is empty, since no one has one yet
- That string can be X, O or C which is the cat's game, that is, a tie
- create the game-board array of dimensions ROWS X COLS in this method
- call
initializeGame()
, which will fill the game-board 2D array with spaces - call
printCurrentBoard()
, which do the obvious - create the game loop that continues as long as the winner has not been found yet:
- the game loop must keep track of whose turn it is
- the game loop calls
getUserInput()
to obtain the user's input and modify the game-board if they select a valid move, and tell them to pick another cell if they select an invalid move - after user input, the game loop calls
printCurrentBoard()
since the game-board has been updated - the game loop calls
getWinner()
to establish if it is the end of the game of not - flip whose turn it is to prepare for the next round
- at the end of the game loop, we check if the game-board is full and a winner hasn't been selected yet, in which case we set winner equal to C, which means it's the cat's game
- Still inside my
runGame()
method, but after the loop, we tell the user if the cat has won, or if there is a winner X or O
- initialize the winner as a string that is empty, since no one has one yet
initializeGame()
- sets the cells of the 2D array to spaces- loops through the game-board and ensures all elements are set to a space
- Decide what character to use for empty game-board cell
printCurrentBoard()
- prints whatever is in the current game-board- prints the lines and the game-board according to their content
getUserInput()
- gets the user input, and if valid, sets the game-board appropriately- takes a parameter representing whose turn it is, and the game-board
- goes into a loop to determine if the input has been valid yet or not
- valid selections include values for rows and cols >= 0 and <= 2
- call
cellAlreadyOccupied()
to determine if we keep asking or consider the selected row and column to be legitimate or not - after the validation loop, fill in the game-board cell with X or O depending on the turn
cellAlreadyOccupied()
- returns ture if a given cell (by row and column) is already occupied- takes the row and column being tested, and the game-board as parameters
- returns whether the game-board has a space at that row and column, which would mean the cell is available
getWinner()
- returns "X", "O" if there is a clear winner, or a " " (a space) if there is no winner yet- take the game-board as parameter
- checks winning conditions for the rows
- checks winning conditions for the columns if there was no row winner
- checks winning condition for diagonal top-left to bottom-right if no column winner
- checks winning condition for diagonal bottom-left to top-right
- default return value is empty string - remember that in the game loop, our winner variable is capturing the return value of
getWinner()
, and if it is empty string "", then the loop will continue
isBoardFull()
- returns if the game-board is full or not- takes the game-board as a parameter
- loops through the game-board and determines if all the cells are occupied
- Hint: there are 9 total cells, if I count nine non-spaces, then that means the game-board is full
- this method is crucial for determining the cat's game, that is, that there is a tie - no winner
Strongly recommended not trying to write all the methods at once. For example, maybe try some "easier" methods that don't depend as much on the others, and then build your program up. Some relatively simple methods to work with at first, or at least get started:
initializeGame()
printCurrentBoard()
isBoardFull()
cellAlreadyOccupied()
java.utils.Arrays This class is a member of the Java Collections Framework.
Where you can find Java Collections Tutorial to go through next
In OOP, an object represents an entity that has both:
- behaviour
- data
This entity with its behaviour and data form OOP encapsulation. Which is first primary principle of OOP.
- Encapsulation
Refers to uniting behaviour and data in a single entity called an object.
Advantages of encapsulation:
- portability - we can combine various behaviour and data, and move/use it across project
- security - information/implementation hiding We write a Java Class, which works like a blueprint for objects.
- Inheritance
- Polymorphism
static
in front of a method/field means the method/field will be one for all class instances created.
No static
method/field is created as a new one for each class instance created and can have access to this
methods/fields.
UML stands for Unified Modeling Language and is not specific to JAVA
Three sections in Class Diagram:
- Class name
- Class fields
- Class methods
Class fields and methods take format:
<+/-> <identifier(name of field/method)> : <field/function return type>
where <+/->
stands for:
+
public type-
private type
My UML modeling tool: lucid.app
https://lucid.app/lucidchart/invitations/accept/inv_ff5e1a22-537f-45fb-8145-070f32e00bf0
Under the link my example on Rectangle Class
Java 14 Math docs
String.format()
java public static String format(String format, Object... args)
Parameters:
format
- A format string with specific syntax
args
- Arguments referenced by the format specifiers in theformat
string.
// little helper to know current working directory
System.out.println(System.getProperty("user.dir"));
PrintWriter class
PrintWriter class is very easy to work with!
BufferedWriter class
/* Walk through Java Collections Framework tutorial
* TODO: https://docs.oracle.com/javase/tutorial/collections/index.html
**/
/* Walk through Java Exceptions tutorial
* TODO: https://docs.oracle.com/javase/tutorial/essential/exceptions/index.html
**/