22 September 2018

Tugas Membuat Remote TV PBO B

Kali ini saya akan membuat simulasi pengoperasian remote TV dalam bahasa Java.

Hanya ada 2 class yang diperlukan, yaitu class Main dan class RemoteTV.

Source code ada di bawah ini.

1. Main

 import java.util.Scanner;    
 /**   
  * Fungsi main.   
  *   
  * @author (Mohammad Nafis Naufally/05111640000038)   
  * @version (1/20180921)   
  */   
 public class Main   
 {   
   public static void main(String args[])  
   {    
     Scanner scan= new Scanner(System.in);    
     int totalvolume, totalchannel, menu;    
     boolean entry;    
     System.out.println("This is your new TV.");  
     System.out.println("#############################");   
     System.out.println("#              #");   
     System.out.println("#     SAMSUNG     #");   
     System.out.println("#              #");  
     System.out.println("#############################");  
     System.out.println("Adjust Volume: ");    
     totalvolume = scan.nextInt();     
     System.out.println("Adjust Channel Number: ");    
     totalchannel = scan.nextInt();    
     RemoteTV KS = new RemoteTV(totalvolume,totalchannel);    
     entry = true;    
     while(entry) {    
       System.out.println("Menu:");    
       System.out.println("1. Check Volume");    
       System.out.println("2. Watch TV");    
       System.out.println("3. Volume Up");    
       System.out.println("4. Volume Down");    
       System.out.println("5. Channel Up");   
       System.out.println("6. Channel Down");   
       System.out.println("7. Exit");    
       System.out.println("Input: ");   
       menu=scan.nextInt();     
       switch(menu) {    
         case 1:    
         totalvolume=KS.GetVolume();    
         System.out.println("Volume: "+ totalvolume);    
         System.out.println("");   
         break;    
           
         case 2:    
         totalchannel = KS.GetChannel();   
         if(totalchannel == 1) {   
           System.out.println("#############################");   
           System.out.println("#NET.           " + totalchannel + "#");   
           System.out.println("#              #");    
           System.out.println("#              #");  
           System.out.println("#############################");  
         }   
         else if(totalchannel == 2) {   
           System.out.println("#############################");   
           System.out.println("#TransTV          " + totalchannel + "#");  
           System.out.println("#              #");    
           System.out.println("#              #");  
           System.out.println("#############################");   
         }   
         else if(totalchannel == 3) {   
           System.out.println("#############################");   
           System.out.println("#Trans7          " + totalchannel + "#");  
           System.out.println("#              #");    
           System.out.println("#              #");  
           System.out.println("#############################");   
         }  
           else if(totalchannel == 4) {   
           System.out.println("#############################");   
           System.out.println("#TVRI           " + totalchannel + "#");  
           System.out.println("#              #");    
           System.out.println("#              #");  
           System.out.println("#############################");   
         }   
           else if(totalchannel == 5) {   
           System.out.println("#############################");   
           System.out.println("#MNCTV           " + totalchannel + "#");  
           System.out.println("#              #");    
           System.out.println("#              #");  
           System.out.println("#############################");   
         }   
         else {   
           System.out.println("#############################");   
           System.out.println("#(Channel doesn't exist)  " + totalchannel + "#");   
           System.out.println("#              #");  
           System.out.println("#              #");   
           System.out.println("#############################");  
         }   
         break;    
           
         case 3:    
         KS.TambahVolume();   
         System.out.println("");   
         break;    
           
         case 4:    
         KS.KurangVolume();    
         System.out.println("");   
         break;    
   
         case 5:    
         totalchannel = KS.TambahChannel();    
         if(totalchannel == 1) {   
           System.out.println("#############################");   
           System.out.println("#NET.           " + totalchannel + "#");  
           System.out.println("#              #");  
           System.out.println("#              #");  
           System.out.println("#############################");  
         }   
         else if(totalchannel == 2) {   
           System.out.println("#############################");   
           System.out.println("#TransTV          " + totalchannel + "#");  
           System.out.println("#              #");  
           System.out.println("#              #");   
           System.out.println("#############################");  
         }   
         else if(totalchannel == 3) {   
           System.out.println("#############################");   
           System.out.println("#Trans7          " + totalchannel + "#");  
           System.out.println("#              #");    
           System.out.println("#              #");  
           System.out.println("#############################");   
         }  
           else if(totalchannel == 4) {   
           System.out.println("#############################");   
           System.out.println("#TVRI           " + totalchannel + "#");  
           System.out.println("#              #");    
           System.out.println("#              #");  
           System.out.println("#############################");   
         }   
           else if(totalchannel == 5) {   
           System.out.println("#############################");   
           System.out.println("#MNCTV           " + totalchannel + "#");  
           System.out.println("#              #");    
           System.out.println("#              #");  
           System.out.println("#############################");   
         }   
         else {   
           System.out.println("#############################");   
           System.out.println("#(Channel doesn't exist)  " + totalchannel + "#");   
           System.out.println("#              #");   
           System.out.println("#              #");  
           System.out.println("#############################");  
         }   
         System.out.println("");   
         break;    
   
         case 6:    
         totalchannel = KS.KurangChannel();    
         if(totalchannel == 1) {   
           System.out.println("#############################");   
           System.out.println("#NET.           " + totalchannel + "#");  
           System.out.println("#              #");   
           System.out.println("#              #");   
           System.out.println("#############################");  
         }   
         else if(totalchannel == 2) {   
           System.out.println("#############################");  
           System.out.println("#TransTV          " + totalchannel + "#");  
           System.out.println("#              #");  
           System.out.println("#              #");  
           System.out.println("#############################");  
         }   
         else if(totalchannel == 3) {   
           System.out.println("#############################");   
           System.out.println("#Trans7          " + totalchannel + "#");  
           System.out.println("#              #");    
           System.out.println("#              #");  
           System.out.println("#############################");   
         }  
           else if(totalchannel == 4) {   
           System.out.println("#############################");   
           System.out.println("#TVRI           " + totalchannel + "#");  
           System.out.println("#              #");    
           System.out.println("#              #");  
           System.out.println("#############################");   
         }   
           else if(totalchannel == 5) {   
           System.out.println("#############################");   
           System.out.println("#MNCTV           " + totalchannel + "#");  
           System.out.println("#              #");    
           System.out.println("#              #");  
           System.out.println("#############################");   
         }   
         else {   
           System.out.println("#############################");  
           System.out.println("#(Channel doesn't exist)  " + totalchannel + "#");   
           System.out.println("#              #");  
           System.out.println("#              #");  
           System.out.println("#############################");  
         }   
         System.out.println("");   
         break;    
   
         case 7:    
         System.out.println("#############################");   
         System.out.println("#              #");   
         System.out.println("#     SAMSUNG     #");   
         System.out.println("#              #");  
         System.out.println("#############################");  
         System.out.println("TV Off");  
         entry = false;    
         break;    
       }    
     }    
   }    
 }   


