You are on page 1of 19

PENGENALAN PEMROGRAMAN

NAMA : Ainaya Dewi Fatmawati


NIM : C2C023516

PRODI INFORMATIKA
FAKULTAS TEKNIK DAN ILMU KOMPUTER
UNIVERSITAS MUHAMMADIYAH SEMARANG
BAB 3.7
Sound and Keyboard Control Practice Activities
A. Pendahuluan
B. Tujuan
C. Pembahasan
1. Open the JF_practices scenario that you created in a previous practice.

2. Add code to play the sound Bang.wav when the plane and the rocket collide.

3. Record your own sound effect that will be played when you catch a barrel
Vocabullary

A method that will play a sound file GreenfootSound


Bab 3.8
World Animation and Game End Practice Activities

1. Open the JF_practices scenario that you created in a previous practice.

2. Add a constructor to the Rocket Class


3. In the Rocket Constructor use the setRotation() method to set the angle of the
rocket randomly between 45 and134 degrees.
4. In the act() method of the Rocket move the rocket at a speed of 2.

5. In the Rocket class create a method called removeAtEdge(). This method will
detect when the rocket is at the edge of the world. If it is it will add another rocket
to the world. It will then remove itself from the world. Add this method to the
act() method.

6. Add two class field variables to Barrel called timer and maxtime. Both of these
will be integers.
7. Add a constructor to barrel that sets timer to 0 and maxtime to 250.

8. Add a method to Barrel called resetBarrel(). Using the pseudo code below, add
code to increment the timer and when the timer is greater than the maxtime value
the Barrel is moved. Once complete add a call to this in the act() method.
Increment timer If timer > maxtime then Reset timer Randomly place barrel on
the screen.

9. Create a method within Rocket called animate(). Using the three images -
rocket1.png, rocket2.png and rocket3.png, code the method animate() to switch
between these 3 images. Add a call to animate() within the act() method.
10. Create a method within Plane called animate(). Using the two images -
airplane1.png and airplane2.png, switch between these two images. Add a call to
animate() within the act() method.

11. In Plane, modify the method handleMovement(). If the up arrow key is pressed
the speed will change to 3 else the speed will stay at 2.
12. Save the scenario

Vocabullary
These statements describe the tasks or operations for the instances to perform in a mix of Java
language and plain English words. This helps us better understand what behaviors we want
the instances to perform before we write the real code: Variable
Declared in a class, this is used to store information for later use, or to pass information. It
can store objects or values: Constructor
A special method that is executed automatically whenever a new instance of the class is
created: Constructor Method
Bab 3.9
Abstraction Practice Activities
1. In the Barrel class add a parameter to the constructor called maxtime, which is an
integer. Update all calls to new Barrel() so that they say new Barrel(250)

2. Modify the Barrel constructor so that the maxtime class variable is set to the maxtime
parameter (this.maxtime = maxtime;)

3. Modify the line above so that we subtract a random number between 0-49 from the
maxtime.

4. Add private class variables called score and lives to the BarrelWorld class. Set lives to
3 and score to 0 as the last two lines in the constructor.
5. Create a method called updateScore() in BarrelWorld that will add one to the score
when called.

6. Use the showText() method to display the score at the bottom left (60, 585) in the
updateScore() method.

7. Create a method called removeLife() in BarrelWorld that will remove a life.

8. Use the showText() method to display the number of lives at the bottom left (60, 570)
in the removeLife() method.

9. In the removeLife() method do a test so that when the number of lives is zero then the
game stops.
10. In the Plane class call the updateScore() and removeLife() in the relevant methods.

11. Save the scenario

Vocabullary
A technique used to command newly-created instances to perform different actions:
Polymorphism
A technique used to tell java that a class is to be considered as another class: Inheritance
Bab 3.10
Loops, Variables, and Arrays Practice Activities
1. True or false: A loop is a statement that can execute a section of code once. It is an
efficient way to pass information to a single instance
False. Sebuah loop adalah konstruksi pemrograman yang memungkinkan bagian kode
dieksekusi berulang-ulang berdasarkan suatu kondisi. Ini tidak dimaksudkan untuk
mengeksekusi kode hanya sekali, dan biasanya digunakan untuk melakukan tugas-
tugas berulang atau memproses beberapa instansi data. Ini tidak dirancang untuk
mengirim informasi ke satu instance saja, tetapi lebih untuk otomatisasi tugas yang
perlu diulang beberapa kali.
2. Which of the following is not a required component of a while loop? a. Java keyword
while b. Condition c. One or more statements d. Instance

Jawaban yang benar adalah:

d. Instance (Instance)

Komponen yang diperlukan dari loop `while` adalah:


a. Kata kunci `while` dalam bahasa pemrograman Java atau bahasa pemrograman
lainnya.
b. Kondisi yang akan dievaluasi untuk menentukan apakah loop harus dijalankan.
c. Satu atau lebih pernyataan atau instruksi yang akan dijalankan berulang kali selama
kondisi terpenuhi.

