Skip to content · ⁨Lompat ke konten⁩

Using Objects and Methods · ⁨Menggunakan Benda dan Metode⁩

AP Computer Science A · ⁨AP Ilmu Komputer A⁩ · Topic 1 · ⁨Topik 1⁩

Video lesson for this topic · ⁨Pelajaran video untuk topik ini⁩ Open the video page · ⁨Buka halaman video⁩
15:05

Menggunakan Benda dan Metode

Berikut dua program Java. Yang pertama kehilangan titik koma. Yang kedua membagi tujuh dengan dua dan menyimpan jawabannya dalam bilangan bulat. Kompiler menghentikan…

English narration · English + 中文 subtitles burned in · ⁨Narasi bahasa Inggris · Subtitle bahasa Inggris + 中文 disematkan langsung⁩

1.1

Introduction to Algorithms, Programming, and Compilers · ⁨Pengenalan Algoritma, Pemrograman, dan Kompiler⁩

Syllabus · ⁨Silabus⁩
English

Learning Objective 1.1.A: Represent patterns and algorithms found in everyday life using written language or diagrams.

  • 1.1.A.1 Algorithms define step-by-step processes to follow when completing a task or solving a problem. These algorithms can be represented using written language or diagrams.
  • 1.1.A.2 Sequencing defines an order for when steps in a process are completed. Steps in a process are completed one at a time.

Learning Objective 1.1.B: Explain the code compilation and execution process.

  • 1.1.B.1 Code can be written in any text editor; however, an integrated development environment (IDE) is often used to write programs because it provides tools for a programmer to write, compile, and run code.
  • 1.1.B.2 A compiler checks code for some errors. Errors detectable by the compiler need to be fixed before the program can be run.

Learning Objective 1.1.C: Identify types of programming errors.

  • 1.1.C.1 A syntax error is a mistake in the program where the rules of the programming language are not followed. These errors are detected by the compiler.
  • 1.1.C.2 A logic error is a mistake in the algorithm or program that causes it to behave incorrectly or unexpectedly. These errors are detected by testing the program with specific data to see if it produces the expected outcome.
  • 1.1.C.3 A run-time error is a mistake in the program that occurs during the execution of a program. Run-time errors typically cause the program to terminate abnormally.
  • 1.1.C.4 An exception is a type of run-time error that occurs as a result of an unexpected error that was not detected by the compiler. It interrupts the normal flow of the program's execution.
Bahasa Indonesia

Tujuan Pembelajaran 1.1.A: Mewakili pola dan algoritma yang ditemukan dalam kehidupan sehari-hari menggunakan bahasa tulisan atau diagram.

  • 1.1.A.1 Algoritma mendefinisikan proses bertahap yang harus diikuti saat menyelesaikan tugas atau memecahkan masalah. Algoritma ini dapat direpresentasikan menggunakan bahasa tulisan atau diagram.
  • 1.1.A.2 Pengurutan menentukan urutan kapan langkah-langkah dalam suatu proses diselesaikan. Langkah-langkah dalam suatu proses diselesaikan satu per satu.

Tujuan Pembelajaran 1.1.B: Jelaskan proses kompilasi dan eksekusi kode.

  • 1.1.B.1 Kode dapat ditulis di editor teks apa pun; namun, lingkungan pengembangan terintegrasi (IDE) sering digunakan untuk menulis program karena menyediakan alat bagi programmer untuk menulis, mengkompilasi, dan menjalankan kode.
  • 1.1.B.2 Kompiler memeriksa kode untuk beberapa kesalahan. Kesalahan yang terdeteksi oleh kompiler harus diperbaiki sebelum program dapat dijalankan.

Tujuan Pembelajaran 1.1.C: Identifikasi jenis-jenis kesalahan pemrograman.

  • 1.1.C.1 Kesalahan sintaks adalah kesalahan dalam program di mana aturan bahasa pemrograman tidak dipatuhi. Kesalahan ini dideteksi oleh kompiler.
  • 1.1.C.2 Kesalahan logika adalah kesalahan dalam algoritma atau program yang menyebabkan program berperilaku tidak benar atau tidak terduga. Kesalahan ini dideteksi dengan menguji program dengan data khusus untuk melihat apakah program menghasilkan hasil yang diharapkan.
  • 1.1.C.3 Kesalahan waktu berjalan adalah kesalahan dalam program yang terjadi selama eksekusi program. Kesalahan waktu berjalan biasanya menyebabkan program berhenti secara tidak normal.
  • 1.1.C.4 Eksception adalah jenis kesalahan waktu berjalan yang terjadi akibat kesalahan tak terduga yang tidak terdeteksi oleh kompiler. Eksception mengganggu alur normal eksekusi program.

Source: College Board AP Course and Exam Description · ⁨Sumber: Deskripsi Kursus dan Ujian College Board AP⁩

English

An algorithm 算法 is a finite, step-by-step procedure that solves a problem. A program 程序 expresses an algorithm in a language a computer can run. Java is compiled 编译: the compiler 编译器 translates your source code into bytecode, which the Java Virtual Machine (JVM) runs. A syntax error 语法错误 (breaking the grammar) is caught by the compiler; a logic error 逻辑错误 (wrong result) is not – the program runs but misbehaves.

Bahasa Indonesia
Kode sumber di stasiun kerja — program ditulis, dikompilasi, dan dijalankan sebagai instruksi presisi
Kode sumber di stasiun kerja — program ditulis, dikompilasi, dan dijalankan sebagai instruksi presisi

Algoritma adalah prosedur terbatas langkah demi langkah yang menyelesaikan masalah. Program mengekspresikan algoritma dalam bahasa yang dapat dijalankan komputer. Java dikompilasi: kompiler menerjemahkan kode sumber Anda menjadi bytecode, yang dijalankan oleh Java Virtual Machine (JVM). Kesalahan sintaks (melanggar tata bahasa) ditangkap oleh kompiler; kesalahan logika (hasil salah) tidak – program tetap berjalan tetapi berperilaku keliru.

Sebuah kompiler menerjemahkan seluruh program sekaligus; sebuah interpreter menjalankannya baris demi baris
Kompiler menerjemahkan seluruh program sekaligus; interpreter menjalankannya baris per baris
Beberapa chip prosesor komputer terlihat dari bawah
Program Java Anda dikompilasi menjadi instruksi yang sebenarnya dijalankan oleh CPU seperti salah satu di atas
Vocabulary · ⁨Kosa kata⁩ Train · ⁨Latih⁩
English Bahasa Indonesia
algorithm/ˈælɡərɪθəm/ algoritma
program/ˈprəʊɡræm/ program
compiled/kəmˈpaɪld/ disusun ulang
compiler/kəmˈpaɪlə/ compiler
syntax error/ˈsɪntæks ˈerə/ kesalahan sintaksis
logic error/ˈlɒdʒɪk ˈerə/ kesalahan logika
variable/ˈveərɪəbl/ pembolehubah
1.2

Variables and Data Types · ⁨Variabel dan Tipe Data⁩

Syllabus · ⁨Silabus⁩
English

Learning Objective 1.2.A: Identify the most appropriate data type category for a particular specification.

  • 1.2.A.1 A data type is a set of values and a corresponding set of operations on those values. Data types can be categorized as either primitive or reference.
  • 1.2.A.2 The primitive data types used in this course define the set of values and corresponding operations on those values for numbers and Boolean values.
  • 1.2.A.3 A reference type is used to define objects that are not primitive types.

Learning Objective 1.2.B: Develop code to declare variables to store numbers and Boolean values.

  • 1.2.B.1 The three primitive data types used in this course are int, double, and boolean. An int value is an integer. A double value is a real number. A boolean value is either true or false.
    • Exclusion statement: The other five primitive data types (long, short, byte, float, and char) are outside the scope of the AP Computer Science A course and exam.
  • 1.2.B.2 A variable is a storage location that holds a value, which can change while the program is running. Every variable has a name and an associated data type. A variable of a primitive type holds a primitive value from that type.
Bahasa Indonesia

Tujuan Pembelajaran 1.2.A: Mengidentifikasi kategori tipe data yang paling tepat untuk spesifikasi tertentu.

  • 1.2.A.1 Tipe data adalah sekumpulan nilai beserta sekumpulan operasi yang sesuai pada nilai-nilai tersebut. Tipe data dapat dikategorikan sebagai primitif atau referensi.
  • 1.2.A.2 Tipe data primitif yang digunakan dalam kursus ini mendefinisikan himpunan nilai dan operasi yang sesuai pada nilai-nilai tersebut untuk angka dan nilai Boolean.
  • 1.2.A.3 Tipe referensi digunakan untuk mendefinisikan objek yang bukan tipe primitif.

Tujuan Pembelajaran 1.2.B: Mengembangkan kode untuk mendeklarasikan variabel guna menyimpan nilai numerik dan Boolean.

  • 1.2.B.1 Tiga tipe data primitif yang digunakan dalam kursus ini adalah int, double, dan boolean. Nilai int adalah bilangan bulat. Nilai double adalah bilangan riil. Nilai boolean dapat berupa true atau false.
    • Pernyataan Pengecualian: Lima tipe data primitif lainnya (long, short, byte, float, dan char) berada di luar cakupan kursus dan ujian AP Computer Science A.
  • 1.2.B.2 Variabel adalah lokasi penyimpanan yang memegang nilai, yang dapat berubah selama program berjalan. Setiap variabel memiliki nama dan tipe data terkait. Variabel dari tipe primitif memegang nilai primitif dari tipe tersebut.

