Download as pdf or txt
Download as pdf or txt
You are on page 1of 17

Tugas Job Sheet 1

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;

public Ball(float x, float y, int radius, float xDelta, float


yDelta) {
this.x = x;
this.y = y;
this.radius = radius;
this.xDelta = xDelta;
this.yDelta = yDelta;
}

public float getX() {


return this.x;
}

public void setX(float x) {


this.x = x;
}

public float getY() {


return this.y;
}

public void setY(float y) {


this.y = y;
}

2
public int getRadius() {
return this.radius;
}

public void setRadius(int radius) {


this.radius = radius;
}

public float getXDelta() {


return (int) Math.sqrt(xDelta * xDelta + yDelta * yDelta);
}

public float getYDelta() {


return (int) Math.toDegrees(Math.atan2(-yDelta, xDelta));
}

public void setYDelta(float yDelta) {


this.yDelta = xDelta;
}

public void setXDelta(float xDelta) {


this.xDelta = xDelta;
}

public void move() {


x += xDelta;
y += yDelta;
}

public void reflectHorizontal() {


xDelta = -xDelta;
}

public void reflectVertical() {


yDelta = -yDelta;
}

public String toString() {


return "Ball at (" + (int) x + ", " + (int) y + ") of
velocity (" + getXDelta() + ", " + getYDelta() + ")";
}
}

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

// Test Setters and Getters


ball.setX(80.0f);
ball.setY(35.0f);
ball.setRadius(5);
ball.setXDelta(4.0f);
ball.setYDelta(6.0f);
System.out.println(ball); // toString()
System.out.println("x is: " + ball.getX());
System.out.println("y is: " + ball.getY());
System.out.println("radius is: " + ball.getRadius());
System.out.println("xDelta is: " + ball.getXDelta());
System.out.println("yDelta is: " + ball.getYDelta());

// Bounce the ball within the boundary


float xMin = 0.0f;
float xMax = 100.0f;
float yMin = 0.0f;
float yMax = 50.0f;

for (int i = 0; i < 15; i++) {


ball.move();
System.out.println(ball);
float xNew = ball.getX();
float yNew = ball.getY();
int radius = ball.getRadius();
// Check boundary value to bounce back

if ((xNew + radius) > xMax || (xNew - radius) < xMin) {


ball.reflectHorizontal();
}

4
if ((yNew + radius) > yMax || (yNew - radius) < yMin) {
ball.reflectVertical();
}
}
}

}
- Output

2. Modifikasi class Ball agar memiliki konstruktor tanpa parameter.


a. Modifikasi pada Class

b. Modifikasi pada Main

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

public class Ball {

private float x;
private float y;
private int radius;
private float xDelta;
private float yDelta;

public Ball(float x, float y, int radius, float xDelta, float yDelta) {


this.x = x;
this.y = y;
this.radius = radius;
this.xDelta = xDelta;
this.yDelta = yDelta;
}
public Ball() {
this(1.1f, 2.2f, 10, 3.3f, 4.4f);
}

public float getX() {


return this.x;
}

public void setX(float x) {


this.x = x;
}

public float getY() {


return this.y;
}

public void setY(float y) {


this.y = y;
}

public int getRadius() {


return this.radius;
}

public void setRadius(int radius) {


this.radius = radius;
}

8
public float getXDelta() {
return (int) Math.sqrt(xDelta * xDelta + yDelta * yDelta);
}

public float getYDelta() {


return (int) Math.toDegrees(Math.atan2(-yDelta, xDelta));
}

public void setYDelta(float yDelta) {


this.yDelta = xDelta;
}

public void setXDelta(float xDelta) {


this.xDelta = xDelta;
}

public void move() {


x += xDelta;
y += yDelta;
}

public void reflectHorizontal() {


xDelta = -xDelta;
}

public void reflectVertical() {


yDelta = -yDelta;
}

public String toString() {


return "Ball at (" + (int) x + ", " + (int) y + ") of velocity (" +
getXDelta() + ", " + getYDelta() + ")";
}
}

5. konstruktor pada class Ball sehingga memiliki parameter speed dan direction
menggantikan Δx dan Δy untuk menggambarkan arah bola.

public class Ball {

private float x;
private int radius;
private float speed;

9
private float direction;

public Ball(float x, float y, int radius, float xDelta, float yDelta) {


this.x = x;
this.y = y;
this.radius = radius;
this.speed = speed;
this.direction = direction;
}
public Ball() {
this(1.1f, 2.2f, 10, 3.3f, 4.4f);
}

public float getX() {


return this.x;
}

public void setX(float x) {


this.x = x;
}

public float getY() {


return this.y;
}

public void setY(float y) {


this.y = y;
}

public int getRadius() {


return this.radius;
}

public void setRadius(int radius) {


this.radius = radius;
}

public float getXDelta() {


return (int) Math.sqrt(speed * speed + direction * direction);
}

public float getYDelta() {


return (int) Math.toDegrees(Math.atan2(-direction, speed));
}

10
public void setYDelta(float direction){
this.direction = direction;
}

public void setXDelta(float speed){


this.speed = speed;
}

public void move() {


x += speed;
y += direction;
}

public void reflectHorizontal() {


speed = -speed;
}

public void reflectVertical() {


direction = -direction;
public String toString() {
return "Ball at (" + (int) x + ", " + (int) y + ") of velocity (" +
getXDelta() + ", " + getYDelta() + ")";
}
}

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());

// Test Book class


Book b1 = new Book("12345", "Java for dummies", a1, 8.8, 88);
System.out.println(b1);

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

public class Author {


private String name;
private String email;

public Author (String name, String email) {


this.name = name;
this.email = email;
}

public String getName(){


return this.name;
}

public String getEmail(){


return this.email;
}

public void setEmail(String email){


this.email=email;
}
public String toString(){
return this.name + "" + this.email;
}

13
- Class Book

public class Book{


private String isbn;
private String name;
private double price;
private Author author;
private int qty = 0;

public Book (String isbn, String name, Author author, double


price) {
this.isbn = isbn;
this.name = name;
this.author = author;
this.price = price;
}

public Book (String isbn, String name, Author author, double


price, int qty) {
this.isbn = isbn;
this.name = name;
this.author = author;
this.price = price;
this.qty = qty;
}

public String getIsbn() {


return this.isbn;
}

public String getName() {


return this.name;
}

public String getAuthor() {


return this.author;
}

public String getPrice() {


return this.price;
}

public String getQty() {


return this.qty;
}
14
public void setQty(int qty) {
this.qty = qty;
public void setQty(int qty) {
this.qty = qty;
}

public String getAuthorName() {


return this.author.getName();
}

public String getAuthorEmail() {


return this.author.getEmail();
}

public String toString() {


return "Book[isbn=" + this.isbn +"], name = "+ this.name +
", Author[name= "+ this.getAuthorName() +"email = " +
this.getAuthorEmail() +"],price" + this.price + ", qty=" +
this.qty;
}
}
2. Verifikasi hasil kelas yang Anda buat dengan menggunakan test case yang telah disediakan.
- Output

3. Tambahkan konstruktor pada kelas Book sehingga instance dapat dibuat tanpa harus
memasukkan parameter.

15
16

You might also like