jpc#8 introduction to java programming
TRANSCRIPT
![Page 1: JPC#8 Introduction to Java Programming](https://reader034.vdocuments.mx/reader034/viewer/2022052619/555da4e6d8b42a257b8b590c/html5/thumbnails/1.jpg)
Introduction to Java Programming
Junior Programmer Camp #8
![Page 2: JPC#8 Introduction to Java Programming](https://reader034.vdocuments.mx/reader034/viewer/2022052619/555da4e6d8b42a257b8b590c/html5/thumbnails/2.jpg)
Chapter 1: Elementary Programming
![Page 3: JPC#8 Introduction to Java Programming](https://reader034.vdocuments.mx/reader034/viewer/2022052619/555da4e6d8b42a257b8b590c/html5/thumbnails/3.jpg)
What is Java?
• A high-level programming language• Useful for developing business applications• Java Development Kit download:http://www.oracle.com/technetwork/java/javase/downloads/index.html
• Case sensitive
![Page 4: JPC#8 Introduction to Java Programming](https://reader034.vdocuments.mx/reader034/viewer/2022052619/555da4e6d8b42a257b8b590c/html5/thumbnails/4.jpg)
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
![Page 5: JPC#8 Introduction to Java Programming](https://reader034.vdocuments.mx/reader034/viewer/2022052619/555da4e6d8b42a257b8b590c/html5/thumbnails/5.jpg)
DrJava
![Page 6: JPC#8 Introduction to Java Programming](https://reader034.vdocuments.mx/reader034/viewer/2022052619/555da4e6d8b42a257b8b590c/html5/thumbnails/6.jpg)
Typical Java Code Skeleton
public class Program{public static void main(String[] args) {
// your main code goes here}
}
![Page 7: JPC#8 Introduction to Java Programming](https://reader034.vdocuments.mx/reader034/viewer/2022052619/555da4e6d8b42a257b8b590c/html5/thumbnails/7.jpg)
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 lineEven more lineBlah blah
*/
![Page 8: JPC#8 Introduction to Java Programming](https://reader034.vdocuments.mx/reader034/viewer/2022052619/555da4e6d8b42a257b8b590c/html5/thumbnails/8.jpg)
Simple Program
public class Program {public static void main(String[] args) {
System.out.println(“Hello!! JPC#8");}
}
OUTPUT:Hello!! JPC#8
![Page 9: JPC#8 Introduction to Java Programming](https://reader034.vdocuments.mx/reader034/viewer/2022052619/555da4e6d8b42a257b8b590c/html5/thumbnails/9.jpg)
Basic Arithmetics
public class Program {public static void main(String[] args) {
System.out.println(1 + 2);}
}
OUTPUT:3
![Page 10: JPC#8 Introduction to Java Programming](https://reader034.vdocuments.mx/reader034/viewer/2022052619/555da4e6d8b42a257b8b590c/html5/thumbnails/10.jpg)
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?
![Page 11: JPC#8 Introduction to Java Programming](https://reader034.vdocuments.mx/reader034/viewer/2022052619/555da4e6d8b42a257b8b590c/html5/thumbnails/11.jpg)
Variable Types
• Integers: byte, short, int, long• Floating-point numbers: float, double• Single character: char• Text: String• True/False: boolean
![Page 12: JPC#8 Introduction to Java Programming](https://reader034.vdocuments.mx/reader034/viewer/2022052619/555da4e6d8b42a257b8b590c/html5/thumbnails/12.jpg)
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
![Page 13: JPC#8 Introduction to Java Programming](https://reader034.vdocuments.mx/reader034/viewer/2022052619/555da4e6d8b42a257b8b590c/html5/thumbnails/13.jpg)
Floating-Point Numbers
• What is 3.0 / 4.0 in Java?• How about 3 / 4 ?
![Page 14: JPC#8 Introduction to Java Programming](https://reader034.vdocuments.mx/reader034/viewer/2022052619/555da4e6d8b42a257b8b590c/html5/thumbnails/14.jpg)
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 ;
![Page 15: JPC#8 Introduction to Java Programming](https://reader034.vdocuments.mx/reader034/viewer/2022052619/555da4e6d8b42a257b8b590c/html5/thumbnails/15.jpg)
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 ;
![Page 16: JPC#8 Introduction to Java Programming](https://reader034.vdocuments.mx/reader034/viewer/2022052619/555da4e6d8b42a257b8b590c/html5/thumbnails/16.jpg)
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
![Page 17: JPC#8 Introduction to Java Programming](https://reader034.vdocuments.mx/reader034/viewer/2022052619/555da4e6d8b42a257b8b590c/html5/thumbnails/17.jpg)
Multiple Variable Declarations
Instead ofint x;
int y;int z;
You can also useint x, y, z;
to declare multiple variables all at once.
![Page 18: JPC#8 Introduction to Java Programming](https://reader034.vdocuments.mx/reader034/viewer/2022052619/555da4e6d8b42a257b8b590c/html5/thumbnails/18.jpg)
Variable Initialization
Instead ofint x;
int y;x = 2;y = 5;
You can also useint x = 2, y = 5;
![Page 19: JPC#8 Introduction to Java Programming](https://reader034.vdocuments.mx/reader034/viewer/2022052619/555da4e6d8b42a257b8b590c/html5/thumbnails/19.jpg)
Shorthand Operators
Operator Example Equivalent+= i += 8 i = i + 8-= i -= 8.0 i = i - 8.0*= i *= 8 i = i * 8/= i /= 8 i = i / 8%= i %= 8 i = i % 8
![Page 20: JPC#8 Introduction to Java Programming](https://reader034.vdocuments.mx/reader034/viewer/2022052619/555da4e6d8b42a257b8b590c/html5/thumbnails/20.jpg)
Shorthand Operators
• ++var• var++• --var• var--
![Page 21: JPC#8 Introduction to Java Programming](https://reader034.vdocuments.mx/reader034/viewer/2022052619/555da4e6d8b42a257b8b590c/html5/thumbnails/21.jpg)
Shorthand Operators
Example: ++var
int x = 3;System.out.print(x); // 3System.out.print(++x); // 4System.out.print(x) // 4
![Page 22: JPC#8 Introduction to Java Programming](https://reader034.vdocuments.mx/reader034/viewer/2022052619/555da4e6d8b42a257b8b590c/html5/thumbnails/22.jpg)
Shorthand Operators
Example: var++
int x = 3;System.out.print(x); // 3System.out.print(x++); // 3System.out.print(x) // 4
![Page 23: JPC#8 Introduction to Java Programming](https://reader034.vdocuments.mx/reader034/viewer/2022052619/555da4e6d8b42a257b8b590c/html5/thumbnails/23.jpg)
Type conversion
Use only primitive data type except boolean
- (<type>)<expression> casting
=
Ex. int i = ‘a’; // i = 97
i = (int)’B’; // i = 66a = (char)80 ; // a = ‘P’x = (int)1.2 ; // x = 1y = (double)2; // y = 2.0
• The variable to keep converted type value must be the type that can keep the value.
![Page 24: JPC#8 Introduction to Java Programming](https://reader034.vdocuments.mx/reader034/viewer/2022052619/555da4e6d8b42a257b8b590c/html5/thumbnails/24.jpg)
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.”;
![Page 25: JPC#8 Introduction to Java Programming](https://reader034.vdocuments.mx/reader034/viewer/2022052619/555da4e6d8b42a257b8b590c/html5/thumbnails/25.jpg)
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
![Page 26: JPC#8 Introduction to Java Programming](https://reader034.vdocuments.mx/reader034/viewer/2022052619/555da4e6d8b42a257b8b590c/html5/thumbnails/26.jpg)
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
![Page 27: JPC#8 Introduction to Java Programming](https://reader034.vdocuments.mx/reader034/viewer/2022052619/555da4e6d8b42a257b8b590c/html5/thumbnails/27.jpg)
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: JohnOUTPUT: Hello, John!
![Page 28: JPC#8 Introduction to Java Programming](https://reader034.vdocuments.mx/reader034/viewer/2022052619/555da4e6d8b42a257b8b590c/html5/thumbnails/28.jpg)
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: 75
OUTPUT: 7 + 5 = 127 - 5 = 27 * 5 = 357 / 5 = 1.4
![Page 29: JPC#8 Introduction to Java Programming](https://reader034.vdocuments.mx/reader034/viewer/2022052619/555da4e6d8b42a257b8b590c/html5/thumbnails/29.jpg)
Programming Errors
• Most common for us.• Syntax error, Runtime error, Logic error
![Page 30: JPC#8 Introduction to Java Programming](https://reader034.vdocuments.mx/reader034/viewer/2022052619/555da4e6d8b42a257b8b590c/html5/thumbnails/30.jpg)
Chapter 2: Selections
![Page 31: JPC#8 Introduction to Java Programming](https://reader034.vdocuments.mx/reader034/viewer/2022052619/555da4e6d8b42a257b8b590c/html5/thumbnails/31.jpg)
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
![Page 32: JPC#8 Introduction to Java Programming](https://reader034.vdocuments.mx/reader034/viewer/2022052619/555da4e6d8b42a257b8b590c/html5/thumbnails/32.jpg)
Boolean Expressions
• Expression that return boolean value (True/False)• Operators:
- < - !- <= - &&- > - ||- >= - ^- == (not same with one = the assignment operator)- !=
• Precedence matters.
![Page 33: JPC#8 Introduction to Java Programming](https://reader034.vdocuments.mx/reader034/viewer/2022052619/555da4e6d8b42a257b8b590c/html5/thumbnails/33.jpg)
Do/Don’t?
• Sometimes you will want your program to decide whether to do something or not based on some conditions.
![Page 34: JPC#8 Introduction to Java Programming](https://reader034.vdocuments.mx/reader034/viewer/2022052619/555da4e6d8b42a257b8b590c/html5/thumbnails/34.jpg)
General Form of if
if(condition) {statements to do if condition is true
}
![Page 35: JPC#8 Introduction to Java Programming](https://reader034.vdocuments.mx/reader034/viewer/2022052619/555da4e6d8b42a257b8b590c/html5/thumbnails/35.jpg)
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
![Page 36: JPC#8 Introduction to Java Programming](https://reader034.vdocuments.mx/reader034/viewer/2022052619/555da4e6d8b42a257b8b590c/html5/thumbnails/36.jpg)
General Form of if-else
if(condition) {statements to do if condition is true
} else{statements to do if condition is false
}
![Page 37: JPC#8 Introduction to Java Programming](https://reader034.vdocuments.mx/reader034/viewer/2022052619/555da4e6d8b42a257b8b590c/html5/thumbnails/37.jpg)
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
![Page 38: JPC#8 Introduction to Java Programming](https://reader034.vdocuments.mx/reader034/viewer/2022052619/555da4e6d8b42a257b8b590c/html5/thumbnails/38.jpg)
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!
![Page 39: JPC#8 Introduction to Java Programming](https://reader034.vdocuments.mx/reader034/viewer/2022052619/555da4e6d8b42a257b8b590c/html5/thumbnails/39.jpg)
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
![Page 40: JPC#8 Introduction to Java Programming](https://reader034.vdocuments.mx/reader034/viewer/2022052619/555da4e6d8b42a257b8b590c/html5/thumbnails/40.jpg)
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)!
![Page 41: JPC#8 Introduction to Java Programming](https://reader034.vdocuments.mx/reader034/viewer/2022052619/555da4e6d8b42a257b8b590c/html5/thumbnails/41.jpg)
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).
![Page 42: JPC#8 Introduction to Java Programming](https://reader034.vdocuments.mx/reader034/viewer/2022052619/555da4e6d8b42a257b8b590c/html5/thumbnails/42.jpg)
General Form of switchswitch (expression) {// expression can be int, char, or booleancase value1:
statements to do when expression == value1
break;case value2:
statements to do when expression == value2
break;// add more cases as neededdefault:
statements to do when nothing above matches
}
![Page 43: JPC#8 Introduction to Java Programming](https://reader034.vdocuments.mx/reader034/viewer/2022052619/555da4e6d8b42a257b8b590c/html5/thumbnails/43.jpg)
Example Usage of switch// imported Scanner and instantiated oneSystem.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...");}
![Page 44: JPC#8 Introduction to Java Programming](https://reader034.vdocuments.mx/reader034/viewer/2022052619/555da4e6d8b42a257b8b590c/html5/thumbnails/44.jpg)
Conditional Operator
• Also known as expression shortcut• General form:
expression ? value_true : value_false
![Page 45: JPC#8 Introduction to Java Programming](https://reader034.vdocuments.mx/reader034/viewer/2022052619/555da4e6d8b42a257b8b590c/html5/thumbnails/45.jpg)
Conditional Operator
Instead of writingint 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;
![Page 46: JPC#8 Introduction to Java Programming](https://reader034.vdocuments.mx/reader034/viewer/2022052619/555da4e6d8b42a257b8b590c/html5/thumbnails/46.jpg)
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:
348
OUTPUT:3 + 4 != 8
![Page 47: JPC#8 Introduction to Java Programming](https://reader034.vdocuments.mx/reader034/viewer/2022052619/555da4e6d8b42a257b8b590c/html5/thumbnails/47.jpg)
Chapter 3: Repetition, Iteration, Loops
![Page 48: JPC#8 Introduction to Java Programming](https://reader034.vdocuments.mx/reader034/viewer/2022052619/555da4e6d8b42a257b8b590c/html5/thumbnails/48.jpg)
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.
![Page 49: JPC#8 Introduction to Java Programming](https://reader034.vdocuments.mx/reader034/viewer/2022052619/555da4e6d8b42a257b8b590c/html5/thumbnails/49.jpg)
General Form of while
while (condition) {statements to do while the condition is true
}
![Page 50: JPC#8 Introduction to Java Programming](https://reader034.vdocuments.mx/reader034/viewer/2022052619/555da4e6d8b42a257b8b590c/html5/thumbnails/50.jpg)
Example While 1-100int i= 1;while (i<= 100) {System.out.println(i);i= i+ 1;
}
OUTPUT:123…100
![Page 51: JPC#8 Introduction to Java Programming](https://reader034.vdocuments.mx/reader034/viewer/2022052619/555da4e6d8b42a257b8b590c/html5/thumbnails/51.jpg)
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)
![Page 52: JPC#8 Introduction to Java Programming](https://reader034.vdocuments.mx/reader034/viewer/2022052619/555da4e6d8b42a257b8b590c/html5/thumbnails/52.jpg)
General Form of do-while
do{statements to do, and to continue to do while the condition is true
} while (condition);
![Page 53: JPC#8 Introduction to Java Programming](https://reader034.vdocuments.mx/reader034/viewer/2022052619/555da4e6d8b42a257b8b590c/html5/thumbnails/53.jpg)
Example Do-while 1-100int i= 1;do {System.out.println(i);i= i+ 1;
} while (i <= 100);
OUTPUT:123…100
![Page 54: JPC#8 Introduction to Java Programming](https://reader034.vdocuments.mx/reader034/viewer/2022052619/555da4e6d8b42a257b8b590c/html5/thumbnails/54.jpg)
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.
![Page 55: JPC#8 Introduction to Java Programming](https://reader034.vdocuments.mx/reader034/viewer/2022052619/555da4e6d8b42a257b8b590c/html5/thumbnails/55.jpg)
General Form of for
for (initialization; condition; iteration) {statements to do as long as condition is true
}
![Page 56: JPC#8 Introduction to Java Programming](https://reader034.vdocuments.mx/reader034/viewer/2022052619/555da4e6d8b42a257b8b590c/html5/thumbnails/56.jpg)
1 –100 using for
Usefor (int i = 1; i <= 100; i++) {System.out.println(i);
}
Instead ofint 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
![Page 57: JPC#8 Introduction to Java Programming](https://reader034.vdocuments.mx/reader034/viewer/2022052619/555da4e6d8b42a257b8b590c/html5/thumbnails/57.jpg)
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.