Source: College Board AP Course and Exam Description · ⁨Sumber: Deskripsi Kursus dan Ujian College Board AP⁩

English

A variable 变量 is a named box that stores a value of a fixed type 类型. Java's main primitive types 基本类型 are int (whole numbers), double (decimals), and boolean (true/false). Declare with the type first:

Bahasa Indonesia

Variabel adalah kotak bernama yang menyimpan nilai dengan tipe tetap. Tipe primatif utama Java adalah int (bilangan bulat), double (desimal), dan boolean (true/false). Deklarasikan dengan tipe terlebih dahulu:

Tipe data dasar Java, masing-masing menyimpan jenis nilai yang berbeda
Tipe data dasar Java, masing-masing menyimpan jenis nilai yang berbeda
int score = 90;
double price = 4.99;
boolean passed = true;
Explore · ⁨Jelajahi⁩

Explore how a variable holds one value at a time · ⁨Jelajahi bagaimana variabel menyimpan satu nilai pada satu waktu⁩

A variable is a named box that stores one value of a fixed type. Step through the lines and watch each box take its value; notice that reassigning score overwrites the old number rather than making a new box. · ⁨Sebuah variabel adalah kotak bernama yang menyimpan satu nilai dengan tipe tetap. Telusuri baris-barisnya dan perhatikan setiap kotak mengambil nilainya; perhatikan bahwa penugasan ulang score menimpa angka lama alih-alih membuat kotak baru.⁩

Vocabulary · ⁨Kosa kata⁩ Train · ⁨Latih⁩
English Bahasa Indonesia
type/taɪp/ tipografi
primitive types/ˈprɪmɪtɪv taɪps/ tipe primitif
expression/ekˈspreʃn/ ekspresi
modulus/ˈmɒdjʊləs/ modulus
escape sequence/eˈskeɪp ˈsiːkwəns/ urutan escape
assignment/əˈsaɪnmənt/ penugasan
Casting/ˈkæstɪŋ/ Casting
library/ˈlaɪbrəri/ perpustakaan
abstraction/əbˈstrækʃn/ abstraksi
Comments/ˈkɒments/ Komentar
method signature/ˈmeθəd ˈsɪɡnɪtʃə/ tanda tangan metode
arguments/ˈɑːɡjuːmənts/ argumen
class (static) method/klæs ˈmeθəd/ metode kelas (statis)
1.3

Expressions and Output · ⁨Ekspresi dan Output⁩

Syllabus · ⁨Silabus⁩
English

Learning Objective 1.3.A: Develop code to generate output and determine the result that would be displayed.

  • 1.3.A.1 System.out.print and System.out.println display information on the computer display. System.out.println moves the cursor to a new line after the information has been displayed, while System.out.print does not.

Learning Objective 1.3.B: Develop code to utilize string literals and determine the result of using string literals.

  • 1.3.B.1 A literal is the code representation of a fixed value.
  • 1.3.B.2 A string literal is a sequence of characters enclosed in double quotes.
  • 1.3.B.3 Escape sequences are special sequences of characters that can be included in a string. They start with a \ and have a special meaning in Java. Escape sequences used in this course include double quote \", backslash \\, and newline \n.

Learning Objective 1.3.C: Develop code for arithmetic expressions and determine the result of these expressions.

  • 1.3.C.1 Arithmetic expressions, which consist of numeric values, variables, and operators, include expressions of type int and double.
  • 1.3.C.2 The arithmetic operators consist of addition +, subtraction -, multiplication *, division /, and remainder %. An arithmetic operation that uses two int values will evaluate to an int value. An arithmetic operation that uses at least one double value will evaluate to a double value.
    • Exclusion statement: Expressions that result in special double values (e.g., infinities and NaN) are outside the scope of the AP Computer Science A course and exam.
  • 1.3.C.3 When dividing numeric values that are both int values, the result is only the integer portion of the quotient. When dividing numeric values that use at least one double value, the result is the quotient.
  • 1.3.C.4 The remainder operator % is used to compute the remainder when one number a is divided by another number b.
    • Exclusion statement: The use of values less than 0 for a and the use of values less than or equal to 0 for b is outside the scope of the AP Computer Science A course and exam.
  • 1.3.C.5 Operators can be used to construct compound expressions. At compile time, numeric values are associated with operators according to operator precedence to determine how they are grouped. Parentheses can be used to modify operator precedence. Multiplication, division, and remainder have precedence over addition and subtraction. Operators with the same precedence are evaluated from left to right.
  • 1.3.C.6 An attempt to divide an integer by the integer zero will result in an ArithmeticException.
    • Exclusion statement: The use of dividing by zero when one numeric value is a double is outside the scope of the AP Computer Science A course and exam.
Bahasa Indonesia

Tujuan Pembelajaran 1.3.A: Mengembangkan kode untuk menghasilkan output dan menentukan hasil yang akan ditampilkan.

  • 1.3.A.1 System.out.print dan System.out.println menampilkan informasi pada layar komputer. System.out.println memindahkan kursor ke baris baru setelah informasi ditampilkan, sedangkan System.out.print tidak.

Tujuan Pembelajaran 1.3.B: Mengembangkan kode untuk memanfaatkan literal string dan menentukan hasil penggunaan literal string.

  • 1.3.B.1 Literal adalah representasi kode dari nilai tetap.
  • 1.3.B.2 Literal string adalah urutan karakter yang dibatasi oleh tanda kutip ganda.
  • 1.3.B.3 Urutan escape adalah urutan khusus karakter yang dapat dimasukkan ke dalam string. Mereka dimulai dengan \ dan memiliki arti khusus dalam Java. Urutan escape yang digunakan dalam kursus ini meliputi tanda kutip ganda \", garis miring terbalik \\, dan baris baru \n.

Tujuan Pembelajaran 1.3.C: Mengembangkan kode untuk ekspresi aritmatika dan menentukan hasil dari ekspresi-ekspresi ini.

  • 1.3.C.1 Ekspresi aritmatika, yang terdiri dari nilai numerik, variabel, dan operator, mencakup ekspresi bertipe int dan double.
  • 1.3.C.2 Operator aritmatika terdiri dari penjumlahan +, pengurangan -, perkalian *, pembagian /, dan sisa bagian %. Operasi aritmatika yang menggunakan dua nilai int akan mengevaluasi menjadi nilai int. Operasi aritmatika yang menggunakan setidaknya satu nilai double akan mengevaluasi menjadi nilai double.
    • Pernyataan Pengecualian: Ekspresi yang menghasilkan nilai double khusus (misalnya tak hingga dan NaN) berada di luar cakupan kursus dan ujian AP Computer Science A.
  • 1.3.C.3 Ketika membagi nilai numerik yang keduanya int, hasilnya hanyalah bagian bilangan bulat dari hasil bagi. Ketika membagi nilai numerik yang menggunakan setidaknya satu double, hasilnya adalah hasil bagi.
  • 1.3.C.4 Operator sisa % digunakan untuk menghitung sisa ketika satu angka a dibagi oleh angka lain b.
    • Pernyataan pengecualian: Penggunaan nilai kurang dari 0 untuk a dan penggunaan nilai kurang dari atau sama dengan 0 untuk b berada di luar cakupan mata kuliah dan ujian AP Computer Science A.
  • 1.3.C.5 Operator dapat digunakan untuk menyusun ekspresi majemuk. Pada waktu kompilasi, nilai numerik dikaitkan dengan operator sesuai dengan prioritas operator untuk menentukan bagaimana pengelompokannya. Kurung dapat digunakan untuk memodifikasi prioritas operator. Perkalian, pembagian, dan sisa memiliki prioritas lebih tinggi daripada penjumlahan dan pengurangan. Operator dengan prioritas yang sama dievaluasi dari kiri ke kanan.
  • 1.3.C.6 Upaya untuk membagi bilangan bulat dengan bilangan bulat nol akan menghasilkan ArithmeticException.
    • Pernyataan pengecualian: Penggunaan pembagian dengan nol ketika satu nilai numerik adalah double berada di luar cakupan mata kuliah dan ujian AP Computer Science A.

Source: College Board AP Course and Exam Description · ⁨Sumber: Deskripsi Kursus dan Ujian College Board AP⁩

English

An expression 表达式 combines values and operators to compute a result: + - * / and % (modulus 取模, the remainder). Integer division truncates: 7 / 2 is 3, while 7 % 2 is 1. Operator precedence follows math (*,/,% before +,-). Print with:

Dividing an integer by the integer 0 (like 7 / 0) is not allowed and crashes at run time with an ArithmeticException. Inside a string, a backslash marks an escape sequence 转义序列: \" prints a double quote, \\ a single backslash, and \n starts a new line – so System.out.println("She said \"hi\""); prints She said "hi".

Bahasa Indonesia

