Professional Documents
Culture Documents
(JS01) 10 - Hilmy Zaky Mustakim
(JS01) 10 - Hilmy Zaky Mustakim
Object
10
Hilmy Zaky Mustakim
SIB 2E
01 September, 2023
Daftar Isi
Daftar
Isi.................................................................................................................................................... 1
Konten....................................................................................................................................................
...2
Konten 2.......................................................................................................................................
2Subab......................................................................................................................................
2
1
Kasus 1 - Kelas “Ball”
Tugas 1
1. Implementasikan class Ball sesuai dengan class diagram yang telah dibuat. Gunakan
test case yang digunakan untuk menguji apakah implementasi yang Anda lakukan sesuai
dengan desain yang diberikan.
- Code Class Ball
public class Ball {
private float x;
private float y;
private int radius;
private float xDelta;
private float yDelta;
2
public int getRadius() {
return this.radius;
}
3
- Code MainTest
/*
* Author: Chua Hock-Chuan
*/
public class TestMain {
public static void main(String[] args) {
// Test constructor and toString()
Ball ball = new Ball(1.1f, 2.2f, 10, 3.3f, 4.4f);
System.out.println(ball); // toString()
4
if ((yNew + radius) > yMax || (yNew - radius) < yMin) {
ball.reflectVertical();
}
}
}
}
- Output
5
Ball ball = new Ball();
6
3. Modifikasi seluruh atribut class Ball sehingga memiliki access modifier berjenis private.
• Atribut pada class Ball sudah memiliki access modifier berjenis private
private float x;
private float y;
private int radius;
private float xDelta;
private float yDelta;
7
4. Modifikasi seluruh method class Ball sehingga dapat diakses oleh class yang lain.
a. Semua method sudah “public” yang berarti bisa diakses oleh class lain
private float x;
private float y;
private int radius;
private float xDelta;
private float yDelta;
8
public float getXDelta() {
return (int) Math.sqrt(xDelta * xDelta + yDelta * yDelta);
}
5. konstruktor pada class Ball sehingga memiliki parameter speed dan direction
menggantikan Δx dan Δy untuk menggambarkan arah bola.
private float x;
private int radius;
private float speed;
9
private float direction;
10
public void setYDelta(float direction){
this.direction = direction;
}
11
Kasus 2 - Kelas "Author" dan "Book"
Tugas 2
1. Implementasikan kelas Author dan Book sesuai dengan desain yang telah diberikan.
- Main
/*
* Author: Chua Hock-Chuan
*/
public class TestMain {
public static void main(String[] args) {
// Test Author class
Author a1 = new Author("Tan Ah Teck", "ahteck@nowhere.com");
System.out.println(a1);
a1.setEmail("ahteck@somewhere.com");
System.out.println(a1);
System.out.println("name is: " + a1.getName());
System.out.println("email is: " + a1.getEmail());
b1.setPrice(9.9);
b1.setQty(99);
System.out.println(b1);
System.out.println("isbn is: " + b1.getIsbn());
System.out.println("name is: " + b1.getName());
System.out.println("price is: " + b1.getPrice());
System.out.println("qty is: " + b1.getQty());
System.out.println("author is: " + b1.getAuthor()); // Author's
toString()
System.out.println("author's name: " + b1.getAuthorName());
System.out.println("author's name: " + b1.getAuthor().getName());
System.out.println("author's email: " + b1.getAuthor().getEmail());
}
}
12
- Class Author
13
- Class Book
3. Tambahkan konstruktor pada kelas Book sehingga instance dapat dibuat tanpa harus
memasukkan parameter.
15
16