Senin, 24 September 2018

Tugas 4 PBO A (24 September 2018)

Haiii, pada kesempatan kali ini, saya akan mempublish tentang membuat tampilan jam digital dengan GUI menggunakan BlueJ.

Untuk Source Codemya terdiri dari 4 bagian,yaitu:
1. Clock Display
2. Test Clock Display
3. Number Display
4. Clock GUI

Clock Display
untuk membuat tampilan display jamnya (desain)


Test Clock Display
Untuk menampung test case (jamnya ditentukan)


Number Display
Untuk pengaturan setting detik menit dan jam
 
Clock GUI
Untuk settingan tampilan GUI,terdapat fitur start, stop, dan step (step ini fungsinya untuk mempercepat detik jam:)
 

Jika dijalankan, hasilnya akan menjadi seperti ini:)
1. Simple Clock


2. With GUI 


Sekian dan terima kasih :)

Jumat, 21 September 2018

Tugas Rumah 3 PBO A (17 September 2018)

 

Program Remote Air Conditioner (AC)

Haii..
Pada post kali ini, saya mempublish post tentang Program Remote AC Sharp menggunakan bahasa Java dan aplikasi BlueJ. Untuk pembuatan programnya, dibutuhkan 2 class, yaitu main function dan Remote function.

Untuk tampilan Classnya, berikut illustrasinya.

Main Function
Berisi  perintah-perintah yang diinginkan oleh user untuk memperlakukan program ini . Pada remote ini, ada beberapa pengaturan, yaitu: naik dan turun suhu, settingan mode, settingan swing, dan untuk power off remotenya.

Berikut source codenya.



Remote Function
Berisi fungsi untuk settingan pada masing-masing fitur, fungsi yang mengatur display/tampilan program.

Berikut source codenya.


Untuk hasil dari compile source code diatas, berikut contohnya :)

Langkah 1
Ketik angka "0" untuk memulai/power ON remote.
Setelah itu, akan muncul tampilan seperti berikut.


Langkah 2
Untuk menaikan suhunya, ketik angka "1". sedangkan  umtuk menurunkan suhu, ketik angka "2".
Setelah itu, akan muncul tampilan seperti berikut.
 


Langkah 3
Untuk mengubah mode, ketik angka "3". Kemudian akan muncul settingan mode yang tersedia.Lalu ketik angka yang diinginkan (contoh : "1"), makan mode akan berubah menjadi "COOL" .
Setelah itu, akan muncul tampilan seperti berikut.




 

Langkah 4
Untuk mengubah settingan Swing, user dapat mengetik angka "4", bisa disimak dari contoh berikut ini.


Settingan swing telah berubah, dari OFF menjadi ON.
Terakhir......
Untuk menghentikan program, kalian bisa ketik angka "5".
Dan program java telah berhenti.
Sekian dan sampai berjumpa di post selanjutnya! :)

Senin, 17 September 2018

Tugas Kelas 3 PBO A (17 September 2018)

Program Ticket Machine 

Haiii
Pada kesempatan kali ini, saya akan post program Ticket Machine
Source Code nya terdiri dari 2 bagian, yaitu Fungsi Ticket Machine dan Main nya.

Main

public class TicketMachine{   
 private int price;  
 private int balance;  
 /*
  * Program Ticket Machine
  * Author: Yemima Sutanto
  * Date : 17 September 2018
  * 
  */
 
 private int total;
 public TicketMachine(int ticketCost){  
  price = ticketCost;  
  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;
 }
 
