SlideShare a Scribd company logo
Introduction to Java Programming

Junior Programmer Camp #8
Chapter 1: Elementary Programming
What is Java?
• A high-level programming language
• Useful for developing business applications
• Java Development Kit download:
http://www.oracle.com/technetwork/java/jav
ase/downloads/index.html

• Case sensitive
What is DrJava?
• A programming tool used for writing code in
Java
• Can be downloaded from www.drjava.org
• Simple and easy to use for beginners
DrJava
Typical Java Code Skeleton
public class Program{
public static void main(String[] args) {
// your main code goes here
Comments
• Sometimes you may want to add some notes
to your Java code, without any effect to the
code.
• In this case, you can write comments.
Two ways:
– System.out.println("test"); // print a test string
– /*
This is a multiple-line comment.
More line
Even more line
Blah blah
*/
Simple Program
public class Program {
public static void main(String[] args) {
System.out.println(“Hello!! JPC#8");
}
}

OUTPUT:
Hello!! JPC#8
Basic Arithmetics
public class Program {
public static void main(String[] args) {
System.out.println(1 + 2);
}
}

OUTPUT:
3
Basic Arithmetics
•
•
•
•
•

Addition: 2 + 3
Subtraction: 4 - 5
Multiplication: 6 * 7
Division: 8 / 4
Remainder: 5 % 3
How about 2 + 3 * 4 ?
• Does precedence matter?
• Can we solve precedence with parentheses?
Variable Types
•
•
•
•
•

Integers: byte, short, int, long
Floating-point numbers: float, double
Single character: char
Text: String
True/False: boolean
Floating-Point Numbers
• You can also use numbers with decimal points:
2.3, –1.5
• Integers:
…, -3, -2, -1, 0, 1, 2, 3, …
• Real numbers:
0.0, 3.14, -2.4, 327.8
Floating-Point Numbers
• What is 3.0 / 4.0 in Java?
• How about 3 / 4 ?
Declaring and Using Variables
Name of variables
- Not preserve words
- Must begin with letters or ‘_’ or ‘$’
<data_type> <variable name>;

- Example:
int x ;
double pi_1 ;
char ch2
boolean a ;
Declaring and Using Variables
Assignment
- Variable or numeric primitive type
- Combination of values and/or variable with
numeric operators
<variable_name> = <variable or value>

- Example:
x = 10 ;
pi = 3.14159 + 0.11 ;
char a = „b‟ ;
y = x ;
Declaring and Using Variables
int x;
int y;
int z;
x = 2;
y = 5;
z = x + y;
System.out.println(x + " + “ + y + " = “ + z);

OUTPUT:
2 + 5 = 7
Multiple Variable Declarations
Instead of
int x;
int y;
int z;

You can also use
int x, y, z;

to declare multiple variables all at once.
Variable Initialization
Instead of
int x;
int y;
x = 2;
y = 5;

You can also use
int x = 2, y = 5;
Shorthand Operators
Operator
+=
-=
*=
/=
%=

Example
i += 8
i -= 8.0
i *= 8
i /= 8
i %= 8

Equivalent
i=i+8
i = i - 8.0
i=i*8
i=i/8
i=i%8
Shorthand Operators
• ++var
• var++
• --var
• var--
Shorthand Operators
Example:

++var

int x = 3;
System.out.print(x);
// 3
System.out.print(++x); // 4
System.out.print(x)
// 4
Shorthand Operators
Example:

var++

int x = 3;
System.out.print(x);
// 3
System.out.print(x++); // 3
System.out.print(x)
// 4
Type conversion
Use only primitive data type except boolean

- (<type>)<expression>

 casting

=

Ex.

int i = „a‟; //

i
a
x
y

=
=
=
=

i = 97

(int)‟B‟; // i = 66
(char)80 ; // a = „P‟
(int)1.2 ; // x = 1
(double)2; // y = 2.0

• The variable to keep converted type value must be
the type that can keep the value.
Character and String
• Use char type to keep an ASCII character
• Use String type to keep characters (text)
• char type is capable with arithmetic operator

Example:
char ch = „B‟;
System.out.println((char)(ch + 1)); // „C‟
String s = “This is a sentence.”;
Getting Inputs from the User
import java.util.Scanner;
public class Program {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int x = sc.nextInt();
x = x * 2;
System.out.println(x);
}
}
INPUT:
5
OUTPUT: 10
Multiple Inputs
import java.util.Scanner;
public class Program {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int x = sc.nextInt();
int y = sc.nextInt();
System.out.println(x);
System.out.println(y);
}
}
INPUT:
1
OUTPUT:
1
2
2
Inputting Text
import java.util.Scanner;
public class Program {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter your name: ");
String name = sc.nextLine();
System.out.println("Hello, "+ name + "!");
}
}
INPUT:
John
OUTPUT: Hello, John!
Give it a try!
• Write a program to get 2 numbers from the user and
output the sum, the difference, the product, and the
quotient of those 2 numbers.
• Hint: Follow the previous example.
INPUT:
OUTPUT:

7
5
7
7
7
7

+
*
/

5
5
5
5

=
=
=
=