Ekspresi menggabungkan nilai dan operator untuk menghitung hasil: + - * / dan % (modulo, sisa bagi). Pembagian bilangan bulat memotong desimal: 7 / 2 adalah 3, sementara 7 % 2 adalah 1. Precedensi operator mengikuti aturan matematika (*,/,% didahulukan sebelum +,-). Cetak dengan:

System.out.print("no newline");
System.out.println("with newline");

Membagi bilangan bulat dengan bilangan bulat 0 (seperti 7 / 0) tidak diperbolehkan dan menyebabkan error saat runtime dengan ArithmeticException. Di dalam string, tanda backslash menandai baris escape: \" mencetak tanda kutip ganda, \\ satu backslash tunggal, dan \n memulai baris baru – sehingga System.out.println("She said \"hi\""); mencetak She said "hi".

Explore · ⁨Jelajahi⁩

Explore the order of operations step by step · ⁨Jelajahi urutan operasi langkah demi langkah⁩

Java applies *, /, % before + and -, working left to right. Watch each step and see why 2 + 3 * 4 is $14$, not $20$ — the multiplication happens first. · ⁨Java menerapkan *, /, % sebelum + dan -, bekerja dari kiri ke kanan. Perhatikan setiap langkah dan lihat mengapa 2 + 3 * 4 bernilai $14$, bukan $20$ — perkalian dilakukan terlebih dahulu.⁩

1.4

Assignment Statements and Input · ⁨Pernyataan Alokasi dan Input⁩

Syllabus · ⁨Silabus⁩
English

Learning Objective 1.4.A: Develop code for assignment statements with expressions and determine the value that is stored in the variable as a result of these statements.

  • 1.4.A.1 Every variable must be assigned a value before it can be used in an expression. That value must be from a compatible data type. A variable is initialized the first time it is assigned a value. Reference types can be assigned a new object or null if there is no object. The literal null is a special value used to indicate that a reference is not associated with any object.
  • 1.4.A.2 The assignment operator = allows a program to initialize or change the value stored in a variable. The value of the expression on the right is stored in the variable on the left.
    • Exclusion statement: The use of assignment operators inside expressions (e.g., a = b = 4; or a[i += 5]) is outside the scope of the AP Computer Science A course and exam.
  • 1.4.A.3 During execution, an expression is evaluated to produce a single value. The value of an expression has a type based on the evaluation of the expression.

Learning Objective 1.4.B: Develop code to read input.

  • 1.4.B.1 Input can come in a variety of forms, such as tactile, audio, visual, or text. The Scanner class is one way to obtain text input from the keyboard.
    • Exclusion statement: Any specific form of input from the user is outside the scope of the AP Computer Science A course and exam.
Bahasa Indonesia

Tujuan Pembelajaran 1.4.A: Mengembangkan kode untuk pernyataan penugasan dengan ekspresi dan menentukan nilai yang disimpan dalam variabel sebagai hasil dari pernyataan tersebut.

  • 1.4.A.1 Setiap variabel harus diberi nilai sebelum dapat digunakan dalam sebuah ekspresi. Nilai tersebut harus berasal dari tipe data yang kompatibel. Variabel diinisialisasi pertama kali saat diberi nilai. Tipe referensi dapat diberikan objek baru atau null jika tidak ada objek. Literal null adalah nilai khusus yang digunakan untuk menunjukkan bahwa referensi tidak terkait dengan objek apa pun.
  • 1.4.A.2 Operator penugasan = memungkinkan program untuk menginisialisasi atau mengubah nilai yang disimpan dalam variabel. Nilai dari ekspresi di sebelah kanan disimpan dalam variabel di sebelah kiri.
    • Pernyataan pengecualian: Penggunaan operator penugasan di dalam ekspresi (misalnya, a = b = 4; atau a[i += 5]) berada di luar cakupan mata kuliah dan ujian AP Computer Science A.
  • 1.4.A.3 Selama eksekusi, sebuah ekspresi dievaluasi untuk menghasilkan satu nilai. Nilai dari sebuah ekspresi memiliki tipe berdasarkan evaluasi ekspresi tersebut.

Tujuan Pembelajaran 1.4.B: Mengembangkan kode untuk membaca masukan.

  • 1.4.B.1 Masukan dapat datang dalam berbagai bentuk, seperti taktil, audio, visual, atau teks. Kelas Scanner adalah salah satu cara untuk mendapatkan teks masukan dari keyboard.
    • Pernyataan pengecualian: Bentuk masukan spesifik apa pun dari pengguna berada di luar cakupan mata kuliah dan ujian AP Computer Science A.

Source: College Board AP Course and Exam Description · ⁨Sumber: Deskripsi Kursus dan Ujian College Board AP⁩

English

An assignment 赋值 x = expr; evaluates the right side and stores it in the left variable. Read input with a Scanner:

Bahasa Indonesia

Alokasi x = expr; mengevaluasi sisi kanan dan menyimpannya ke variabel kiri. Baca input menggunakan Scanner:

Scanner in = new Scanner(System.in);
int age = in.nextInt();
String name = in.next();
1.5

Casting and Range of Variables · ⁨Casting dan Rentang Variabel⁩

Syllabus · ⁨Silabus⁩
English

Learning Objective 1.5.A: Develop code to cast primitive values to different primitive types in arithmetic expressions and determine the value that is produced as a result.

  • 1.5.A.1 The casting operators (int) and (double) can be used to convert from a double value to an int value (or vice versa).
  • 1.5.A.2 Casting a double value to an int value causes the digits to the right of the decimal point to be truncated.
  • 1.5.A.3 Some code causes int values to be automatically cast (widened) to double values.
  • 1.5.A.4 Values of type double can be rounded to the nearest integer by (int)(x + 0.5) for non-negative numbers or (int)(x - 0.5) for negative numbers.

Learning Objective 1.5.B: Describe conditions when an integer expression evaluates to a value out of range.

  • 1.5.B.1 The constant Integer.MAX_VALUE holds the value of the largest possible int value. The constant Integer.MIN_VALUE holds the value of the smallest possible int value.
  • 1.5.B.2 Integer values in Java are represented by values of type int, which are stored using a finite amount (4 bytes) of memory. Therefore, an int value must be in the range from Integer.MIN_VALUE to Integer.MAX_VALUE inclusive.
  • 1.5.B.3 If an expression would evaluate to an int value outside of the allowed range, an integer overflow occurs. The result is an int value in the allowed range but not necessarily the value expected.

Learning Objective 1.5.C: Describe conditions that limit accuracy of expressions.

  • 1.5.C.1 Computers allot a specified amount of memory to store data based on the data type. If an expression would evaluate to a double that is more precise than can be stored in the allotted amount of memory, a round-off error occurs. The result will be rounded to the representable value. To avoid rounding errors that naturally occur, use int values.
    • Exclusion statement: Other special decimal data types that can be used to avoid rounding errors are outside the scope of the AP Computer Science A course and exam.
Bahasa Indonesia

Tujuan Pembelajaran 1.5.A: Mengembangkan kode untuk melakukan casting pada nilai primitif ke tipe primitif berbeda dalam ekspresi aritmatika dan menentukan nilai yang dihasilkan sebagai akibatnya.

  • 1.5.A.1 Operator casting (int) dan (double) dapat digunakan untuk mengonversi dari nilai double ke nilai int (atau sebaliknya).
  • 1.5.A.2 Melakukan casting pada nilai double ke nilai int menyebabkan digit-digit di sebelah kanan koma desimal dipotong.
  • 1.5.A.3 Beberapa kode menyebabkan nilai int secara otomatis dikast (diperluas) menjadi nilai double.
  • 1.5.A.4 Nilai bertipe double dapat dibulatkan ke bilangan bulat terdekat oleh (int)(x + 0.5) untuk bilangan non-negatif atau (int)(x - 0.5) untuk bilangan negatif.

Tujuan Pembelajaran 1.5.B: Mendeskripsikan kondisi ketika ekspresi bilangan bulat mengevaluasi ke nilai di luar rentang.

  • 1.5.B.1 Konstanta Integer.MAX_VALUE menyimpan nilai terbesar yang mungkin dari nilai int. Konstanta Integer.MIN_VALUE menyimpan nilai terkecil yang mungkin dari nilai int.
  • 1.5.B.2 Nilai bilangan bulat dalam Java direpresentasikan oleh nilai bertipe int, yang disimpan menggunakan jumlah memori terbatas (4 byte). Oleh karena itu, nilai int harus berada dalam rentang dari Integer.MIN_VALUE hingga Integer.MAX_VALUE termasuk.
  • 1.5.B.3 Jika sebuah ekspresi akan mengevaluasi ke nilai int di luar rentang yang diperbolehkan, terjadi overflow bilangan bulat. Hasilnya adalah nilai int dalam rentang yang diperbolehkan tetapi tidak necessarily merupakan nilai yang diharapkan.

Tujuan Pembelajaran 1.5.C: Mendeskripsikan kondisi yang membatasi akurasi ekspresi.

  • 1.5.C.1 Komputer mengalokasikan jumlah memori tertentu untuk menyimpan data berdasarkan tipe data. Jika sebuah ekspresi akan mengevaluasi ke double yang lebih presisi daripada yang dapat disimpan dalam jumlah memori yang dialokasikan, terjadi kesalahan pembulatan. Hasilnya akan dibulatkan ke nilai yang dapat direpresentasikan. Untuk menghindari kesalahan pembulatan yang terjadi secara alami, gunakan nilai int.
    • Pernyataan pengecualian: Tipe data desimal khusus lainnya yang dapat digunakan untuk menghindari kesalahan pembulatan berada di luar cakupan mata kuliah dan ujian AP Computer Science A.