2. RemoteTV


 import java.util.Scanner;    
 /**   
  * Mekanisme pengoperasian remote TV.  
  *   
  * @author (Mohammad Nafis Naufally/05111640000038)   
  * @version (1/20180921)   
  */   
 public class RemoteTV   
 {   
   private int volume;   
   private int channel;   
   public RemoteTV(int totalvolume,int totalchannel) {   
     volume = totalvolume;   
     channel = totalchannel;   
   }   
   public int GetVolume() {   
     return volume;   
   }   
   public int GetChannel() {   
     return channel;   
   }   
   public void TambahVolume() {   
     if(volume == 100) {   
       System.out.println("Maximum Volume");   
     }   
     else {   
       volume = volume + 1;   
       System.out.println("Volume: " + volume);   
     }   
   }   
   public void KurangVolume() {   
     if(volume == 0) {   
       System.out.println("Minimum Volume");   
     }   
     else {   
       volume = volume - 1;   
       System.out.println("Volume: " + volume);   
     }   
   }   
   public int TambahChannel() {   
     channel = channel + 1;   
     return channel;   
   }   
   public int KurangChannel() {   
     channel = channel - 1;   
     return channel;   
   }   
 }   

Setelah di-compile, jalankan fungsi pada class Main. Maka muncul terminal BlueJ, dan ikuti instruksi yang ada.

