TICKET MACHINE

Fields
Di dalam bahasa pemrograman java field merupakan sebuah atribut. Field bisa berupa variable dari sebuah class, variable sebuah object, variable sebuah object method atau sebuah parameter dari sebuah function atau code.

constructor
constructor merupakan suatu method yang akan memberikan nilai awal pada saat suatu objek dibuat

Parameters
parameter merupakan suatu bagian dari method yang mana didalam suatu method tersebut bisa memiliki satu atau lebih parameter yang bisa menentukan suatu nilai.
Accessor Methods
Merupakan sebuah metode yang digunakan untuk mengembalikan nilai dari sebuah private field. Skema ini biasanya ditandai dengan awalan nama "get"
Mutator Methods Merupakan sebuah metode yang digunakan untuk menetapkan nilai dari sebuah private field. Skema ini biasanya ditandai dengan awalan nama "set"
Contoh Methods:
Assignment Assignment adalah sebuah operator yang disimbolkan dengan lambang sama dengan (=). Conditional Statement suatu pilihan atau opsi dengan kondisi tertentu. Jika kondisi yang menjadi syarat terpenuhi, maka opsi atau pilihan dijalankan, jika tidak maka sebaliknya. Dalam java terdapat 4 macam jenis percabangan, if, if-else, else-if, dan juga switch. Keempat jenis ini memiliki penggunaan masing-masing. Source Code
Class TicketMachine

Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1.  public class TicketMachine extends IntMain  
  2.  {  
  3.  // The price of a ticket from this machine.  
  4.  private int price;  
  5.  // The amount of money entered by a customer so far.  
  6.  private int balance;  
  7.  // The total amount of money collected by this machine.  
  8.  private int total;  
  9.  /**  
  10.  * Create a machine that issues tickets of the given price.  
  11.  * Note that the price must be greater than zero, and there  
  12.  * are no checks to ensure this.  
  13.  */  
  14.  public TicketMachine(int ticketCost)  
  15.  {  
  16.   price = ticketCost;  
  17.   balance = 0;  
  18.   total = 0;  
  19.  }  
  20.  /**  
  21.  * Return the price of a ticket.  
  22.  */  
  23.  public int getPrice()  
  24.  {  
  25.   return price;  
  26.  }  
  27.  /**  
  28.  * Return the amount of money already inserted for the  
  29.  * next ticket.  
  30.  */  
  31.  public int getBalance()  
  32.  {  
  33.    return balance;  
  34.  }  
  35.  /**  
  36.  * Receive an amount of money in cents from a customer.  
  37.  */  
  38.  public void insertMoney(int amount)  
  39.  {  
  40.    balance = balance + amount;  
  41.  }  
  42.  /**  
  43.  * Print a ticket.  
  44.  * Update the total collected and  
  45.  * reduce the balance to zero.  
  46.  */  
  47.  public void printTicket()  
  48.  {  
  49.    // Simulate the printing of a ticket.  
  50.    System.out.println("##################");  
  51.    System.out.println("# The BlueJ Line");  
  52.    System.out.println("# Ticket");  
  53.    System.out.println("# " + price +"cents.");  
  54.    System.out.println("##################");  
  55.    System.out.println();  
  56.    // Update the total collected with the balance.  
  57.    total = total + balance;  
  58.    // Clear the balance.  
  59.    balance = 0;  
  60.  }  
  61.  }

Class IntMain


Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. import java.util.Scanner;  
  2. public class IntMain  
  3. {
  4.     public static int main()  
  5.     {
  6.         Scanner scan= new Scanner(System.in);  
  7.         int cost,menu;
  8.         System.out.println("Masukkan harga tiket \n");  
  9.         cost=scan.nextInt();  
  10.         TicketMachine ticket=new TicketMachine(cost);
  11.         while (true)
  12.         {
  13.             System.out.println("1. Get Price");  
  14.             System.out.println("2. Get Balance");  
  15.             System.out.println("3. Insert Money");  
  16.             System.out.println("4. Print Ticket");  
  17.             System.out.println("5. Exit");
  18.             menu=scan.nextInt();  
  19.             switch(menu)  
  20.             {  
  21.                 case 1:  
  22.                 cost=ticket.getPrice();  
  23.                 System.out.println(cost);  
  24.                 break;  
  25.    
  26.                 case 2:  
  27.                 System.out.println(ticket.getBalance());  
  28.                 break;  
  29.            
  30.                 case 3:  
  31.                 int money=scan.nextInt();  
  32.                 ticket.insertMoney(money);  
  33.                 break;  
  34.    
  35.                 case 4:  
  36.                 ticket.printTicket();  
  37.                 break;  
  38.          
  39.                 case 5:
  40.                 return 0;
  41.             }  
  42.       }    
  43.     }
  44. }