 /**  
 * 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 rather than: "+ amount);
   }
 }
 
 /**  
 * 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("# RP" + price + ".");  
   System.out.println("##################");  
   System.out.println();  
   // Update the total collected with the price.  
   total = total + price;  
   // Reduce the balance by the price. 
   int refund = balance - price;
   System.out.println("Your Change is RP"+ refund);
    }
   else{
       System.out.println("You must insert at least: RP " + (price-balance)+ "more.");
   }
   System.out.println("Terima Kasih! Selamat menikmati perjalanan anda :)");
 }
}

Function

import java.util.Scanner;
 
 public class IntMain{  
 public static void main(String args[]){  
  Scanner scan= new Scanner(System.in);  
  int cost,menu,balance; 
  boolean entry;
  System.out.print("Selamat Datang di KRT Machine\n");
  System.out.println("Masukkan harga tiket: ");  
  cost=scan.nextInt();  
  
  TicketMachine ticket=new TicketMachine(cost);
  entry= true;
  
  while(entry){
  System.out.println("1. Get Price");  
  System.out.println("2. Insert Money");  
  System.out.println("3. Get Balance");  
  System.out.println("4. Print Ticket"); 
  System.out.println("5. Exit");
  menu=scan.nextInt();  
  switch(menu){  
   case 1: cost=ticket.getPrice();  
   System.out.println("Harga tiket anda: RP" + cost);
   break;  
   case 2: int money=scan.nextInt();  
   ticket.insertMoney(money);
   break;  
   case 3: ticket.getBalance(); 
   break;  
   case 4: ticket.printTicket();  
   break;
   case 5: entry = false;
   break;
     }  
  }  
 }
}

Menu yang dapat dipakai user ada 5:
1. Get Price : menampilkan harga tiket
2. Insert money : input harga tiket
3. Get Balance : menampung uang yang telah diinput
    Jika uang yang diinput user tidak memenuhi, maka user tidak bisa mencetak tiket.
4. Print Ticket : cetak tiket dan menampilkan kembalian (jika ada)
5. Exit : Keluar dari program (menu)

Untuk hasil compile source code, berikut screenshotnya.
 Sekian dan terima kasih!:)

Senin, 10 September 2018

Tugas Rumah PBO A (10 September 2018)

Haiii!!!
Pada post kali ini, saya akan mempublish tugas rumah PBO A tentang implementasi Picture Project (menggambar rumah menggunakan aplikasi BlueJ).

Untuk pengerjaan project kali ini, dibutuhkan 5 class, yaitu:
1. Main (atau Picture)
    berisi perintah untuk menampilkan gambar secara keseluruhan, dimana bangun-bangun 2D tersebut diposisikan sesuai dengan keinginan kita
2. Canvas
3. Square
4. Circle
5. Triangle

1. Main

 /**  
   * Write a description of class main here.  
  *  
  * @author Yemima Sutanto  
  * @version 1.7 15 Sept 2018  
  */  

 public class main{  
   private Square hall1,hall2;   
   private Square wd1,wd2,wd3,wd4,pintuhall,tiang1,tiang2,kotak1,kotak2;  
   private Triangle ataphall1,ataphall2,ataptambahan;  
   private Circle matahari;  
   
   /**   
   * Draw this picture.   
   */ 
  
