0% found this document useful (0 votes)
20 views

Day 1 - Basic Java Programming Java Core & Object Oriented Programming

Uploaded by

Tatsuya Shiba
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Day 1 - Basic Java Programming Java Core & Object Oriented Programming

Uploaded by

Tatsuya Shiba
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 55

1.

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 Platform


Java History - Gist

● Java is over 20 years old


● Initially called Oak but was later renamed to Java
● Create 1994 ad by James Gosling for their set-top box project back in 1991
● Introduced early 1995
● J2EE release in December 1999
● J2SE renamed to Java SE and J2EE renamed to Java EE in 2006
● JavaFX introduced in 2006
● Oracle acquired Sun Microsystems by 2010
● Currently most popular version is Java 8, the LTS version is Java 17, and the
latest version is Java 18
JRE and JDK

● 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)

JVM JVM JVM

Windows Mac Linux


Just In Time Compiler (JIT)
The just-in-time (JIT) compiler is a At Compile Time
program that turns Java bytecode into
.java Compiler .class
instructions that can be sent directly to
the processor

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

● Start Small - start with a small working program


● Incremental Development - keep adding small changes towards your goal,
code then test it, repeat this process
● KISS - Keep it simple stupid - Make your program simple and easy to
understand
● YAGNI - You Ain’t Gonna Need It - Don’t add features you don’t need yet, no
matter how cool that feature is

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?

80% of results comes from 20% of the effort - Pareto Principle

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 business 80% of your income comes from 20% of your sales

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;

public class MyFirstJavaProgram {


public static void main(String []args) {
System.out.println("Hello World");
}
}
Code Structure

package com.groundgurus.day1;

import java.text.SimpleDateFormat;
import java.util.Date;

public class WhatIsTheDateToday {


public static void main(String[] args) {
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
System.out.println("Today is " + sdf.format(new Date()));
}
}

package - set of hierarchical directories that organize codes


import - import code from a different package or library
class - the code that represents the code of the Java file
Day 1

Java Language
Fundamentals
Variables

● A variable is a temporary container for data.


● Naming a variable:
○ Case sensitive
○ Can start with a letter, underscore or dollar sign.
○ Can be a combination of letters, numbers, underscores or dollar signs.
○ White spaces is not allowed.
● Best practices
○ Variable should have meaningful names if possible.
○ The convention is a variable should start with a letter. Not an underscore or dollar sign.
○ Use camel case
● Examples
employeeName
department
attachments
Data Types 1 of 3

● A data type is a way to specify what type of data can be stored.


● They can be categorized to Primitive Types and Reference Types.
● Below are the primitive types:

Whole Number Floating Point Character Boolean


byte - 8 bits float - 16 bits char - 16 bits boolean
short - 16 bits double - 32 bits
int - 32 bits
long - 64 bits
Data Types 2 of 3

Below is the syntax for declaring a variable.


[data type] variableName = [value];

Examples:

int age = 28;


double sum;
Data Types 3 of 3

A local variable is declared inside a method.


[data type] variableName = [value];

int age = 28;

System.out.println(age);

A local variable cannot be used without a value.

double sum;
// code below does not compile
System.out.println(sum);
Literal Values 1 of 2
Whole number variables can contain whole numbers.

int myInt = 200;


long myLong = 300L;

Floating point variables can contain decimal numbers.


float myFloat = 505.200F;
double myDouble = 1024.98765;
Literal Values 2 of 2
A char type variable can contain a single character.

char someCharacter = 'a';


char slash = '/';
char backslash = '\\';
char unicodeNull = '\u0000';
String Class
A class that represents a collection of characters.

String name = "John Smith";


String message = "You Are \n Not Alone";
String amyPond = "The " + "Girl" + "Who" + "Waited";
Reference Types
Types that represent a class

Examples are:

String

BigDecimal

SimpleDateFormat
Scopes

● A scope is how long a variable’s value is accessible

● 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.

public static void main(String[] args) {


int aVariable = 500;
}
Scopes and Blocks 2 of 2
You can define a block inside the method. This will limit the lifespan of
the variable.

public static void main(String[] args) {


int aVariable = 500;

{
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

postfix (unary) expr++ expr--

prefix (unary) ++expr --expr +expr -expr ~ !

multiplicative */%

additive +-

shift << >> >>>

relational < > <= >= instanceof

equality == !=
Operator Precedence 2 of 2
Operators Precedence

bitwise AND &

bitwise exclusive OR ^

bitwise inclusive OR |

logical AND &&

logical OR ||

ternary ?:

assignment = += -= *= /= %= &= ^= |= <<= >>= >>>=


Expressions 1 of 3
Expressions can be used to evaluate values.
int total = 10 + 20 + 30 + 40 + 50;

String resultMessage = "You have a total of " + total;

Increment and decrement operators either increments or decrements a


value by 1.

int counter = 100;


int secondCounter = counter++;
// prints 101
System.out.println(++secondCounter);
Expressions 2 of 3
Relations, equality and logical operators return boolean result.
double salaryA = 25000.0;
double salaryB = 30000.0;

System.out.println(salaryA == salaryB);
System.out.println(salaryA != salaryB);

Bitwise operators can return either boolean or whole number result.


System.out.println(10 & 3); // returns 2
System.out.println(45 + 2 & 20 + 1); // returns 68
Expressions 3 of 3
Since Java 7 whole numbers and floating point values can now have underscores for
readability.
double ceoSalary = 927_987_654_321.0;
Comments 1 of 3
Single line comment
// this gets the balance of stuff
double balance = (400.0 + 300.0) / 5;

Multi line comment


/* this gets the balance of stuff
and more...
*/
double balance = (400.0 + 300.0) / 5;
Comments 2 of 3
Java documentation comment

/**
This program is used to handle the accounts of all employees.

@author Rory Williams


@since July 3, 2018
@version 1.0
*/
public class AccountManager {
public static void main(String[] args) {
// code that handles accounts goes here
}
}
Comments 3 of 3

Best practices

● Use single-line or multi-line comments inside a method


● Use javadoc comments in methods and classes
Day 1

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 >= 75) {


System.out.println("You've passed");
} else {
System.out.println("You've failed");
}
If Else If Statement
int score = 91;

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)

String position = "Mid-Level Programmer";

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

Below is the traditional for loop

String days[] = { "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun" };


for (int i = 0; i < days.length; i++) {
System.out.println(days[i]);
}
Enhanced For Loop (Java 5)

Below is the enhanced for loop

String days[] = { "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun" };


for (String day : days) {
System.out.println(day);
}
While Loop
Random random = new Random();
int randomValue = random.nextInt(100) + 1;

// will continue to loop until value is between 50-59


while (!(randomValue >= 50 && randomValue <= 59)) {
System.out.println(randomValue + " ...not yet");
randomValue = random.nextInt(100) + 1;
}

System.out.println(randomValue + " ...Done!");


Do While Loop
Random rnd = new Random();
int dice;
int guess = 3;

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

The break label stops the loop


for (int i = 1; i <= 10; i++) {
if (i == 4) {
break;
}
System.out.println(i);
}
Loop Controls - Continue

The continue keyword continues the loop


for (int i = 1; i <= 10; i++) {
if (i == 4) {
continue;
}
System.out.println(i);
}
Loop Controls - Break/Continue Label

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)

Just use an if statement to check

Output

Enter first number: 4

Enter second number: 5

5 is higher than 4
Day 1 - Exercise 4 (Medium)

Write a program that takes two inputs and prints out a rectangle using asterisks

Use looping statements

Output:

Enter width: 4

Enter height: 5

****

****

****

****

****

You might also like