Output Exercise 2.83
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. public class Book {
  2.     private String author;
  3.     private String title;
  4.    
  5.     public Book(String bookAuthor, String bookTitle) {
  6.         author = bookAuthor;
  7.         title = bookTitle;
  8.     }
  9.    
  10.     public String getAuthor() {
  11.         return author;
  12.     }
  13.     public String getTitle() {
  14.         return title;
  15.     }
  16. }
Exercise 2.84
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. public class Book {
  2.     private String author;
  3.     private String title;
  4.    
  5.     public Book(String bookAuthor, String bookTitle) {
  6.         author = bookAuthor;
  7.         title = bookTitle;
  8.     }
  9.    
  10.     public String getAuthor() {
  11.         return author;
  12.     }
  13.     public String getTitle() {
  14.         return title;
  15.     }
  16.    
  17.     public void printAuthor() {
  18.         System.out.println(author);
  19.     }
  20.    
  21.     public void printTitle() {
  22.         System.out.println(title);
  23.     }
  24. }
Exercise 2.85
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. public class Book {
  2.     private String author;
  3.     private String title;
  4.     private int pages;
  5.    
  6.     public Book(String bookAuthor, String bookTitle, int bookPages) {
  7.         author = bookAuthor;
  8.         title = bookTitle;
  9.         pages = bookPages;
  10.     }
  11.    
  12.     public String getAuthor() {
  13.         return author;
  14.     }
  15.     public String getTitle() {
  16.         return title;
  17.     }
  18.    
  19.     public int getPages() {
  20.         return pages;
  21.     }
  22.    
  23.     public void printAuthor() {
  24.         System.out.println(author);
  25.     }
  26.    
  27.     public void printTitle() {
  28.         System.out.println(title);
  29.     }
  30.    
  31. }
Exercise 2.86
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. public class Book {
  2.     private String author;
  3.     private String title;
  4.     private int pages;
  5.    
  6.     public Book(String bookAuthor, String bookTitle, int bookPages) {
  7.         author = bookAuthor;
  8.         title = bookTitle;
  9.         pages = bookPages;
  10.     }
  11.    
  12.     public String getAuthor() {
  13.         return author;
  14.     }
  15.     public String getTitle() {
  16.         return title;
  17.     }
  18.    
  19.     public int getPages() {
  20.         return pages;
  21.     }
  22.    
  23.     public void printAuthor() {
  24.         System.out.println(author);
  25.     }
  26.    
  27.     public void printTitle() {
  28.         System.out.println(title);
  29.     }
  30.    
  31.     public void printDetails() {
  32.         System.out.println("Title: " + title + ", Author: " + author + ", Pages: " + pages);
  33.     }
  34. }
Exercise 2.87
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. public class Book {
  2.     private String author;
  3.     private String title;
  4.     private String refNumber;
  5.     private int pages;
  6.    
  7.     public Book(String bookAuthor, String bookTitle, String bookRefNumber, int bookPages) {
  8.         author = bookAuthor;
  9.         title = bookTitle;
  10.         pages = bookPages;
  11.         refNumber = "";
  12.     }
  13.    
  14.     public String getAuthor() {
  15.         return author;
  16.     }
  17.     public String getTitle() {
  18.         return title;
  19.     }
  20.    
  21.     public int getPages() {
  22.         return pages;
  23.     }
  24.    
  25.     public String getRefNumber() {
  26.         return refNumber;
  27.     }
  28.    
  29.     public void setRefNumber(String refNumber) {
  30.         this.refNumber = refNumber;    
  31.     }
  32.    
  33.     public void printAuthor() {
  34.         System.out.println(author);
  35.     }
  36.    
  37.     public void printTitle() {
  38.         System.out.println(title);
  39.     }
  40.    
  41.     public void printDetails() {
  42.         System.out.println("Title: " + title + ", Author: " + author + ", Pages: " + pages);
  43.     }
  44. }