Hasilnya bisa dilihat di screenshot berikut.






Demikian penerapan simulasi pengoperasian remote TV menggunakan Java. Terima kasih atas kunjungannya.




Jangan lupa, semangat ketik 10 jari. :)

17 September 2018

Tugas 3 PBO B

Pada saat ini, saya akan membuat simulasi mesin tiket kereta sederhana yang terinspirasi dari banyaknya mesin tiket kereta di negara Jepang. Seperti biasa, saya akan menggunakan bahasa Java dalam IDE BlueJ untuk membuatnya.

Hanya ada 2 class yang diperlukan, yaitu class Main dan class TicketMachine.

Berikut ini adalah source codenya.

1. Main

 import java.util.Scanner;  
 /**  
  * Fungsi main.  
  *  
  * @author (Mohammad Nafis Naufally/05111640000038)  
  * @version (1/20180917)  
  */  
 public class IntMain   
 {   
   public static void main(String args[])   
   {   
     Scanner scan= new Scanner(System.in);   
     int cost,menu;   
     System.out.println("SET THE TICKET PRICE \n");   
     cost=scan.nextInt();   
     TicketMachine ticket=new TicketMachine(cost);  
       
     menu=0;  
     while(menu!=5)  
     {  
       System.out.println("");  
       System.out.println("Select Action:");  
       System.out.println("1. CHECK PRICE");   
       System.out.println("2. CHECK YOUR 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:   
         cost=ticket.getPrice();   
         System.out.println(cost);   
         break;   
         case 2:   
         int balance=ticket.getBalance();  
         System.out.println(balance);  
         break;   
         case 3:   
         int money=scan.nextInt();   
         ticket.insertMoney(money);   
         break;   
         case 4:   
         ticket.printTicket();   
         break;   
         case 5:  
         System.out.println("Thank you for using our services. Good bye.");  
         break;  
       }   
     }  
   }   
 }

2. TicketMachine
   
 /**  
  * Mengimplementasikan cara kerja mesin tiket.  
  *  
  * @author (Mohammad Nafis Naufally/05111640000038)  
  * @version (1/20180917)  
  */  
 public class TicketMachine  
 {  
    // The price of a ticket from this machine.   
    private int price;   
    // The amount of money entered by a customer so far.   
    private int balance;   
    // The total amount of money collected by this machine.   
    private int total;   
    /**   
    * Create a machine that issues tickets of the given price.   
    * Note that the price must be greater than zero, and there are no checks to ensure this.   
    */   
    public TicketMachine(int ticketCost)   
    {   
      if(ticketCost > 0)  
      {  
        price = ticketCost;   
      }  
      else  
      {  
        price = 100;  
        System.out.println("Price set to " + price + " as default price.");  
      }    
      balance = 0;   
      total = 0;   
    }   
    /**   
    * Return the price of a ticket.   
    */   
    public int getPrice()   
    {   
      return price;   
    }   
    /**   
    * Return the amount of money already inserted for the next ticket.   
    */   
    public int getBalance()   
    {   
      return balance;   
    }   
    /**  
    * Set balance manually.  
    */  
    public void setBalance(int amount)  
    {  
      if (amount > 0)   
      {  
        balance = amount;  
      }  
    }     
    /**   
    * Receive an amount of money in cents from a customer.   
    */   
    public void insertMoney(int amount)   
    {   
      if(amount > 0)  
      {  
        balance = balance + amount;   
      }  
      else  
      {  
        System.out.println("Use a positive amount. You cannot insert " + amount + " cents.");  
      }  
    }   
    /**   
    * Print a ticket.   
    * Update the total collected and reduce the balance to zero.   
    */   
    public void printTicket()   
    {   
      if(balance >= price)   
      {  
        // Simulate the printing of a ticket.   
        System.out.println("##################");   
        System.out.println("# The BlueJ Line #");   
        System.out.println("#   Ticket   #");   
        System.out.println("#  " + price + " cents.  #");   
        System.out.println("##################");   
        System.out.println();   
          
        // Update the total collected with the price.   
        total = total + price;   
        // Reduce the balance by the price.   
        balance = balance - price;   
      }  
      else {  
       System.out.println("You must insert at least: " + (price - balance) + " more cents.");  
      }  
    }   
 }   