12
2
35
1.4
Programming Errors
• Most common for us.
• Syntax error, Runtime error, Logic error
Chapter 2: Selections
Introducing boolean Data Type
• Normally you can use int to store integer
numbers, double to store floating-point
numbers.
• What if all you want is to store a truth
value: either true or false?
Answer: use boolean data type
Boolean Expressions
• Expression that return boolean value
(True/False)
• Operators:
-

<
-!
<=
- &&
>
- ||
>=
-^
== (not same with one = the assignment operator)
!=

• Precedence matters.
Do/Don’t?
• Sometimes you will want your program
to decide whether to do something or
not based on some conditions.
General Form of if
if(condition) {

statements to do if condition is true
}
If This Then Do That
if (1 == 2) {
System.out.println("1 is equal to 2");
if (1 != 2) {
System.out.println("1 is not equal to 2");

OUTPUT:
1 is not equal to 2
General Form of if-else
if(condition) {

statements to do if condition is true
} else{

statements to do if condition is false
}
Do This or That
if (1 == 2) {
System.out.println("1 is equal to 2");
} else {
System.out.println("1 is not equal to 2");
}

OUTPUT:
1 is not equal to 2
General Form of if-elseif-else
if(condition 1) {

statements to do if condition 1 is true
} else if (condition 2) {
statements to do if condition 2 is true
} else if (condition 3) {
statements to do if condition 3 is true
} else{

statements to do if none of the conditions is
true
}
// You can have as many conditions as you like!
Do This or This, otherwise That
if (3 < 2) {
System.out.println("3 is less than 2");
} else if (3 > 2) {
System.out.println("3 is greater than 2");
} else{
System.out.println("3 is equal to 2");
}

OUTPUT:
3 is greater than 2
Using boolean Data Type
boolean check = (1 < 2);
if(check) {
System.out.println("1 < 2");
} else{
System.out.println("1 >= 2");
}

• The condition part of the if statement can be a
boolean expression (1 < 2) or a boolean variable
(check)!
Condition with Multiple Cases
• In some cases, you may need multiple
condition statements to achieve your goals.
• Writing them as multiple else-if statements
can be tedious.
• Use of switch statement is recommended
(with some exceptions).
General Form of switch
switch (expression) {
// expression can be int, char, or boolean
case value1:

statements to do when expression == value1
break;
case value2:

statements to do when expression == value2
break;
// add more cases as needed
default:

statements to do when nothing above matches
}
Example Usage of switch
// imported Scanner and instantiated one
System.out.print("Enter a number between 1 –3: ");
int x = sc.nextInt();
switch (x) {
case 1 : System.out.println("Entered 1");
break;
case 2 : System.out.println("Entered 2");
break;
case 3 : System.out.println("Entered 3");
break;
default: System.out.println("Entered else...");
}
Conditional Operator
• Also known as expression shortcut
• General form:
expression ? value_true : value_false
Conditional Operator
Instead of writing
int x = 0;
int y;
if(x == 0) {
y = 0;
} else {
y = x;
}

You can also use this:
int x = 0;
int y = x == 0 ? 0 : x;
Give it a try!
• Write a program to get 3 numbers: x, y, and z. The
program should output whether x + y = z or not.
• Can you do this with a switch statement?
INPUT:
3
4
8

OUTPUT:
3 + 4 != 8
Chapter 3: Repetition, Iteration, Loops
Iteration Fundamentals
• Sometimes you need to do certain things over
and over again, probably in the same or
similar manner.
• Writing 1000 Java statements to display
numbers 1 to 1000 is not feasible: you
wouldn't want to try to copy-and-paste the
code thousand times.
• In this case, you can make use of the iteration
feature of Java instead.
General Form of while
while (condition) {
statements to do while the condition is true

}
Example While 1-100
int i= 1;
while (i<= 100) {
System.out.println(i);
i= i+ 1;
}

OUTPUT:
1
2
3
…
100
Infinite Loop
• Get stuck in an infinite loop because
condition to exit the loop is tend to never met.
• Should use condition that can exit the loop.
(e.g.: exit when a value of variable exceed
max, when the value hit 0 or something)
General Form of do-while
do{

statements to do, and to continue to do while
the condition is true
} while (condition);
Example Do-while 1-100
int i= 1;
do {
System.out.println(i);
i= i+ 1;
} while (i <= 100);

OUTPUT:
1
2
3
…
100
While and do-while differences
• For the while loop, the condition is checked
before any statement is executed. Therefore, if
the condition is false in the first place, no
statement in the loop will be executed.
• For the do-while loop, in contrast, the
statements in the loop are executed once at
first. Then, for each iteration, the condition is
checked in the same way as in the while loop.
General Form of for
for (initialization; condition; iteration) {

statements to do as long as condition is true
}
1 –100 using for
Use
for (int i = 1; i <= 100; i++) {
System.out.println(i);
}

Instead of
int i= 1;
while (i<= 1000) {
System.out.println(i);
i++; // Same as i= i+ 1;
}

• Each type of loop can be written in another type of
loop equivalently
Give it a try!
• Write a game that let the player "guess" a random number
generated by the computer between 1 and 100. Each turn,
display whether the input number is less than or greater than
the random one. Game ends when the player guesses
correctly. Otherwise, keep asking for another number.
• To random a number between 5 to 70:
– int x = (int) (Math.random() * (70 – 5 + 1) + 5);

• Optional features:
– The allowed min and max should change accordingly for
each turn. For example, if the random number is 40, and
the user guesses 50, the game should now allow only
numbers between 1 and 49 in the next turn.
Ad

More Related Content

What's hot (20)

Control statements in java programmng
Control statements in java programmngControl statements in java programmng
Control statements in java programmng
Savitribai Phule Pune University
 
Nesting of if else statement & Else If Ladder
Nesting of if else statement & Else If Ladder Nesting of if else statement & Else If Ladder
Nesting of if else statement & Else If Ladder
Vishvesh Jasani
 
Conditional and control statement
Conditional and control statementConditional and control statement
Conditional and control statement
narmadhakin
 
Control structures in C++ Programming Language
Control structures in C++ Programming LanguageControl structures in C++ Programming Language
Control structures in C++ Programming Language
Ahmad Idrees
 
Unit iii
Unit iiiUnit iii
Unit iii
snehaarao19
 
Control structure of c
Control structure of cControl structure of c
Control structure of c
Komal Kotak
 
Control structure in c
Control structure in cControl structure in c
Control structure in c
baabtra.com - No. 1 supplier of quality freshers
 
Java conditional statements
Java conditional statementsJava conditional statements
Java conditional statements
Kuppusamy P
 
Simple if else statement,nesting of if else statement &amp; else if ladder
Simple if else statement,nesting of if else statement &amp; else if ladderSimple if else statement,nesting of if else statement &amp; else if ladder
Simple if else statement,nesting of if else statement &amp; else if ladder
Moni Adhikary
 
Structure in programming in c or c++ or c# or java
Structure in programming  in c or c++ or c# or javaStructure in programming  in c or c++ or c# or java
Structure in programming in c or c++ or c# or java
Samsil Arefin
 
Control structure
Control structureControl structure
Control structure
Samsil Arefin
 
Control structures in c
Control structures in cControl structures in c
Control structures in c
baabtra.com - No. 1 supplier of quality freshers
 
Control Structures
Control StructuresControl Structures
Control Structures
Ghaffar Khan
 
Java control flow statements
Java control flow statementsJava control flow statements
Java control flow statements
Future Programming
 
Control structures in C
Control structures in CControl structures in C
Control structures in C
baabtra.com - No. 1 supplier of quality freshers
 
12. Java Exceptions and error handling
12. Java Exceptions and error handling12. Java Exceptions and error handling
12. Java Exceptions and error handling
Intro C# Book
 
Control structures i
Control structures i Control structures i
Control structures i
Ahmad Idrees
 
C Programming: Control Structure
C Programming: Control StructureC Programming: Control Structure
C Programming: Control Structure
Sokngim Sa
 
Operators in java
Operators in javaOperators in java
Operators in java
Then Murugeshwari
 
Control and conditional statements
Control and conditional statementsControl and conditional statements
Control and conditional statements
rajshreemuthiah
 
Nesting of if else statement & Else If Ladder
Nesting of if else statement & Else If Ladder Nesting of if else statement & Else If Ladder
Nesting of if else statement & Else If Ladder
Vishvesh Jasani
 
Conditional and control statement
Conditional and control statementConditional and control statement
Conditional and control statement
narmadhakin
 
Control structures in C++ Programming Language
Control structures in C++ Programming LanguageControl structures in C++ Programming Language
Control structures in C++ Programming Language
Ahmad Idrees
 
Control structure of c
Control structure of cControl structure of c
Control structure of c
Komal Kotak
 
Java conditional statements
Java conditional statementsJava conditional statements
Java conditional statements
Kuppusamy P
 
Simple if else statement,nesting of if else statement &amp; else if ladder
Simple if else statement,nesting of if else statement &amp; else if ladderSimple if else statement,nesting of if else statement &amp; else if ladder
Simple if else statement,nesting of if else statement &amp; else if ladder
Moni Adhikary
 
Structure in programming in c or c++ or c# or java
Structure in programming  in c or c++ or c# or javaStructure in programming  in c or c++ or c# or java
Structure in programming in c or c++ or c# or java
Samsil Arefin
 
Control Structures
Control StructuresControl Structures
Control Structures
Ghaffar Khan
 
12. Java Exceptions and error handling
12. Java Exceptions and error handling12. Java Exceptions and error handling
12. Java Exceptions and error handling
Intro C# Book
 
Control structures i
Control structures i Control structures i
Control structures i
Ahmad Idrees
 
C Programming: Control Structure
C Programming: Control StructureC Programming: Control Structure
C Programming: Control Structure
Sokngim Sa
 
Control and conditional statements
Control and conditional statementsControl and conditional statements
Control and conditional statements
rajshreemuthiah
 

Similar to JPC#8 Introduction to Java Programming (20)

Control statments in c
Control statments in cControl statments in c
Control statments in c
CGC Technical campus,Mohali
 
Data structures
Data structuresData structures
Data structures
Khalid Bana
 
Control Structures in C
Control Structures in CControl Structures in C
Control Structures in C
sana shaikh
 
C++ decision making
C++ decision makingC++ decision making
C++ decision making
Zohaib Ahmed
 
Repetition Structure
Repetition StructureRepetition Structure
Repetition Structure
PRN USM
 
INPUT AND OUTPUT STATEMENTS IN PROGRAMMING IN C
INPUT AND OUTPUT STATEMENTS IN PROGRAMMING IN CINPUT AND OUTPUT STATEMENTS IN PROGRAMMING IN C
INPUT AND OUTPUT STATEMENTS IN PROGRAMMING IN C
Dr. Chandrakant Divate
 
Java fundamentals
Java fundamentalsJava fundamentals
Java fundamentals
HCMUTE
 
Oop object oriented programing topics
Oop object oriented programing topicsOop object oriented programing topics
Oop object oriented programing topics
(•̮̮̃•̃) Prince Do Not Work
 
ch04-conditional-execution.ppt
ch04-conditional-execution.pptch04-conditional-execution.ppt
ch04-conditional-execution.ppt
Mahyuddin8
 
C Sharp Jn (3)
C Sharp Jn (3)C Sharp Jn (3)
C Sharp Jn (3)
jahanullah
 
C Programming with oops Concept and Pointer
C Programming with oops Concept and PointerC Programming with oops Concept and Pointer
C Programming with oops Concept and Pointer
Jeyarajs7
 
Java căn bản - Chapter6
Java căn bản - Chapter6Java căn bản - Chapter6
Java căn bản - Chapter6
Vince Vo
 
3 flow
3 flow3 flow
3 flow
suresh rathod
 
3 flow
3 flow3 flow
3 flow
suresh rathod
 
Loops
LoopsLoops
Loops
Kamran
 
Comp102 lec 6
Comp102   lec 6Comp102   lec 6
Comp102 lec 6
Fraz Bakhsh
 
Control statements
Control statementsControl statements
Control statements
raksharao
 
Unit 2-data types,Variables,Operators,Conitionals,loops and arrays
Unit 2-data types,Variables,Operators,Conitionals,loops and arraysUnit 2-data types,Variables,Operators,Conitionals,loops and arrays
Unit 2-data types,Variables,Operators,Conitionals,loops and arrays
DevaKumari Vijay
 
Programming ppt files (final)
Programming ppt files (final)Programming ppt files (final)
Programming ppt files (final)
yap_raiza
 
00_Introduction to Java.ppt
00_Introduction to Java.ppt00_Introduction to Java.ppt
00_Introduction to Java.ppt
HongAnhNguyn285885
 
Control Structures in C
Control Structures in CControl Structures in C
Control Structures in C
sana shaikh
 
C++ decision making
C++ decision makingC++ decision making
C++ decision making
Zohaib Ahmed
 
Repetition Structure
Repetition StructureRepetition Structure
Repetition Structure
PRN USM
 
INPUT AND OUTPUT STATEMENTS IN PROGRAMMING IN C
INPUT AND OUTPUT STATEMENTS IN PROGRAMMING IN CINPUT AND OUTPUT STATEMENTS IN PROGRAMMING IN C
INPUT AND OUTPUT STATEMENTS IN PROGRAMMING IN C
Dr. Chandrakant Divate
 
Java fundamentals
Java fundamentalsJava fundamentals
Java fundamentals
HCMUTE
 
ch04-conditional-execution.ppt
ch04-conditional-execution.pptch04-conditional-execution.ppt
ch04-conditional-execution.ppt
Mahyuddin8
 
C Sharp Jn (3)
C Sharp Jn (3)C Sharp Jn (3)
C Sharp Jn (3)
jahanullah
 
C Programming with oops Concept and Pointer
C Programming with oops Concept and PointerC Programming with oops Concept and Pointer
C Programming with oops Concept and Pointer
Jeyarajs7
 
Java căn bản - Chapter6
Java căn bản - Chapter6Java căn bản - Chapter6
Java căn bản - Chapter6
Vince Vo
 
Control statements
Control statementsControl statements
Control statements
raksharao
 
Unit 2-data types,Variables,Operators,Conitionals,loops and arrays
Unit 2-data types,Variables,Operators,Conitionals,loops and arraysUnit 2-data types,Variables,Operators,Conitionals,loops and arrays
Unit 2-data types,Variables,Operators,Conitionals,loops and arrays
DevaKumari Vijay
 
Programming ppt files (final)
Programming ppt files (final)Programming ppt files (final)
Programming ppt files (final)
yap_raiza
 
Ad

More from Pathomchon Sriwilairit (6)

Social Networks: Opinion Presentation
Social Networks: Opinion PresentationSocial Networks: Opinion Presentation
Social Networks: Opinion Presentation
Pathomchon Sriwilairit
 
Will the tissue get wet? Mini-science experiment
Will the tissue get wet? Mini-science experimentWill the tissue get wet? Mini-science experiment
Will the tissue get wet? Mini-science experiment
Pathomchon Sriwilairit
 
JPC#8 Foundation of Computer Science
JPC#8 Foundation of Computer ScienceJPC#8 Foundation of Computer Science
JPC#8 Foundation of Computer Science
Pathomchon Sriwilairit
 
JQuery - Effect - Animate method
JQuery - Effect - Animate methodJQuery - Effect - Animate method
JQuery - Effect - Animate method
Pathomchon Sriwilairit
 
20131028 Techniques of Addictive Games
20131028 Techniques of Addictive Games20131028 Techniques of Addictive Games
20131028 Techniques of Addictive Games
Pathomchon Sriwilairit
 
20131014 Designing Slides Layout
20131014 Designing Slides Layout20131014 Designing Slides Layout
20131014 Designing Slides Layout
Pathomchon Sriwilairit
 
Ad

Recently uploaded (20)

Operations Management (Dr. Abdulfatah Salem).pdf
Operations Management (Dr. Abdulfatah Salem).pdfOperations Management (Dr. Abdulfatah Salem).pdf
Operations Management (Dr. Abdulfatah Salem).pdf
Arab Academy for Science, Technology and Maritime Transport
 
Exercise Physiology MCQS By DR. NASIR MUSTAFA
Exercise Physiology MCQS By DR. NASIR MUSTAFAExercise Physiology MCQS By DR. NASIR MUSTAFA
Exercise Physiology MCQS By DR. NASIR MUSTAFA
Dr. Nasir Mustafa
 
Kenan Fellows Participants, Projects 2025-26 Cohort
Kenan Fellows Participants, Projects 2025-26 CohortKenan Fellows Participants, Projects 2025-26 Cohort
Kenan Fellows Participants, Projects 2025-26 Cohort
EducationNC
 
How to manage Multiple Warehouses for multiple floors in odoo point of sale
How to manage Multiple Warehouses for multiple floors in odoo point of saleHow to manage Multiple Warehouses for multiple floors in odoo point of sale
How to manage Multiple Warehouses for multiple floors in odoo point of sale
Celine George
 
CBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - Worksheet
CBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - WorksheetCBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - Worksheet
CBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - Worksheet
Sritoma Majumder
 
Odoo Inventory Rules and Routes v17 - Odoo Slides
Odoo Inventory Rules and Routes v17 - Odoo SlidesOdoo Inventory Rules and Routes v17 - Odoo Slides
Odoo Inventory Rules and Routes v17 - Odoo Slides
Celine George
 
K12 Tableau Tuesday - Algebra Equity and Access in Atlanta Public Schools
K12 Tableau Tuesday  - Algebra Equity and Access in Atlanta Public SchoolsK12 Tableau Tuesday  - Algebra Equity and Access in Atlanta Public Schools
K12 Tableau Tuesday - Algebra Equity and Access in Atlanta Public Schools
dogden2
 
Geography Sem II Unit 1C Correlation of Geography with other school subjects
Geography Sem II Unit 1C Correlation of Geography with other school subjectsGeography Sem II Unit 1C Correlation of Geography with other school subjects
Geography Sem II Unit 1C Correlation of Geography with other school subjects
ProfDrShaikhImran
 
Introduction-to-Communication-and-Media-Studies-1736283331.pdf
Introduction-to-Communication-and-Media-Studies-1736283331.pdfIntroduction-to-Communication-and-Media-Studies-1736283331.pdf
Introduction-to-Communication-and-Media-Studies-1736283331.pdf
james5028
 
Grade 2 - Mathematics - Printable Worksheet
Grade 2 - Mathematics - Printable WorksheetGrade 2 - Mathematics - Printable Worksheet
Grade 2 - Mathematics - Printable Worksheet
Sritoma Majumder
 
Biophysics Chapter 3 Methods of Studying Macromolecules.pdf
Biophysics Chapter 3 Methods of Studying Macromolecules.pdfBiophysics Chapter 3 Methods of Studying Macromolecules.pdf
Biophysics Chapter 3 Methods of Studying Macromolecules.pdf
PKLI-Institute of Nursing and Allied Health Sciences Lahore , Pakistan.
 
dynastic art of the Pallava dynasty south India
dynastic art of the Pallava dynasty south Indiadynastic art of the Pallava dynasty south India
dynastic art of the Pallava dynasty south India
PrachiSontakke5
 
APM Midlands Region April 2025 Sacha Hind Circulated.pdf
APM Midlands Region April 2025 Sacha Hind Circulated.pdfAPM Midlands Region April 2025 Sacha Hind Circulated.pdf
APM Midlands Region April 2025 Sacha Hind Circulated.pdf
Association for Project Management
 
How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...
How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...
How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...
Celine George
 
BỘ ĐỀ TUYỂN SINH VÀO LỚP 10 TIẾNG ANH - 25 ĐỀ THI BÁM SÁT CẤU TRÚC MỚI NHẤT, ...
BỘ ĐỀ TUYỂN SINH VÀO LỚP 10 TIẾNG ANH - 25 ĐỀ THI BÁM SÁT CẤU TRÚC MỚI NHẤT, ...BỘ ĐỀ TUYỂN SINH VÀO LỚP 10 TIẾNG ANH - 25 ĐỀ THI BÁM SÁT CẤU TRÚC MỚI NHẤT, ...
BỘ ĐỀ TUYỂN SINH VÀO LỚP 10 TIẾNG ANH - 25 ĐỀ THI BÁM SÁT CẤU TRÚC MỚI NHẤT, ...
Nguyen Thanh Tu Collection
 
World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...
World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...
World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...
larencebapu132
 
THE STG QUIZ GROUP D.pptx quiz by Ridip Hazarika
THE STG QUIZ GROUP D.pptx   quiz by Ridip HazarikaTHE STG QUIZ GROUP D.pptx   quiz by Ridip Hazarika
THE STG QUIZ GROUP D.pptx quiz by Ridip Hazarika
Ridip Hazarika
 
Political History of Pala dynasty Pala Rulers NEP.pptx
Political History of Pala dynasty Pala Rulers NEP.pptxPolitical History of Pala dynasty Pala Rulers NEP.pptx
Political History of Pala dynasty Pala Rulers NEP.pptx
Arya Mahila P. G. College, Banaras Hindu University, Varanasi, India.
 
How to Manage Purchase Alternatives in Odoo 18
How to Manage Purchase Alternatives in Odoo 18How to Manage Purchase Alternatives in Odoo 18
How to Manage Purchase Alternatives in Odoo 18
Celine George
 
YSPH VMOC Special Report - Measles Outbreak Southwest US 5-3-2025.pptx
YSPH VMOC Special Report - Measles Outbreak  Southwest US 5-3-2025.pptxYSPH VMOC Special Report - Measles Outbreak  Southwest US 5-3-2025.pptx
YSPH VMOC Special Report - Measles Outbreak Southwest US 5-3-2025.pptx
Yale School of Public Health - The Virtual Medical Operations Center (VMOC)
 
Exercise Physiology MCQS By DR. NASIR MUSTAFA
Exercise Physiology MCQS By DR. NASIR MUSTAFAExercise Physiology MCQS By DR. NASIR MUSTAFA
Exercise Physiology MCQS By DR. NASIR MUSTAFA
Dr. Nasir Mustafa
 
Kenan Fellows Participants, Projects 2025-26 Cohort
Kenan Fellows Participants, Projects 2025-26 CohortKenan Fellows Participants, Projects 2025-26 Cohort
Kenan Fellows Participants, Projects 2025-26 Cohort
EducationNC
 
How to manage Multiple Warehouses for multiple floors in odoo point of sale
How to manage Multiple Warehouses for multiple floors in odoo point of saleHow to manage Multiple Warehouses for multiple floors in odoo point of sale
How to manage Multiple Warehouses for multiple floors in odoo point of sale
Celine George
 
CBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - Worksheet
CBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - WorksheetCBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - Worksheet
CBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - Worksheet
Sritoma Majumder
 
Odoo Inventory Rules and Routes v17 - Odoo Slides
Odoo Inventory Rules and Routes v17 - Odoo SlidesOdoo Inventory Rules and Routes v17 - Odoo Slides
Odoo Inventory Rules and Routes v17 - Odoo Slides
Celine George
 
K12 Tableau Tuesday - Algebra Equity and Access in Atlanta Public Schools
K12 Tableau Tuesday  - Algebra Equity and Access in Atlanta Public SchoolsK12 Tableau Tuesday  - Algebra Equity and Access in Atlanta Public Schools
K12 Tableau Tuesday - Algebra Equity and Access in Atlanta Public Schools
dogden2
 
Geography Sem II Unit 1C Correlation of Geography with other school subjects
Geography Sem II Unit 1C Correlation of Geography with other school subjectsGeography Sem II Unit 1C Correlation of Geography with other school subjects
Geography Sem II Unit 1C Correlation of Geography with other school subjects
ProfDrShaikhImran
 
Introduction-to-Communication-and-Media-Studies-1736283331.pdf
Introduction-to-Communication-and-Media-Studies-1736283331.pdfIntroduction-to-Communication-and-Media-Studies-1736283331.pdf
Introduction-to-Communication-and-Media-Studies-1736283331.pdf
james5028
 
Grade 2 - Mathematics - Printable Worksheet
Grade 2 - Mathematics - Printable WorksheetGrade 2 - Mathematics - Printable Worksheet
Grade 2 - Mathematics - Printable Worksheet
Sritoma Majumder
 
dynastic art of the Pallava dynasty south India
dynastic art of the Pallava dynasty south Indiadynastic art of the Pallava dynasty south India
dynastic art of the Pallava dynasty south India
PrachiSontakke5
 
How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...
How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...
How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...
Celine George
 
BỘ ĐỀ TUYỂN SINH VÀO LỚP 10 TIẾNG ANH - 25 ĐỀ THI BÁM SÁT CẤU TRÚC MỚI NHẤT, ...
BỘ ĐỀ TUYỂN SINH VÀO LỚP 10 TIẾNG ANH - 25 ĐỀ THI BÁM SÁT CẤU TRÚC MỚI NHẤT, ...BỘ ĐỀ TUYỂN SINH VÀO LỚP 10 TIẾNG ANH - 25 ĐỀ THI BÁM SÁT CẤU TRÚC MỚI NHẤT, ...
BỘ ĐỀ TUYỂN SINH VÀO LỚP 10 TIẾNG ANH - 25 ĐỀ THI BÁM SÁT CẤU TRÚC MỚI NHẤT, ...
Nguyen Thanh Tu Collection
 
World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...
World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...
World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...
larencebapu132
 
THE STG QUIZ GROUP D.pptx quiz by Ridip Hazarika
THE STG QUIZ GROUP D.pptx   quiz by Ridip HazarikaTHE STG QUIZ GROUP D.pptx   quiz by Ridip Hazarika
THE STG QUIZ GROUP D.pptx quiz by Ridip Hazarika
Ridip Hazarika
 
How to Manage Purchase Alternatives in Odoo 18
How to Manage Purchase Alternatives in Odoo 18How to Manage Purchase Alternatives in Odoo 18
How to Manage Purchase Alternatives in Odoo 18
Celine George
 

JPC#8 Introduction to Java Programming

  • 1. Introduction to Java Programming Junior Programmer Camp #8
  • 2. Chapter 1: Elementary Programming
  • 3. What is Java? • A high-level programming language • Useful for developing business applications • Java Development Kit download: http://www.oracle.com/technetwork/java/jav ase/downloads/index.html • Case sensitive
  • 4. What is DrJava? • A programming tool used for writing code in Java • Can be downloaded from www.drjava.org • Simple and easy to use for beginners
  • 6. Typical Java Code Skeleton public class Program{ public static void main(String[] args) { // your main code goes here
  • 7. Comments • Sometimes you may want to add some notes to your Java code, without any effect to the code. • In this case, you can write comments. Two ways: – System.out.println("test"); // print a test string – /* This is a multiple-line comment. More line Even more line Blah blah */
  • 8. Simple Program public class Program { public static void main(String[] args) { System.out.println(“Hello!! JPC#8"); } } OUTPUT: Hello!! JPC#8
  • 9. Basic Arithmetics public class Program { public static void main(String[] args) { System.out.println(1 + 2); } } OUTPUT: 3
  • 10. Basic Arithmetics • • • • • Addition: 2 + 3 Subtraction: 4 - 5 Multiplication: 6 * 7 Division: 8 / 4 Remainder: 5 % 3 How about 2 + 3 * 4 ? • Does precedence matter? • Can we solve precedence with parentheses?
  • 11. Variable Types • • • • • Integers: byte, short, int, long Floating-point numbers: float, double Single character: char Text: String True/False: boolean
  • 12. Floating-Point Numbers • You can also use numbers with decimal points: 2.3, –1.5 • Integers: …, -3, -2, -1, 0, 1, 2, 3, … • Real numbers: 0.0, 3.14, -2.4, 327.8
  • 13. Floating-Point Numbers • What is 3.0 / 4.0 in Java? • How about 3 / 4 ?
  • 14. Declaring and Using Variables Name of variables - Not preserve words - Must begin with letters or ‘_’ or ‘$’ <data_type> <variable name>; - Example: int x ; double pi_1 ; char ch2 boolean a ;
  • 15. Declaring and Using Variables Assignment - Variable or numeric primitive type - Combination of values and/or variable with numeric operators <variable_name> = <variable or value> - Example: x = 10 ; pi = 3.14159 + 0.11 ; char a = „b‟ ; y = x ;
  • 16. Declaring and Using Variables int x; int y; int z; x = 2; y = 5; z = x + y; System.out.println(x + " + “ + y + " = “ + z); OUTPUT: 2 + 5 = 7
  • 17. Multiple Variable Declarations Instead of int x; int y; int z; You can also use int x, y, z; to declare multiple variables all at once.
  • 18. Variable Initialization Instead of int x; int y; x = 2; y = 5; You can also use int x = 2, y = 5;
  • 19. Shorthand Operators Operator += -= *= /= %= Example i += 8 i -= 8.0 i *= 8 i /= 8 i %= 8 Equivalent i=i+8 i = i - 8.0 i=i*8 i=i/8 i=i%8
  • 20. Shorthand Operators • ++var • var++ • --var • var--
  • 21. Shorthand Operators Example: ++var int x = 3; System.out.print(x); // 3 System.out.print(++x); // 4 System.out.print(x) // 4
  • 22. Shorthand Operators Example: var++ int x = 3; System.out.print(x); // 3 System.out.print(x++); // 3 System.out.print(x) // 4
  • 23. Type conversion Use only primitive data type except boolean - (<type>)<expression>  casting = Ex. int i = „a‟; // i a x y = = = = i = 97 (int)‟B‟; // i = 66 (char)80 ; // a = „P‟ (int)1.2 ; // x = 1 (double)2; // y = 2.0 • The variable to keep converted type value must be the type that can keep the value.
  • 24. Character and String • Use char type to keep an ASCII character • Use String type to keep characters (text) • char type is capable with arithmetic operator Example: char ch = „B‟; System.out.println((char)(ch + 1)); // „C‟ String s = “This is a sentence.”;
  • 25. Getting Inputs from the User import java.util.Scanner; public class Program { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int x = sc.nextInt(); x = x * 2; System.out.println(x); } } INPUT: 5 OUTPUT: 10
  • 26. Multiple Inputs import java.util.Scanner; public class Program { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int x = sc.nextInt(); int y = sc.nextInt(); System.out.println(x); System.out.println(y); } } INPUT: 1 OUTPUT: 1 2 2
  • 27. Inputting Text import java.util.Scanner; public class Program { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter your name: "); String name = sc.nextLine(); System.out.println("Hello, "+ name + "!"); } } INPUT: John OUTPUT: Hello, John!
  • 28. Give it a try! • Write a program to get 2 numbers from the user and output the sum, the difference, the product, and the quotient of those 2 numbers. • Hint: Follow the previous example. INPUT: OUTPUT: 7 5 7 7 7 7 + * / 5 5 5 5 = = = = 12 2 35 1.4
  • 29. Programming Errors • Most common for us. • Syntax error, Runtime error, Logic error
  • 31. Introducing boolean Data Type • Normally you can use int to store integer numbers, double to store floating-point numbers. • What if all you want is to store a truth value: either true or false? Answer: use boolean data type
  • 32. Boolean Expressions • Expression that return boolean value (True/False) • Operators: - < -! <= - && > - || >= -^ == (not same with one = the assignment operator) != • Precedence matters.
  • 33. Do/Don’t? • Sometimes you will want your program to decide whether to do something or not based on some conditions.
  • 34. General Form of if if(condition) { statements to do if condition is true }
  • 35. If This Then Do That if (1 == 2) { System.out.println("1 is equal to 2"); if (1 != 2) { System.out.println("1 is not equal to 2"); OUTPUT: 1 is not equal to 2
  • 36. General Form of if-else if(condition) { statements to do if condition is true } else{ statements to do if condition is false }
  • 37. Do This or That if (1 == 2) { System.out.println("1 is equal to 2"); } else { System.out.println("1 is not equal to 2"); } OUTPUT: 1 is not equal to 2
  • 38. General Form of if-elseif-else if(condition 1) { statements to do if condition 1 is true } else if (condition 2) { statements to do if condition 2 is true } else if (condition 3) { statements to do if condition 3 is true } else{ statements to do if none of the conditions is true } // You can have as many conditions as you like!
  • 39. Do This or This, otherwise That if (3 < 2) { System.out.println("3 is less than 2"); } else if (3 > 2) { System.out.println("3 is greater than 2"); } else{ System.out.println("3 is equal to 2"); } OUTPUT: 3 is greater than 2
  • 40. Using boolean Data Type boolean check = (1 < 2); if(check) { System.out.println("1 < 2"); } else{ System.out.println("1 >= 2"); } • The condition part of the if statement can be a boolean expression (1 < 2) or a boolean variable (check)!
  • 41. Condition with Multiple Cases • In some cases, you may need multiple condition statements to achieve your goals. • Writing them as multiple else-if statements can be tedious. • Use of switch statement is recommended (with some exceptions).
  • 42. General Form of switch switch (expression) { // expression can be int, char, or boolean case value1: statements to do when expression == value1 break; case value2: statements to do when expression == value2 break; // add more cases as needed default: statements to do when nothing above matches }
  • 43. Example Usage of switch // imported Scanner and instantiated one System.out.print("Enter a number between 1 –3: "); int x = sc.nextInt(); switch (x) { case 1 : System.out.println("Entered 1"); break; case 2 : System.out.println("Entered 2"); break; case 3 : System.out.println("Entered 3"); break; default: System.out.println("Entered else..."); }
  • 44. Conditional Operator • Also known as expression shortcut • General form: expression ? value_true : value_false
  • 45. Conditional Operator Instead of writing int x = 0; int y; if(x == 0) { y = 0; } else { y = x; } You can also use this: int x = 0; int y = x == 0 ? 0 : x;
  • 46. Give it a try! • Write a program to get 3 numbers: x, y, and z. The program should output whether x + y = z or not. • Can you do this with a switch statement? INPUT: 3 4 8 OUTPUT: 3 + 4 != 8
  • 47. Chapter 3: Repetition, Iteration, Loops
  • 48. Iteration Fundamentals • Sometimes you need to do certain things over and over again, probably in the same or similar manner. • Writing 1000 Java statements to display numbers 1 to 1000 is not feasible: you wouldn't want to try to copy-and-paste the code thousand times. • In this case, you can make use of the iteration feature of Java instead.
  • 49. General Form of while while (condition) { statements to do while the condition is true }
  • 50. Example While 1-100 int i= 1; while (i<= 100) { System.out.println(i); i= i+ 1; } OUTPUT: 1 2 3 … 100
  • 51. Infinite Loop • Get stuck in an infinite loop because condition to exit the loop is tend to never met. • Should use condition that can exit the loop. (e.g.: exit when a value of variable exceed max, when the value hit 0 or something)
  • 52. General Form of do-while do{ statements to do, and to continue to do while the condition is true } while (condition);
  • 53. Example Do-while 1-100 int i= 1; do { System.out.println(i); i= i+ 1; } while (i <= 100); OUTPUT: 1 2 3 … 100
  • 54. While and do-while differences • For the while loop, the condition is checked before any statement is executed. Therefore, if the condition is false in the first place, no statement in the loop will be executed. • For the do-while loop, in contrast, the statements in the loop are executed once at first. Then, for each iteration, the condition is checked in the same way as in the while loop.
  • 55. General Form of for for (initialization; condition; iteration) { statements to do as long as condition is true }
  • 56. 1 –100 using for Use for (int i = 1; i <= 100; i++) { System.out.println(i); } Instead of int i= 1; while (i<= 1000) { System.out.println(i); i++; // Same as i= i+ 1; } • Each type of loop can be written in another type of loop equivalently
  • 57. Give it a try! • Write a game that let the player "guess" a random number generated by the computer between 1 and 100. Each turn, display whether the input number is less than or greater than the random one. Game ends when the player guesses correctly. Otherwise, keep asking for another number. • To random a number between 5 to 70: – int x = (int) (Math.random() * (70 – 5 + 1) + 5); • Optional features: – The allowed min and max should change accordingly for each turn. For example, if the random number is 40, and the user guesses 50, the game should now allow only numbers between 1 and 49 in the next turn.