Instance (instansi) tidak termasuk dalam komponen yang diperlukan untuk loop
`while`. Loop `while` tidak memerlukan instance khusus untuk dijalankan; itu
tergantung pada kondisi yang diberikan.

3. In the following example while loop, describe what the loop body executes: int i = 0;
while (i < 10,000) { addObject (new Bee (), 110, 130); i = i + 1; }

Dalam contoh loop `while` berikut ini:

Loop ini akan menjalankan tubuh loop sebagai berikut:

1. Menginisialisasi variabel `i` dengan nilai 0.


2. Memeriksa apakah nilai `i` kurang dari 10.000. Jika benar, langkah berikutnya akan
dijalankan.
3. Menambahkan objek baru ke dalam game (mungkin dalam konteks permainan atau
aplikasi tertentu) dengan menggunakan `addObject(new Bee(), 110, 130)`.
4. Menambahkan 1 ke nilai `i` dengan pernyataan `i = i + 1`.
5. Kembali ke langkah 2 dan memeriksa apakah `i` masih kurang dari 10.000. Jika ya,
langkah-langkah di atas diulang.

Loop ini akan terus berjalan sampai `i` mencapai atau melebihi 10.000. Itu berarti
objek "Bee" akan ditambahkan ke permainan 10.000 kali dengan posisi yang sama
(110, 130).

4. In the following example loop, describe why it is an infinite loop: int i = 0; while (i <
10,000) { addObject (new Bee (), 180, 120); }

Loop dalam contoh ini adalah sebuah *infinite loop* (loop tak terbatas) karena
kondisi `i < 10,000` selalu benar. Dalam loop ini:

1. Variabel `i` diinisialisasi dengan nilai 0.


2. Kemudian, loop `while` memeriksa apakah nilai `i` kurang dari 10,000. Pada
awalnya, ini benar (0 < 10,000).
3. Kemudian, objek "Bee" ditambahkan ke dalam permainan dengan posisi (180,
120).
4. Namun, setelah langkah ini, nilai `i` tidak diperbarui. Ini berarti bahwa kondisi `i <
10,000` akan tetap benar selama loop dijalankan, dan objek "Bee" akan terus
ditambahkan ke permainan tanpa henti.

Tidak ada instruksi dalam loop yang mengubah nilai `i`, sehingga loop ini akan
berjalan terus-menerus dan menjadi *infinite loop*, yang akan terus menambahkan
objek "Bee" tanpa akhir. Untuk menghindari *infinite loop*, Anda perlu memastikan
ada pernyataan yang memperbarui nilai `i` sehingga kondisi dapat akhirnya menjadi
salah.

5. How do you resolve an infinite loop?

Untuk mengatasi infinite loop (loop yang tidak berhenti), Anda perlu mengambil
tindakan tertentu agar loop dapat dihentikan atau pastikan bahwa loop akhirnya
akan berhenti. Berikut adalah beberapa cara umum untuk mengatasi infinite loop:

a. Tambahkan Kondisi Berhenti: Masukkan kondisi di dalam loop yang, ketika


terpenuhi, akan menyebabkan loop berhenti. Kondisi ini bisa berdasarkan
penghitungan, nilai tertentu, atau peristiwa eksternal. Pastikan loop memeriksa
kondisi ini dan keluar ketika kondisinya benar.
b. Perbaiki Pernyataan Kondisi: Tinjau pernyataan kondisi loop. Pastikan bahwa
kondisi sudah benar dan pada akhirnya akan menjadi salah. Perbaiki kesalahan
logika yang dapat menyebabkan infinite loop.
c. Perbarui Variabel Pengendali Loop: Pastikan variabel pengendali loop (seperti
penghitung atau penanda) diperbarui di dalam loop. Jika variabel-variabel ini tidak
diperbarui, loop akan terus berjalan tanpa henti.
d. Pernyataan Keluar: Masukkan pernyataan keluar dalam loop yang secara eksplisit
menghentikan loop berdasarkan kondisi tertentu. Misalnya, Anda dapat
menggunakan pernyataan break dalam bahasa seperti Java, C++, atau Python
untuk keluar dari loop ketika kondisi tertentu terpenuhi.
e. Mekanisme Batas Waktu: Terapkan mekanisme batas waktu yang membuat loop
berhenti setelah jangka waktu tertentu. Pendekatan ini bermanfaat jika Anda
berurusan dengan operasi yang berkaitan dengan waktu.
f. Gunakan Alat Pemecah Masalah: Jika Anda kesulitan mengidentifikasi
masalahnya, gunakan alat pemecah masalah dan pernyataan cetak (print
statement) untuk melacak eksekusi loop dan mengidentifikasi di mana loop
berhenti.
g. Tinjau Logika Loop: Ulangi logika loop, termasuk sumber input atau data, untuk
memastikan bahwa tidak ada faktor eksternal yang menyebabkan loop berjalan
terus tanpa henti.
h. Uji Secara Bertahap: Jika Anda ragu tentang perubahan yang perlu dilakukan, uji
loop secara bertahap dengan perubahan kecil untuk melihat bagaimana loop
berperilaku.