Setelah di-compile, jalankan fungsi main pada class Main. Maka akan muncul simulasi pemesanan tiket pada terminal BlueJ.

Lebih jelasnya bisa disaksikan melalui video berikut.


Atau bisa dilihat dari screenshot di bawah ini.





Demikian tugas ketiga saya pada mata kuliah Pemrograman Berbasis Objek. Terima kasih atas kunjungannya.



Jangan lupa, semangat ketik 10 jari. :)

Tugas Menggambar Pemandangan PBO B

Pada kesempatan ini, saya akan membuat gambar sederhana yang terdiri dari bangun 2 dimensi. Saya akan menggunakan bahasa Java untuk menampilkannya.

Ada 5 Class yang diperlukan untuk mengerjakan proyek ini, yaitu:
  1. Canvas
  2. Square
  3. Circle
  4. Triangle
  5. Picture

Berikut ini adalah source codenya.



CANVAS

 import javax.swing.*;   
 import java.awt.*;   
 import java.util.List;   
 import java.util.*;   
 /**  
  * Class yang digunakan sebagai media untuk menggambar.  
  *  
  * @author (Mohammad Nafis Naufally/05111640000038)  
  * @version (1/20180916)  
  */  
 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 Picture Demo", 500, 300, Color.white);   
    }   
    canvasSingleton.setVisible(true);   
    return canvasSingleton;  
    }   
    // ----- instance part -----   
    private JFrame frame;   
    private CanvasPane canvas;   
    private Graphics2D graphic;   
    private Color backgroundColor;   
    private Image canvasImage;   
    private List<Object> objects;   
    private HashMap<Object, ShapeDescription> 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 bgColor the desired background color of the canvas   
    */   
    private Canvas(String title, int width, int height, Color bgColor)   
    {   
      frame = new JFrame();   
      canvas = new CanvasPane();   
      frame.setContentPane(canvas);   
      frame.setTitle(title);   
      frame.setLocation(30, 30);   
      canvas.setPreferredSize(new Dimension(width, height));   
      backgroundColor = bgColor;   
      frame.pack();   
      objects = new ArrayList<Object>();   
      shapes = new HashMap<Object, ShapeDescription>();   
    }   
    /**   
    * 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 color   
        Dimension size = canvas.getSize();   
        canvasImage = canvas.createImage(size.width, size.height);   
        graphic = (Graphics2D)canvasImage.getGraphics();   
        graphic.setColor(backgroundColor);   
        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 color of the Canvas.   
    * @param newColor the new color for the foreground of the Canvas    
    */   
    public void setForegroundColor(String colorString)   
    {   
    if(colorString.equals("red")) {   
     graphic.setColor(new Color(235,25,25));   
    }  
    else if(colorString.equals("cyan")) {  
      graphic.setColor(new Color(0,255,255));  
    }  
    else if(colorString.equals("blue")) {   
     graphic.setColor(new Color(0,0,255));   
    }   
    else if(colorString.equals("very dark blue")) {   
     graphic.setColor(new Color(30,75,153));   
    }   
    else if(colorString.equals("yellow")) {   
     graphic.setColor(new Color(255, 204, 0));   
    }   
    else if(colorString.equals("light green")) {   
     graphic.setColor(new Color(0,255,51));   
    }   
    else if(colorString.equals("green")) {   
     graphic.setColor(new Color(80, 160, 60));   
    }   
    else if(colorString.equals("brown")) {   
     graphic.setColor(new Color(102,51,0));   
    }   
    else if(colorString.equals("white")) {   
     graphic.setColor(Color.white);   
    }   
    else if(colorString.equals("purple")){   
     graphic.setColor(new Color(102,0,153));   
    }   
    else if(colorString.equals("black")) {   
     graphic.setColor(Color.black);   
    }   
    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(Object shape : objects) {   
       shapes.get(shape).draw(graphic);   
     }   
     canvas.repaint();   
   }   
   /**   
    * Erase the whole canvas. (Does not repaint.)   
    */   
   private void erase()   
   {   
     Color original = graphic.getColor();   
     graphic.setColor(backgroundColor);   
     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);   
     }   
   }   
 }  