Exercise 2.88
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. public class Book {
  2.     private String author;
  3.     private String title;
  4.     private String refNumber;
  5.     private int pages;
  6.    
  7.     public Book(String bookAuthor, String bookTitle, String bookRefNumber, int bookPages) {
  8.         author = bookAuthor;
  9.         title = bookTitle;
  10.         pages = bookPages;
  11.         refNumber = "";
  12.     }
  13.    
  14.     public String getAuthor() {
  15.         return author;
  16.     }
  17.     public String getTitle() {
  18.         return title;
  19.     }
  20.    
  21.     public int getPages() {
  22.         return pages;
  23.     }
  24.    
  25.     public String getRefNumber() {
  26.         return refNumber;
  27.     }
  28.    
  29.     public void setRefNumber(String refNumber) {
  30.         this.refNumber = refNumber;    
  31.     }
  32.    
  33.     public void printAuthor() {
  34.         System.out.println(author);
  35.     }
  36.    
  37.     public void printTitle() {
  38.         System.out.println(title);
  39.     }
  40.    
  41.     public void printDetails() {
  42.         String refNumPrint = refNumber;
  43.        
  44.         System.out.println("Title: " + title + ", Author: " + author + ", Pages: " + pages);
  45.        
  46.         if(refNumPrint.length() < 0) {
  47.             refNumPrint = "ZZZ";
  48.         }
  49.         System.out.println("Reference number:  " + refNumPrint);
  50.     }
  51. }
Exercise 2.89
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. public class Book {
  2.     private String author;
  3.     private String title;
  4.     private String refNumber;
  5.     private int pages;
  6.    
  7.     public Book(String bookAuthor, String bookTitle, String bookRefNumber, int bookPages) {
  8.         author = bookAuthor;
  9.         title = bookTitle;
  10.         pages = bookPages;
  11.         refNumber = "";
  12.     }
  13.    
  14.     public String getAuthor() {
  15.         return author;
  16.     }
  17.     public String getTitle() {
  18.         return title;
  19.     }
  20.    
  21.     public int getPages() {
  22.         return pages;
  23.     }
  24.    
  25.     public String getRefNumber() {
  26.         return refNumber;
  27.     }
  28.    
  29.     public void setRefNumber(String refNumber) {
  30.         String errMsg = "Reference Number harus diatas 3 karakter";
  31.        
  32.         if(refNumber.length()<3) {
  33.             System.out.println(errMsg);
  34.         }
  35.         else {
  36.             this.refNumber = refNumber;
  37.         }
  38.        
  39.            
  40.     }
  41.    
  42.     public void printAuthor() {
  43.         System.out.println(author);
  44.     }
  45.    
  46.     public void printTitle() {
  47.         System.out.println(title);
  48.     }
  49.    
  50.     public void printDetails() {
  51.         String refNumPrint = refNumber;
  52.        
  53.         System.out.println("Title: " + title + ", Author: " + author + ", Pages: " + pages);
  54.        
  55.         if(refNumPrint.length() < 0) {
  56.             refNumPrint = "ZZZ";
  57.         }
  58.         System.out.println("Reference number:  " + refNumPrint);
  59.     }
  60. }
Exercise 2.90
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. public class Book {
  2.     private String author;
  3.     private String title;
  4.     private String refNumber;
  5.     private int pages;
  6.     private int borrowedTime;
  7.    
  8.     public Book(String bookAuthor, String bookTitle, String bookRefNumber, int bookPages, int bookBorrowedTime) {
  9.         author = bookAuthor;
  10.         title = bookTitle;
  11.         pages = bookPages;
  12.         refNumber = "";
  13.         borrowedTime = bookBorrowedTime;
  14.     }
  15.    
  16.     public String getAuthor() {
  17.         return author;
  18.     }
  19.     public String getTitle() {
  20.         return title;
  21.     }
  22.    
  23.     public int getPages() {
  24.         return pages;
  25.     }
  26.    
  27.     public String getRefNumber() {
  28.         return refNumber;
  29.     }
  30.    
  31.     public int getBorrowedTime() {
  32.         return borrowedTime;
  33.     }
  34.    
  35.     public void setRefNumber(String refNumber) {
  36.         String errMsg = "Reference Number harus diatas 3 karakter";
  37.        
  38.         if(refNumber.length()<3) {
  39.             System.out.println(errMsg);
  40.         }
  41.         else {
  42.             this.refNumber = refNumber;
  43.         }
  44.        
  45.            
  46.     }
  47.    
  48.     public void printAuthor() {
  49.         System.out.println(author);
  50.     }
  51.    
  52.     public void printTitle() {
  53.         System.out.println(title);
  54.     }
  55.    
  56.     public void printDetails() {
  57.         String refNumPrint = refNumber;
  58.        
  59.         System.out.println("Title: " + title + ", Author: " + author + ", Pages: " + pages);
  60.        
  61.         if(refNumPrint.length() < 0) {
  62.             refNumPrint = "ZZZ";
  63.         }
  64.         System.out.println("Reference number:  " + refNumPrint);
  65.         System.out.println("Borrowed: " + borrowedTime);
  66.     }
  67.    
  68.     public void bookBorrowed() {
  69.         borrowedTime++;
  70.     }
  71. }
