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:
  1. public class Book
  2. {
  3.     // The fields.
  4.     private String author;
  5.     private String title;
  6.     /**
  7.     * Set the author and title fields when this object
  8.     * is constructed.
  9.     */
  10.     public Book(String bookAuthor, String bookTitle)
  11.         {
  12.         author = bookAuthor;
  13.         title = bookTitle;
  14.         }
  15.     public String getAuthor()
  16.     {
  17.         return author;
  18.     }
  19.     public String getTitle()
  20.     {
  21.         return title;
  22.     }
  23. }

Exercise 2.84.
  1. public class Book
  2. {
  3.     // The fields.
  4.     private String author;
  5.     private String title;
  6.     /**
  7.     * Set the author and title fields when this object
  8.     * is constructed.
  9.     */
  10.     public Book(String bookAuthor, String bookTitle)
  11.         {
  12.         author = bookAuthor;
  13.         title = bookTitle;
  14.         }
  15.     public String getAuthor()
  16.     {
  17.         return author;
  18.     }
  19.    
  20.     public String getTitle()
  21.     {
  22.         return title;
  23.     }
  24.    
  25.     public void printAuthor()
  26.     {
  27.         System.out.println(author);
  28.     }
  29.    
  30.     public void printTitle()
  31.     {
  32.         System.out.println(title);
  33.     }
  34. }


Exercise 2.85
  1. public class Book
  2. {
  3.     // The fields.
  4.     private String author;
  5.     private String title;
  6.     private int pages;
  7.     /**
  8.     * Set the author and title fields when this object
  9.     * is constructed.
  10.     */
  11.     public Book(String bookAuthor, String bookTitle, int bookPages)
  12.     {
  13.         author = bookAuthor;
  14.         title = bookTitle;
  15.         pages = bookPages;
  16.     }
  17.        
  18.     public String getAuthor()
  19.     {
  20.         return author;
  21.     }
  22.    
  23.     public String getTitle()
  24.     {
  25.         return title;
  26.     }
  27.    
  28.     public void printAuthor()
  29.     {
  30.         System.out.println(author);
  31.     }
  32.    
  33.     public void printTitle()
  34.     {
  35.         System.out.println(title);
  36.     }
  37.    
  38.     public int getPages()
  39.     {
  40.         return pages;
  41.     }
  42.    
  43. }

Exercise 2.86
  1. public class Book
  2. {
  3.     // The fields.
  4.     private String author;
  5.     private String title;
  6.     private int pages;
  7.     /**
  8.     * Set the author and title fields when this object
  9.     * is constructed.
  10.     */
  11.     public Book(String bookAuthor, String bookTitle, int bookPages)
  12.     {
  13.         author = bookAuthor;
  14.         title = bookTitle;
  15.         pages = bookPages;
  16.     }
  17.        
  18.     public String getAuthor()
  19.     {
  20.         return author;
  21.     }
  22.    
  23.     public String getTitle()
  24.     {
  25.         return title;
  26.     }
  27.    
  28.     public void printAuthor()
  29.     {
  30.         System.out.println(author);
  31.     }
  32.    
  33.     public void printTitle()
  34.     {
  35.         System.out.println(title);
  36.     }
  37.    
  38.     public int getPages()
  39.     {
  40.         return pages;
  41.     }
  42.    
  43.     public void printDetails()
  44.     {
  45.         System.out.println("Title  :" + title);
  46.         System.out.println("Author  :" + author);
  47.         System.out.println("Page(s)  :" + pages);
  48.     }
  49.    
  50. }