SQUARE
 import java.awt.*;    
 /**  
  * Class untuk mendefinisikan bentuk persegi.  
  *  
  * @author (Mohammad Nafis Naufally/05111640000038)  
  * @version (1/20180916)  
  */  
 public class Square  
 {  
   private int size;    
   private int xPosition;    
   private int yPosition;    
   private String color;    
   private boolean isVisible;    
   /**    
   * Create a new square at default position with default color.    
   */    
   public Square()    
   {    
     size = 125;    
     xPosition = 50;    
     yPosition = 50;    
     color = "green";    
     isVisible = false;    
   }    
   /**    
   * Make this square visible. If it was already visible, do nothing.    
   */    
   public void makeVisible()    
   {    
     isVisible = true;    
     draw();    
   }    
   /**    
   * Make this square invisible. If it was already invisible, do nothing.    
   */    
   public void makeInvisible()    
   {    
     erase();    
     isVisible = false;    
   }    
   /**    
   * Move the square a few pixels to the right.    
   */    
   public void moveRight()    
   {    
     moveHorizontal(20);    
   }    
   /**    
   * Move the square a few pixels to the left.    
   */    
   public void moveLeft()    
   {    
     moveHorizontal(-20);    
   }    
   /**    
   * Move the square a few pixels up.    
   */    
   public void moveUp()    
   {    
     moveVertical(20);    
   }    
   /**    
   * Move the square a few pixels down.    
   */    
   public void moveDown()    
   {    
     moveVertical(-20);    
   }    
   /**    
   * Move the square horizontally by 'distance' pixels.    
   */    
   public void moveHorizontal(int distance)    
   {    
     erase();    
     xPosition += distance;    
     draw();    
   }    
   /**    
   * Move the square vertically by 'distance' pixels.    
   */    
   public void moveVertical(int distance)    
   {    
     erase();    
     yPosition += distance;    
     draw();    
   }    
   /**    
   * Slowly move the square 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 square 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 newSize)    
   {    
     erase();    
     size = newSize;    
     draw();    
   }    
   /**    
   * Change the color. Valid colors are "red", "yellow", "blue", "green",    
   * "magenta" and "black".    
   */    
   public void changeColor(String newColor)    
   {    
     color = newColor;    
     draw();    
   }    
   /*    
   * Draw the square with current specifications on screen.    
   */    
   private void draw()    
   {    
     if(isVisible) {    
       Canvas canvas = Canvas.getCanvas();    
       canvas.draw(this, color,    
       new Rectangle(xPosition, yPosition, size, size));    
       canvas.wait(10);    
     }    
   }    
   /*    
   * Erase the square on screen.    
   */    
   private void erase()    
   {    
     if(isVisible) {    
       Canvas canvas = Canvas.getCanvas();    
       canvas.erase(this);    
     }    
   }    
 }  

CIRCLE
 import java.awt.*;    
 import java.awt.geom.*;   
 /**  
  * Class untuk mendefinisikan bentuk lingkaran.  
  *  
  * @author (Mohammad Nafis Naufally/05111640000038)  
  * @version (1/20180916)  
  */  
 public class Circle  
 {  
   private double diameter;   
   private double xPosition,yPosition;   
   private String color;   
   private boolean isVisible;   
   public Circle()   
   {   
    diameter=30;   
    xPosition=20;   
    yPosition=60;   
    color="blue";   
    isVisible=false;   
   }   
   public void makeVisible()   
   {   
    isVisible=true;   
    draw();   
   }   
   public void makeInVisible()   
   {   
    erase();   
    isVisible=false;   
   }   
   public void moveRight()   
   {   
    moveHorizontal(20);   
   }   
   public void moveLeft()   
   {   
    moveHorizontal(-40);   
   }   
   public void moveUp()   
   {   
    moveVertical(20);   
   }   
   public void moveDown()   
   {   
    moveVertical(-40);   
   }   
   public void moveHorizontal(int distance)   
   {   
    erase();   
    xPosition+=distance;   
    draw();   
   }   
   public void moveVertical(int distance)   
   {   
    erase();   
    yPosition+=distance;   
    draw();   
   }   
   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();    
     }    
   }    
   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();    
     }    
   }    
   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);    
     }    
   }    
   private void erase()    
   {    
     if(isVisible) {    
       Canvas canvas = Canvas.getCanvas();    
       canvas.erase(this);    
     }    
   }    
 }  