Exercise 2.91
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. public class Book {
  2.     private String author;
  3.     private String title;
  4.     private String refNumber;
  5.     private int pages;
  6.     private int borrowedTime;
  7.     public boolean courseText;
  8.    
  9.     public Book(String bookAuthor, String bookTitle, String bookRefNumber, int bookPages, int bookBorrowedTime) {
  10.         author = bookAuthor;
  11.         title = bookTitle;
  12.         pages = bookPages;
  13.         refNumber = "";
  14.         borrowedTime = bookBorrowedTime;
  15.     }
  16.    
  17.     public String getAuthor() {
  18.         return author;
  19.     }
  20.     public String getTitle() {
  21.         return title;
  22.     }
  23.    
  24.     public int getPages() {
  25.         return pages;
  26.     }
  27.    
  28.     public String getRefNumber() {
  29.         return refNumber;
  30.     }
  31.    
  32.     public int getBorrowedTime() {
  33.         return borrowedTime;
  34.     }
  35.    
  36.     public boolean isCourseText() {
  37.         return courseText;
  38.     }
  39.    
  40.     public void setRefNumber(String refNumber) {
  41.         String errMsg = "Reference Number harus diatas 3 karakter";
  42.        
  43.         if(refNumber.length()<3) {
  44.             System.out.println(errMsg);
  45.         }
  46.         else {
  47.             this.refNumber = refNumber;
  48.         }
  49.        
  50.            
  51.     }
  52.    
  53.     public void printAuthor() {
  54.         System.out.println(author);
  55.     }
  56.    
  57.     public void printTitle() {
  58.         System.out.println(title);
  59.     }
  60.    
  61.     public void printDetails() {
  62.         String refNumPrint = refNumber;
  63.        
  64.         System.out.println("Title: " + title + ", Author: " + author + ", Pages: " + pages);
  65.        
  66.         if(refNumPrint.length() < 0) {
  67.             refNumPrint = "ZZZ";
  68.         }
  69.         System.out.println("Reference number:  " + refNumPrint);
  70.         System.out.println("Borrowed: " + borrowedTime);
  71.     }
  72.    
  73.     public void bookBorrowed() {
  74.         borrowedTime++;
  75.     }
  76. }
Exercise 2.92
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. public class Heater {
  2.     private double temperature;
  3.    
  4.     public Heater() {
  5.         temperature = 15.00;
  6.     }
  7.     public void warmer() {
  8.         temperature += 5.00;
  9.     }
  10.     public void cooler() {
  11.         temperature -= 5.00;
  12.     }
  13.     public double getTemperature() {
  14.         return temperature;
  15.     }
  16. }
Exercise 2.93
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. public class Heater
  2. {
  3.     private int temperature;
  4.     /**
  5.      *  Creates a new Heater with an initial temperature of 15.
  6.      */
  7.     public Heater()
  8.     {
  9.         temperature = 15;
  10.     }
  11.    
  12.     /**
  13.      * Increases the temperature by 5 degrees
  14.      */
  15.     public void warmer()
  16.     {
  17.         temperature += 5;
  18.     }
  19.     /**
  20.      * Decreases the temperature by 5 degrees
  21.      */
  22.     public void cooler()
  23.     {
  24.         temperature -= 5;
  25.     }
  26.     /**
  27.      * Gets the current temperature of the heater
  28.      */
  29.     public int getTemperature()
  30.     {
  31.         return temperature;
  32.     }
  33. }

Komentar

Postingan Populer