Exercise 2.87
  1. import java.util.Scanner;
  2. public class Book
  3. {
  4.     Scanner scan = new Scanner(System.in);
  5.     // The fields.
  6.     private String author;
  7.     private String title;
  8.     private int pages;
  9.     private String refNumber;
  10.     /**
  11.     * Set the author and title fields when this object
  12.     * is constructed.
  13.     */
  14.     public Book(String bookAuthor, String bookTitle, int bookPages)
  15.     {
  16.         author = bookAuthor;
  17.         title = bookTitle;
  18.         pages = bookPages;
  19.         refNumber = "";
  20.     }
  21.        
  22.     public String getAuthor()
  23.     {
  24.         return author;
  25.     }
  26.    
  27.     public String getTitle()
  28.     {
  29.         return title;
  30.     }
  31.    
  32.     public String getRefNumber()
  33.     {
  34.         return refNumber;
  35.     }
  36.    
  37.     public int getPages()
  38.     {
  39.         return pages;
  40.     }
  41.    
  42.     public void printAuthor()
  43.     {
  44.         System.out.println(author);
  45.     }
  46.    
  47.     public void printTitle()
  48.     {
  49.         System.out.println(title);
  50.     }
  51.    
  52.     public void setRefNumber(String ref)
  53.     {
  54.         refNumber = ref;
  55.     }
  56.    
  57.     public void printDetails()
  58.     {
  59.         System.out.println("Title  :" + title);
  60.         System.out.println("Author  :" + author);
  61.         System.out.println("Page(s)  :" + pages);
  62.     }
  63.    
  64. }

Exercise 2.88
  1. import java.util.Scanner;
  2. public class Book
  3. {
  4.     Scanner scan = new Scanner(System.in);
  5.     // The fields.
  6.     private String author;
  7.     private String title;
  8.     private int pages;
  9.     private String refNumber;
  10.     /**
  11.     * Set the author and title fields when this object
  12.     * is constructed.
  13.     */
  14.     public Book(String bookAuthor, String bookTitle, int bookPages)
  15.     {
  16.         author = bookAuthor;
  17.         title = bookTitle;
  18.         pages = bookPages;
  19.         refNumber = "";
  20.     }
  21.        
  22.     public String getAuthor()
  23.     {
  24.         return author;
  25.     }
  26.    
  27.     public String getTitle()
  28.     {
  29.         return title;
  30.     }
  31.    
  32.     public String getRefNumber()
  33.     {
  34.         return refNumber;
  35.     }
  36.    
  37.     public int getPages()
  38.     {
  39.         return pages;
  40.     }
  41.    
  42.     public void printAuthor()
  43.     {
  44.         System.out.println(author);
  45.     }
  46.    
  47.     public void printTitle()
  48.     {
  49.         System.out.println(title);
  50.     }
  51.    
  52.     public void setRefNumber(String ref)
  53.     {
  54.         refNumber = ref;
  55.     }
  56.    
  57.     public void printDetails()
  58.     {
  59.         System.out.println("Title  :" + title);
  60.         System.out.println("Author  :" + author);
  61.         System.out.println("Page(s)  :" + pages);
  62.         if (refNumber.length() != 0)
  63.             {
  64.                 System.out.println("Ref. Num    :" + refNumber);
  65.             }
  66.         else System.out.println("Ref. Num    :ZZZ");
  67.     }
  68.    
  69. }


Exercise 2.89
  1. import java.util.Scanner;
  2. public class Book
  3. {
  4.     Scanner scan = new Scanner(System.in);
  5.     // The fields.
  6.     private String author;
  7.     private String title;
  8.     private int pages;
  9.     private String refNumber;
  10.     /**
  11.     * Set the author and title fields when this object
  12.     * is constructed.
  13.     */
  14.     public Book(String bookAuthor, String bookTitle, int bookPages)
  15.     {
  16.         author = bookAuthor;
  17.         title = bookTitle;
  18.         pages = bookPages;
  19.         refNumber = "";
  20.     }
  21.        
  22.     public String getAuthor()
  23.     {
  24.         return author;
  25.     }
  26.    
  27.     public String getTitle()
  28.     {
  29.         return title;
  30.     }
  31.    
  32.     public String getRefNumber()
  33.     {
  34.         return refNumber;
  35.     }
  36.    
  37.     public int getPages()
  38.     {
  39.         return pages;
  40.     }
  41.    
  42.     public void printAuthor()
  43.     {
  44.         System.out.println(author);
  45.     }
  46.    
  47.     public void printTitle()
  48.     {
  49.         System.out.println(title);
  50.     }
  51.    
  52.     public void setRefNumber(String ref)
  53.     {
  54.         if (ref.length() >= 3)
  55.         {
  56.             refNumber = ref;
  57.             System.out.println("Set success!");
  58.         }
  59.         else System.out.println("Set failed.");
  60.     }
  61.    
  62.     public void printDetails()
  63.     {
  64.         System.out.println("Title  :" + title);
  65.         System.out.println("Author  :" + author);
  66.         System.out.println("Page(s)  :" + pages);
  67.         if (refNumber.length() != 0)
  68.             {
  69.                 System.out.println("Ref. Num    :" + refNumber);
  70.             }
  71.         else System.out.println("Ref. Num    :ZZZ");
  72.     }
  73.    
  74. }


