java programming (junior programmer camp 7)
TRANSCRIPT
-
8/8/2019 Java Programming (Junior Programmer Camp 7)
1/44
Java Programming
Junior Programmer Camp #7
-
8/8/2019 Java Programming (Junior Programmer Camp 7)
2/44
CHAPTER 1: BASICS OF JAVAJava Programming
-
8/8/2019 Java Programming (Junior Programmer Camp 7)
3/44
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
-
8/8/2019 Java Programming (Junior Programmer Camp 7)
4/44
What is BlueJ?
A programming tool used for writing code in Java
Can be downloaded from bluej.org
Simple and easy to use for beginners
-
8/8/2019 Java Programming (Junior Programmer Camp 7)
5/44
-
8/8/2019 Java Programming (Junior Programmer Camp 7)
6/44
Typical Java Code Skeleton
public class Program {
public static void main(String[] args) {
// your main code goes here
}
}
-
8/8/2019 Java Programming (Junior Programmer Camp 7)
7/44
Comments
Sometimes you may want to add some notes to yourJava 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 line
Blah blah
*/
-
8/8/2019 Java Programming (Junior Programmer Camp 7)
8/44
Simple Program
public class Program {
public static void main(String[] args) {
System.out.println("Junior Programmer Camp 7");
}
}
OUTPUT:
Junior Programmer Camp 7
-
8/8/2019 Java Programming (Junior Programmer Camp 7)
9/44
Basic Arithmetics
public class Program {
public static void main(String[] args) {
System.out.println(1 + 2);
}
}
OUTPUT:
3
-
8/8/2019 Java Programming (Junior Programmer Camp 7)
10/44
Basic Arithmetics
Addition: 2 + 3
Subtraction: 4 5
Multiplication: 6 * 7
Division: 8 / 4
How about 2 + 3 * 4 ?
-
8/8/2019 Java Programming (Junior Programmer Camp 7)
11/44
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
What is 3.0 / 4.0 in Java? 0.75
How about 3 / 4 ?
-
8/8/2019 Java Programming (Junior Programmer Camp 7)
12/44
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
-
8/8/2019 Java Programming (Junior Programmer Camp 7)
13/44
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.
-
8/8/2019 Java Programming (Junior Programmer Camp 7)
14/44
Variable Initialization
Instead of
int x;
int y;
x = 2;y = 5;
You can also use
int x = 2, y = 5;
Try.
-
8/8/2019 Java Programming (Junior Programmer Camp 7)
15/44
Variable Types
Integers: byte, short, int, long
Floating-point numbers: float, double
Single character: char
Text: StringTrue/False: boolean
-
8/8/2019 Java Programming (Junior Programmer Camp 7)
16/44
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: 1234
OUTPUT: 2468
-
8/8/2019 Java Programming (Junior Programmer Camp 7)
17/44
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: 12
OUTPUT: 12
-
8/8/2019 Java Programming (Junior Programmer Camp 7)
18/44
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!
-
8/8/2019 Java Programming (Junior Programmer Camp 7)
19/44
Quiz!
Write a program to get 2 numbers from the user and output the sum,the difference, and the product of those 2 numbers.
Hint: Follow the previous example.
INPUT: 73
OUTPUT: 10
4
21
-
8/8/2019 Java Programming (Junior Programmer Camp 7)
20/44
CHAPTER 2: CONTROL STRUCTURESJava Programming
-
8/8/2019 Java Programming (Junior Programmer Camp 7)
21/44
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 trueor false?
Answer: use boolean data type
Usage of this will be introduced later.
-
8/8/2019 Java Programming (Junior Programmer Camp 7)
22/44
To Do or not to Do?
Sometimes you will want your program to decidewhether to do something or not based on someconditions.
There are many ways to achieve this, for example:
if
switch
-
8/8/2019 Java Programming (Junior Programmer Camp 7)
23/44
General Form of if
if (condition) {
statements to do if condition is true
}
-
8/8/2019 Java Programming (Junior Programmer Camp 7)
24/44
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
-
8/8/2019 Java Programming (Junior Programmer Camp 7)
25/44
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
-
8/8/2019 Java Programming (Junior Programmer Camp 7)
26/44
General Form of if-else
if (condition) {
statements to do if condition is true
} else {
statements to do if condition is false
}
-
8/8/2019 Java Programming (Junior Programmer Camp 7)
27/44
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
-
8/8/2019 Java Programming (Junior Programmer Camp 7)
28/44
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!
-
8/8/2019 Java Programming (Junior Programmer Camp 7)
29/44
Using boolean Data Type
boolean check = (1 < 2);
if (check) {
System.out.println("1 < 2");
} else {
System.out.println("1 >= 2");}
/* Saw something?
The condition part of the if statement can be a boolean
expression (1 < 2) or a boolean variable (check)! */
-
8/8/2019 Java Programming (Junior Programmer Camp 7)
30/44
Condition with Multiple Cases
In some cases, you may need multipleconditionstatements to achieve your goals.
Writing them as multiple else-if statements can betedious.
Use of switch statement is recommended (with someexceptions).
-
8/8/2019 Java Programming (Junior Programmer Camp 7)
31/44
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
}
-
8/8/2019 Java Programming (Junior Programmer Camp 7)
32/44
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 something else...");
}
-
8/8/2019 Java Programming (Junior Programmer Camp 7)
33/44
if-else Expression Shortcut
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;
-
8/8/2019 Java Programming (Junior Programmer Camp 7)
34/44
Quiz!
Write a program to get 3 numbers: x, y, and z. The program shouldoutput whether x + y = z or not. Can you do this with a switchstatement?
INPUT: 3
4
8
OUTPUT: 3 + 4 != 8
-
8/8/2019 Java Programming (Junior Programmer Camp 7)
35/44
CHAPTER 3: ITERATIONSJava Programming
-
8/8/2019 Java Programming (Junior Programmer Camp 7)
36/44
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 notfeasible: you wouldn't want to try to copy-and-paste the codethousand times.
In this case, you can make use of the iteration feature of Javainstead.
There are 3 main ways to do this:
while do-while
for
-
8/8/2019 Java Programming (Junior Programmer Camp 7)
37/44
General Form of while
while (condition) {
statements to do while the condition is true
}
-
8/8/2019 Java Programming (Junior Programmer Camp 7)
38/44
1 - 1000
int i = 1;
while (i
-
8/8/2019 Java Programming (Junior Programmer Camp 7)
39/44
General Form of do-while
do {
statements to do, and to continue to do while the
condition is true
} while (condition);
-
8/8/2019 Java Programming (Junior Programmer Camp 7)
40/44
while and do-while differences
For the while loop, the conditionis checked before any statement isexecuted. Therefore, if the conditionis false in the first place, nostatement in the loop will be executed.
For the do-while loop, in contrast, the statements in the loop areexecuted once at first. Then, for each iteration, the condition ischecked in the same way as in the while loop.
-
8/8/2019 Java Programming (Junior Programmer Camp 7)
41/44
General Form of for
for (initialization; condition; iteration) {
statements to do as long as condition is true
}
-
8/8/2019 Java Programming (Junior Programmer Camp 7)
42/44
1 1000 using for
Use
for (int i = 1; i
-
8/8/2019 Java Programming (Junior Programmer Camp 7)
43/44
Quiz!
Write a game that let the player "guess" a random numbergenerated by the computer between 1 and 100. Each turn, displaywhether the input number is less than or greater than the randomone. 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 eachturn. For example, if the random number is 40, and the userguesses 50, the game should now allow only numbers between1 and 49 in the next turn.
Game ends when there is only one number left to guess.
-
8/8/2019 Java Programming (Junior Programmer Camp 7)
44/44
CHAPTER 4: PROGRAMMING DRILLSJava Programming