Presented by TU RUIXUAN
Copyright © TU RUIXUAN, All Rights Reserved
Tailored for College Board® Advanced Placement® Program
According to Course and Exam Description Effective Fall 2020
Install the environment to edit and compile the code.
↓ Vocabulary ↓
Java Development Kit (JDK) 8: https://www.oracle.com/java/technologies/javase/javase-jdk8-downloads.html
Visual Studio Code: https://code.visualstudio.com/
Be sure to add .../jdk_1.8.*/bin/ (the folder which you installed the JDK in) to the PATH environment variable.
↓ Sample Video ↓
↓ Vocabulary ↓
Extensions tab, install the extension Java Extension Pack provided by Microsoft;Run tab, click create a launch.json file, choose Java in the following pop-up, and then delete all configurations EXCEPT Debug (Launch) - Current File (if there is any).↓ Sample launch.json ↓
The launch.json should look like this
{ // Use IntelliSense to learn about possible attributes. // Hover to view descriptions of existing attributes. // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 "version": "0.2.0", "configurations": [ { "type": "java", "name": "Debug (Launch) - Current File", "request": "launch", "mainClass": "${file}" } ] }
File - New File menu item to create your first program named hlw.java;Code section of T 1.1 to hlw.java, and press F5 or use Run - Start Debugging menu item to run it;TERMINAL window named Java Debug Console should be opened by Visual Studio Code, and you can see the result in it.↓ Video Showing How to Do Without VSCode ↓
hlw.java
public class hlw { public static void main(String[] args) { System.out.println("Hello, world!"); } }
Output
Hello, world!
hlw.java
public class hlw { public static void main(String[] args) { String str = "Hello, world!"; System.out.println(str); } }
Output
Hello, world!
istr.java
import java.util.*; public class istr { public static void main(String[] args) { String str; Scanner s = new Scanner(System.in); str = s.nextLine(); s.close(); System.out.println(str); } }
Sample Input
I am typing this string.
Sample Output
I am typing this string.
Be sure to close the Scanner instance after input.
sum.java
import java.util.*; public class sum { public static void main(String[] args) { int a, b; Scanner s = new Scanner(System.in); a = s.nextInt(); b = s.nextInt(); s.close(); System.out.println(a + b); } }
Sample Input
1 2
Sample Output
3
↓ Note ↓
+: plus-: minus*: multiply/: divide (/ 0 can cause ArithmeticException)%: mod (get remainder)int is between Integer.MAX_VALUE (2147483647) and Integer.MIN_VALUE (-2147483648)↓ Why ↓
int: By default, the int data type is a 32-bit signed two's complement integer, which has a minimum value of −231 and a maximum value of 231−1 (© Oracle)12345 → 00000000000000000011000000111001bb stands for binary (h stands for hexadecimal)0 for positive, 1 for negative)12345 → 00000000000000000011000000111001b-12345 ↓-00000000000000000011000000111001b (informal) ↓11111111111111111100111111000110+1 (inverse, informal) ↓11111111111111111100111111000111bInteger.MAX_VALUE 231−1=2147483647 → 01111111111111111111111111111111bInteger.MIN_VALUE −231=−2147483648 → 10000000000000000000000000000000bdec.java
import java.util.*; public class dec { public static void main(String[] args) { double a, b; Scanner s = new Scanner(System.in); a = s.nextDouble(); b = s.nextDouble(); s.close(); System.out.println(a / b); } }
↓ Sample ↓
Sample Input #1
1 2
Sample Output #1
0.5
Sample Input #2
2 1
Sample Output #2
2.0
fsum.java
import java.util.*; public class fsum { public static int sum(int x, int y) { int answer = x + y; return answer; } public static void main(String[] args) { int a, b; Scanner s = new Scanner(System.in); a = s.nextInt(); b = s.nextInt(); s.close(); System.out.println(sum(a, b)); } }
Sample Input
1 2
Sample Output
3
↓ Try to define other functions, like minus(b, a), to test how a function works ↓
fminus.java
import java.util.*; public class fminus { public static int minus(int b, int a) { int answer = a - b; return answer; } public static void main(String[] args) { int a = 1, b = 2; System.out.println(minus(a, b)); } }
Output
1
tc.java
public class tc { public static void main(String[] args) { int x = 5; double y = x; System.out.println(x); System.out.println(y); System.out.println((double)x); // int z = y; // (ERROR) Type mismatch: cannot convert from double to int int z = (int)y; System.out.println(z); } }
Output
5
5.0
5.0
5
You must explicitly convert double to int (with precision loss), but you can implicitly convert int to double (without precision loss).
Sample Input
OK
Sample Output
OK
OK
OK
The input contains two non-negative integers.
Sample Input #1
1 2
Sample Output #1
3
-1
2
1
1
Sample Input #2
2 1
Sample Output #2
3
1
2
2
0
Presented by TU RUIXUAN
Copyright © TU RUIXUAN, All Rights Reserved
AP® Computer Science A Series