8.3. Tugas materi 4,Vending machine dan exercise halaman 56+61
PBO D - MATERI 4
Object : Bagian-bagian yang membentuk sebuah model adalah object yang muncul di domain problem. Contohnya dalam TicketMachine adalah new TicketMachine().
Classes : Adalah sebuah model yang mengelompokkan beberapa object sedemikian sehingga memiliki sebuah fungsi tertentu. Contohnya adalah class TicketMachine.
Fields : Adalah data-data yang didefinisikan oleh class. Class masing-masing memiliki data yang tidak bisa sembarangan diakses oleh object di luar class tersebut. Contohnya private int price.
Method : Adalah sebuah fungsi yang berguna untuk menentukan perilaku atau behaviour dari sebuah objek. Contohnya dalam TicketMachine;
Public int getPrice()
{
Return price;
}
Constructor : Adalah method yang membentuk sebuah sekumpulan data yang menjadi value awal dari data-data tersebut. Contohnya dalam object TicketMachine, public Ticket() adalah constructornya.Karena bila tidak ada public ticket maka program tidak dapat dijalankan
Parameters : Adalah sebuah acuan awal data sebuah object atau classes, sebelum diubah dengan method-method yang lain. Umumnya ditaruh di awal class. Contohnya balance =50
Source code.
public class TicketMachine
{
private int price;
private int balance;
private int total;
private int refund;
private int tickets;
public TicketMachine(int Cost)
{
price = Cost;
balance = 0;
total = 0;
refund = 0;
tickets = 0;
}
public int getPrice()
{
return price;
}
public void insertMoney(int amount)
{
if(amount > 0)
{
balance += amount;
}
else
{
System.out.println("Please inser a sensible amount of money");
}
}
public void printTicket()
{
if(balance > 0)
{
if(balance >= price)
{
tickets = purchaseTickets();
int i = 1;
while (i <= tickets)
{
System.out.println("--------------------");
System.out.println("--Tiket Kereta Api--");
System.out.println("--Ticket " + i +"-----");
System.out.println("- " + price + " rupiah");
System.out.println("-------------------");
i++;
}
total += balance;
refund = refundBalance();
if(refund==0)
{
System.out.println("No Change Given");
}
else
{
System.out.println("Amount to refund: " +refund+ "rupiah");
}
balance = 0;
}
else
{
System.out.println("Please inser " +(price - balance) +"more rupiah");
}
}
else
{
System.out.println("Please insert a positive amount of money");
}
}
public int refundBalance()
{
int amountToRefund;
amountToRefund = balance - price * tickets;
balance = 0;
return amountToRefund;
}
public int purchaseTickets()
{
int numberOfTickets;
numberOfTickets = balance / price;
return numberOfTickets;
}
}
Hasil Source code
-------------------
--Tiket Kereta Api--
--Ticket 1-----
- 50 rupiah
-------------------
--------------------
--Tiket Kereta Api--
--Ticket 2-----
- 50 rupiah
-------------------
Amount to refund: 20rupiah
Exercise 2.83
Source Code:
- public class Book
- {
- // The fields.
- private String author;
- private String title;
- /**
- * Set the author and title fields when this object
- * is constructed.
- */
- public Book(String bookAuthor, String bookTitle)
- {
- author = bookAuthor;
- title = bookTitle;
- }
- public String getAuthor()
- {
- return author;
- }
- public String getTitle()
- {
- return title;
- }
- }
Exercise 2.84.
- public class Book
- {
- // The fields.
- private String author;
- private String title;
- /**
- * Set the author and title fields when this object
- * is constructed.
- */
- public Book(String bookAuthor, String bookTitle)
- {
- author = bookAuthor;
- title = bookTitle;
- }
- public String getAuthor()
- {
- return author;
- }
- public String getTitle()
- {
- return title;
- }
- public void printAuthor()
- {
- System.out.println(author);
- }
- public void printTitle()
- {
- System.out.println(title);
- }
- }
Exercise 2.85
- public class Book
- {
- // The fields.
- private String author;
- private String title;
- private int pages;
- /**
- * Set the author and title fields when this object
- * is constructed.
- */
- public Book(String bookAuthor, String bookTitle, int bookPages)
- {
- author = bookAuthor;
- title = bookTitle;
- pages = bookPages;
- }
- public String getAuthor()
- {
- return author;
- }
- public String getTitle()
- {
- return title;
- }
- public void printAuthor()
- {
- System.out.println(author);
- }
- public void printTitle()
- {
- System.out.println(title);
- }
- public int getPages()
- {
- return pages;
- }
- }
Exercise 2.86
- public class Book
- {
- // The fields.
- private String author;
- private String title;
- private int pages;
- /**
- * Set the author and title fields when this object
- * is constructed.
- */
- public Book(String bookAuthor, String bookTitle, int bookPages)
- {
- author = bookAuthor;
- title = bookTitle;
- pages = bookPages;
- }
- public String getAuthor()
- {
- return author;
- }
- public String getTitle()
- {
- return title;
- }
- public void printAuthor()
- {
- System.out.println(author);
- }
- public void printTitle()
- {
- System.out.println(title);
- }
- public int getPages()
- {
- return pages;
- }
- public void printDetails()
- {
- System.out.println("Title :" + title);
- System.out.println("Author :" + author);
- System.out.println("Page(s) :" + pages);
- }
- }
Exercise 2.87
- import java.util.Scanner;
- public class Book
- {
- Scanner scan = new Scanner(System.in);
- // The fields.
- private String author;
- private String title;
- private int pages;
- private String refNumber;
- /**
- * Set the author and title fields when this object
- * is constructed.
- */
- public Book(String bookAuthor, String bookTitle, int bookPages)
- {
- author = bookAuthor;
- title = bookTitle;
- pages = bookPages;
- refNumber = "";
- }
- public String getAuthor()
- {
- return author;
- }
- public String getTitle()
- {
- return title;
- }
- public String getRefNumber()
- {
- return refNumber;
- }
- public int getPages()
- {
- return pages;
- }
- public void printAuthor()
- {
- System.out.println(author);
- }
- public void printTitle()
- {
- System.out.println(title);
- }
- public void setRefNumber(String ref)
- {
- refNumber = ref;
- }
- public void printDetails()
- {
- System.out.println("Title :" + title);
- System.out.println("Author :" + author);
- System.out.println("Page(s) :" + pages);
- }
- }
Exercise 2.88
- import java.util.Scanner;
- public class Book
- {
- Scanner scan = new Scanner(System.in);
- // The fields.
- private String author;
- private String title;
- private int pages;
- private String refNumber;
- /**
- * Set the author and title fields when this object
- * is constructed.
- */
- public Book(String bookAuthor, String bookTitle, int bookPages)
- {
- author = bookAuthor;
- title = bookTitle;
- pages = bookPages;
- refNumber = "";
- }
- public String getAuthor()
- {
- return author;
- }
- public String getTitle()
- {
- return title;
- }
- public String getRefNumber()
- {
- return refNumber;
- }
- public int getPages()
- {
- return pages;
- }
- public void printAuthor()
- {
- System.out.println(author);
- }
- public void printTitle()
- {
- System.out.println(title);
- }
- public void setRefNumber(String ref)
- {
- refNumber = ref;
- }
- public void printDetails()
- {
- System.out.println("Title :" + title);
- System.out.println("Author :" + author);
- System.out.println("Page(s) :" + pages);
- if (refNumber.length() != 0)
- {
- System.out.println("Ref. Num :" + refNumber);
- }
- else System.out.println("Ref. Num :ZZZ");
- }
- }
Exercise 2.89
- import java.util.Scanner;
- public class Book
- {
- Scanner scan = new Scanner(System.in);
- // The fields.
- private String author;
- private String title;
- private int pages;
- private String refNumber;
- /**
- * Set the author and title fields when this object
- * is constructed.
- */
- public Book(String bookAuthor, String bookTitle, int bookPages)
- {
- author = bookAuthor;
- title = bookTitle;
- pages = bookPages;
- refNumber = "";
- }
- public String getAuthor()
- {
- return author;
- }
- public String getTitle()
- {
- return title;
- }
- public String getRefNumber()
- {
- return refNumber;
- }
- public int getPages()
- {
- return pages;
- }
- public void printAuthor()
- {
- System.out.println(author);
- }
- public void printTitle()
- {
- System.out.println(title);
- }
- public void setRefNumber(String ref)
- {
- if (ref.length() >= 3)
- {
- refNumber = ref;
- System.out.println("Set success!");
- }
- else System.out.println("Set failed.");
- }
- public void printDetails()
- {
- System.out.println("Title :" + title);
- System.out.println("Author :" + author);
- System.out.println("Page(s) :" + pages);
- if (refNumber.length() != 0)
- {
- System.out.println("Ref. Num :" + refNumber);
- }
- else System.out.println("Ref. Num :ZZZ");
- }
- }
Exercise 2.90
- import java.util.Scanner;
- public class Book
- {
- Scanner scan = new Scanner(System.in);
- // The fields.
- private String author;
- private String title;
- private int pages;
- private String refNumber;
- private int borrowed;
- private boolean courseText;
- /**
- * Set the author and title fields when this object
- * is constructed.
- */
- public Book(String bookAuthor, String bookTitle, int bookPages, boolean bookCourseText)
- {
- author = bookAuthor;
- title = bookTitle;
- pages = bookPages;
- refNumber = "";
- borrowed = 0;
- courseText = bookCourseText;
- }
- public String getAuthor()
- {
- return author;
- }
- public String getTitle()
- {
- return title;
- }
- public String getRefNumber()
- {
- return refNumber;
- }
- public int getPages()
- {
- return pages;
- }
- public int getBorrowed()
- {
- return borrowed;
- }
- public boolean isCourseText()
- {
- return courseText;
- }
- public void printAuthor()
- {
- System.out.println(author);
- }
- public void printTitle()
- {
- System.out.println(title);
- }
- public void setRefNumber(String ref)
- {
- if (ref.length() >= 3)
- {
- refNumber = ref;
- System.out.println("Set success!");
- }
- else System.out.println("Set failed.");
- }
- public void borrow()
- {
- borrowed = borrowed + 1;
- }
- public void printDetails()
- {
- System.out.println("Title :" + title);
- System.out.println("Author :" + author);
- System.out.println("Page(s) :" + pages);
- if (refNumber.length() != 0)
- {
- System.out.println("Ref. Num :" + refNumber);
- }
- else System.out.println("Ref. Num :ZZZ");
- System.out.println("Borrowed :" + borrowed);
- }
- }
Exercise 2.91
Exercise 2.92
Exercise 2.93
- import java.util.Scanner;
- public class Book
- {
- Scanner scan = new Scanner(System.in);
- // The fields.
- private String author;
- private String title;
- private int pages;
- private String refNumber;
- private int borrowed;
- private boolean courseText;
- /**
- * Set the author and title fields when this object
- * is constructed.
- */
- public Book(String bookAuthor, String bookTitle, int bookPages, boolean bookCourseText)
- {
- author = bookAuthor;
- title = bookTitle;
- pages = bookPages;
- refNumber = "";
- borrowed = 0;
- courseText = bookCourseText;
- }
- public String getAuthor()
- {
- return author;
- }
- public String getTitle()
- {
- return title;
- }
- public String getRefNumber()
- {
- return refNumber;
- }
- public int getPages()
- {
- return pages;
- }
- public int getBorrowed()
- {
- return borrowed;
- }
- public boolean isCourseText()
- {
- return courseText;
- }
- public void printAuthor()
- {
- System.out.println(author);
- }
- public void printTitle()
- {
- System.out.println(title);
- }
- public void setRefNumber(String ref)
- {
- if (ref.length() >= 3)
- {
- refNumber = ref;
- System.out.println("Set success!");
- }
- else System.out.println("Set failed.");
- }
- public void borrow()
- {
- borrowed = borrowed + 1;
- }
- public void printDetails()
- {
- System.out.println("Title :" + title);
- System.out.println("Author :" + author);
- System.out.println("Page(s) :" + pages);
- if (refNumber.length() != 0)
- {
- System.out.println("Ref. Num :" + refNumber);
- }
- else System.out.println("Ref. Num :ZZZ");
- System.out.println("Borrowed :" + borrowed);
- }
- }
Exercise 2.92
- public class Heater
- {
- // instance variables - replace the example below with your own
- private double temperature;
- /**
- * Constructor for objects of class Heater
- */
- public Heater()
- {
- // initialise instance variables
- temperature = 15.0;
- }
- public void warmer()
- {
- temperature = temperature + 5.0;
- }
- public void cooler()
- {
- temperature = temperature - 5.0;
- }
- public double getTemperature()
- {
- return temperature;
- }
- }
Exercise 2.93
- public class Heater
- {
- // instance variables - replace the example below with your own
- private double temperature;
- private double min;
- private double max;
- private double increment;
- /**
- * Constructor for objects of class Heater
- */
- public Heater(double temperatureMin, double temperatureMax)
- {
- temperature = 15.0;
- min = temperatureMin;
- max = temperatureMax;
- increment = 5.0;
- }
- public void warmer()
- {
- if (temperature + increment > max)
- {
- System.out.println("Set failed.");
- }
- else
- {
- temperature = temperature + 5.0;
- System.out.println("Set success!");
- }
- }
- public void cooler()
- {
- if (temperature + increment < min)
- {
- System.out.println("Set failed.");
- }
- else
- {
- temperature = temperature - 5.0;
- System.out.println("Set success!");
- }
- }
- public void setIncrement(double inc)
- {
- if (inc < 0)
- System.out.println("Set failed.");
- else increment = inc;
- }
- public double getTemperature()
- {
- return temperature;
- }
- }
Komentar
Posting Komentar