Exercise 2.90
  1. import java.util.Scanner;
  2. public class Book
  3. {
  4.     Scanner scan = new Scanner(System.in);
  5.     // The fields.
  6.     private String author;
  7.     private String title;
  8.     private int pages;
  9.     private String refNumber;
  10.     private int borrowed;
  11.     private boolean courseText;
  12.     /**
  13.     * Set the author and title fields when this object
  14.     * is constructed.
  15.     */
  16.     public Book(String bookAuthor, String bookTitle, int bookPages, boolean bookCourseText)
  17.     {
  18.         author = bookAuthor;
  19.         title = bookTitle;
  20.         pages = bookPages;
  21.         refNumber = "";
  22.         borrowed = 0;
  23.         courseText = bookCourseText;
  24.     }
  25.        
  26.     public String getAuthor()
  27.     {
  28.         return author;
  29.     }
  30.    
  31.     public String getTitle()
  32.     {
  33.         return title;
  34.     }
  35.    
  36.     public String getRefNumber()
  37.     {
  38.         return refNumber;
  39.     }
  40.    
  41.     public int getPages()
  42.     {
  43.         return pages;
  44.     }
  45.    
  46.     public int getBorrowed()
  47.     {
  48.         return borrowed;
  49.     }
  50.    
  51.     public boolean isCourseText()
  52.     {
  53.         return courseText;
  54.     }
  55.    
  56.     public void printAuthor()
  57.     {
  58.         System.out.println(author);
  59.     }
  60.    
  61.     public void printTitle()
  62.     {
  63.         System.out.println(title);
  64.     }
  65.    
  66.     public void setRefNumber(String ref)
  67.     {
  68.         if (ref.length() >= 3)
  69.         {
  70.             refNumber = ref;
  71.             System.out.println("Set success!");
  72.         }
  73.         else System.out.println("Set failed.");
  74.     }
  75.    
  76.     public void borrow()
  77.     {
  78.         borrowed = borrowed + 1;
  79.     }
  80.    
  81.     public void printDetails()
  82.     {
  83.         System.out.println("Title  :" + title);
  84.         System.out.println("Author  :" + author);
  85.         System.out.println("Page(s)  :" + pages);
  86.         if (refNumber.length() != 0)
  87.             {
  88.                 System.out.println("Ref. Num    :" + refNumber);
  89.             }
  90.         else System.out.println("Ref. Num    :ZZZ");
  91.         System.out.println("Borrowed    :" + borrowed);
  92.        
  93.     }
  94.    
  95. }