   public void draw(){   
    hall1 = new Square();   
    hall1.moveVertical(160);   
    hall1.moveHorizontal(0);   
    hall1.changeSize(90);   
    hall1.makeVisible(); 
  
    hall2 = new Square();  
    hall2.moveVertical(160);   
    hall2.moveHorizontal(90);  
    hall2.changeSize(90);  
    hall2.makeVisible();
  
    ataphall1 = new Triangle();  
    ataphall1.changeColor("red");  
    ataphall1.changeSize(50, 90);   
    ataphall1.moveHorizontal(55);   
    ataphall1.moveVertical(145);   
    ataphall1.makeVisible();
   
    ataphall2 = new Triangle();   
    ataphall2.changeColor("red");  
    ataphall2.changeSize(50, 90);   
    ataphall2.moveHorizontal(145);   
    ataphall2.moveVertical(145);   
    ataphall2.makeVisible();
  
    ataptambahan = new Triangle();  
    ataptambahan.changeColor("red");  
    ataptambahan.changeSize(-50, 90);   
    ataptambahan.moveHorizontal(100);   
    ataptambahan.moveVertical(195);   
    ataptambahan.makeVisible();
  
    wd1 = new Square();  
    wd1.changeColor("black");  
    wd1.changeSize(20);  
    wd1.moveHorizontal(20);   
    wd1.moveVertical(180);   
    wd1.makeVisible();
   
    wd2 = new Square();  
    wd2.changeColor("black");  
    wd2.changeSize(20);  
    wd2.moveHorizontal(40);   
    wd2.moveVertical(180);   
    wd2.makeVisible();
  
    wd3 = new Square();  
    wd3.changeColor("black");  
    wd3.changeSize(20);  
    wd3.moveHorizontal(125);   
    wd3.moveVertical(180);   
    wd3.makeVisible(); 
 
    wd4 = new Square();  
    wd4.changeColor("black");  
    wd4.changeSize(20);  
    wd4.moveHorizontal(145);   
    wd4.moveVertical(180);    
    wd4.makeVisible(); 
 
    pintuhall = new Square();   
    pintuhall.changeColor("lightblue");  
    pintuhall.moveHorizontal(70);   
    pintuhall.moveVertical(210);   
    pintuhall.changeSize(45);   
    pintuhall.makeVisible();
   
    matahari = new Circle();   
    matahari.moveHorizontal(80);   
    matahari.moveVertical(-100);   
    matahari.changeSize(100);   
    matahari.makeVisible(); 
  
    tiang1 = new Square();  
    tiang1.changeColor("black");  
    tiang1.moveHorizontal(-190);   
    tiang1.moveVertical(140);   
    tiang1.changeSize(140);   
    tiang1.makeVisible(); 
  
    tiang2 = new Square();  
    tiang2.changeColor("black");  
    tiang2.moveHorizontal(230);   
    tiang2.moveVertical(140);   
    tiang2.changeSize(140);   
    tiang2.makeVisible();
   
    kotak1 = new Square();  
    kotak1.changeColor("green");  
    kotak1.moveHorizontal(-50);   
    kotak1.moveVertical(210);   
    kotak1.changeSize(50);   
    kotak1.makeVisible(); 
 
    kotak2 = new Square();  
    kotak2.changeColor("green");  
    kotak2.moveHorizontal(180);   
    kotak2.moveVertical(210);   
    kotak2.changeSize(50);   
    kotak2.makeVisible();  
   }   
   
   /**   
   * Change this picture to black/white display   
   */   
   
    public void setBlackAndWhite(){   
    if(hall1 != null){ // only if it's painted already...   
     hall1.changeColor("black");   
     wd1.changeColor("white");   
     ataphall1.changeColor("black");   
     matahari.changeColor("black");   
    }   
   } 
  
   /**   
   * Change this picture to use color display   
   */  
 
   public void setColor(){   
    if(hall1 != null){ // only if it's painted already...   
     hall1.changeColor("blue");   
     wd1.changeColor("black");   
     ataphall1.changeColor("red");   
     matahari.changeColor("yellow");   
    }   
   } 
 }  

2. 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 Yemima Sutanto  
  * @version 1.7 15 Sept 2018  
  */  

 public class Canvas{   
   private static Canvas canvasSingleton;  
   public static final Color brown = new Color(102,51,0);  
   public static final Color lightblue = new Color(51,204,255);
   /**  
    * Factory method to get the canvas singleton object.  
    */  

   public static Canvas getCanvas(){  
     if(canvasSingleton == null) {  
       canvasSingleton = new Canvas("Ini Canvas", 300, 300,   
           Color.gray);  
     }  
     canvasSingleton.setVisible(true);  
     return canvasSingleton;  
   }  

