Sunday, October 14, 2018

ETS PBO - SIstem Parkir

7:58 PM Posted by Unknown No comments
 Class yang digunakan
  1. Class Vehicle, merupakan class untuk input jenis kendaraan, nopol, serta durasi parkir
  2. Class Input Reader, sebagai class untuk melakukan input
  3. Class ParkingMachine, merupakan class yang berupa mesin parkir, dimana class ini menjadi pusat pengaturan operasi-operasi dan obyek-obyek pada mesin

  • Class Vehicle
 public class Vehicle   
 {   
   private InputReader reader;   
   public int gvehicle = 0;   
   public int durasi = 0;   
   public String nopol;   
   public void createVehicle(){   
    reader = new InputReader();   
    gvehicle = getVehicle();   
    nopol = getNopol();   
    durasi = getDurasi();   
   }   
   public int getVehicle(){   
     System.out.println("Pilih jenis kendaraan anda:");   
     System.out.println("1. Motor");   
     System.out.println("2. Mobil");   
     int gvehicle = reader.getInt();   
    return gvehicle;   
   }   
   public String getNopol(){   
    System.out.println("Silakan masukkan nomor polisi kendaraan anda: ");   
     String nopol = reader.getInput();   
    return nopol;   
   }   
   public int getDurasi(){   
    System.out.println("Durasi Parkir (dalam jam):");   
    int durasi = reader.getInt();   
    return durasi;   
   }   
 }  

  • Class InputReader
 import java.util.Scanner;   
 public class InputReader   
 {   
    public String getInput()   
 {   
    Scanner sc = new Scanner(System.in);   
    String input = sc.nextLine();   
    return input;   
 }   
 public int getInt(){   
    Scanner sc = new Scanner(System.in);   
    int input = sc.nextInt();   
    return input;   
   }   
 }   

  • Class ParkingMachine
 import java.util.Scanner;   
 public class ParkingMachine   
 {   
   private InputReader reader;   
   private Vehicle vehicle;   
   private int biaya = 0;   
 public ParkingMachine()   
 {   
    reader = new InputReader();   
    vehicle = new Vehicle();   
 }   
 public void start()   
 {   
    printWelcome();   
    vehicle.createVehicle();   
    biaya = 2000*vehicle.gvehicle*vehicle.durasi;    
    System.out.println("Jenis kendaraan : " + vehicle.gvehicle);   
    System.out.println("Nomor polisi : " + vehicle.nopol);   
    System.out.println("Durasi   : " + vehicle.durasi + "jam");   
    System.out.println("Biaya Parkir : Rp" + biaya );   
    System.out.println("Tekan enter untuk melanjutkan");   
    reader.getInput();   
    System.out.println("Silakan tempelkan kartu pembayaran anda");   
    System.out.println("Tekan enter untuk melanjutkan");  
    reader.getInput();   
    printTicket();   
 }   
 private void printWelcome(){   
    System.out.println("Selamat Datang");   
 }   
 public void printTicket(){    
    System.out.println("------------Tiket 1-----------");  
    System.out.println("---Sistem Parkir Elektronik---");   
    System.out.println("Jenis kendaraan : " + vehicle.gvehicle);   
    System.out.println("Nomor polisi : " + vehicle.nopol);   
    System.out.println("Durasi   : " + vehicle.durasi + "jam");   
    System.out.println("Biaya Parkir : Rp" + biaya );    
    System.out.println("-------------------------------");   
    System.out.println();    
    System.out.println("------------Tiket 2-----------");  
    System.out.println("---Sistem Parkir Elektronik---");   
    System.out.println("Jenis kendaraan : " + vehicle.gvehicle);   
    System.out.println("Nomor polisi : " + vehicle.nopol);   
    System.out.println("Durasi   : " + vehicle.durasi + "jam");   
    System.out.println("Biaya Parkir : Rp" + biaya );    
    System.out.println("------------------------------");   
   }   
 }  


Berikut tampilan workspace


 NB : tarif parkir yang dikenakan adalah Rp2000,- per jam

Sunday, October 7, 2018

Tugas PBO - Support System

7:44 PM Posted by Unknown No comments
Class yang digunakan :

  1. Class SupportSystem, sebagai main class untuk menjalankan program dan tempat proses support system dilakukan
  2. Class InputReader, berisi input dari pengguna
  3. Class Responder, berisi respon dari sistem setelah pengguna melakukan input

  • Class SupportSystem
 public class SupportSystem  
 {  
   private InputReader reader;  
   private Responder responder;  
    /**  
    * Creates a technical support system.  
    */  
   public SupportSystem()  
   {  
     reader = new InputReader();  
     responder = new Responder();  
   }  
    /**  
    * Start the technical support system. This will print a  
    * welcome message and enter into a dialog with the user,  
    * until the user ends the dialog.  
    */  
   public void start()  
   {  
     boolean finished = false;  
     printWelcome();  
     while(!finished) {  
       String input = reader.getInput();  
       if(input.startsWith("bye")) {  
         finished = true;  
       }  
       else {  
         String response = responder.generateResponse();  
         System.out.println(response);  
       }  
     }  
     printGoodbye();  
   }  
    /**  
    * Print a welcome message to the screen.  
    */  
   private void printWelcome()  
   {  
     System.out.println(  
     "Welcome to the DodgySoft Technical Support System.");  
     System.out.println();  
     System.out.println("Please tell us about your problem.");  
     System.out.println(  
     "We will assist you with any problem you might have.");  
     System.out.println(  
     "Please type 'bye' to exit our system.");  
   }  
    /**  
    * Print a good-bye message to the screen.  
    */  
   private void printGoodbye()  
   {  
     System.out.println("Nice talking to you. Bye. . .");  
   }  
 }  

  • Class InputReader
 import java.util.Scanner;  
 public class InputReader  
 {  
   private Scanner sc;  
   public InputReader(){  
     sc = new Scanner(System.in);  
   }  
   public String getInput(){  
     String input;  
     input = sc.nextLine();  
     return input;  
   }   
 }  

  • Class Responder
 public class Responder  
 {  
    /**  
    * Construct a Responder - nothing to do  
    */  
   public Responder()  
   {  
   }  
    /**  
    * Generate a response.  
    * @return A string that should be displayed as the  
    * response  
    */  
   public String generateResponse()  
   {  
     return "That sounds interesting. Tell me more. . .";  
   }  
 }  




Tugas PBO - Auction System