Exercise 2.91

  1. import java.util.Scanner;
  2. public class Book
  3. {
  4.     Scanner scan = new Scanner(System.in);
  5.     // The fields.
  6.     private String author;
  7.     private String title;
  8.     private int pages;
  9.     private String refNumber;
  10.     private int borrowed;
  11.     private boolean courseText;
  12.     /**
  13.     * Set the author and title fields when this object
  14.     * is constructed.
  15.     */
  16.     public Book(String bookAuthor, String bookTitle, int bookPages, boolean bookCourseText)
  17.     {
  18.         author = bookAuthor;
  19.         title = bookTitle;
  20.         pages = bookPages;
  21.         refNumber = "";
  22.         borrowed = 0;
  23.         courseText = bookCourseText;
  24.     }
  25.        
  26.     public String getAuthor()
  27.     {
  28.         return author;
  29.     }
  30.    
  31.     public String getTitle()
  32.     {
  33.         return title;
  34.     }
  35.    
  36.     public String getRefNumber()
  37.     {
  38.         return refNumber;
  39.     }
  40.    
  41.     public int getPages()
  42.     {
  43.         return pages;
  44.     }
  45.    
  46.     public int getBorrowed()
  47.     {
  48.         return borrowed;
  49.     }
  50.    
  51.     public boolean isCourseText()
  52.     {
  53.         return courseText;
  54.     }
  55.    
  56.     public void printAuthor()
  57.     {
  58.         System.out.println(author);
  59.     }
  60.    
  61.     public void printTitle()
  62.     {
  63.         System.out.println(title);
  64.     }
  65.    
  66.     public void setRefNumber(String ref)
  67.     {
  68.         if (ref.length() >= 3)
  69.         {
  70.             refNumber = ref;
  71.             System.out.println("Set success!");
  72.         }
  73.         else System.out.println("Set failed.");
  74.     }
  75.    
  76.     public void borrow()
  77.     {
  78.         borrowed = borrowed + 1;
  79.     }
  80.    
  81.     public void printDetails()
  82.     {
  83.         System.out.println("Title  :" + title);
  84.         System.out.println("Author  :" + author);
  85.         System.out.println("Page(s)  :" + pages);
  86.         if (refNumber.length() != 0)
  87.             {
  88.                 System.out.println("Ref. Num    :" + refNumber);
  89.             }
  90.         else System.out.println("Ref. Num    :ZZZ");
  91.         System.out.println("Borrowed    :" + borrowed);
  92.        
  93.     }
  94.    
  95. }


Exercise 2.92
  1. public class Heater
  2. {
  3.     // instance variables - replace the example below with your own
  4.     private double temperature;
  5.     /**
  6.      * Constructor for objects of class Heater
  7.      */
  8.     public Heater()
  9.     {
  10.         // initialise instance variables
  11.         temperature = 15.0;
  12.     }
  13.     public void warmer()
  14.     {
  15.         temperature = temperature + 5.0;
  16.     }
  17.    
  18.     public void cooler()
  19.     {
  20.         temperature = temperature - 5.0;
  21.     }
  22.    
  23.     public double getTemperature()
  24.     {
  25.         return temperature;
  26.     }
  27. }

Exercise 2.93
  1. public class Heater
  2. {
  3.     // instance variables - replace the example below with your own
  4.     private double temperature;
  5.     private double min;
  6.     private double max;
  7.     private double increment;
  8.     /**
  9.      * Constructor for objects of class Heater
  10.      */
  11.     public Heater(double temperatureMin, double temperatureMax)
  12.     {
  13.         temperature = 15.0;
  14.         min = temperatureMin;
  15.         max = temperatureMax;
  16.         increment = 5.0;
  17.     }
  18.     public void warmer()
  19.     {
  20.         if (temperature + increment > max)
  21.         {
  22.             System.out.println("Set failed.");
  23.         }
  24.         else
  25.         {
  26.             temperature = temperature + 5.0;
  27.             System.out.println("Set success!");
  28.         }
  29.     }
  30.    
  31.     public void cooler()
  32.     {
  33.         if (temperature + increment < min)
  34.         {
  35.             System.out.println("Set failed.");
  36.         }
  37.         else
  38.         {
  39.             temperature = temperature - 5.0;
  40.             System.out.println("Set success!");
  41.         }
  42.     }
  43.    
  44.     public void setIncrement(double inc)
  45.     {
  46.         if (inc < 0)
  47.         System.out.println("Set failed.");
  48.         else increment = inc;
  49.     }
  50.    
  51.     public double getTemperature()
  52.     {
  53.         return temperature;
  54.     }
  55. }


Komentar

Postingan populer dari blog ini

ATM study case.

FP PBO :Calendar viewer