AP® Computer Science A Lecture 1

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

Contents (AP CED Equivalent)

  • Pre: Environment Configuration
  • Unit 1: Primitive Types (excluding 1.4)

Before You Can Start

Install the environment to edit and compile the code.

\darr Vocabulary \darr

  • Environment: necessary hardware and software used by the programmer;
  • Compile: the process of convertion of the code (which human can understand) to an executable program (which machine can understand).

Software to Download, Install, and Configure

Java Development Kit (JDK) 8: https://www.oracle.com/java/technologies/javase/javase-jdk8-downloads.html

Visual Studio Code: https://code.visualstudio.com/

For Windows Users

Be sure to add .../jdk_1.8.*/bin/ (the folder which you installed the JDK in) to the PATH environment variable.

\darr Sample Video \darr

Create your Workspace

  • Create a folder at your desktop, your home folder, or wherever you can assure that the path of its parent folder does not contain any non-ASCII character;
  • Similarly, the name of the new folder should also not contain any non-ASCII character.

\darr Vocabulary \darr

  • ASCII: American Standard Code for Information Interchange, containing English characters and symbols
  • ASCII Table © Wikipedia

Configure VSCode

  • Open Visual Studio Code, drag the folder to the window;
  • From the Extensions tab, install the extension Java Extension Pack provided by Microsoft;
  • From the 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).

\darr Sample launch.json \darr

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}"
        }
    ]
}

The First Program by VSCode

  • Use File - New File menu item to create your first program named hlw.java;
  • Copy the Code section of T 1.1 to hlw.java, and press F5 or use Run - Start Debugging menu item to run it;
  • A TERMINAL window named Java Debug Console should be opened by Visual Studio Code, and you can see the result in it.

\darr Video Showing How to Do Without VSCode \darr

Topics

T 1.1: Output a fixed string

hlw.java

public class hlw {
    public static void main(String[] args) {
        System.out.println("Hello, world!");
    }
}

Output

Hello, world!

T 1.2: Output a defined string

hlw.java

public class hlw {
    public static void main(String[] args) {
        String str = "Hello, world!";
        System.out.println(str);
    }
}

Output

Hello, world!

T 1.3: Input a string

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.

T 1.4: Process integers

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

\darr Note \darr

  • Assignment Operators
    • +: plus
    • -: minus
    • *: multiply
    • /: divide (/ 0 can cause ArithmeticException)
    • %: mod (get remainder)
  • The range of int is between Integer.MAX_VALUE (2147483647) and Integer.MIN_VALUE (-2147483648)

\darr Why \darr

  • int: By default, the int data type is a 32-bit signed two's complement integer, which has a minimum value of 231-2^{31} and a maximum value of 23112^{31}-1 (© Oracle)
  • 32-bit
    • 12345 \rarr 00000000000000000011000000111001b
    • b stands for binary (h stands for hexadecimal)
    • 32 means there are 32 binary characters to form the number
    • The first bit from left to right: signal (0 for positive, 1 for negative)
  • Two's Complement
    • Non-negative Integer
      • 12345 \rarr 00000000000000000011000000111001b
    • Negative Integer
      • -12345 \darr
      • -00000000000000000011000000111001b (informal) \darr
      •    11111111111111111100111111000110+1 (inverse, informal) \darr
      •    11111111111111111100111111000111b
  • Extreme Values
    • Integer.MAX_VALUE 2311=21474836472^{31}-1=2147483647 \rarr 01111111111111111111111111111111b
    • Integer.MIN_VALUE 231=2147483648-2^{31}=-2147483648 \rarr 10000000000000000000000000000000b

T 1.5: Process decimal numbers

dec.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);
    }
}

\darr Sample \darr

Sample Input #1

1 2

Sample Output #1

0.5

Sample Input #2

2 1

Sample Output #2

2.0

T 1.6: Define functions

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

\darr Try to define other functions, like minus(b, a), to test how a function works \darr

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

T 1.7: Type casting

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).

Homework

H 1.1: repeat

Sample Input

OK

Sample Output

OK
OK
OK

H 1.2: calc

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

Thanks for Watching

Presented by TU RUIXUAN
Copyright © TU RUIXUAN, All Rights Reserved

AP® Computer Science A Series