   // ----- instance part -----  
   private JFrame frame;  
   private CanvasPane canvas;  
   private Graphics2D graphic;  
   private Color backgroundColour;  
   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 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<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 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("brown"))   
     graphic.setColor(brown);   
     else if(colorString.equals("white"))   
     graphic.setColor(Color.white);   
     else if(colorString.equals("orange"))  
     graphic.setColor(Color.orange);  
     else if(colorString.equals("lightblue"))  
     graphic.setColor(lightblue);  
     else if(colorString.equals("gray"))  
     graphic.setColor(Color.gray);  
     else if(colorString.equals("darkgreen"))  
     graphic.setColor(new Color(0,102, 0));
     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);  
     }  
   }  
 }  

3. Square

 import java.awt.*;  
 
  /**  
  * A square that can be manipulated and that draws itself on a canvas.  
  *   
  * @author Yemima Sutanto  
  * @version 1.7 15 Sept 2018  
  */ 
 
 public class Square{  
   private int size;  
   private int sbX;  
   private int sbY;  
   private String color;  
   private boolean isVisible;  

   /**  
    * Create a new square at default position with default color.  
    */  

   public Square(){  
     size = 30;  
     sbX = 60;  
     sbY = 50;  
     color = "blue";  
     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();  
     sbX += distance;  
     draw();  
   }
  
   /**  
    * Move the square vertically by 'distance' pixels.  
    */
  
   public void moveVertical(int distance){  
     erase();  
     sbY += 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++){  
       sbX += 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++){  
       sbY += 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(sbX, sbY, size, size));  
       canvas.wait(10);  
     }  
   }  

   /*  
    * Erase the square on screen.  
    */  

   private void erase(){  
     if(isVisible) {  
       Canvas canvas = Canvas.getCanvas();  
       canvas.erase(this);  
     }  
   }  
 }  

4. Circle

 import java.awt.*;  
 import java.awt.geom.*;  
 /**  
  * A circle that can be manipulated and that draws itself on a canvas.  
  *   
  * @author Yemima Sutanto  
  * @version 1.7 15 Sept 2018  
  */  
 public class Circle {  
   private int diameter;  
   private int sbX;  
   private int sbY;  
   private String color;  
   private boolean isVisible;
  
   /**  
    * Create a new circle at default position with default color.  
    */
  
   public Circle(){  
     diameter = 30;  
     sbX = 20;  
     sbY = 60;  
     color = "yellow";  
     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();  
     sbX += distance;  
     draw();  
   }  

   /**  
    * Move the circle vertically by 'distance' pixels.  
    */  

   public void moveVertical(int distance){  
     erase();  
     sbY += 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++){  
       sbX += 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++){  
       sbY += 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(sbX, sbY,   
           diameter, diameter));  
       canvas.wait(10);  
     }  
   }
  
   /*  
    * Erase the circle on screen.  
    */  

   private void erase(){  
     if(isVisible) {  
       Canvas canvas = Canvas.getCanvas();  
       canvas.erase(this);  
     }  
   }  
 }  

5. Triangle

 import java.awt.*;  
 /**  
  * A triangle that can be manipulated and that draws itself on a canvas.  
  *   
  * @author Yemima Sutanto  
  * @version 1.7 15 Sept 2018  
  */  
 public class Triangle{  
   private int height;  
   private int width;  
   private int sbX;  
   private int sbY;  
   private String color;  
   private boolean isVisible;  
   /**  
    * Create a new triangle at default position with default color.  
    */  
   public Triangle(){  
     height = 30;  
     width = 40;  
     sbX = 50;  
     sbY = 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();  
     sbX += distance;  
     draw();  
   }  
   /**  
    * Move the triangle vertically by 'distance' pixels.  
    */  
   public void moveVertical(int distance){  
     erase();  
     sbY += 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++){  
       sbX += 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++){  
       sbY += 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 = { sbX, sbX + (width/2), sbX - (width/2) };  
       int[] ypoints = { sbY, sbY + height, sbY + 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);  
     }  
   }  
 }  

