Professional Documents
Culture Documents
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
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.
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.
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
d. Instance (Instance)
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; }
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:
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.
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:
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; } }
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”};
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.
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