Source: College Board AP Course and Exam Description · ⁨Sumber: Deskripsi Kursus dan Ujian College Board AP⁩

English
int range, overflow and truncation

Each type has a fixed range; an int overflows past about 2.1 billion. Casting 类型转换 converts between types. Widening (int to double) is automatic; narrowing needs an explicit cast, which truncates (does not round):

Exam skill: watch for integer division producing a truncated result when a decimal was expected – cast one operand to double first.

Worked example. Trace each expression:

  • 7 / 2 → 3 (both int, so division truncates);
  • 7.0 / 2 → 3.5 (one double forces real division);
  • 7 % 2 → 1 (the remainder);
  • (double) 7 / 2 → 3.5 (the cast binds tighter than /, so it is 7.0 / 2);
  • (double) (7 / 2) → 3.0 (the parentheses compute 7 / 2 = 3 in int first, then widen).

The last two look alike but differ – the position of the cast decides whether the truncation happens.

Bahasa Indonesia
Rentang int, overflow dan truncation

Setiap tipe memiliki rentang tetap; sebuah int akan meluber (overflow) melewati sekitar 2.1 miliar. Pencastan mengubah antar tipe. Pelebaran (int ke double) dilakukan otomatis; penyempitan memerlukan pencastan eksplisit, yang memotong (tidak membulatkan):

double avg = (double) total / count;   // force real division
int whole = (int) 3.9;                 // 3, truncated

Kemampuan ujian: waspadai pembagian bilangan bulat yang menghasilkan hasil terpotong saat desimal diharapkan – cast satu operand ke double terlebih dahulu.

Contoh terarah. Jejakkan setiap ekspresi:

  • 7 / 2 → 3 (keduanya int, jadi pembagian dipotong);
  • 7.0 / 2 → 3.5 (satu double memaksa pembagian riil);
  • 7 % 2 → 1 (sisa baginya)
  • (double) 7 / 2 → 3.5 (penyimpanan tipe mengikat lebih ketat daripada /, sehingga menjadi 7.0 / 2);
  • (double) (7 / 2) → 3.0 (tanda kurung menghitung 7 / 2 = 3 dalam int pertama, kemudian memperlebar).

Dua terakhir terlihat mirip tetapi berbeda – posisi penyimpanan tipe menentukan apakah pemotongan terjadi.

Explore · ⁨Jelajahi⁩

Why int and double store numbers differently · ⁨Mengapa int dan double menyimpan angka berbeda⁩

An int holds only whole numbers in a fixed range; a double stores a mantissa and an exponent, trading exactness for a huge range. Casting double→int throws away the fraction, and a value past an int's range overflows. · ⁨Sebuah int hanya menyimpan bilangan bulat dalam rentang tetap; sebuah double menyimpan mantissa dan eksponen, menukar ketepatan untuk rentang yang sangat besar. Mengonversi double→int membuang pecahan, dan nilai melampaui rentang int akan menyebabkan overflow.⁩

1.6

Compound Assignment Operators · ⁨Operator Penugasan Majemuk⁩

Syllabus · ⁨Silabus⁩
English

Learning Objective 1.6.A: Develop code for assignment statements with compound assignment operators and determine the value that is stored in the variable as a result.

  • 1.6.A.1 Compound assignment operators +=, -=, *=, /=, and %= can be used in place of the assignment operator in numeric expressions. A compound assignment operator performs the indicated arithmetic operation between the value on the left and the value on the right and then assigns the result to the variable on the left.
  • 1.6.A.2 The post-increment operator ++ and post-decrement operator -- are used to add 1 or subtract 1 from the stored value of a numeric variable. The new value is assigned to the variable.
    • Exclusion statement: The use of increment and decrement operators in prefix form (e.g., ++x) is outside the scope of the AP Computer Science A course and exam. The use of increment and decrement operators inside other expressions (e.g., arr[x++]) is outside the scope of the AP Computer Science A course and exam.
Bahasa Indonesia

Tujuan Pembelajaran 1.6.A: Mengembangkan kode untuk pernyataan penugasan dengan operator penugasan majemuk dan menentukan nilai yang disimpan dalam variabel sebagai akibatnya.

  • 1.6.A.1 Operator penugasan majemuk +=, -=, *=, /=, dan %= dapat digunakan sebagai pengganti operator penugasan dalam ekspresi numerik. Operator penugasan majemuk melakukan operasi aritmatika yang ditunjukkan antara nilai di sebelah kiri dan nilai di sebelah kanan, lalu menetapkan hasilnya ke variabel di sebelah kiri.
  • 1.6.A.2 Operator post-increment ++ dan operator post-decrement -- digunakan untuk menambahkan 1 atau mengurangi 1 dari nilai yang disimpan dalam variabel numerik. Nilai baru ditetapkan ke variabel.
    • Pernyataan Pengecualian: Penggunaan operator increment dan decrement dalam bentuk prefiks (misalnya, ++x) berada di luar cakupan kursus dan ujian AP Computer Science A. Penggunaan operator increment dan decrement di dalam ekspresi lain (misalnya, arr[x++]) berada di luar cakupan kursus dan ujian AP Computer Science A.

Source: College Board AP Course and Exam Description · ⁨Sumber: Deskripsi Kursus dan Ujian College Board AP⁩

English

Shorthands combine an operation with assignment: x += 5 means x = x + 5; likewise -=, *=, /=, %=. The increment and decrement operators x++ and x-- add or subtract one.

Bahasa Indonesia

Penulisan singkat menggabungkan operasi dengan penugasan: x += 5 berarti x = x + 5; demikian pula -=, *=, /=, %=. Operator increment dan decrement x++ dan x-- menambah atau mengurangi satu.

1.7

Application Program Interface (API) and Libraries · ⁨Application Program Interface (API) dan Pustaka⁩

Syllabus · ⁨Silabus⁩
English

Learning Objective 1.7.A: Identify the attributes and behaviors of a class found in the libraries contained in an API.

  • 1.7.A.1 Libraries are collections of classes. An application programming interface (API) specification informs the programmer how to use those classes. Documentation found in API specifications and libraries is essential to understanding the attributes and behaviors of a class defined by the API. A class defines a specific reference type. Classes in the APIs and libraries are grouped into packages. Existing classes and class libraries can be utilized to create objects.
  • 1.7.A.2 Attributes refer to the data related to the class and are stored in variables. Behaviors refer to what instances of the class can do (or what can be done with them) and are defined by methods.
Bahasa Indonesia

Tujuan Pembelajaran 1.7.A: Mengidentifikasi atribut dan perilaku kelas yang ditemukan dalam pustaka yang terkandung dalam API.

  • 1.7.A.1 Pustaka adalah kumpulan kelas. Spesifikasi application programming interface (API) memberi tahu programmer cara menggunakan kelas-kelas tersebut. Dokumentasi yang ditemukan dalam spesifikasi API dan pustaka sangat penting untuk memahami atribut dan perilaku kelas yang didefinisikan oleh API. Sebuah kelas mendefinisikan tipe referensi tertentu. Kelas dalam API dan pustaka dikelompokkan ke dalam paket. Kelas dan pustaka kelas yang sudah ada dapat digunakan untuk membuat objek.
  • 1.7.A.2 Atribut merujuk pada data yang terkait dengan kelas dan disimpan dalam variabel. Perilaku merujuk pada apa yang dapat dilakukan oleh instance dari kelas tersebut (atau apa yang dapat dilakukan dengannya) dan didefinisikan oleh metode.

Source: College Board AP Course and Exam Description · ⁨Sumber: Deskripsi Kursus dan Ujian College Board AP⁩

English

An API (Application Programming Interface) 应用程序接口 is the published list of classes and methods you may use. A library 库 is a collection of ready-made classes (like Math, String, Scanner). You read the API documentation to learn what a method needs (its parameters) and returns, without seeing its inner code – an example of abstraction 抽象.

Bahasa Indonesia

Sebuah API (Application Programming Interface) adalah daftar kelas dan metode yang dipublikasikan yang dapat Anda gunakan. Sebuah pustaka adalah kumpulan kelas siap pakai (seperti Math, String, Scanner). Anda membaca dokumentasi API untuk mempelajari apa yang dibutuhkan sebuah metode (parameternya) dan dikembalikan, tanpa melihat kode dalamnya – contoh dari abstraksi.

Vocabulary · ⁨Kosa kata⁩ Train · ⁨Latih⁩
English Bahasa Indonesia
Interface/ˈɪntəfeɪs/ Antarmuka
1.8

Documentation with Comments · ⁨Dokumentasi dengan Komentar⁩

Syllabus · ⁨Silabus⁩
English