TRIANGLE
 import java.awt.*;   
 /**  
  * Class untuk mendefinisikan bentuk segitiga.  
  *  
  * @author (Mohammad Nafis Naufally/05111640000038)  
  * @version (1/20180916)  
  */  
 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 = 60;   
     width = 80;   
     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(40);   
   }   
   /**   
   * 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(40);   
   }   
   /**   
   * 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);   
     }   
   }   
 }  

PICTURE
   
 /**  
  * Class untuk menampilkan gambar secara keseluruhan.  
  *  
  * @author (Mohammad Nafis Naufally/05111640000038)  
  * @version (1/20180916)  
  */  
 public class Picture  
 {  
     private Square background;    
     private Circle sun;    
     private Square wall;    
     private Square wall1;    
     private Square wall2;    
     private Square wall3;    
     private Square wall4;    
     private Circle tree;    
     private Square house1;   
     private Square window11;   
     private Square window12;   
     private Square door1;   
     private Triangle roof1;   
     private Square grass;   
     private Circle cloud1;   
     private Circle cloud2;   
     private Circle cloud3;   
     private Circle cloud4;   
     private Circle cloud5;   
     private Circle cloud6;   
     private Circle tree6;    
       
     /**    
       * Draw this picture.    
       */    
     public void draw() {   
       background=new Square();   
       background.changeColor("cyan");   
       background.changeSize(500);   
       background.moveHorizontal(-50);    
       background.moveVertical(-50);    
       background.makeVisible();    
         
       sun=new Circle();    
       sun.changeColor("yellow");   
       sun.changeSize(80);   
       sun.moveHorizontal(360);    
       sun.moveVertical(-40);    
       sun.makeVisible();   
         
       grass=new Square();   
       grass.changeColor("green");   
       grass.changeSize(500);   
       grass.moveHorizontal(-50);    
       grass.moveVertical(230);    
       grass.makeVisible();  
         
       house1=new Square();   
       house1.changeColor("blue");   
       house1.changeSize(120);   
       house1.moveHorizontal(5);    
       house1.moveVertical(150);    
       house1.makeVisible();   
       window11=new Square();   
       window11.changeColor("black");   
       window11.changeSize(30);   
       window11.moveHorizontal(90);    
       window11.moveVertical(175);    
       window11.makeVisible();   
       window12=new Square();   
       window12.changeColor("black");   
       window12.changeSize(30);   
       window12.moveHorizontal(10);    
       window12.moveVertical(175);    
       window12.makeVisible();   
       door1=new Square();   
       door1.changeColor("brown");   
       door1.changeSize(40);   
       door1.moveHorizontal(45);    
       door1.moveVertical(210);    
       door1.makeVisible();   
       roof1=new Triangle();   
       roof1.changeColor("red");   
       roof1.changeSize(100,120);   
       roof1.moveHorizontal(65);    
       roof1.moveVertical(85);    
       roof1.makeVisible();   
   
       cloud1=new Circle();    
       cloud1.changeColor("white");   
       cloud1.changeSize(50);   
       cloud1.moveHorizontal(200);    
       cloud1.moveVertical(-20);    
       cloud1.makeVisible();   
       cloud2=new Circle();    
       cloud2.changeColor("white");   
       cloud2.changeSize(30);   
       cloud2.moveHorizontal(180);    
       cloud2.moveVertical(-10);    
       cloud2.makeVisible();   
       cloud5=new Circle();    
       cloud5.changeColor("white");   
       cloud5.changeSize(30);   
       cloud5.moveHorizontal(240);    
       cloud5.moveVertical(-10);    
       cloud5.makeVisible();   
       cloud3=new Circle();   
       cloud3.changeColor("white");   
       cloud3.changeSize(50);   
       cloud3.moveHorizontal(60);    
       cloud3.moveVertical(-20);    
       cloud3.makeVisible();   
       cloud4=new Circle();    
       cloud4.changeColor("white");   
       cloud4.changeSize(30);   
       cloud4.moveHorizontal(40);    
       cloud4.moveVertical(-10);    
       cloud4.makeVisible();   
       cloud5=new Circle();    
       cloud5.changeColor("white");   
       cloud5.changeSize(30);   
       cloud5.moveHorizontal(100);    
       cloud5.moveVertical(-10);    
       cloud5.makeVisible();   
     }  
 }  

