Day 1 - Basic Java Programming Java Core & Object Oriented Programming
Day 1 - Basic Java Programming Java Core & Object Oriented Programming
Introduce yourself
2. Ground Gurus
3. Partner/Senior Consultant
4. 14 years
Basic Java
Programming
Java Core & Object Oriented Programming
Agenda - Day 1
● The Java Platform
● Java Language Fundamentals
● Control Flow Structures
● Exercises
Day 1
● The Java Runtime Environment (JRE) contains the Java libraries and the
Java Virtual Machine (JVM)
● The Java Development Kit (JDK) contains the JRE, compilers and related
tools for Java development.
The Java Virtual Machine
Java File (.java)
A Java virtual machine (JVM) is an
abstract computing machine that Java
enables a computer to run a Java Compiler
program.
Java Class (.class)
At Runtime
Native JIT
Code Compiler
Integrated Development Environment (IDE)
An integrated development
environment (IDE) is a software
application that provides
comprehensive facilities to computer
programmers for software
development.
Programming Development Process
https://www.leepoint.net/principles_and_practices/10techniques.html
80/20 Rule
Say you work in a company that requires semi-formal attire from Monday to
Thursday, and allows casual attire on Fridays
If you have two sets of semi-formal pants and one maon jeans, do you even need to buy
more?
From Vilfredo Pareto, when he noticed that in the early 1900’s 80% of the land in
Italy is owned by 20% of the people
In a major project 20% of your initial effort provides 80% of your output
80/20 Rule Applied to Learning Java
The first 20% of your effort on learning Java provides 80% of what you need to
accomplish
Say for a week, you only have free time to learn during weekends, and if you
consider that the time your only active is 8 hours a day, then give learning Java 2
hours every Saturday and Sunday
Java has a lot to offer and it can be overwhelming, but at the same time can also
be exciting
Day 1
Java Development
Overview
Basic Java Code Structure
package ph.groundgurus.helloworld;
package com.groundgurus.day1;
import java.text.SimpleDateFormat;
import java.util.Date;
Java Language
Fundamentals
Variables
Examples:
System.out.println(age);
double sum;
// code below does not compile
System.out.println(sum);
Literal Values 1 of 2
Whole number variables can contain whole numbers.
Examples are:
String
BigDecimal
SimpleDateFormat
Scopes
● Values that are no longer accessible are garbage collected (This only applies
to reference types) or removed from the memory
Scopes and Blocks 1 of 2
The lifespan of a variable is defined by the opening and closing braces
of a method.
{
String message = "Kidneys! I have new kidneys! I don't
know the color";
}
// code below does not compile
System.out.println(message);
}
Operator Precedence 1 of 2
Operators Precedence
multiplicative */%
additive +-
equality == !=
Operator Precedence 2 of 2
Operators Precedence
bitwise exclusive OR ^
bitwise inclusive OR |
logical OR ||
ternary ?:
System.out.println(salaryA == salaryB);
System.out.println(salaryA != salaryB);
/**
This program is used to handle the accounts of all employees.
Best practices
Control Flow
Structures
Conditional Statements
if (boolean expression) {
switch (variable) {
// code
[case value:
} [else if (boolean expression) {
// code
// code
break;
}] [else {
]
// code
[ default:
}]
// code
break;]
}
If Else Statement
int score = 90;
if (score == 100) {
System.out.println("Fantastic!");
} else if (score >= 90 && score <= 99) {
System.out.println("Geronimo!");
} else if (score >= 80 && score <= 89) {
System.out.println("Allons-y!");
} else if (score >= 75 && score <= 79) {
System.out.println("Meh!");
} else {
System.out.println("Doh!");
}
Switch Statement
String position = "Mid-Level Programmer";
switch (position) {
case "Programmer":
System.out.println("Teach me master");
break;
case "Mid-Level Programmer":
System.out.println("Learning is the key to success");
break;
case "Senior Programmer":
System.out.println("Let me show you how to do it");
break;
default:
System.out.println("HTML is a programming language");
}
Enhanced Switch Statement (Introduced in Java 13)
switch (position) {
case "Programmer" -> System.out.println("Teach me master");
case "Mid-Level Programmer" -> System.out.println("Learning is the key to success");
case "Senior Programmer" -> System.out.println("Let me show you how to do it");
default -> System.out.println("HTML is a programming language");
}
Looping Statements
for (initialization; termination; increment) {
// code do {
} // code
} while (expression);
while (expression) {
// code
}
For Loop
do {
dice = rnd.nextInt(6) + 1;
if (dice != guess) {
System.out.println("We rolled " + dice + " Let's keep rolling!");
}
} while (dice != guess);
System.out.println("Got it!");
Loop Controls - Break
You can break or continue a defined label (Use feature this sparingly to avoid
spaghetti code)
hoopla:
for (int i = 1; i <= 10; i++) {
for (int j = 1; j <= 5; j++) {
if (i == 2 && j == 2) {
break hoopla;
} else {
System.out.println("i = " + i + ", j = " + j);
}
}
}
Day 1
Exercises
Day 1 - Exercise 1 (Easy)
Write a program that takes two number inputs to print the sum, difference, product
and quotient. (Research on the class Scanner)
For this, use the appropriate operators. To handle decimal values use float or
double
Output
Enter first number: 4
Enter second number: 5
The sum is 9
The difference is -1
The product is 20
The quotient is 0.8
Day 1 - Exercise 2 (Easy)
For this just edit the example provided in the slides and print the date and time.
See the Java API documentation
Output
Today is 06/14/2019 9:30:34
Day 1 - Exercise 3 (Easy)
Write a program that takes two integer inputs and identifies which is higher. (Easy)
Output
5 is higher than 4
Day 1 - Exercise 4 (Medium)
Write a program that takes two inputs and prints out a rectangle using asterisks
Output:
Enter width: 4
Enter height: 5
****
****
****
****
****