Learning Objective 1.8.A: Describe the functionality and use of code through comments.

  • 1.8.A.1 Comments are written for both the original programmer and other programmers to understand the code and its functionality, but are ignored by the compiler and are not executed when the program is run. Three types of comments in Java include /* */, which generates a block of comments; //, which generates a comment on one line; and /** */, which are Javadoc comments and are used to create API documentation.
  • 1.8.A.2 A precondition is a condition that must be true just prior to the execution of a method in order for it to behave as expected. There is no expectation that the method will check to ensure preconditions are satisfied.
  • 1.8.A.3 A postcondition is a condition that must always be true after the execution of a method. Postconditions describe the outcome of the execution in terms of what is being returned or the current value of the attributes of an object.
Bahasa Indonesia

Tujuan Pembelajaran 1.8.A: Mendeskripsikan fungsionalitas dan penggunaan kode melalui komentar.

  • 1.8.A.1 Komentar ditulis baik untuk programmer asli maupun programmer lain agar dapat memahami kode dan fungsionalitasnya, tetapi diabaikan oleh compiler dan tidak dieksekusi saat program dijalankan. Tiga jenis komentar dalam Java meliputi /* */, yang menghasilkan blok komentar; //, yang menghasilkan komentar pada satu baris; dan /** */, yang merupakan komentar Javadoc dan digunakan untuk membuat dokumentasi API.
  • 1.8.A.2 Prasyarat adalah kondisi yang harus benar tepat sebelum eksekusi sebuah metode agar dapat berperilaku sesuai harapan. Tidak ada ekspektasi bahwa metode akan memeriksa apakah prasyarat terpenuhi.
  • 1.8.A.3 Pasca-syarat adalah kondisi yang harus selalu benar setelah eksekusi sebuah metode. Pasca-syarat menggambarkan hasil eksekusi dalam hal apa yang dikembalikan atau nilai saat ini dari atribut suatu objek.

Source: College Board AP Course and Exam Description · ⁨Sumber: Deskripsi Kursus dan Ujian College Board AP⁩

English

Comments 注释 are ignored by the compiler but explain code to humans: // for a single line, /* ... */ for a block, and /** ... */ for a Javadoc comment that documents a method's purpose, parameters, and return value. Precise preconditions and postconditions are written here.

Bahasa Indonesia

Komentar diabaikan oleh compiler tetapi menjelaskan kode kepada manusia: // untuk satu baris, /* ... */ untuk blok, dan /** ... */ untuk komentar Javadoc yang mendokumentasikan tujuan, parameter, dan nilai pengembalian sebuah metode. Prasyarat dan pasca-syarat yang presisi ditulis di sini.

1.9

Method Signatures · ⁨Tanda Tangan Metode⁩

Syllabus · ⁨Silabus⁩
English

Learning Objective 1.9.A: Identify the correct method to call based on documentation and method signatures.

  • 1.9.A.1 A method is a named block of code that only runs when it is called. A block of code is any section of code that is enclosed in braces. Procedural abstraction allows a programmer to use a method by knowing what the method does even if they do not know how the method was written.
  • 1.9.A.2 A parameter is a variable declared in the header of a method or constructor and can be used inside the body of the method. This allows values or arguments to be passed and used by a method or constructor. A method signature for a method with parameters consists of the method name and the ordered list of parameter types. A method signature for a method without parameters consists of the method name and an empty parameter list.

Learning Objective 1.9.B: Describe how to call methods.

  • 1.9.B.1 A void method does not have a return value and is therefore not called as part of an expression.
  • 1.9.B.2 A non-void method returns a value that is the same type as the return type in the header. To use the return value when calling a non-void method, it must be stored in a variable or used as part of an expression.
  • 1.9.B.3 An argument is a value that is passed into a method when the method is called. The arguments passed to a method must be compatible in number and order with the types identified in the parameter list of the method signature. When calling methods, arguments are passed using call by value. Call by value initializes the parameters with copies of the arguments.
  • 1.9.B.4 Methods are said to be overloaded when there are multiple methods with the same name but different signatures.
  • 1.9.B.5 A method call interrupts the sequential execution of statements, causing the program to first execute the statements in the method before continuing. Once the last statement in the method has been executed or a return statement is executed, the flow of control is returned to the point immediately following where the method was called.
Bahasa Indonesia

Tujuan Pembelajaran 1.9.A: Mengidentifikasi metode yang benar untuk dipanggil berdasarkan dokumentasi dan tanda tangan metode.

  • 1.9.A.1 Sebuah metode adalah blok kode bernama yang hanya berjalan ketika dipanggil. Sebuah blok kode adalah bagian kode apa pun yang dibungkus dalam kurung kurawal. Abstraksi prosedural memungkinkan seorang programmer menggunakan sebuah metode dengan mengetahui apa yang dilakukan metode tersebut meskipun mereka tidak tahu bagaimana metode tersebut ditulis.
  • 1.9.A.2 Sebuah parameter adalah variabel yang dideklarasikan dalam header metode atau konstruktor dan dapat digunakan di dalam tubuh metode. Hal ini memungkinkan nilai atau argumen untuk dikirim dan digunakan oleh metode atau konstruktor. Tanda tangan metode untuk sebuah metode dengan parameter terdiri dari nama metode dan daftar berurutan tipe parameter. Tanda tangan metode untuk sebuah metode tanpa parameter terdiri dari nama metode dan daftar parameter kosong.

Tujuan Pembelajaran 1.9.B: Mendeskripsikan cara memanggil metode.

  • 1.9.B.1 Sebuah metode void tidak memiliki nilai pengembalian dan oleh karena itu tidak dipanggil sebagai bagian dari ekspresi.
  • 1.9.B.2 Sebuah metode non-void mengembalikan nilai yang sama tipenya dengan tipe pengembalian di header. Untuk menggunakan nilai pengembalian saat memanggil metode non-void, nilai tersebut harus disimpan dalam variabel atau digunakan sebagai bagian dari ekspresi.
  • 1.9.B.3 Sebuah argumen adalah nilai yang dikirim ke dalam sebuah metode saat metode tersebut dipanggil. Argumen yang dikirim ke sebuah metode harus kompatibel dalam jumlah dan urutan dengan tipe yang diidentifikasi dalam daftar parameter tanda tangan metode. Saat memanggil metode, argumen dikirim menggunakan call by value. Call by value menginisialisasi parameter dengan salinan argumen.
  • 1.9.B.4 Metode dikatakan overloaded (terbeban) ketika terdapat beberapa metode dengan nama yang sama tetapi berbeda tanda tangannya.
  • 1.9.B.5 Sebuah pemanggilan metode mengganggu eksekusi sekuensial pernyataan, menyebabkan program untuk pertama kali mengeksekusi pernyataan-pernyataan di dalam metode sebelum melanjutkan. Setelah pernyataan terakhir dalam metode dieksekusi atau pernyataan return dieksekusi, alur kendali dikembalikan ke titik tepat di mana pemanggilan metode dilakukan.

Source: College Board AP Course and Exam Description · ⁨Sumber: Deskripsi Kursus dan Ujian College Board AP⁩

English

A method signature 方法签名 is a method's name plus its parameter types, e.g. nextInt() or substring(int, int). To call a method you must supply arguments 实参 that match the parameters in number, type, and order. The method header (the full declaration) also states the return type – the type of value the method gives back (void if none) – but the return type is not part of the signature, which is why two methods cannot differ by return type alone.

Bahasa Indonesia

Sebuah tanda tangan metode adalah nama metode beserta jenis parameternya, mis. nextInt() atau substring(int, int). Untuk memanggil sebuah metode, Anda harus menyediakan argumen yang sesuai dengan parameternya dalam jumlah, jenis, dan urutan. Header metode (deklarasi lengkap) juga menyatakan jenis pengembalian – jenis nilai yang dikembalikan metode (void jika tidak ada) – tetapi jenis pengembalian bukan bagian dari tanda tangan, itulah sebabnya dua metode tidak dapat berbeda hanya oleh jenis pengembalian.

1.10

Calling Class Methods · ⁨Memanggil Metode Kelas⁩

Syllabus · ⁨Silabus⁩
English

Learning Objective 1.10.A: Develop code to call class methods and determine the result of those calls.

  • 1.10.A.1 Class methods are associated with the class, not instances of the class. Class methods include the keyword static in the header before the method name.
  • 1.10.A.2 Class methods are typically called using the class name along with the dot operator. When the method call occurs in the defining class, the use of the class name is optional in the call.
Bahasa Indonesia

Tujuan Pembelajaran 1.10.A: Mengembangkan kode untuk memanggil metode kelas dan menentukan hasil dari pemanggilan tersebut.

  • 1.10.A.1 Metode kelas terkait dengan kelas, bukan instansinya. Metode kelas mencakup kata kunci static dalam header sebelum nama metode.
  • 1.10.A.2 Metode kelas biasanya dipanggil menggunakan nama kelas bersama dengan operator titik. Ketika pemanggilan metode terjadi di dalam kelas yang mendefinisikannya, penggunaan nama kelas adalah opsional dalam pemanggilan.

Source: College Board AP Course and Exam Description · ⁨Sumber: Deskripsi Kursus dan Ujian College Board AP⁩

English

A class (static) method 类方法 belongs to the class itself, so you call it on the class name: ClassName.method(args). No object is needed.

Bahasa Indonesia

Sebuah metode kelas (statis) milik kelas itu sendiri, sehingga Anda memanggilnya pada nama kelas: ClassName.method(args). Tidak perlu objek.