Untuk hasil compile dari code-code diatas, berikut saya tampilkan.. :)

Sekian dan terima kasih :D

Tugas Kelas 2 PBO A (10 September 2018)

Pada post kali ini, saya akan mempublish tugas PBO ke 2 tentang implementasi Class mengenai bangun datar 2D.

     Class

















1. Main

/**
 * Main Function menghitung Keliling dan Luas Bangun Datar
 *
 * @author Yemima Sutanto
 * @version 2 (100918)
 */

class mainn{
    public static void main(){
        fungsilingkaran afungsilingkaran;
        afungsilingkaran = new fungsilingkaran();
        double luasL = afungsilingkaran.luasL();
        double kelilingL =afungsilingkaran.kelilingL();
        System.out.println("Radius="+afungsilingkaran.r+" Luas Lingkaran="+luasL);
        System.out.println("Radius="+afungsilingkaran.r+" Keliling Lingkaran="+kelilingL);
        
        fungsipersegi afungsipersegi;
        afungsipersegi = new fungsipersegi();
        double luasp = afungsipersegi.luasp();
        double kelilingp =afungsipersegi.kelilingp();
        System.out.println("\nsisi="+afungsipersegi.s+" Luas Persegi="+luasp);
        System.out.println("sisi="+afungsipersegi.s+" Keliling Persegi="+kelilingp);
        
        fungsisegitiga afungsisegitiga;
        afungsisegitiga = new fungsisegitiga();
        double luass = afungsisegitiga.luass();
        double kelilings=afungsisegitiga.kelilings();
        System.out.println("\nAlas="+afungsisegitiga.alas+" Tinggi="+afungsisegitiga.tinggi+" Luas Segitiga="+luass);
        System.out.println("Alas="+afungsisegitiga.alas+" Keliling Segitiga="+kelilings);
        
        fungsipersegipanjang afungsipersegipanjang;
        afungsipersegipanjang = new fungsipersegipanjang();
        double luasps = afungsipersegipanjang.luasps();
        double kelilingps = afungsipersegipanjang.kelilingps();
        System.out.println("\nPanjang="+afungsipersegipanjang.p+" Lebar="+afungsipersegipanjang.l+" Luas Persegi Panjang="+luasps);
        System.out.println("Panjang="+afungsipersegipanjang.p+" Lebar="+afungsipersegipanjang.l+" Keliling Persegi Panjang="+kelilingps);
        
        fungsijajarangenjang afungsijajarangenjang;
        afungsijajarangenjang = new fungsijajarangenjang();
        double luasjg = afungsijajarangenjang.luasjg();
        double kelilingjg = afungsijajarangenjang.kelilingjg();
        System.out.println("\nAlas="+afungsijajarangenjang.alas+" Tinggi="+afungsijajarangenjang.t+" Luas Jajaran Genjang="+luasjg);
        System.out.println("Alas="+afungsijajarangenjang.alas+" Sisi Miring="+afungsijajarangenjang.miring+" Keliling Jajaran Genjang="+kelilingjg);
        
        fungsibelahketupat afungsibelahketupat;
        afungsibelahketupat = new fungsibelahketupat();
        double luasbk = afungsibelahketupat.luasbk();
        double kelilingbk = afungsibelahketupat.kelilingbk();
        System.out.println("\nDiagonal1="+afungsibelahketupat.d1+" Diagonal2="+afungsibelahketupat.d2+" Luas Belah Ketupat="+luasbk);
        System.out.println("Sisi="+afungsibelahketupat.sisi+" Keliling Belah Ketupat="+kelilingbk);
    
    }
}

2. Lingkaran
/**
 * rumus Keliling dan Luas Lingkaran
 *
 * @author Yemima Sutanto
 * @version 2 (10092018)
 */