10:56 AM Posted by Unknown No comments
Class yang digunakan :
  1. Class Auction, sebagai class utama tempat proses auction (pelelangan) dilakukan
  2. Class Lot, berfungsi mendaftarkan dan menyimpan deskripsi lot
  3. Class Person, berfungsi tempat menyimpat data / informasi orang
  4. Class Bid, berfungsi tempat menyimpan nilai bid
  • Class Auction
 /**  
  * Write a description of class Auction here.  
  *  
  * @author Bima S. Ramadhan  
  * @version 1.0  
  */  
 import java.util.ArrayList;  
 public class Auction  
 {  
   private ArrayList<Lot> lots;  
   private int nextLotNumber;  
   //Constructor  
   public Auction()  
   {  
     lots = new ArrayList<Lot>();  
     nextLotNumber = 1;  
   }  
   //Mendaftarkan lot baru  
   public void enterLot(String description)  
   {  
     lots.add(new Lot(nextLotNumber, description));  
     nextLotNumber++;  
   }  
   //Print Semua Lot  
   public void showLots()  
   {  
     for(Lot lot : lots) {  
       System.out.println(lot.toString());  
     }  
   }  
   //Melakukan Bid pada suatu Lot  
   public void makeABid(int lotNumber, Person bidder, long value)  
   {  
     Lot selectedLot = getLot(lotNumber);  
     if(selectedLot != null) {  
       Bid bid = new Bid(bidder, value);  
       boolean successful = selectedLot.bidFor(bid);  
       if(successful) {  
         System.out.println("Bid untuk Lot pada urutan " +  
          lotNumber + " sukses didaftarkan.");  
       }  
       else {  
         Bid highestBid = selectedLot.getHighestBid();  
         System.out.println("Lot urutan ke: " + lotNumber +  
         " memiliki Bid sebesar: " +  
         highestBid.getValue());  
       }  
     }  
   }  
   //Mencari Lot yang terdaftar  
   public Lot getLot(int lotNumber)  
   {  
     if((lotNumber >= 1) && (lotNumber < nextLotNumber)) {  
       Lot selectedLot = lots.get(lotNumber-1);  
       if(selectedLot.getNumber() != lotNumber) {  
          System.out.println("Internal error: Lot urutan ke " +  
         selectedLot.getNumber() +  
         " dimunculkan, sebagai ganti dari " +  
         lotNumber);  
         selectedLot = null;  
       }  
       return selectedLot;  
     }  
     else {  
       System.out.println("Lot urutan ke: " + lotNumber +  
       " tidak ditemukan.");  
       return null;  
     }  
   }  
   public void close()    
   {    
     System.out.println("Closing auction.");    
     for (Lot lot : lots)    
     {     
        System.out.println(lot.getNumber() + ": " + lot.getDescription());    
          if (lot.getHighestBid() == null)    
          {    
            System.out.println (" (No bids) ");    
          }    
          else    
          {    
            Bid highestBid = lot.getHighestBid();    
            System.out.println(" sold to " + highestBid.getBidder().getName() + " for " + highestBid.getValue());    
          }    
     }    
   }     
 }  

  • Class Lot
 /**  
  * Write a description of class Lot here.  
  *  
  * @author Bima S. Ramadhan  
  * @version 1.0  
  */  
 public class Lot  
 {  
   //ID Lot  
   private final int number;  
   //Deskripsi dari sebuah Lot  
   private String description;  
   //Bid tertinggi dari Lot  
   private Bid highestBid;  
   //Constructor  
   public Lot(int number, String description)  
   {  
     this.number = number;  
     this.description = description;  
     this.highestBid = null;  
   }  
   public boolean bidFor(Bid bid)  
   {  
     if(highestBid == null) {  
       //Bid belum ada  
       highestBid = bid;  
       return true;  
     }  
     else if(bid.getValue() > highestBid.getValue()) {  
       //Bid lebih besar dari nilai Bid sebelumnya  
       highestBid = bid;  
       return true;  
     }  
     else {  
       //Bid lebih kecil dari nilai Bid sebelumnya  
       return false;  
     }  
   }  
   //Deskripsi rincian Lot  
   public String toString()  
   {  
     String details = number + ": " + description;  
     if(highestBid != null) {  
       details += "  Bid: " +  
             highestBid.getValue();  
     }  
     else {  
       details += "  (No bid)";  
     }  
     return details;  
   }  
   //Return ID Lot  
   public int getNumber()  
   {  
     return number;  
   }  
   //Return Deskripsi Lot  
   public String getDescription()  
   {  
     return description;  
   }  
   //Return Bid tertinggi  
   public Bid getHighestBid()  
   {  
     return highestBid;  
   }  
 }  

  • Class Person
 /**  
  * Write a description of class Person here.  
  *  
  * @author Bima S. Ramadhan  
  * @version 1.0  
  */  
 public class Person  
 {  
   //variable final untuk nama orang (1 kali inisialisasi)  
   private final String personname;  
   //constructor  
   public Person(String name)  
   {  
     this.personname = name;  
   }  
   //value berupa nama orang  
   public String getName()  
   {  
     return personname;  
   }  
 }  

  • Class Bid 
 /**  
  * Write a description of class Bid here.  
  *  
  * @author Bima S. Ramadhan  
  * @version 1.0  
  */  
 public class Bid  
 {  
   //variable yang hanya bisa diinisialisasi 1 kali  
   private final Person biddername;  
   private final long value;  
   public Bid(Person name, long v)  
   {  
     this.biddername = name;  
     this.value = v;  
   }  
   //value nama bidder  
   public Person getBidder()  
   {  
     return biddername;  
   }  
   //value berupa nilai bid  
   public long getValue()  
   {  
     return value;  
   }  
 }  






Saturday, September 29, 2018

Tugas PBO - Jam Digital

4:24 AM Posted by Unknown No comments
Berikut class dan source code yang digunakan dalam membuat Jam Sederhana

  • Class NumberDisplay
 /**  
  * Write a description of class NumberDisplay here.  
  *  
  * @author Bima S. Ramadhan  
  * @version 1.0  
  */   
 public class NumberDisplay  
 {  
   private int limit;  
   private int value;  
   public NumberDisplay(int rollOverLimit)  
   {  
     limit = rollOverLimit;  
     value = 0;  
   }  
   public int getValue()  
   {  
     return value;  
   }  
   public void setValue(int replacementValue)  
   {  
     if((replacementValue >= 0) && (replacementValue < limit)) {  
           value = replacementValue;  
     }  
   }  
   public String getDisplayValue()  
   {  
     if(value < 10) {  
       return "0" +value ;  
     }  
     else {  
       return "" +value ;  
     }  
   }  
   public void increment()  
   {  
     value = (value + 1) % limit;  
   }  
 }  

  • Class ClockDisplay
 /**  
  * Write a description of class ClockDisplay here.  
  *  
  * @author Bima S. Ramadhan  
  * @version 1.0  
  */   
 public class ClockDisplay  
 {  
   private NumberDisplay hours;  
   private NumberDisplay minutes;  
   private String displayString; //simulates the actual display  
   public ClockDisplay()  
   {  
     hours = new NumberDisplay(24);  
     minutes = new NumberDisplay(60);  
     updateDisplay();  
   }  
   public ClockDisplay(int hour, int minute)  
   {  
     hours = new NumberDisplay(24);  
     minutes = new NumberDisplay(60);  
     setTime(hour, minute);  
   }  
   public void timeTick()  
   {  
     minutes.increment();  
     if(minutes.getValue() == 0) { //it just rolled over  
       hours.increment();  
     }  
     updateDisplay();  
   }  
   public void setTime(int hour, int minute)  
   {  
     hours.setValue(hour);  
     minutes.setValue(minute);  
     updateDisplay();  
   }  
   public String getTime()  
   {  
     return displayString;  
   }  
   private void updateDisplay()  
   {  
     displayString = hours.getDisplayValue() + ":" +  
             minutes.getDisplayValue();  
   }  
 }  

  • Class TestClockDisplay
 /**  
  * Write a description of class TestClockDisplay here.  
  *  
  * @author Bima S. Ramadhan  
  * @version 1.0  
  */   
 public class TestClockDisplay  
 {  
   public void test()  
   {  
     ClockDisplay clock = new ClockDisplay();  
     clock.setTime(0,0);  
     System.out.println(clock.getTime());  
     clock.setTime(5,30);  
     System.out.println(clock.getTime());  
     clock.setTime(20,00);  
     System.out.println(clock.getTime());  
     clock.setTime(23,59);  
     System.out.println(clock.getTime());  
   }  
 }  