Explore · ⁨Jelajahi⁩

Follow a class-method call on the stack · ⁨Ikuti pemanggilan metode kelas pada tumpukan (stack)⁩

Calling a class method like Math.max pushes a new frame onto the call stack; when the method returns a value, its frame pops and control goes back to the caller. Step through to watch the stack grow and shrink. · ⁨Memanggil metode kelas seperti Math.max mendorong bingkai baru ke tumpukan panggilan; ketika metode mengembalikan nilai, bingkainya dilepas dan kendali kembali ke Caller. Telusuri untuk melihat tumpukan membesar dan mengecil.⁩

Vocabulary · ⁨Kosa kata⁩ Train · ⁨Latih⁩
English Bahasa Indonesia
class/klæs/ kelas
1.11

Math Class · ⁨Kelas Math⁩

Syllabus · ⁨Silabus⁩
English

Learning Objective 1.11.A: Develop code to write expressions that incorporate calls to built-in mathematical libraries and determine the value that is produced as a result.

  • 1.11.A.1 The Math class is part of the java.lang package. Classes in the java.lang package are available by default.
  • 1.11.A.2 The Math class contains only class methods. The following Math class methods—including what they do and when they are used—are part of the Java Quick Reference:
    • static int abs(int x) returns the absolute value of an int value.
    • static double abs(double x) returns the absolute value of a double value.
    • static double pow(double base, double exponent) returns the value of the first parameter raised to the power of the second parameter.
    • static double sqrt(double x) returns the nonnegative square root of a double value.
    • static double random() returns a double value greater than or equal to 0.0 and less than 1.0.
  • 1.11.A.3 The values returned from Math.random() can be manipulated using arithmetic and casting operators to produce a random int or double in a defined range based on specified criteria. Each endpoint of the range can be inclusive, meaning the value is included, or exclusive, meaning the value is not included.
Bahasa Indonesia

Tujuan Pembelajaran 1.11.A: Mengembangkan kode untuk menulis ekspresi yang menyertakan pemanggilan ke perpustakaan matematika bawaan dan menentukan nilai yang dihasilkan sebagai akibatnya.

  • 1.11.A.1 Kelas Math merupakan bagian dari paket java.lang. Kelas-kelas dalam paket java.lang tersedia secara default.
  • 1.11.A.2 Kelas Math hanya berisi metode kelas. Berikut ini Math metode kelas—termasuk fungsinya dan kapan digunakan—merupakan bagian dari Rujukan Cepat Java:
    • static int abs(int x) mengembalikan nilai mutlak dari nilai int.
    • static double abs(double x) mengembalikan nilai mutlak dari nilai double.
    • static double pow(double base, double exponent) mengembalikan nilai parameter pertama yang dipangkatkan dengan parameter kedua.
    • static double sqrt(double x) mengembalikan akar kuadrat nonnegatif dari nilai double.
    • static double random() mengembalikan nilai double yang lebih besar atau sama dengan 0.0 dan kurang dari 1.0.
  • 1.11.A.3 Nilai-nilai yang dikembalikan dari Math.random() dapat dimanipulasi menggunakan operator aritmatika dan pemetaan (casting) untuk menghasilkan bilangan acak int atau double dalam rentang tertentu berdasarkan kriteria yang ditentukan. Setiap ujung rentang dapat bersifat inklusif, artinya nilai tersebut termasuk, atau eksklusif, artinya nilai tersebut tidak termasuk.

Source: College Board AP Course and Exam Description · ⁨Sumber: Deskripsi Kursus dan Ujian College Board AP⁩

English

The Math class provides static math methods: Math.abs(x), Math.pow(base, exp), Math.sqrt(x), and Math.random() (a double in $[0,1)$). To get a random integer from 0 to n-1: (int)(Math.random() * n).

Bahasa Indonesia

Kelas Math menyediakan metode matematika statis: Math.abs(x), Math.pow(base, exp), Math.sqrt(x), dan Math.random() (sebuah double dalam $[0,1)$). Untuk mendapatkan bilangan bulat acak dari 0 hingga n-1: (int)(Math.random() * n).

1.12

Objects: Instances of Classes · ⁨Objek: Instansi Kelas⁩

Syllabus · ⁨Silabus⁩
English

Learning Objective 1.12.A: Explain the relationship between a class and an object.

  • 1.12.A.1 An object is a specific instance of a class with defined attributes. A class is the formal implementation, or blueprint, of the attributes and behaviors of an object.
  • 1.12.A.2 A class hierarchy can be developed by putting common attributes and behaviors of related classes into a single class called a superclass. Classes that extend a superclass, called subclasses, can draw upon the existing attributes and behaviors of the superclass without replacing these in the code. This creates an inheritance relationship from the subclasses to the superclass.
    • Exclusion statement: Designing and implementing inheritance relationships are outside the scope of the AP Computer Science A course and exam.
  • 1.12.A.3 All classes in Java are subclasses of the Object class.

Learning Objective 1.12.B: Develop code to declare variables to store reference types.

  • 1.12.B.1 A variable of a reference type holds an object reference, which can be thought of as the memory address of that object.
Bahasa Indonesia

Tujuan Pembelajaran 1.12.A: Menjelaskan hubungan antara kelas dan objek.

  • 1.12.A.1 Sebuah objek adalah instans spesifik dari sebuah kelas dengan atribut yang didefinisikan. Sebuah kelas adalah implementasi formal, atau cetak biru, dari atribut dan perilaku objek.
  • 1.12.A.2 Hierarki kelas dapat dikembangkan dengan menempatkan atribut dan perilaku umum dari kelas-kelas yang berkaitan ke dalam satu kelas yang disebut superclass. Kelas-kelas yang memperluas superclass, yang disebut subclasses, dapat memanfaatkan atribut dan perilaku yang sudah ada dari superclass tanpa menggantinya dalam kode. Hal ini menciptakan hubungan pewarisan dari subclass menuju superclass.
    • Pernyataan pengecualian: Merancang dan mengimplementasikan hubungan pewarisan berada di luar cakupan kursus dan ujian AP Computer Science A.
  • 1.12.A.3 Semua kelas dalam Java adalah subclass dari kelas Object.

Tujuan Pembelajaran 1.12.B: Mengembangkan kode untuk mendeklarasikan variabel guna menyimpan tipe referensi.

  • 1.12.B.1 Variabel dari tipe referensi memegang referensi objek, yang dapat dipandang sebagai alamat memori dari objek tersebut.

Source: College Board AP Course and Exam Description · ⁨Sumber: Deskripsi Kursus dan Ujian College Board AP⁩

English
= copies the reference, not the object

A class 类 is a blueprint; an object 对象 is a concrete instance 实例 built from it. A class bundles data (fields) with behavior (methods) – the heart of object-oriented programming 面向对象编程. String, Scanner, and ArrayList are all classes you instantiate.

Classes can be organised into a hierarchy. A superclass 父类 holds attributes and behaviors shared by several subclasses 子类 that extend it – an inheritance relationship 继承关系. Every class in Java is ultimately a subclass of the built-in Object class, which is why every object already has a toString method; writing a subclass method with the same signature as a superclass one is method overriding 方法重写. (Designing your own inheritance is beyond this course, but you are expected to recognise this vocabulary.)

Bahasa Indonesia
= menyalin referensi, bukan objek

Sebuah kelas adalah cetak biru; sebuah objek adalah instansi konkret yang dibangun darinya. Sebuah kelas mengemas data (lapang) dengan perilaku (metode) – inti dari pemrograman berorientasi objek. String, Scanner, dan ArrayList adalah semua kelas yang Anda instansiasi.

Kelas dapat diorganisir ke dalam hierarki. Sebuah superclass memegang atribut dan perilaku yang dibagikan oleh beberapa subclass yang extend – hubungan pewarisan. Setiap kelas dalam Java pada akhirnya adalah subclass dari kelas bawaan Object, yang mengapa setiap objek sudah memiliki metode toString; menulis metode subclass dengan tanda tangan yang sama seperti superclass adalah penggantian metode. (Merancang pewarisan Anda sendiri berada di luar kursus ini, tetapi Anda diharapkan mengenali kosakata ini.)

Diagram kelas: atribut privat dan publik metode
Diagram kelas: atribut privat dan publik metode
Kelas adalah cetak biru; setiap objek adalah satu instansi dibangun darinya
Kelas adalah cetak biru; setiap objek adalah satu instansi dibangun darinya
Vocabulary · ⁨Kosa kata⁩ Train · ⁨Latih⁩
English Bahasa Indonesia
object/ˈɒbdʒekt/ benda itu sendiri
superclass/ˈsuːpəklæs/ superclass
subclasses/ˈsʌbklæsɪz/ subkelas
inheritance relationship/ɪnˈherɪtəns rɪˈleɪʃənʃɪp/ hubungan pewarisan
method overriding/ˈmeθəd ˌəʊvəˈraɪdɪŋ/ penimpaan metode
Instantiation/ˌɪnstænʃɪˈeɪʃn/ Instantiasi
constructor/kənˈstrʌktə/ konstruktor
reference/ˈrefrəns/ referensi
1.13

