Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
10 views

Java Workshop Version Student

for who want learn java

Uploaded by

muhammad mahdi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Java Workshop Version Student

for who want learn java

Uploaded by

muhammad mahdi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 130

JAVA

WORKSHOP
Introduction to Java

SULAM PROJECT UTHM


WHAT IS PROGRAMMING
LANGUAGE?
a way for programmers (developers) to
communicate with computers

consist of a set of rules that allows string


values to be converted into various ways of
generating machine code
TYPES OF PROGRAMMING LANGUAGES

uses procedures or functions to


Procedural abbreviate and categorize the code into
reusable blocks is a procedural
Programming programming language
eg: C, Pascal and FORTRAN

more flexible and is composed of a series


Functional of functions which focused the use of
model computations and data
Programming transformation
eg: Haskell, Clojure, Lisp and Scala
TYPES OF PROGRAMMING LANGUAGES

Scripting the type of language that is


interpreted rather than compiled
Languages eg: Phyton, Perl and Bash

based on the concept of objects,


Object-oriented which focuses on organizing code into
reusable objects that have properties
Programming and behavior
eg: Java, C++, and Python
BUT .....

THIS WORKSHOP WILL


COVER JAVA
PROGRAMMING
INTRODUCTION
Java was created by Sun Microsystems
team led by Patrick Naughton and James
Gosling (1991).
Sun Microsystems was purchased by
Oracle in 2010
Java is originally named as Oak (Gosling
liked the look of an oak tree that was right
outside his window at Sun).
3 EDITIONS OF THE JAVA API

Java Standard Edition (Java SE)


to develop client-side applications
can run standalone or as applets running from a Web browser

Java Enterprise Edition (Java EE)


to develop server-side applications, such as Javaservlets,
JavaServer Pages (JSP), and JavaServer Faces (JSF)

Java Micro Edition (Java ME)


to develop applications for mobile devices, such as cell phone
Spotify Twitter Netflix Instagram

Some popular Java Application


WHAT IS INTEGRATED
DEVELOPMENT
ENVIRONMENT?
a software application that helps
programmers develop software code
efficiently
MOST POPULAR
IDES FOR JAVA
Eclipse Netbeans

IntelliJ IDEA BlueJ


RUN JAVA PROGRAMS

JVM (Java Virtual Machine) is an abstract machine that enables


01 your computer to run a Java program.