Setelah code diatas di-compile, jalankan fungsi new Picture() dan akan muncul objek di bawah.

Kemudian jalankan fungsi void draw() maka muncullah gambar saya berikut.

Demikian cara menggambar pemandangan dengan menggunakan Java. Terima kasih atas kunjungannya.



Jangan lupa, semangat ketik 10 jari. :)

10 September 2018

Tugas 2 PBO B

Kali ini saya akan membahas tentang cara membuat class bangun 3 dimensi, dilengkapi dengan atributnya dalam field dan juga method-method yang digunakan untuk menghitung luas permukaan dan volume.



Berikut ini merupakan source code java-nya.

1. Class Main

1:    
2:  /**  
3:   * Mencari Volume dan Luas Permukaan sebuah bangun 3 dimensi,  
4:   * dan menampilkan hasilnya pada terminal.  
5:   *  
6:   * @author (Mohammad Nafis Naufally/05111640000038)  
7:   * @version (1/20180910)  
8:   */  
9:  public class MyMain  
10:  {  
11:    // instance variables - replace the example below with your own  
12:    //private int x;  
13:    public static void main(String args[])  
14:      {  
15:        Kubus aKubus;  
16:        aKubus = new Kubus();   
17:        aKubus.x = 8.0;   
18:        double LP = aKubus.LuasPermukaan();   
19:        double V = aKubus.Volume();   
20:        System.out.println("KUBUS");   
21:        System.out.println("Sisi= "+aKubus.x);   
22:        System.out.println("Luas Permukaan= "+LP);   
23:        System.out.println("Volume= "+V);   
24:        System.out.println("\n");  
25:       
26:        Balok aBalok;   
27:        aBalok = new Balok();   
28:        aBalok.p = 15.0;   
29:        aBalok.l = 10.0;   
30:        aBalok.t = 5.0;   
31:        LP = aBalok.LuasPermukaan();   
32:        V = aBalok.Volume();   
33:        System.out.println("BALOK");   
34:        System.out.println("Panjang = "+aBalok.p);   
35:        System.out.println("Lebar = "+aBalok.l);   
36:        System.out.println("Tinggi = "+aBalok.t);   
37:        System.out.println("Luas Permukaan = "+LP);   
38:        System.out.println("Volume = "+V);   
39:        System.out.println("\n");   
40:       
41:        Tabung aTabung;   
42:        aTabung = new Tabung();   
43:        aTabung.r = 10.0;   
44:        aTabung.t = 20.0;   
45:        LP = aTabung.LuasPermukaan();   
46:        V = aTabung.Volume();   
47:        System.out.println("TABUNG");   
48:        System.out.println("Radius = "+aTabung.r);   
49:        System.out.println("Tinggi = "+aTabung.t);   
50:        System.out.println("Luas Permukaan = "+LP);   
51:        System.out.println("Volume = "+V);   
52:        System.out.println("\n");   
53:       
54:        Bola aBola;   
55:        aBola = new Bola();   
56:        aBola.r = 10.0;   
57:        LP = aBola.LuasPermukaan();   
58:        V = aBola.Volume();   
59:        System.out.println("BOLA");   
60:        System.out.println("Radius = "+aBola.r);   
61:        System.out.println("Luas Permukaan = "+LP);   
62:        System.out.println("Volume = "+V);   
63:      }  
64:  }  
65:    

2. Class Kubus

1:    
2:  /**  
3:   * Mencari Volume dan Luas Permukaan Kubus.  
4:   *  
5:   * @author (Mohammad Nafis Naufally/05111640000038)  
6:   * @version (1/20180910)  
7:   */  
8:  public class Kubus   
9:   {   
10:    public double x;  
11:      
12:    public double LuasPermukaan()   
13:    {   
14:     return 6*x*x;   
15:    }   
16:    public double Volume()   
17:    {   
18:     return x*x*x;   
19:    }   
20:   }   