Object Creation and Storage (Instantiation) · ⁨Pembuatan Objek dan Penyimpanan (Instansiasi)⁩

Syllabus · ⁨Silabus⁩
English

Learning Objective 1.13.A: Identify, using its signature, the correct constructor being called.

  • 1.13.A.1 A class contains constructors that are called to create objects. They have the same name as the class.
  • 1.13.A.2 A constructor signature consists of the constructor's name, which is the same as the class name, and the ordered list of parameter types. The parameter list, in the header of a constructor, lists the types of the values that are passed and their variable names.
  • 1.13.A.3 Constructors are said to be overloaded when there are multiple constructors with different signatures.

Learning Objective 1.13.B: Develop code to declare variables of the correct types to hold object references.

  • 1.13.B.1 A variable of a reference type holds an object reference or, if there is no object, null.

Learning Objective 1.13.C: Develop code to create an object by calling a constructor.

  • 1.13.C.1 An object is typically created using the keyword new followed by a call to one of the class's constructors.
  • 1.13.C.2 Parameters allow constructors to accept values to establish the initial values of the attributes of the object.
  • 1.13.C.3 A constructor argument is a value that is passed into a constructor when the constructor is called. The arguments passed to a constructor must be compatible in order and number with the types identified in the parameter list in the constructor signature. When calling constructors, arguments are passed using call by value. Call by value initializes the parameters with copies of the arguments.
  • 1.13.C.4 A constructor call interrupts the sequential execution of statements, causing the program to first execute the statements in the constructor before continuing. Once the last statement in the constructor has been executed, the flow of control is returned to the point immediately following where the constructor was called.
Bahasa Indonesia

Tujuan Pembelajaran 1.13.A: Mengidentifikasi, menggunakan tandanya, konstruktor mana yang benar sedang dipanggil.

  • 1.13.A.1 Sebuah kelas memiliki konstruktor yang dipanggil untuk membuat objek. Mereka memiliki nama yang sama dengan kelasnya.
  • 1.13.A.2 Tanda konstruktor terdiri dari nama konstruktor, yang sama dengan nama kelas, dan daftar berurutan dari tipe parameternya. Daftar parameter, dalam header konstruktor, mencantumkan tipe dari nilai-nilai yang diluluskan dan nama variabel mereka.
  • 1.13.A.3 Konstruktor dikatakan tumpang tindih (overloaded) ketika terdapat beberapa konstruktor dengan tanda yang berbeda.

Tujuan Pembelajaran 1.13.B: Mengembangkan kode untuk mendeklarasikan variabel dengan tipe yang tepat untuk menampung referensi objek.

  • 1.13.B.1 Variabel dari tipe referensi memegang referensi objek atau, jika tidak ada objek, null.

Tujuan Pembelajaran 1.13.C: Mengembangkan kode untuk membuat objek dengan memanggil konstruktor.

  • 1.13.C.1 Objek biasanya dibuat menggunakan kata kunci new diikuti dengan pemanggilan ke salah satu konstruktor kelas.
  • 1.13.C.2 Parameter memungkinkan konstruktor menerima nilai untuk menetapkan nilai awal dari atribut objek.
  • 1.13.C.3 Argumen konstruktor adalah nilai yang diluluskan ke dalam konstruktor ketika konstruktor dipanggil. Argumen-argumen yang diluluskan ke konstruktor harus kompatibel baik secara urutan maupun jumlah dengan tipe-tipe yang teridentifikasi dalam daftar parameter pada tanda konstruktor. Saat memanggil konstruktor, argumen diluluskan menggunakan call by value. Call by value menginisialisasi parameter dengan salinan argumen.
  • 1.13.C.4 Pemanggilan konstruktor menghentikan eksekusi pernyataan secara berurutan, menyebabkan program terlebih dahulu mengeksekusi pernyataan-pernyataan dalam konstruktor sebelum melanjutkan. Setelah pernyataan terakhir dalam konstruktor dieksekusi, alur kendali dikembalikan ke titik segera setelah tempat konstruktor tersebut dipanggil.

Source: College Board AP Course and Exam Description · ⁨Sumber: Deskripsi Kursus dan Ujian College Board AP⁩

English

Instantiation 实例化 creates an object with the new keyword, which calls a constructor 构造函数:

The variable holds a reference 引用 (the object's address), not the object itself. Two references can point to the same object; comparing them with == compares addresses, not contents.

A reference can also point to nothing: the special value null 空值 means "not attached to any object". Calling a method on a null reference crashes at run time with a NullPointerException. Guard against it by testing with ==/!= and checking null first, so && short-circuits before the method runs: if (s != null && s.length() > 0).

Bahasa Indonesia

Instansiasi membuat objek dengan kata kunci new, yang memanggil konstruktor:

Scanner in = new Scanner(System.in);
String s = new String("hi");   // or just "hi"

Variabel tersebut memegang referensi (alamat objek), bukan objek itu sendiri. Dua referensi dapat menunjuk ke sama objek; membandingkan mereka dengan == membandingkan alamat, bukan isi.

Referensi juga dapat menunjuk ke sesuatu yang kosong: nilai khusus null berarti "tidak terikat pada objek apa pun". Memanggil metode pada referensi null menyebabkan crash saat waktu berjalan dengan NullPointerException. Lindungi terhadapnya dengan menguji dengan ==/!= dan memeriksa null pertama, agar && pendek-circuit sebelum metode berjalan: if (s != null && s.length() > 0).

Variabel primitif memegang nilainya langsung, referensi memegang panah ke objek
Variabel primitif memegang nilainya langsung, referensi memegang panah ke objek
Vocabulary · ⁨Kosa kata⁩ Train · ⁨Latih⁩
English Bahasa Indonesia
null/nʌl/ null
instance method/ˈɪnstəns ˈmeθəd/ metode instans
immutable/ɪˈmjuːtəbl/ tidak dapat diubah
1.14

Calling Instance Methods · ⁨Memanggil Metode Instance⁩

Syllabus · ⁨Silabus⁩
English

Learning Objective 1.14.A: Develop code to call instance methods and determine the result of these calls.

  • 1.14.A.1 Instance methods are called on objects of the class. The dot operator is used along with the object name to call instance methods.
  • 1.14.A.2 A method call on a null reference will result in a NullPointerException.
Bahasa Indonesia

Tujuan Pembelajaran 1.14.A: Mengembangkan kode untuk memanggil metode instans dan menentukan hasil dari pemanggilan-pemanggilan ini.

  • 1.14.A.1 Metode instans dipanggil pada objek dari kelas tersebut. Operator titik digunakan bersama dengan nama objek untuk memanggil metode instans.
  • 1.14.A.2 Pemanggilan metode pada referensi null akan menghasilkan NullPointerException.

Source: College Board AP Course and Exam Description · ⁨Sumber: Deskripsi Kursus dan Ujian College Board AP⁩

English

An instance method 实例方法 acts on a specific object, so you call it on the object reference: object.method(args). Example: in.nextInt(), word.length().

Bahasa Indonesia

Sebuah metode instance bertindak pada objek tertentu, sehingga Anda memanggilnya pada referensi objek: object.method(args). Contoh: in.nextInt(), word.length().

Vocabulary · ⁨Kosa kata⁩ Train · ⁨Latih⁩
English Bahasa Indonesia
instance/ˈɪnstəns/ instansi
object-oriented programming/ˈɒbdʒekt ˈɔːrɪəntɪd ˈprəʊɡræmɪŋ/ pemrograman berorientasi objek
1.15

String Manipulation · ⁨Manipulasi String⁩

Syllabus · ⁨Silabus⁩
English

Learning Objective 1.15.A: Develop code to create string objects and determine the result of creating and combining strings.

  • 1.15.A.1 A String object represents a sequence of characters and can be created by using a string literal or by calling the String class constructor.
  • 1.15.A.2 The String class is part of the java.lang package. Classes in the java.lang package are available by default.
  • 1.15.A.3 A String object is immutable, meaning once a String object is created, its attributes cannot be changed. Methods called on a String object do not change the content of the String object.
  • 1.15.A.4 Two String objects can be concatenated together or combined using the + or += operator, resulting in a new String object. A primitive value can be concatenated with a String object. This causes the implicit conversion of the primitive value to a String object.
  • 1.15.A.5 A String object can be concatenated with any object, which implicitly calls the object's toString method (a behavior that is guaranteed to exist by the inheritance relationship every class has with the Object class). An object's toString method returns a string value representing the object. Subclasses of Object often override the toString method with class-specific implementation. Method overriding occurs when a public method in a subclass has the same method signature as a public method in the superclass, but the behavior of the method is specific to the subclass.
    • Exclusion statement: Overriding the toString method of a class is outside the scope of the AP Computer Science A course and exam.

Learning Objective 1.15.B: Develop code to call methods on string objects and determine the result of calling these methods.

  • 1.15.B.1 A String object has index values from 0 to one less than the length of the string. Attempting to access indices outside this range will result in a StringIndexOutOfBoundsException.
  • 1.15.B.2 The following String methods—including what they do and when they are used—are part of the Java Quick Reference:
    • int length() returns the number of characters in a String object.
    • String substring(int from, int to) returns the substring beginning at index from and ending at index to - 1.
    • String substring(int from) returns substring(from, length()).
    • int indexOf(String str) returns the index of the first occurrence of str; returns -1 if not found.
    • boolean equals(Object other) returns true if this corresponds to the same sequence of characters as other; returns false otherwise.
    • int compareTo(String other) returns a value < 0 if this is less than other; returns zero if this is equal to other; returns a value > 0 if this is greater than other. Strings are ordered based upon the alphabet.
    • Exclusion statement: Using the equals method to compare one String object with an object of a type other than String is outside the scope of the AP Computer Science A course and exam.
  • 1.15.B.3 A string identical to the single element substring at position index can be created by calling substring(index, index + 1).
Bahasa Indonesia

Tujuan Pembelajaran 1.15.A: Mengembangkan kode untuk membuat objek string dan menentukan hasil dari pembuatan dan penggabungan string.

  • 1.15.A.1 Objek String merepresentasikan urutan karakter dan dapat dibuat dengan menggunakan literal string atau dengan memanggil konstruktor kelas String.
  • 1.15.A.2 Kelas String merupakan bagian dari paket java.lang. Kelas-kelas dalam paket java.lang tersedia secara default.
  • 1.15.A.3 Objek String bersifat tidak dapat diubah (immutable), artinya setelah objek String dibuat, atributnya tidak dapat diubah lagi. Metode yang dipanggil pada objek String tidak mengubah isi dari objek String tersebut.
  • 1.15.A.4 Dua objek String dapat digabungkan atau disatukan menggunakan operator + atau +=, menghasilkan objek String baru. Nilai primitif dapat digabungkan dengan objek String. Hal ini menyebabkan konversi implisit nilai primitif menjadi objek String.
  • 1.15.A.5 Objek String dapat digabungkan dengan objek apa pun, yang secara implisit memanggil metode toString dari objek tersebut (sifat yang dijamin ada karena hubungan pewarisan yang dimiliki setiap kelas terhadap kelas Object). Metode toString dari sebuah objek mengembalikan nilai string yang mewakili objek tersebut. Subkelas dari Object sering menimpa (override) metode toString dengan implementasi khusus kelas. Penimpangan metode terjadi ketika metode publik dalam subkelas memiliki tanda tangan metode yang sama dengan metode publik dalam superclass, tetapi perilaku metode tersebut spesifik untuk subkelas.
    • Pernyataan pengecualian: Menimpa metode toString dari sebuah kelas berada di luar cakupan kursus dan ujian AP Computer Science A.

Tujuan Pembelajaran 1.15.B: Mengembangkan kode untuk memanggil metode pada objek string dan menentukan hasil dari pemanggilan metode-metode tersebut.

  • 1.15.B.1 Sebuah objek String memiliki nilai indeks mulai dari 0 hingga satu kurang dari panjang string. Mencoba mengakses indeks di luar rentang ini akan menghasilkan StringIndexOutOfBoundsException.
  • 1.15.B.2 Metode-metode String berikut—termasuk fungsi dan penggunaannya—merupakan bagian dari Rujukan Cepat Java:
    • int length() mengembalikan jumlah karakter dalam sebuah objek String.
    • String substring(int from, int to) mengembalikan substrings yang dimulai dari indeks from dan berakhir pada indeks to - 1.
    • String substring(int from) mengembalikan substring(from, length()).
    • int indexOf(String str) mengembalikan indeks kemunculan pertama dari str; mengembalikan -1 jika tidak ditemukan.
    • boolean equals(Object other) mengembalikan true jika this sesuai dengan urutan karakter yang sama dengan other; mengembalikan false jika tidak.
    • int compareTo(String other) mengembalikan nilai < 0 if this is less than other; returns zero if this is equal to other; returns a value > 0 jika this lebih besar dari other. String diurutkan berdasarkan abjad.
    • Pernyataan pengecualian: Menggunakan metode equals untuk membandingkan satu objek String dengan objek bertipe lain selain String berada di luar cakupan kursus dan ujian AP Computer Science A.
  • 1.15.B.3 String yang identik dengan substring elemen tunggal pada posisi index dapat dibuat dengan memanggil substring(index, index + 1).

Source: College Board AP Course and Exam Description · ⁨Sumber: Deskripsi Kursus dan Ujian College Board AP⁩

English
Strings are immutable

String objects are immutable 不可变 – methods return a new string rather than changing the original. Key methods (all indices start at 0):

Exam skill: substring(a, b) includes index a but excludes b, and String comparison must use .equals, not == – two of the most-tested String pitfalls.

Worked example. Let String s = "COMPUTER"; (indices 0–7). Then s.length() is 8; s.substring(0, 4) is "COMP" (indices 0,1,2,3 – index 4 excluded); s.substring(4) is "UTER" (from index 4 to the end); s.indexOf("PU") is 3; and s.indexOf("X") is -1 (not found). Counting the excluded endpoint of substring is the single most common slip.

Asking for an index outside 0 to length()-1 (a bad substring or charAt argument, e.g. s.substring(0, 20) here) crashes with a StringIndexOutOfBoundsException – the String cousin of the array-index error.

Bahasa Indonesia
String bersifat tak berubah

Objek String tak berubah – metode mengembalikan string baru alih-alih mengubah yang asli. Metode utama (semua indeks dimulai dari 0):

s.length();            // number of characters
s.substring(2, 5);     // chars at index 2,3,4 (5 excluded)
s.indexOf("ab");       // first position, or -1
s.equals(other);       // content comparison (never use == for Strings)
s.compareTo(other);    // <0, 0, >0 by dictionary order

Keterampilan ujian: substring(a, b) mencakup indeks a tetapi mengecualikan b, dan perbandingan String harus menggunakan .equals, bukan == – dua jebakan String yang paling sering diujikan.

Contoh terpecahkan. Misalkan String s = "COMPUTER"; (indeks 0–7). Maka s.length() adalah 8; s.substring(0, 4) adalah "COMP" (indeks 0,1,2,3 – indeks 4 dikecualikan); s.substring(4) adalah "UTER" (dari indeks 4 hingga akhir); s.indexOf("PU") adalah 3; dan s.indexOf("X") adalah -1 (tidak ditemukan). Menghitung ujung yang dikecualikan dari substring adalah kesalahan tunggal yang paling umum.

Meminta indeks di luar 0 hingga length()-1 (argumen substring atau charAt yang buruk, mis. s.substring(0, 20) di sini) menyebabkan crash dengan StringIndexOutOfBoundsException – sepupu String dari error indeks array.

Indeks string dimulai dari 0
Indeks string dimulai dari 0
Explore · ⁨Jelajahi⁩

Explore string indices and slicing · ⁨Jelajahi indeks string dan pemotongan (slicing)⁩

Every character has an index, and the numbering starts at 0. Drag the start and end to see how substring(from, to) takes the characters from from up to — but not including — to. · ⁨Setiap karakter memiliki indeks, dan pengnomoran dimulai dari 0. Seret awal dan akhir untuk melihat bagaimana substring(from, to) mengambil karakter dari from hingga — tetapi tidak termasuk — to.⁩

1.15

Exam tips · ⁨Tips ujian⁩

English
  • Trace code by hand line by line, tracking each variable's value in a table — the exam rewards careful tracing over guessing.
  • Know Java's primitive types and that integer division truncates ($7/2$ gives $3$); use a cast or a double for real division.
  • Distinguish compile-time errors (syntax, types) from run-time errors – know the named ones: ArithmeticException (int ÷ 0), NullPointerException (method on a null reference), StringIndexOutOfBoundsException / ArrayIndexOutOfBoundsException – and logic errors (wrong output).
  • Follow operator precedence and initialise every variable before you use it.
  • On the free-response, write complete, compilable Java — return the right type and match the method header exactly.
Bahasa Indonesia
  • Jejakkan kode secara manual baris demi baris, melacak nilai setiap variabel dalam tabel – ujian menghargai pelacakan teliti dibandingkan menebak.
  • Kenali tipe primitif Java dan bahwa pembagian bilangan bulat memotong ($7/2$ memberikan $3$); gunakan penyimpanan tipe atau double untuk pembagian sebenarnya.
  • Bedakan kesalahan compile-time (sintaks, tipe) dari kesalahan run-time – pahami yang bernama: ArithmeticException (int ÷ 0), NullPointerException (metode pada referensi null), StringIndexOutOfBoundsException / ArrayIndexOutOfBoundsException – dan kesalahan logika (output salah).
  • Ikuti urutan prioritas operator dan inisialisasi setiap variabel sebelum Anda menggunakannya.
  • Pada soal uraian, tuliskan Java lengkap, dapat dikompilasi – kembalikan tipe yang benar dan sesuaikan dengan header metode secara presisi.

Interactive lessons on this topic · ⁨Pelajaran interaktif untuk topik ini⁩

Work through it step by step, with instant-check exercises. · ⁨Kerjakan langkah demi langkah, dengan latihan pengecekan instan.⁩

Past Papers · ⁨Soal-Soil Masa Lalu⁩

More topics in AP Computer Science A · ⁨AP Ilmu Komputer A⁩ · ⁨Topik lain dalam AP Computer Science A · ⁨AP Ilmu Komputer A⁩⁩

Log in or create account · ⁨Masuk atau buat akun⁩

IGCSE, A-Level & AP