Berikut hasilnya setelah dijalankan



Kemudian setelah membuat jam yang sederhana, saya membuat jam yang memiliki sebuah tampilan grafis. Maka dari itu saya membuat class baru yaitu class ClockGUI

  • Class ClockGUI
 /**  
  * Write a description of class ClockGUI here  
  *  
  * @author Bima S. Ramadhan  
  * @version 1.0  
  */   
  import javax.swing.*;  
  import javax.swing.border.*;  
  import java.awt.*;  
  import java.awt.event.*;  
  public class ClockGUI   
  {   
   private JFrame frame;   
   private JLabel label;   
   private ClockDisplay clock;   
   private boolean clockOn = false;   
   private TimerThread timerThread;   
   public void Clock()   
   {   
    makeFrame();   
    clock = new ClockDisplay();   
   }   
   private void start()   
   {   
    clockOn = true;   
    timerThread = new TimerThread();   
    timerThread.start();   
   }   
   private void stop()   
   {   
    clockOn = false;   
   }   
   private void step()   
   {   
    clock.timeTick();   
    label.setText(clock.getTime());   
   }   
   private void showTentang()   
   {   
    JOptionPane.showMessageDialog (frame, "Jam Digital\n" +    
    "Jam digital dengan Bahasa Java.",   
    "Tentang",  
    JOptionPane.INFORMATION_MESSAGE);   
   }   
   private void quit()   
   {   
    System.exit(0);   
   }   
   private void makeFrame()   
   {   
    frame = new JFrame("Jam");   
    JPanel contentPane = (JPanel)frame.getContentPane();   
    contentPane.setBorder(new EmptyBorder(1,60,1,60));   
    makeMenuBar(frame);   
    contentPane.setLayout(new BorderLayout(12,12));   
    label = new JLabel("00:00", SwingConstants.CENTER);   
    Font displayFont = label.getFont().deriveFont(96.0f);   
    label.setFont(displayFont);   
    contentPane.add(label, BorderLayout.CENTER);   
    JPanel toolbar = new JPanel();   
    toolbar.setLayout(new GridLayout(1,0));   
    JButton startButton = new JButton("Start");   
    startButton.addActionListener(e->start());   
    toolbar.add(startButton);   
    JButton stopButton = new JButton("Stop");   
    stopButton.addActionListener(e->stop());   
    toolbar.add(stopButton);   
    JButton stepButton = new JButton("Step");   
    stepButton.addActionListener(e->step());   
    toolbar.add(stepButton);   
    JPanel flow = new JPanel();   
    flow.add(toolbar);   
    contentPane.add(flow, BorderLayout.SOUTH);   
    frame.pack();   
    Dimension d = Toolkit.getDefaultToolkit().getScreenSize();   
    frame.setLocation(d.width/2 - frame.getWidth()/2, d.height/2 - frame.getHeight()/2);   
    frame.setVisible(true);   
   }   
   private void makeMenuBar(JFrame frame)   
   {   
    final int SHORTCUT_MASK =    
    Toolkit.getDefaultToolkit().getMenuShortcutKeyMask();   
    JMenuBar menubar = new JMenuBar();   
    frame.setJMenuBar(menubar);   
    JMenu menu;   
    JMenuItem item;   
    menu = new JMenu("File");   
    menubar.add(menu);   
    item = new JMenuItem("Tentang Jam...");   
     item.addActionListener(e->showTentang());   
    menu.add(item);   
    menu.addSeparator();   
    item = new JMenuItem("Quit");   
     item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q,SHORTCUT_MASK));   
     item.addActionListener(e->quit());   
    menu.add(item);   
   }   
   class TimerThread extends Thread   
   {   
    public void run()   
     {   
      while(clockOn)   
      {   
       step();   
       tunda();   
      }   
     }   
     private void tunda()   
     {   
      try    
      {   
       Thread.sleep(900);   
      }   
      catch(InterruptedException exc)   
      {   
      }   
    }   
   }   
  }  

Tampilan awal setelah program dijalankan adalah seperti di bawah. Kemudian saya klik Start maka waktu akan berjalan

Setelah waktu berjalan kemudian saya tekan Stop dan berhenti
  

Tampilan di BlueJ


Sunday, September 23, 2018

Tugas PBO - Remote AC