Ingatlah bahwa mengatasi infinite loop bergantung pada konteks dan kode
program Anda. Teliti kode, temukan penyebab utamanya, dan terapkan solusi
yang sesuai untuk menghentikan loop dengan aman.

6. True or false: Logic operators are used to connect one or more boolean expressions.

True. Operator logika digunakan untuk menghubungkan satu atau lebih ekspresi
Boolean. Operator-operator ini meliputi "DAN" (&&), "ATAU" (||), dan "TIDAK"
(!), dan mereka memungkinkan Anda untuk membuat kondisi gabungan dengan
menggabungkan beberapa ekspresi Boolean dalam operasi logika.

7. In the following example code, identify the logic operators by circling them, and
define how they are used: public void act() { if (!isDown && Greenfoot.isKeyDown
(“x”)) { setImage (“dog.png”); isDown = true; } if (isDown && !
Greenfoot.isKeyDown(“y”)){ setImage (“dog2.png”); isDown = false; } }

Dalam kode yang diberikan, operator logika digunakan sebagai berikut:

1. `!` (operator NOT):


- `if (!isDown && Greenfoot.isKeyDown("x"))`: Operator `!` digunakan untuk
membalikkan nilai boolean dari `isDown`. Ini memeriksa apakah `isDown` adalah
`false` dan apakah tombol "x" ditekan. Jika kedua kondisi terpenuhi, maka blok kode
di dalam pernyataan `if` pertama akan dieksekusi.

2. `&&` (operator AND):


- `if (!isDown && Greenfoot.isKeyDown("x"))`: Operator `&&` digunakan untuk
menggabungkan dua kondisi. Dalam hal ini, ini memeriksa apakah baik `isDown`
adalah `false` dan tombol "x" ditekan secara bersamaan untuk mengeksekusi blok
kode di dalam pernyataan `if` pertama.

3. `!` (operator NOT):


- `if (isDown && !Greenfoot.isKeyDown("y"))`: Operator `!` digunakan kembali
untuk membalikkan nilai boolean dari `Greenfoot.isKeyDown("y")`. Ini memeriksa
apakah `isDown` adalah `true` dan tombol "y" tidak ditekan. Jika kedua kondisi
terpenuhi, maka blok kode di dalam pernyataan `if` kedua akan dieksekusi.

Operator logika `!` dan `&&` digunakan untuk mengendalikan alur program
berdasarkan keadaan variabel boolean dan penekanan tombol dalam kode yang
diberikan.

8. Write the statement that accesses the “a” key in the following array: private String[]
keyboardNames = {“a”, “b”, “c”, “d”, “e”, “f”, “g”, “h”};

Untuk mengakses kunci "a" dalam array berikut:

Gunakan indeks elemen array. Dalam hal ini, indeks elemen "a" adalah 0 karena array
dimulai dari indeks 0. Jadi, pernyataan untuk mengakses kunci "a" adalah:

Dengan pernyataan ini, Anda dapat mengakses nilai "a" dari array keyboardNames.
9. What is the local variable below initialized to? int i = 5;

Variabel lokal `i` diinisialisasi dengan nilai 5. Jadi, `i` akan memiliki nilai awal 5
setelah inisialisasi, dan Anda dapat menggunakannya dalam operasi dan perhitungan
selanjutnya dalam lingkup di mana itu dideklarasikan.

Note: The rest of the questions refer to the Barrel project.

10. Create a field variable in Plane called isGoingFast that will store a Boolean value.
11. Modify the handleMovement() method so that the isGoingFast variable is set to true
when the up arrow is clicked and set to false if it isn’t.

12. Add code to the handleMovement() method so that if the up arrow is pressed the
image changes to airplaneFaster.png.
13. Modify the animate() method so that it will not animate if isGoingFast is true;

14. Modify the BarrelWorld constructor so that you will use a loop to add 3 Rockets to
the world.

15. Modify the BarrelWorld constructor so that you will use a loop to add 3 Barrels to the
world.
Vocabullary
A type of item stored in an array, such as a string or integer, accessed using an index:
Element
A statement that can execute a section of code multiple times: Loop
Symbols that can be used to combine multiple boolean expressions into one boolean
expression: Logical Operators
A position number in the array object that specifies which array element to access: Index
An object that holds multiple variablesAn index can be used to access the variables: Array
A variable declared inside the body of the method to temporarily store values, such as
references to objects or integers: Local Variable
A loop that causes the code to keep executing. The code does not stop because the end to the
code isn't established: Infinite Loop

You might also like