02 When you run the Java program, Java compiler first compiles
your Java code to bytecode (with extension .class file). Then, the
JVM translates bytecode into native machine code (set of
instructions that a computer's CPU executes directly)
RUN JAVA PROGRAMS

Java is a platform-independent language. It's because when you


03 write Java code, it's ultimately written for JVM but not your physical
machine (computer). Since JVM executes the Java bytecode which is
platform independent, Java is platform-independent.

JVMs exist on many platforms:


04 • Windows • Macintosh • Linux • BSD • Etc.
JAVA PROGRAM DEVELOPMENT PROCESS
saves Java statements
Text source code
editor y (.java)
ead b
is r

Java produces byte code


compiler by (.class)
reted
nterp
is i
Java virtual program
result in
machine execution
DOWNLOAD THE JAVA
DEVELOPMENT KIT (JDK)
STEP 1
Download JDK at
https://download.oracle.com/java/17/latest/jdk-
17_windowsx64_bin.exe
DOWNLOAD THE JAVA
DEVELOPMENT KIT (JDK)
STEP 2
After finish downloading, double click on the installer
to proceed with the installation
DOWNLOAD THE JAVA
DEVELOPMENT KIT (JDK)
STEP 3
A folder named Java under Program Files directory will
be created upon successful installation.
JAVA
WORKSHOP
Session 1: Syntax and Operation

SULAM PROJECT UTHM


ECLIPSE SETUP

Click This!
Run Button

Package Explorer
Console
Coding Area
START CODING USING ECLIPSE

Click Create a Java Project


Type "JavaWorkshop"

Uncheck this

Click Finish
Right click src folder in Package Manager

Choose New > Class


Type JavaWorkshop at here

Tick this

Click Finish
CODING AREA EXPLAIN
main will always
execute when
we click run

Type all your code


inside here!
JAVA ERROR

Your code have error! Click


Cancel to check your code!
JAVA ERROR

Your code have error! Check your code!


JAVA OUTPUT
Java Output
System.out.println() is used to show
the thing on the console. Type what
you want to show in the bracket!
Java Output
To show a text in the console, type the text with two double quote.
Java Output
To show a text in the console, type the text with two double quote.
WATCH OUT!!!
Don't forget to add semicolon (;)
after each line!!! (Except after "}")
TRY IT!
JAVA COMMENTS
Java Comment
Use double slash (//) in front of the text, Java will
ignore things behind the double slash.
(The comment will became green colour in
Eclipse)
Java Comment
If you want to write comment at several line,
you can use /* and */. Use /* at the start of the
comment and */ at the end of the comment.
(We often call this multiline comment)
Java Output
To show a text in the console, type the text with two double quote.
Java Output
To show a text in the console, type the text with two double quote.
TRY IT!
JAVA VARIABLES
Java Variable
Variables is a container that store
data value.
Java Variable
Declare the variable first before you use it! You need to tell Java what is
the value, name and data type of your variable.
Java Variable
Declare the variable first before you use it! You need to tell Java what is
the value, name and data type of your variable.
Java Variable
If you want to declare multiple variable with the same data type, you can
also write like this!
WATCH OUT!!!
Don't add string and integer together!!!
WATCH OUT!!!
Don't add string and integer together!!!
Java Variable
Use a unique name that are descriptive when declaring
variable!!!
Java Variable
General rules to declare variables.
Names can contain letters, digits, underscores, and dollar signs
Names must begin with a letter
Names should start with a lowercase letter and it cannot contain
whitespace
Names can also begin with $ and _
Names are case sensitive ("myVar" and "myvar" are different
variables)
Reserved words (like Java keywords, such as int or boolean) cannot
be used as names
Java Variable (Advanced)
Add a "final" keyword to prevent the value of the variable changed!
JAVA DATA TYPES
Java Data Types
Data Types is what is the type of
the value of your variable.
JAVA DATA TYPES

1. int 2.double 3. char

4. boolean 5. String
INT
What is an int?
Int (Integer) is a data type that
store whole number that include
negative, positive and 0 numbers.
Negative integers: -1, -46,
-67463
Positive integers: 5, 84, 77855
Zero: 0
EXAMPLE
JAVA INT DATA TYPE (ADVANCED)

Data Types Used when your integer How to declare

byte -128 to 127 byte myNum = 100;

short -32768 to 32767 short myNum = 5000;

-2147483648 to
int int myNum = 100000;
2147483647

-9223372036854775808 to
long long myNum = 15000000000L;
9223372036854775807
DOUBLE
What is a double?
Double is a data type that
store decimal numbers.
Negative double: -1.334,
-463.5, -0.3544
Positive double: 5.67,
0.345, 54323.67889
EXAMPLE
JAVA DOUBLE DATA TYPE (ADVANCED)

Used when your data


Data Types How to declare
have

6, 7 number after the


float float myNum = 43.64f;
decimal

15 number after the double myNum =


double
decimal 436.65656;
STRING
What is a string?
String is a data type that store text.
Sequence of characters: "What is a
string?", "Hello"
Sequence of digits: "123542",
"4546", "-545645"
Sequence of symbol: "#&*^#&"
Space: " ", " "

Noted: String always start and end


with double quote (").
EXAMPLE
PROBLEMS
SPECIAL CHARACTER
JAVA SPECIAL CHARACTER

Code Result Examples

\' ' String myString = "This is \'A\'";

\" " String myString = "This is \"A\"";

\\ \ String myString = "This is \\A\\";

\n New Line String myString = "This is \nA\n";

\t Tab String myString = "This is \tA\t";


EXAMPLE
EXAMPLE
EXAMPLE
CHAR
What is a char?
Char is a data type that store
single letter, digit, symbol or
blank space.
Single letter: 'A', 'a', 'y'
Sequence of digits: '4', '5', '0'
Sequence of symbol: '#', '&'
Space: ' '

Noted: Char always start and end


with single quote (').
EXAMPLE
JAVA CHAR DATA TYPE (ADVANCED)

Actually we can also use ASCII code to enter character that we


want!

For Example:
BOOLEAN
What is a boolean?
Boolean (or commonly we write
as bool) is a data type that only
store 2 value, true or false.
EXAMPLE
TRY IT!
JAVA OPERATORS
WHAT IS OPERATOR?
Operator are used to perform operations on variables and values.

1.Arithmetic Operator 2.Comparison Operator 3.Logical Operator


Used to find value. Used to find true or Used to find true or
false. false.
ARITHMETIC OPERATORS
Operator Name Description Example

+ Addition Adds together two values x+y

Subtracts one value from


- Subtraction
another
x-y

* Multiplication Multiplies two values x*y

/ Division Divides one value by another x/y


ARITHMETIC OPERATORS
Operator Name Description Example

Returns the division


% Modulus
remainder
x%y

Increases the value of a


++ Increment
variable by 1
++x

Decreases the value of a


-- Decrement
variable by 1
--x
WHAT IS MODULUS?
Modulus is the remainder of the division!!!

Example:
1723 % 5 = 3, because 15 % 7 = 1, because 34 % 2 = 0, because
INCREMENT AND DECREMENT
++number means number+1 !!!

--number means number -1 !!!


Let's try: x=1
++x?
++x?
--x?
++x?
--x?
TRY IT!
COMPARISON OPERATORS
Operator Name Example

== Equal to x == y

!= Not equal x != y

> Greater than x>y

< Less than x<y

>= Greater than or equal to x >= y

<= Less than or equal to x <= y


TRY IT!
LOGICAL OPERATORS
Operator Name Description Example

Returns true if both statements are


&& And
true
x < 5 && x < 10

Returns true if one of the


|| Or
statements is true
x < 5 || x < 4

Reverse the result, returns false if


! Not
the result is true
!(x < 5 && x < 10)
Case 01
A && B, A is true and B is true, so A && B is
true or false?

AND Case 02

OPERATOR A && B, A is false and B is true, so A && B is


true or false?

Given A and B, A and B must be


Case 03
true so that all statement is true.
A && B, A is true and B is false, so A && B is
true or false?

Case 04
A && B, A is false and B is false, so A && B is
true or false?
Case 01
A || B, A is true and B is true, so A || B is
true or false?

OR Case 02

OPERATOR A || B, A is true and B is false, so A || B


is true or false?
Given A or B, either one must
be true so that all statement is Case 03
true. A || B, A is false and B is true, so A || B
is true or false?

Case 04
A || B, A is false and B is false, so A || B
is true or false?
NOT Case 01
OPERATOR A is true, !A is true or false?

Given A is true, Not A is false, and


vice versa. Case 02
A is false, !A is true or false?
TRY TO ANSWER THE QUESTIONS
1. x=10, x<10 || x > 10

2. x=34, x<=34 && x == 34

3. x=50, x>49 && x<51

4. x=13, !(++x == 14 && x+4 != 0)

5. x= true, x && 1

6. x= 2, !(!(2+2) != !(2*2))
TRY IT!
JAVA INPUT
Java Input
To use the Java Input, first, you need to write
"import java.util.Scanner" at the top of the code,
then write "Scanner scanner = new
Scanner(System.in)" at the top of the main function
Java Input
After we declare scanner, we can use
scanner to get the user input. You can
decide the data type of the input.
Java Input
Type your answer in the console.

Type your answer here!!!


If you want data in double
Declare age as double and use
nextDouble()

If you want data in int


Declare age as int and use nextInt()

If you want data in string


Declare age as String and use
nextLine()

If you want data in boolean


Declare age as boolean and use
nextBoolean()
TRY IT!
CHECK MY
UNDERSTANDING
JAVA
WORKSHOP
Session 2: Control Statement

SULAM PROJECT UTHM


Java supports the usual logical conditions
from mathematics:
Less than: a < b
Less than or equal to: a <= b
Greater than: a > b LOGIC
Greater than or equal to: a >= b
Equal to a == b
Not Equal to: a != b
JAVA IF ... ELSE
IF SYNTAX
TRY IT NOW
TRY IT WITH VARIABLES
IF ... ELSE SYNTAX
TRY IF ... ELSE NOW
IF ... ELSE IF SYNTAX
TRY IT NOW
JAVA SWITCH
SWITCH CASE SYNTAX
TRY IT NOW
JAVA LOOP
Why we use looping?

Loops can execute a block of code as


long as a specified condition is reached.
Loops are handy because they save time,
reduce errors, and they make code more
readable.
WHILE LOOP
WHILE LOOP SYNTAX
TRY IT NOW
DO WHILE LOOP
DO WHILE LOOP SYNTAX
TRY IT NOW
FOR LOOP
FOR LOOP SYNTAX
TRY IT NOW
DIFFERENCE
BETWEEN
LOOP
JAVA ARRAYS
ARRAY
Arrays are used to store multiple values in a
single variable, instead of declaring separate
variables for each value.

To declare an array, define the variable type with


square brackets:

The index number for an array start form 0.


EXAMPLE
EXAMPLE 2
ACCESS THE ELEMENTS OF AN
ARRAY
CHANGE AN ARRAY ELEMENT
ARRAY LENGTH
CHECK MY
UNDERSTANDING
THANK YOU
link feedback

You might also like