10:12 AM Posted by Unknown No comments
Class yang digunakan :

  • RemoteAC Class : berisi fungsi dari remote AC yang buat seperti menaikkan temperatur, menurunkan temperatur, mengganti mode AC, dll
  • ModeAC Class : berisi fungsi dari menu pilihan untuk mode AC
  • mainRemote Class : menghubungkan class mainRemote dan ModeAC atau dengan kata lain fungsi untuk menjalankan Remote AC yang buat

  • Remote AC Class
 /**  
  * Write a description of enum RemoteAC here.  
  *  
  * @author Bima S. Ramadhan  
  * @version 1.0  
  */  
 public class RemoteAC  
 {   
   private static final String SWING_ON = "ON ";  
   private static final String SWING_OFF = "OFF";  
   private static final int MAX_TEMP_LIMIT = 30;  
   private static final int MIN_TEMP_LIMIT = 16;  
   private static final int MAX_SPEED_LIMIT = 3;  
   private static final int MIN_SPEED_LIMIT = 1;  
   private int mTemp, mSpeed;   
   private ModeAC mMode;  
   private boolean mIsSwing;  
   public RemoteAC() {  
     mMode = ModeAC.AUTO;  
     mSpeed = 1;  
     mIsSwing = false;  
     mTemp = 20;  
   }  
   public void tempUp() {  
     if(mTemp == MAX_TEMP_LIMIT) {  
       showError("Temperatur sudah mencapai batas maksimum");  
       return;  
     }  
     mTemp++;  
     showDisplay();  
   }  
   public void tempDown() {  
     if(mTemp == MIN_TEMP_LIMIT) {  
       showError("Temperatur sudah mencapai batas minimum");   
       return;  
     }  
     mTemp--;  
     showDisplay();  
   }  
   public void setMode(int mode){  
     if(mode < 1 || mode > 5) {  
       showError("Input yang anda masukkan salah");  
       return;  
     }  
     mMode = ModeAC.fromInt(mode);  
     showDisplay();  
   }  
   public void speedUp() {  
     if(mSpeed == MAX_SPEED_LIMIT) {  
       showError("Kecepatan sudah mencapai batas maksimum");  
       return;  
     }  
     mSpeed++;  
     showDisplay();  
   }  
   public void speedDown() {  
     if(mSpeed == MIN_SPEED_LIMIT) {  
       showError("Kecepatan sudah mencapai batas minimum");  
       return;  
     }  
     mSpeed--;  
     showDisplay();  
   }  
   public void switchSwing() {  
     mIsSwing = !mIsSwing;  
     showDisplay();  
   }  
   private String getSwingString() {  
     return mIsSwing ? SWING_ON : SWING_OFF;   
   }  
   private void showError(String message) {  
     System.out.println(message);  
   }  
   public void showDisplayMode(){   
     System.out.println(" INI MENU MODE");  
     System.out.println("||  1.AUTO  ||");   
     System.out.println("||  2.COOL  ||");   
     System.out.println("||  3.DRY   ||");   
     System.out.println("||  4.HEAT  ||");  
   }  
   public void showDisplay(){  
     System.out.println("|======================|");   
     System.out.println("|---- INI REMOT AC ----|");   
     System.out.println("|    " + mTemp + " °C     |");   
     System.out.println("|   SPEED  " + mSpeed + "    |");  
     System.out.println("|   MODE = " + mMode.getValueString() +"   |");   
     System.out.println("|   SWING = "+ getSwingString() +"   |");   
     System.out.println("|-------- MENU --------|");   
     System.out.println("|  1.Temperature ↑  |");   
     System.out.println("|  2.Temperature ↓  |");   
     System.out.println("|  3.Ubah Mode    |");   
     System.out.println("|  4.Switch Swing  |");  
     System.out.println("|  5.Fan Speed ↑   |");  
     System.out.println("|  6.Fan Speed ↓   |");  
     System.out.println("|  0.Matikan AC   |");   
     System.out.println("|======================|");   
     System.out.println("|======================|");   
     System.out.println();  
   }  
 }  

  • ModeAC Class
 /**  
  * Write a description of enum ModeAC here.  
  *  
  * @author Bima S. Ramadhan  
  * @version 1.0  
  */  
 public enum ModeAC  
 {  
   AUTO(1, "AUTO"), COOL(2, "COOL"), DRY(3, "DRY"), HEAT(4, "HEAT");  
   private final int mValue;  
   private final String mValueString;  
   ModeAC(int value, String valueString){  
     mValue = value;  
     mValueString = valueString;  
   }  
   public int getValue(){  
     return mValue;  
   }  
   public String getValueString(){  
     return mValueString;  
   }  
   public static ModeAC fromInt(int value) {  
     switch(value) {  
       case 1:  
         return ModeAC.AUTO;  
       case 2:  
         return ModeAC.COOL;  
       case 3:  
         return ModeAC.DRY;  
       case 4:  
         return ModeAC.HEAT;  
       default:  
         return ModeAC.AUTO;  
     }  
   }  
 }  

  • mainRemote Class
 /**  
  * Write a description of enum mainRemote here.  
  *  
  * @author Bima S. Ramadhan  
  * @version 1.0  
  */  
 import java.util.Scanner;  
 public class mainRemote  
 {  
   public static void mainRemote(){  
     Scanner scan = new Scanner(System.in);  
     int input, mode;  
     System.out.println("Masukkan 0 untuk menyalakan AC");  
     RemoteAC remote = new RemoteAC();  
     input = scan.nextInt();  
     while(input != 0) {  
       System.out.println("Masukkan 0 untuk menyalakan AC");  
       input = scan.nextInt();  
     }  
     remote.showDisplay();  
     input = -1;  
     while(input != 0){  
       input = scan.nextInt();  
       switch(input) {  
         case 1:  
           remote.tempUp();  
           break;  
         case 2:  
           remote.tempDown();  
           break;  
         case 3:  
           remote.showDisplayMode();  
           mode = scan.nextInt();  
           remote.setMode(mode);  
           break;  
         case 4:  
           remote.switchSwing();  
           break;  
         case 5:   
           remote.speedUp();  
           break;  
         case 6:  
           remote.speedDown();  
           break;  
       }  
     }  
     System.exit(0);  
   }  
 }  


  • Cara Kerja
  1.  Tampilan awal adalah seperti di bawah. pertama kita harus memasukkan 0 untuk menyalakan AC

  2. Setelah itu akan muncul tampilan remot AC beserta menu-menunya

  3. Apabila ketik 1 maka suhu AC akan naik dari 20°C menjadi 21°C

  4. Apabila kita ketik 2 maka suhu AC turun dan kembali lagi menjadi 20°C

  5. Saat kita masukkan 3 maka akan muncul pilihan mode AC. disini kita masukkan 2 mengubahnya menjadi mode COOL dan kemudia akan muncul di tampilan bahwa mode AC telah berubah menjadi COOL yang sebelumnya adalah AUTO
  6. Untuk mengubah switch menjadi ON maka kita masukkan 4 dan lihat swing yang awalnya OFF menjadi ON

  7. Untuk mempercapat dan memperlambat fan AC bisa kita masukkan 5 atau 6


Tampilan di BLUEJ

Sunday, September 16, 2018

Tugas PBO - Ticket Machine

9:04 PM Posted by Unknown No comments
  • Class TicketMachine
 /**  
  * Ini adalah program Ticket Machine yang bisa menampilkan harga tiket,   
  * sisa saldo, dan tampilan print ticket. apabila uang mencukupi akan ditampilkan  
  * tampilan tiket apabila kurang mencukupi akan ditampilkan kurangnya berapa  
  * yang harus ditambahkan  
  *  
  * @author Bima S. Ramadhan  
  * @version 1.0  
  */  
 public class TicketMachine  
 {  
   private int price; private int balance; private int total;  
   public TicketMachine(int ticketCost)  
   {  
     price=ticketCost; balance = 0; total=0;  
   }  
   public int getPrice()  
   {  
    return price;  
   }  
   public int getBalance()  
   {  
     return balance;  
   }  
   public void insertMoney(int amount)  
   {  
     balance=balance+amount;  
   }  
   public void printTicket()  
   {  
     if(balance>=price){  
       System.out.println("================================");   
       System.out.println("SELAMAT DATANG DI BLUEJ AIRLINES");   
       System.out.println("     HARGA TIKET ");   
       System.out.println("     Rp" + price +",-");   
       System.out.println("================================");   
       System.out.println();  
       balance = balance-price;  
     }  
     else{  
      System.out.println("===================================");  
      System.out.println("Maaf Uang yang anda masukkan kurang");  
      System.out.println("Mohon masukkan Rp"+ (price-balance)+",-");  
      System.out.println("===================================");  
     }  
   }  
 }  
  • Class mainTicket
 /**  
  * Ini adalah program Ticket Machine yang bisa menampilkan harga tiket,   
  * sisa saldo, dan tampilan print ticket. apabila uang mencukupi akan ditampilkan  
  * tampilan tiket apabila kurang mencukupi akan ditampilkan kurangnya berapa  
  * yang harus ditambahkan  
  *  
  * @author Bima S. Ramadhan  
  * @version 1.0  
  */  
 import java.util.Scanner;  
 public class mainTicket  
 {  
    public static int main()  
    {  
      Scanner scan = new Scanner(System.in);  
      int cost,menu;  
      System.out.println("Masukkan harga tiket: ");  
      cost=scan.nextInt();  
      TicketMachine ticket= new TicketMachine(cost);  
      while(true)  
      {  
       System.out.println("1. Get Price");   
       System.out.println("2. Get Balance");   
       System.out.println("3. Insert Money");   
       System.out.println("4. Print Ticket");  
       System.out.println("5. Exit");  
       menu=scan.nextInt();  
       switch(menu)  
       {  
         case 1:   
           System.out.println("Harga tiket : Rp"+ticket.getPrice()+",-");   
           break;  
         case 2:   
           System.out.println("Sisa saldo Anda : Rp"+ticket.getBalance()+",-");   
           break;   
         case 3:   
           int money=scan.nextInt();   
           ticket.insertMoney(money);   
           break;   
         case 4:   
           ticket.printTicket();   
           break;   
         case 5:  
           return 0;  
       }  
     }  
   }  
 }  

  • Hasil Output


Saturday, September 15, 2018

Tugas Rumah PBO - Membuat Gambar Rumah