3. Class Balok

1:    
2:  /**  
3:   * Mencari Volume dan Luas Permukaan Balok.  
4:   *  
5:   * @author (Mohammad Nafis Naufally/05111640000038)  
6:   * @version (1/20180910)  
7:   */  
8:  public class Balok   
9:   {   
10:    public double p,l,t;  
11:      
12:    public double LuasPermukaan()   
13:    {   
14:     return 2*(p*l)+2*(l*t)+2*(p*t);   
15:    }   
16:    public double Volume()   
17:    {   
18:     return p*l*t;   
19:    }   
20:   }   

4. Class Tabung

1:    
2:  /**  
3:   * Mencari Volume dan Luas Permukaan Tabung.  
4:   *  
5:   * @author (Mohammad Nafis Naufally/05111640000038)  
6:   * @version (1/20180910)  
7:   */  
8:  public class Tabung   
9:   {   
10:    public double r,t;  
11:      
12:    public double LuasPermukaan()   
13:    {   
14:     return 2*3.14*r*(r+t);   
15:    }   
16:    public double Volume()   
17:    {   
18:     return 3.14*r*r*t;   
19:    }   
20:   }   
21:    

5. Class Bola

1:    
2:  /**  
3:   * Mencari Volume dan Luas Permukaan Bola.  
4:   *  
5:   * @author (Mohammad Nafis Naufally/05111640000038)  
6:   * @version (1/20180910)  
7:   */  
8:   public class Bola   
9:   {   
10:    public double r;  
11:      
12:    public double LuasPermukaan()   
13:    {   
14:     return 4*3.14*r*r;   
15:    }   
16:    public double Volume()   
17:    {   
18:     return (4*3.14*r*r*r)/3;   
19:    }   
20:   }   

Setelah itu, compile semua class kemudian klik kanan pada class MyMain dan jalankan fungsi void di bawah ini.



Selanjutnya akan muncul terminal yang menampilkan hasil seperti pada screenshot di awal tadi.

Demikian tugas kedua saya pada mata kuliah Pemrograman Berbasis Objek. Terima kasih atas kunjungannya.



Jangan lupa, semangat ketik 10 jari. :)

3 September 2018

Tugas 1 PBO B

Kita bisa memprogram code Java pada software BlueJ. Berikut merupakan source code agar BlueJ dapat melakukan print out message dari sebuah program Java ini.


   
 /**  
  * Bagian dari project tugas 1, mengeprint message dari program di bawah ini.  
  *  
  * @author (Mohammad Nafis Naufally)  
  * @version (0.1/20180903)  
  */  
 public class Tugas1  
 {  
   // instance variables - replace the example below with your own  
   private int x;  
   
   /**  
    * Constructor for objects of class Tugas1  
    */  
   public Tugas1()  
   {  
     // initialise instance variables  
     x = 0;  
     System.out.println("Nama      : Mohammad Nafis Naufally");  
     System.out.println("Kelas      : PBO B");  
     System.out.println("Alamat Rumah  : Dsn. Sruni, Ds. Banjartanggul Kec. Pungging, Mojokerto");  
     System.out.println("Email      : naf1s.n4ufally@gmail.com");  
     System.out.println("Blog      : http://mnafisnaufally.blogspot.com/");  
     System.out.println("No HP/WA    : 088803216015");  
     System.out.println("Twitter     : @MNafisNaufally");  
   }  
   
   /**  
    * An example of a method - replace this comment with your own  
    *  
    * @param y a sample parameter for a method  
    * @return  the sum of x and y  
    */  
   public int sampleMethod(int y)  
   {  
     // put your code here  
     return x + y;  
   }  
 }  
   

Setelah code di atas di-compile, lalu kita run code-nya sehingga muncul pesan pada screenshot bawah ini.




Demikian tugas pertama saya pada mata kuliah Pemrograman Berbasis Objek. Terima kasih atas kunjungannya.



 Jangan lupa, semangat ketik 10 jari. :)