public class fungsilingkaran extends mainn{
    // instance variables - replace the example below with your own
    public double x,y;
    public double r;
    public fungsilingkaran(){
        r=5;
    }
    
    public double kelilingL(){
        // initialise instance variables
        return 2*3.14*r;
    }
    
    public double luasL(){
        // put your code here
        return 3.14*r*r;
    }
}

3. Persegi Panjang
/**
 * rumus Keliling dan Luas Persegi Panjang
 *
 * @author Yemima Sutanto
 * @version 2 (100918)
 */

public class fungsipersegipanjang extends mainn{
    public double x,y;
    public double p,l;
    public fungsipersegipanjang(){
        p=5;
        l=3;
    }
    public double kelilingps(){
        return 2*(p+l);
    }

    public double luasps(){
        return p*l;
    }
}

4. Persegi
/**
 * rumus Keliling dan Luas Persegi
 *
 * @author Yemima Sutanto
 * @version 2 (10091018)
 */

public class fungsipersegi extends mainn{
    public double x,y;
    public double s;
    public fungsipersegi(){
        s=5;
    }
    public double kelilingp(){
        return 4*s;
    }

    public double luasp(){
        return s*s;
    }
}

5. Belah Ketupat
/**
 * Rumus Keliling dan Luas Belah Ketupat
 *
 * @author Yemima Sutanto
 * @version 2 (10092018)
 */

public class fungsibelahketupat extends mainn{
    public double x,y;
    public double sisi,d1,d2;
    public fungsibelahketupat(){
        sisi=5;
        d1=3;
        d2=2;
    }
    public double kelilingbk(){
        return 4*sisi;
    }

    public double luasbk(){
        return 0.5*d1*d2;
    }
}

6. Jajaran Genjang
/**
 * Rumus Keliling dan Luas Jajaran Genjang
 *
 * @author Yemima Sutanto
 * @version 2 (10092018)
 */

public class fungsijajarangenjang extends mainn{
    public double x,y;
    public double alas,miring,t;
    public fungsijajarangenjang(){
        alas=5;
        t=3;
        miring=2;
    }
    public double kelilingjg(){
        return 2*(alas+lebar);
    }

    public double luasjg(){
        return alas*t;
    }
}

7. Segitiga (Siku-Siku)

/**
 * Rumus Keliling dan Luas Segitiga sama sisi
 *
 * @author Yemima Sutanto
 * @version 2 (10092018)
 */

public class fungsisegitiga extends mainn{
    public double x,y;
    public double alas,tinggi,miring;
    
    public fungsisegitiga(){
        alas=6;
        tinggi=8;
        sisi miring=10;
    }
    
    public double kelilings(){
        return alas+tinggi+miribg;
    }

    public double luass(){
        return 0.5*alas*tinggi;
    }
}

    Berikut Hasil Compile code-code tersebut


 


Tugas Kelas PBO ATugas kelas ke 2 PBO A

Senin, 03 September 2018

Tugas 1 PBO A (3 September 2018)

Hai! Kali ini saya akan memposting tugas PBO , tentang implementasi penulisan biodata menggunakan Bahasa pemrograman Java dengan IDE BlueJ.

Source Codenya seperti berikut:

 /**  
  * Program Biodata  
  *  
  * @author (Yemima Sutanto)  
  * @version (3 Sept 2018)  
  */  
 public class HelloWorld{  
   public HelloWorld(){  
     System.out.print("#PBO-Tugas1\n");  
     System.out.print("Nama\t: Yemima Sutanto\n");  
     System.out.print("Kelas\t: PBO A\n");  
     System.out.print("Alamat Rumah: Ploso Timur 1/68\n");  
     System.out.print("Email\t: yemimasutanto28@gmail.com\n");  
     System.out.print("Blog\t: yemimasutanto28.blogspot.com\n");  
     System.out.print("No HP/WA: 081357657416\n");  
     System.out.print("Twitter\t: @yemimasutanto\n");  
   }  
 }