2:32 PM Posted by Unknown No comments
Untuk project membuat gambar rumah ini, saya menggunakan 6 class yaitu :
  1. Canvas
  2. Picture
  3. Box
  4. Triangle
  5. Circle
  6. Elips
  • Canvas
 import javax.swing.*;  
 import java.awt.*;  
 import java.util.List;  
 import java.util.*;  
 /**  
  * Canvas is a class to allow for simple graphical drawing on a canvas.  
  * This is a modification of the general purpose Canvas, specially made for  
  * the BlueJ "shapes" example.   
  *  
  * @author Bima S. Ramadhan  
  *  
  * @version 1.0  
  */  
 public class Canvas  
 {  
   // Note: The implementation of this class (specifically the handling of  
   // shape identity and colors) is slightly more complex than necessary. This  
   // is done on purpose to keep the interface and instance fields of the  
   // shape objects in this project clean and simple for educational purposes.  
   private static Canvas canvasSingleton;  
   /**  
    * Factory method to get the canvas singleton object.  
    */  
   public static Canvas getCanvas()  
   {  
     if(canvasSingleton == null) {  
       canvasSingleton = new Canvas("BlueJ Shapes Demo", 1000, 800, Color.white);  
     }  
     canvasSingleton.setVisible(true);  
     return canvasSingleton;  
   }  
   // ----- instance part -----  
   private JFrame frame;  
   private CanvasPane canvas;  
   private Graphics2D graphic;  
   private Color backgroundColour;  
   private Image canvasImage;  
   private List objects;  
   private HashMap shapes;  
   /**  
    * Create a Canvas.  
    * @param title title to appear in Canvas Frame  
    * @param width the desired width for the canvas  
    * @param height the desired height for the canvas  
    * @param bgClour the desired background colour of the canvas  
    */  
   private Canvas(String title, int width, int height, Color bgColour)  
   {  
     frame = new JFrame();  
     canvas = new CanvasPane();  
     frame.setContentPane(canvas);  
     frame.setTitle(title);  
     canvas.setPreferredSize(new Dimension(width, height));  
     backgroundColour = bgColour;  
     frame.pack();  
     objects = new ArrayList();  
     shapes = new HashMap();  
   }  
   /**  
    * Set the canvas visibility and brings canvas to the front of screen  
    * when made visible. This method can also be used to bring an already  
    * visible canvas to the front of other windows.  
    * @param visible boolean value representing the desired visibility of  
    * the canvas (true or false)   
    */  
   public void setVisible(boolean visible)  
   {  
     if(graphic == null) {  
       // first time: instantiate the offscreen image and fill it with  
       // the background colour  
       Dimension size = canvas.getSize();  
       canvasImage = canvas.createImage(size.width, size.height);  
       graphic = (Graphics2D)canvasImage.getGraphics();  
       graphic.setColor(backgroundColour);  
       graphic.fillRect(0, 0, size.width, size.height);  
       graphic.setColor(Color.black);  
     }  
     frame.setVisible(visible);  
   }  
   /**  
    * Draw a given shape onto the canvas.  
    * @param referenceObject an object to define identity for this shape  
    * @param color      the color of the shape  
    * @param shape      the shape object to be drawn on the canvas  
    */  
    // Note: this is a slightly backwards way of maintaining the shape  
    // objects. It is carefully designed to keep the visible shape interfaces  
    // in this project clean and simple for educational purposes.  
   public void draw(Object referenceObject, String color, Shape shape)  
   {  
     objects.remove(referenceObject);  // just in case it was already there  
     objects.add(referenceObject);   // add at the end  
     shapes.put(referenceObject, new ShapeDescription(shape, color));  
     redraw();  
   }  
   /**  
    * Erase a given shape's from the screen.  
    * @param referenceObject the shape object to be erased   
    */  
   public void erase(Object referenceObject)  
   {  
     objects.remove(referenceObject);  // just in case it was already there  
     shapes.remove(referenceObject);  
     redraw();  
   }  
   /**  
    * Set the foreground colour of the Canvas.  
    * @param newColour  the new colour for the foreground of the Canvas   
    */  
   public void setForegroundColor(String colorString)  
   {  
     if(colorString.equals("red"))  
       graphic.setColor(Color.red);  
     else if(colorString.equals("black"))  
       graphic.setColor(Color.black);  
     else if(colorString.equals("blue"))  
       graphic.setColor(Color.blue);  
     else if(colorString.equals("yellow"))  
       graphic.setColor(Color.yellow);  
     else if(colorString.equals("green"))  
       graphic.setColor(Color.green);  
     else if(colorString.equals("magenta"))  
       graphic.setColor(Color.magenta);  
     else if(colorString.equals("white"))  
       graphic.setColor(Color.white);  
     else if(colorString.equals("light brown"))  
       graphic.setColor(new Color(153,102,0));  
     else if(colorString.equals("brown"))  
       graphic.setColor(new Color(102,51,0));  
     else if(colorString.equals("grey"))  
       graphic.setColor(new Color(190,190,190));  
     else if(colorString.equals("light blue"))  
       graphic.setColor(new Color(0,191,255));  
     else  
       graphic.setColor(Color.black);  
   }  
   /**  
    * Wait for a specified number of milliseconds before finishing.  
    * This provides an easy way to specify a small delay which can be  
    * used when producing animations.  
    * @param milliseconds the number   
    */  
   public void wait(int milliseconds)  
   {  
     try  
     {  
       Thread.sleep(milliseconds);  
     }   
     catch (Exception e)  
     {  
       // ignoring exception at the moment  
     }  
   }  
   /**  
    * Redraw ell shapes currently on the Canvas.  
    */  
   private void redraw()  
   {  
     erase();  
     for(Iterator i=objects.iterator(); i.hasNext(); ) {  
       ((ShapeDescription)shapes.get(i.next())).draw(graphic);  
     }  
     canvas.repaint();  
   }  
   /**  
    * Erase the whole canvas. (Does not repaint.)  
    */  
   private void erase()  
   {  
     Color original = graphic.getColor();  
     graphic.setColor(backgroundColour);  
     Dimension size = canvas.getSize();  
     graphic.fill(new Rectangle(0, 0, size.width, size.height));  
     graphic.setColor(original);  
   }  
   /************************************************************************  
    * Inner class CanvasPane - the actual canvas component contained in the  
    * Canvas frame. This is essentially a JPanel with added capability to  
    * refresh the image drawn on it.  
    */  
   private class CanvasPane extends JPanel  
   {  
     public void paint(Graphics g)  
     {  
       g.drawImage(canvasImage, 0, 0, null);  
     }  
   }  
   /************************************************************************  
    * Inner class CanvasPane - the actual canvas component contained in the  
    * Canvas frame. This is essentially a JPanel with added capability to  
    * refresh the image drawn on it.  
    */  
   private class ShapeDescription  
   {  
     private Shape shape;  
     private String colorString;  
     public ShapeDescription(Shape shape, String color)  
     {  
       this.shape = shape;  
       colorString = color;  
     }  
     public void draw(Graphics2D graphic)  
     {  
       setForegroundColor(colorString);  
       graphic.fill(shape);  
     }  
   }  
 }  
  • Picture
 /**  
  * This class represents a simple picture. You can draw the picture using  
  * the draw method. But wait, there's more: being an electronic picture, it  
  * can be changed. You can set it to black-and-white display and back to  
  * colors (only after it's been drawn, of course).  
  *  
  * This class was written as an early example for teaching Java with BlueJ.  
  *   
  * @author Bima S. Ramadhan  
  * @version 1.0  
  */  
 public class Picture  
 {  
   private Box wall;  
   private Box door;  
   private Box window;  
   private Triangle roof;  
   private Circle sun;  
   private Box trunk;  
   private Circle leaf;  
   private Elips ground;  
   private Circle handle;  
   private Box road;  
   private Circle cloud;  
   /**  
    * Constructor for objects of class Picture  
    */  
   public Picture()  
   {  
     // nothing to do... instance variables are automatically set to null  
   }  
   /**  
    * Draw this picture.  
    */  
   public void draw()  
   {  
     ground = new Elips();  
     ground.changeColor("light brown");  
     ground.moveHorizontal(-200);  
     ground.moveVertical(520);  
     ground.changeWidth(1400);  
     ground.changeHeight(500);  
     ground.makeVisible();  
     wall = new Box();  
     wall.changeColor("blue");  
     wall.moveVertical(400);  
     wall.moveHorizontal(300);  
     wall.changeWidth(300);  
     wall.changeHeight(200);  
     wall.makeVisible();  
     window = new Box();  
     window.changeColor("black");  
     window.moveHorizontal(516);  
     window.moveVertical(500);  
     window.changeWidth(45);  
     window.changeHeight(35);  
     window.makeVisible();  
     window = new Box();  
     window.changeColor("black");  
     window.moveHorizontal(516);  
     window.moveVertical(440);  
     window.changeWidth(45);  
     window.changeHeight(35);  
     window.makeVisible();  
     window = new Box();  
     window.changeColor("black");  
     window.moveHorizontal(428);  
     window.moveVertical(440);  
     window.changeWidth(45);  
     window.changeHeight(35);  
     window.makeVisible();  
     window = new Box();  
     window.changeColor("black");  
     window.moveHorizontal(340);  
     window.moveVertical(440);  
     window.changeWidth(45);  
     window.changeHeight(35);  
     window.makeVisible();  
     window = new Box();  
     window.changeColor("black");  
     window.moveHorizontal(340);  
     window.moveVertical(500);  
     window.changeWidth(45);  
     window.changeHeight(35);  
     window.makeVisible();  
     door = new Box();  
     door.changeColor("magenta");  
     door.moveHorizontal(426);  
     door.moveVertical(500);  
     door.changeWidth(50);  
     door.changeHeight(100);  
     door.makeVisible();  
     road = new Box();  
     road.changeColor("grey");  
     road.moveHorizontal(410);  
     road.moveVertical(600);  
     road.changeWidth(80);  
     road.changeHeight(200);  
     road.makeVisible();  
     roof = new Triangle();   
     roof.changeColor("red");  
     roof.changeSize(100, 400);  
     roof.moveHorizontal(460);  
     roof.moveVertical(350);  
     roof.makeVisible();  
     sun = new Circle();  
     sun.changeColor("yellow");  
     sun.moveHorizontal(850);  
     sun.moveVertical(-150);  
     sun.changeSize(250);  
     sun.makeVisible();  
     trunk = new Box();  
     trunk.changeColor("brown");  
     trunk.moveHorizontal(730);  
     trunk.moveVertical(320);  
     trunk.changeWidth(50);  
     trunk.changeHeight(265);  
     trunk.makeVisible();  
     leaf = new Circle();  
     leaf.changeColor("green");  
     leaf.moveHorizontal(728);  
     leaf.moveVertical(215);  
     leaf.changeSize(135);  
     leaf.makeVisible();  
     trunk = new Box();  
     trunk.changeColor("brown");  
     trunk.moveHorizontal(850);  
     trunk.moveVertical(380);  
     trunk.changeWidth(50);  
     trunk.changeHeight(265);  
     trunk.makeVisible();  
     leaf = new Circle();  
     leaf.changeColor("green");  
     leaf.moveHorizontal(848);  
     leaf.moveVertical(275);  
     leaf.changeSize(135);  
     leaf.makeVisible();  
     trunk = new Box();  
     trunk.changeColor("brown");  
     trunk.moveHorizontal(150);  
     trunk.moveVertical(320);  
     trunk.changeWidth(50);  
     trunk.changeHeight(265);  
     trunk.makeVisible();  
     leaf = new Circle();  
     leaf.changeColor("green");  
     leaf.moveHorizontal(148);  
     leaf.moveVertical(215);  
     leaf.changeSize(135);  
     leaf.makeVisible();  
     trunk = new Box();  
     trunk.changeColor("brown");  
     trunk.moveHorizontal(30);  
     trunk.moveVertical(380);  
     trunk.changeWidth(50);  
     trunk.changeHeight(265);  
     trunk.makeVisible();  
     leaf = new Circle();  
     leaf.changeColor("green");  
     leaf.moveHorizontal(28);  
     leaf.moveVertical(275);  
     leaf.changeSize(135);  
     leaf.makeVisible();  
     handle = new Circle();  
     handle.changeColor("black");  
     handle.moveHorizontal(500);  
     handle.moveVertical(525);  
     handle.changeSize(20);  
     handle.makeVisible();  
     cloud = new Circle();  
     cloud.changeColor("light blue");  
     cloud.moveHorizontal(180);  
     cloud.moveVertical(80);  
     cloud.changeSize(65);  
     cloud.makeVisible();  
     cloud = new Circle();  
     cloud.changeColor("light blue");  
     cloud.moveHorizontal(50);  
     cloud.moveVertical(80);  
     cloud.changeSize(65);  
     cloud.makeVisible();  
     cloud = new Circle();  
     cloud.changeColor("light blue");  
     cloud.moveHorizontal(100);  
     cloud.moveVertical(50);  
     cloud.changeSize(100);  
     cloud.makeVisible();  
     cloud = new Circle();  
     cloud.changeColor("light blue");  
     cloud.moveHorizontal(480);  
     cloud.moveVertical(120);  
     cloud.changeSize(65);  
     cloud.makeVisible();  
     cloud = new Circle();  
     cloud.changeColor("light blue");  
     cloud.moveHorizontal(350);  
     cloud.moveVertical(120);  
     cloud.changeSize(65);  
     cloud.makeVisible();  
     cloud = new Circle();  
     cloud.changeColor("light blue");  
     cloud.moveHorizontal(400);  
     cloud.moveVertical(90);  
     cloud.changeSize(100);  
     cloud.makeVisible();  
     cloud = new Circle();  
     cloud.changeColor("light blue");  
     cloud.moveHorizontal(740);  
     cloud.moveVertical(30);  
     cloud.changeSize(65);  
     cloud.makeVisible();  
     cloud = new Circle();  
     cloud.changeColor("light blue");  
     cloud.moveHorizontal(610);  
     cloud.moveVertical(30);  
     cloud.changeSize(65);  
     cloud.makeVisible();  
     cloud = new Circle();  
     cloud.changeColor("light blue");  
     cloud.moveHorizontal(660);  
     cloud.moveVertical(0);  
     cloud.changeSize(100);  
     cloud.makeVisible();  
   }  
 }  
  • Box
 import java.awt.*;  
 /**  
  * A Box that can be manipulated and that draws itself on a canvas.  
  *   
  * @author Bima S. Ramadhan  
  * @version 1.0   
  */  
 public class Box  
 {  
   private int width;  
   private int height;  
   private int xPosition;  
   private int yPosition;  
   private String color;  
   private boolean isVisible;  
   /**  
    * Create a new Box at default position with default color.  
    */  
   public Box()  
   {  
     width = 30;  
     height = 30;  
     xPosition = 60;  
     yPosition = 50;  
     color = "red";  
     isVisible = false;  
   }  
   /**  
    * Make this Box visible. If it was already visible, do nothing.  
    */  
   public void makeVisible()  
   {  
     isVisible = true;  
     draw();  
   }  
   /**  
    * Make this Box invisible. If it was already invisible, do nothing.  
    */  
   public void makeInvisible()  
   {  
     erase();  
     isVisible = false;  
   }  
   /**  
    * Move the Box a few pixels to the right.  
    */  
   public void moveRight()  
   {  
     moveHorizontal(20);  
   }  
   /**  
    * Move the Box a few pixels to the left.  
    */  
   public void moveLeft()  
   {  
     moveHorizontal(-20);  
   }  
   /**  
    * Move the Box a few pixels up.  
    */  
   public void moveUp()  
   {  
     moveVertical(-20);  
   }  
   /**  
    * Move the Box a few pixels down.  
    */  
   public void moveDown()  
   {  
     moveVertical(20);  
   }  
   /**  
    * Move the Box horizontally by 'distance' pixels.  
    */  
   public void moveHorizontal(int distance)  
   {  
     erase();  
     xPosition += distance;  
     draw();  
   }  
   /**  
    * Move the Box vertically by 'distance' pixels.  
    */  
   public void moveVertical(int distance)  
   {  
     erase();  
     yPosition += distance;  
     draw();  
   }  
   /**  
    * Slowly move the Box horizontally by 'distance' pixels.  
    */  
   public void slowMoveHorizontal(int distance)  
   {  
     int delta;  
     if(distance < 0)   
     {  
       delta = -1;  
       distance = -distance;  
     }  
     else   
     {  
       delta = 1;  
     }  
     for(int i = 0; i < distance; i++)  
     {  
       xPosition += delta;  
       draw();  
     }  
   }  
   /**  
    * Slowly move the Box vertically by 'distance' pixels.  
    */  
   public void slowMoveVertical(int distance)  
   {  
     int delta;  
     if(distance < 0)   
     {  
       delta = -1;  
       distance = -distance;  
     }  
     else   
     {  
       delta = 1;  
     }  
     for(int i = 0; i < distance; i++)  
     {  
       yPosition += delta;  
       draw();  
     }  
   }  
   /**  
    * Change the width to the new width (in pixels). Width must be >= 0.  
    */  
   public void changeWidth(int newWidth)  
   {  
     erase();  
     width = newWidth;  
     draw();  
   }  
   /**  
    * Change the height to the new height (in pixels). Height must be >= 0.  
    */  
   public void changeHeight(int newHeight)  
   {  
     erase();  
     height = newHeight;  
     draw();  
   }  
   /**  
    * Change the color. Valid colors are "red", "yellow", "blue", "green",  
    * "magenta" and "black".  
    */  
   public void changeColor(String newColor)  
   {  
     color = newColor;  
     draw();  
   }  
   /*  
    * Draw the Box with current specifications on screen.  
    */  
   private void draw()  
   {  
     if(isVisible) {  
       Canvas canvas = Canvas.getCanvas();  
       canvas.draw(this, color, new Rectangle(xPosition, yPosition, width, height));  
       canvas.wait(10);  
     }  
   }  
   /*  
    * Erase the Box on screen.  
    */  
   private void erase()  
   {  
     if(isVisible) {  
                Canvas canvas = Canvas.getCanvas();  
                canvas.erase(this);  
           }  
      }  
 }  
  • Triangle
 import java.awt.*;  
 /**  
  * A triangle that can be manipulated and that draws itself on a canvas.  
  *   
  * @author Bima S. Ramadhan  
  * @version 1.0   
  */  
 public class Triangle  
 {  
   private int height;  
   private int width;  
   private int xPosition;  
   private int yPosition;  
   private String color;  
   private boolean isVisible;  
   /**  
    * Create a new triangle at default position with default color.  
    */  
   public Triangle()  
   {  
     height = 30;  
     width = 40;  
     xPosition = 50;  
     yPosition = 15;  
     color = "green";  
     isVisible = false;  
   }  
   /**  
    * Make this triangle visible. If it was already visible, do nothing.  
    */  
   public void makeVisible()  
   {  
     isVisible = true;  
     draw();  
   }  
   /**  
    * Make this triangle invisible. If it was already invisible, do nothing.  
    */  
   public void makeInvisible()  
   {  
     erase();  
     isVisible = false;  
   }  
   /**  
    * Move the triangle a few pixels to the right.  
    */  
   public void moveRight()  
   {  
     moveHorizontal(20);  
   }  
   /**  
    * Move the triangle a few pixels to the left.  
    */  
   public void moveLeft()  
   {  
     moveHorizontal(-20);  
   }  
   /**  
    * Move the triangle a few pixels up.  
    */  
   public void moveUp()  
   {  
     moveVertical(-20);  
   }  
   /**  
    * Move the triangle a few pixels down.  
    */  
   public void moveDown()  
   {  
     moveVertical(20);  
   }  
   /**  
    * Move the triangle horizontally by 'distance' pixels.  
    */  
   public void moveHorizontal(int distance)  
   {  
     erase();  
     xPosition += distance;  
     draw();  
   }  
   /**  
    * Move the triangle vertically by 'distance' pixels.  
    */  
   public void moveVertical(int distance)  
   {  
     erase();  
     yPosition += distance;  
     draw();  
   }  
   /**  
    * Slowly move the triangle horizontally by 'distance' pixels.  
    */  
   public void slowMoveHorizontal(int distance)  
   {  
     int delta;  
     if(distance < 0)   
     {  
       delta = -1;  
       distance = -distance;  
     }  
     else   
     {  
       delta = 1;  
     }  
     for(int i = 0; i < distance; i++)  
     {  
       xPosition += delta;  
       draw();  
     }  
   }  
   /**  
    * Slowly move the triangle vertically by 'distance' pixels.  
    */  
   public void slowMoveVertical(int distance)  
   {  
     int delta;  
     if(distance < 0)   
     {  
       delta = -1;  
       distance = -distance;  
     }  
     else   
     {  
       delta = 1;  
     }  
     for(int i = 0; i < distance; i++)  
     {  
       yPosition += delta;  
       draw();  
     }  
   }  
   /**  
    * Change the size to the new size (in pixels). Size must be >= 0.  
    */  
   public void changeSize(int newHeight, int newWidth)  
   {  
     erase();  
     height = newHeight;  
     width = newWidth;  
     draw();  
   }  
   /**  
    * Change the color. Valid colors are "red", "yellow", "blue", "green",  
    * "magenta" and "black".  
    */  
   public void changeColor(String newColor)  
   {  
     color = newColor;  
     draw();  
   }  
   /*  
    * Draw the triangle with current specifications on screen.  
    */  
   private void draw()  
   {  
     if(isVisible) {  
       Canvas canvas = Canvas.getCanvas();  
       int[] xpoints = { xPosition, xPosition + (width/2), xPosition - (width/2) };  
       int[] ypoints = { yPosition, yPosition + height, yPosition + height };  
       canvas.draw(this, color, new Polygon(xpoints, ypoints, 3));  
       canvas.wait(10);  
     }  
   }  
   /*  
    * Erase the triangle on screen.  
    */  
   private void erase()  
   {  
     if(isVisible) {  
       Canvas canvas = Canvas.getCanvas();  
       canvas.erase(this);  
     }  
   }  
 }  
  • Circle
 import java.awt.*;  
 import java.awt.geom.*;  
 /**  
  * A circle that can be manipulated and that draws itself on a canvas.  
  *   
  * @author Bima S. Ramadhan  
  * @version 1.0   
  */  
 public class Circle  
 {  
   private int diameter;  
      private int xPosition;  
      private int yPosition;  
      private String color;  
      private boolean isVisible;  
   /**  
    * Create a new circle at default position with default color.  
    */  
   public Circle()  
   {  
           diameter = 30;  
           xPosition = 20;  
           yPosition = 60;  
           color = "blue";  
           isVisible = false;  
   }  
      /**  
       * Make this circle visible. If it was already visible, do nothing.  
       */  
      public void makeVisible()  
      {  
           isVisible = true;  
           draw();  
      }  
      /**  
       * Make this circle invisible. If it was already invisible, do nothing.  
       */  
      public void makeInvisible()  
      {  
           erase();  
           isVisible = false;  
      }  
   /**  
    * Move the circle a few pixels to the right.  
    */  
   public void moveRight()  
   {  
           moveHorizontal(20);  
   }  
   /**  
    * Move the circle a few pixels to the left.  
    */  
   public void moveLeft()  
   {  
           moveHorizontal(-20);  
   }  
   /**  
    * Move the circle a few pixels up.  
    */  
   public void moveUp()  
   {  
           moveVertical(-20);  
   }  
   /**  
    * Move the circle a few pixels down.  
    */  
   public void moveDown()  
   {  
           moveVertical(20);  
   }  
   /**  
    * Move the circle horizontally by 'distance' pixels.  
    */  
   public void moveHorizontal(int distance)  
   {  
           erase();  
           xPosition += distance;  
           draw();  
   }  
   /**  
    * Move the circle vertically by 'distance' pixels.  
    */  
   public void moveVertical(int distance)  
   {  
           erase();  
           yPosition += distance;  
           draw();  
   }  
   /**  
    * Slowly move the circle horizontally by 'distance' pixels.  
    */  
   public void slowMoveHorizontal(int distance)  
   {  
           int delta;  
           if(distance < 0)   
           {  
                delta = -1;  
                distance = -distance;  
           }  
           else   
           {  
                delta = 1;  
           }  
           for(int i = 0; i < distance; i++)  
           {  
                xPosition += delta;  
                draw();  
           }  
   }  
   /**  
    * Slowly move the circle vertically by 'distance' pixels.  
    */  
   public void slowMoveVertical(int distance)  
   {  
           int delta;  
           if(distance < 0)   
           {  
                delta = -1;  
                distance = -distance;  
           }  
           else   
           {  
                delta = 1;  
           }  
           for(int i = 0; i < distance; i++)  
           {  
                yPosition += delta;  
                draw();  
           }  
   }  
   /**  
    * Change the size to the new size (in pixels). Size must be >= 0.  
    */  
   public void changeSize(int newDiameter)  
   {  
           erase();  
           diameter = newDiameter;  
           draw();  
   }  
   /**  
    * Change the color. Valid colors are "red", "yellow", "blue", "green",  
       * "magenta" and "black".  
    */  
   public void changeColor(String newColor)  
   {  
           color = newColor;  
           draw();  
   }  
      /*  
       * Draw the circle with current specifications on screen.  
       */  
      private void draw()  
      {  
           if(isVisible) {  
                Canvas canvas = Canvas.getCanvas();  
                canvas.draw(this, color, new Ellipse2D.Double(xPosition, yPosition, diameter, diameter));  
                canvas.wait(10);  
           }  
      }  
      /*  
       * Erase the circle on screen.  
       */  
      private void erase()  
      {  
           if(isVisible) {  
                Canvas canvas = Canvas.getCanvas();  
                canvas.erase(this);  
           }  
      }  
 }  
  • Elips
 import java.awt.*;  
 import java.awt.geom.*;  
 /**  
  * An Elips that can be manipulated and that draws itself on a canvas.  
  *   
  * @author Bima S. Ramadhan  
  * @version 1.0   
  */  
 public class Elips  
 {  
   private int width;  
   private int height;  
   private int xPosition;  
   private int yPosition;  
   private String color;  
   private boolean isVisible;  
   /**  
    * Create a new Elips at default position with default color.  
    */  
   public Elips()  
   {  
     width = 30;  
     height = 30;  
     xPosition = 20;  
     yPosition = 60;  
     color = "blue";  
     isVisible = false;  
   }  
   /**  
    * Make this Elips visible. If it was already visible, do nothing.  
    */  
   public void makeVisible()  
   {  
     isVisible = true;  
     draw();  
   }  
   /**  
    * Make this Elips invisible. If it was already invisible, do nothing.  
    */  
   public void makeInvisible()  
   {  
     erase();  
     isVisible = false;  
   }  
   /**  
    * Move the Elips a few pixels to the right.  
    */  
   public void moveRight()  
   {  
     moveHorizontal(20);  
   }  
   /**  
    * Move the Elips a few pixels to the left.  
    */  
   public void moveLeft()  
   {  
     moveHorizontal(-20);  
   }  
   /**  
    * Move the Elips a few pixels up.  
    */  
   public void moveUp()  
   {  
     moveVertical(-20);  
   }  
   /**  
    * Move the Elips a few pixels down.  
    */  
   public void moveDown()  
   {  
     moveVertical(20);  
   }  
   /**  
    * Move the Elips horizontally by 'distance' pixels.  
    */  
   public void moveHorizontal(int distance)  
   {  
     erase();  
     xPosition += distance;  
     draw();  
   }  
   /**  
    * Move the Elips vertically by 'distance' pixels.  
    */  
   public void moveVertical(int distance)  
   {  
     erase();  
     yPosition += distance;  
     draw();  
   }  
   /**  
    * Slowly move the Elips horizontally by 'distance' pixels.  
    */  
   public void slowMoveHorizontal(int distance)  
   {  
     int delta;  
     if(distance < 0)   
     {  
       delta = -1;  
       distance = -distance;  
     }  
     else   
     {  
       delta = 1;  
     }  
     for(int i = 0; i < distance; i++)  
     {  
       xPosition += delta;  
       draw();  
     }  
   }  
   /**  
    * Slowly move the Elips vertically by 'distance' pixels.  
    */  
   public void slowMoveVertical(int distance)  
   {  
     int delta;  
     if(distance < 0)   
     {  
       delta = -1;  
       distance = -distance;  
     }  
     else   
     {  
       delta = 1;  
     }  
     for(int i = 0; i < distance; i++)  
     {  
       yPosition += delta;  
       draw();  
     }  
   }  
   /**  
    * Change the width to the new width (in pixels). Width must be >= 0.  
    */  
   public void changeWidth(int newWidth)  
   {  
     erase();  
     width = newWidth;  
     draw();  
   }  
   /**  
    * Change the height to the new height (in pixels). Height must be >= 0.  
    */  
   public void changeHeight(int newHeight)  
   {  
     erase();  
     height = newHeight;  
     draw();  
   }  
   /**  
    * Change the color. Valid colors are "red", "yellow", "blue", "green",  
    * "magenta" and "black".  
    */  
   public void changeColor(String newColor)  
   {  
     color = newColor;  
     draw();  
   }  
   /*  
    * Draw the Elips with current specifications on screen.  
    */  
   private void draw()  
   {  
     if(isVisible) {  
       Canvas canvas = Canvas.getCanvas();  
       canvas.draw(this, color, new Ellipse2D.Double(xPosition, yPosition, width, height));  
       canvas.wait(10);  
     }  
   }  
   /*  
    * Erase the Elips on screen.  
    */  
   private void erase()  
   {  
     if(isVisible) {  
       Canvas canvas = Canvas.getCanvas();  
       canvas.erase(this);  
     }  
   }  
 }  

  • Gambar

tampilan pada BlueJ

hasil gambar setelah program dijalankan