Sunday, September 30, 2018

PBO A-5 Tugas Membuat Digital Clock

Nama; Muhammad Naufal Refadi
NRP: 05111740000097
Kelas: PBO-A


Ini adalah tugas rumah saya membuat Digital Clock dengan menggunakan Java.

Berikut Formatted Source code saya:

A) NumberDisplay
  public class NumberDisplay    
  {    
   private int limit;    
   private int value;    
   public NumberDisplay(int Maks)    
   {    
   limit = Maks;    
   value = 0;    
   }    
   public int getValue()    
   {    
   return value;    
   }    
   public void increment()    
   {    
   value = (value + 1) % limit;    
   }    
   public String getDisplayValue()    
   {    
   if(value >= 10)    
   {    
   return "" + value;    
   }    
   else    
   {    
   return "0" + value;    
   }    
   }    
   public void setValue(int replacementValue)    
   {    
   if((replacementValue >= 0) && (replacementValue < limit))    
   {    
   value = replacementValue;    
   }    
   }    
  }    

B)Clock Display
 public class ClockDisplay    
  {    
   private NumberDisplay hours;    
   private NumberDisplay minutes;    
   private String displayString;    
   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)    
   {    
   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();    
   }    
  }    
C) ClockGUI
 import java.awt.*;    
  import java.awt.event.*;    
  import javax.swing.*;    
  import javax.swing.border.*;    
  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 showAbout()    
   {    
   JOptionPane.showMessageDialog (frame, "Clock Version 0.1\n" +    
   "Make a digital clock with java",    
   "About Clock",    
   JOptionPane.INFORMATION_MESSAGE);    
   }    
   private void quit()    
   {    
   System.exit(0);    
   }    
   private void makeFrame()    
   {    
   frame = new JFrame("Clock");    
   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("About Clock...");    
   item.addActionListener(e->showAbout());    
   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();    
    pause();    
   }    
   }    
   private void pause()    
   {    
   try    
   {    
    Thread.sleep(900);    
   }    
   catch(InterruptedException exc)    
   {    
   }    
   }    
   }    
  }    

D) Test Clock Display
  /**   
  * Write a description of class Clock here.   
  *   
  * @author (Muhammad Naufal Refadi)   
  * @version (30-9-2018)   
  */    
  public class TestClockDisplay    
  {    
   public void test()    
   {    
   ClockDisplay clock = new ClockDisplay();    
   clock.setTime(11,50);    
   System.out.println(clock.getTime());    
   clock.setTime(9,05);    
   System.out.println(clock.getTime());    
   clock.setTime(23,58);    
   System.out.println(clock.getTime());    
   clock.setTime(17,59);    
   System.out.println(clock.getTime());  
   clock.setTime(12,01);  
   System.out.println(clock.getTime());  
   }    
  }  

Dan berikut adalah screenshot hasil outputnya


Sunday, September 23, 2018

PBO A-Tugas 4: Remote AC

Nama; Muhammad Naufal Refadi
NRP: 05111740000097
Kelas: PBO-A


Ini adalah tugas saya pada pertemuan minggu ke-5 dengan membuat Remote AC dengan menggunakan class Java/bahasa java.

A. Main
 import java.util.Scanner;    
  public class Main    
  {    
   public static void main(String args[])    
   {     
   Scanner scan= new Scanner(System.in);    
   int power,option;  
   RemoteAC remote=new RemoteAC();   
   while (true){  
   System.out.println("Sharp Air Conditioner \n");  
   System.out.println("To turn on press 1 \n");  
   power=scan.nextInt();    
   while (power==1){  
     remote.print();  
   option=scan.nextInt();    
   if(option==0){    
     power=0;break;    
   }   
   else if(option==1){    
    remote.tempplus();    
   }    
   else if(option==2){    
    remote.tempmin();    
   }    
   else if(option==3){    
    remote.changemode();   
   }  
   else if(option==4){  
    remote.changefanspeed();  
   }  
   else if(option==5){  
    remote.changeswing();  

   }  
   }  
} } }
B. Class RemoteAC
 /**  
  * Write a description of class RemoteAC here.  
  * @author Muhammad Naufal Refadi  
  * @version 22-9-2018  
  */  
 public class RemoteAC  
 {  
   private int temp;  
   private String mode;  
   private String swing;  
   private String fanspeed;  
   public RemoteAC()  
   {  
     temp=25;  
     mode= new String("AUTO");  
     swing= new String("ON");  
     fanspeed= new String("MEDIUM");  
   }  
   public int tempplus(){  
    if(temp<30){temp++;}  
    else{  
     System.out.println("The maximum temperature is 30");  
     }  
     return temp;  
   }  
   public int tempmin(){  
    if(temp>16){temp--;}  
    else{  
       System.out.println("The maximum temperature is 16");  
    }  
    return temp;  
   }  
   public String changemode(){  
     if(mode.equals("AUTO")){mode= new String("FAN");}  
     else if(mode.equals("FAN")){mode= new String("COOL");}  
     else if(mode.equals("COOL")){mode= new String("HEAT");}  
     else if(mode.equals("HEAT")){mode= new String("DRY");}  
     else if (mode.equals("DRY")){mode= new String("AUTO");}  
     System.out.println(mode);  
     return mode;  
   }  
   public String changefanspeed(){  
    if(fanspeed.equals("LOW")){fanspeed= new String("MEDIUM");}  
    else if(fanspeed.equals("MEDIUM")){fanspeed= new String("HIGH");}  
    else if(fanspeed.equals("HIGH")){fanspeed= new String("LOW");}  
    return fanspeed;  
   }  
   public String changeswing(){  
     if(swing.equals("ON")){swing= new String("OFF");}  
     else if(swing.equals("OFF")){swing= new String("ON");}  
     return swing;    
   }  
   public void print(){  
    System.out.println("#####################");   
    System.out.println("#  REMOTE CONTROL #");   
    System.out.println("# Air Conditioner #");   
    System.out.println("#  "+temp+" Celcius   #");   
    System.out.println("#  MODE = "+mode+"  #");  
    System.out.println("#  FAN = "+fanspeed+"  #");  
    System.out.println("#  SWING = "+swing+"   #");   
    System.out.println("#------OPTION-------#");   
    System.out.println("# 0.Turn OFF AC  #");  
    System.out.println("# 1.Temperature ^ #");   
    System.out.println("# 2.Temperature v #");   
    System.out.println("# 3.Change Mode  #");  
    System.out.println("# 4.Fan Speed   #");  
    System.out.println("# 5.Swing Mode   #");   
    System.out.println("#-------------------#");   
    System.out.println("#####################");   
    System.out.println();   
   }  
 }  

Screenshot Input dan Output
-Nyalakan Ac

 -Naikkan Suhu

-Turunkan suhu dan mengubah mode AC



-Naikkan fan speed

- Switch Swing mode On/OFF


Sunday, September 16, 2018

PBO A-Tugas 3 : Ticket Machine

Nama; Muhammad Naufal Refadi
NRP: 05111740000097
Kelas: PBO-A

Ini adalah tugas saya pada pertemuan minggu ke-4 dengan membuat Ticket Machine dengan menggunakan class Java/bahasa java.

Berikut formatted souce code saya:
A)Class Main

 import java.util.Scanner;   
  public class Main   
  {   
   public static void main(String args[])   
   {   
    Scanner scan= new Scanner(System.in);   
    int cost,option;   
    cost = 50;   
    System.out.println("Masukkan harga tiket \n");   
    cost=scan.nextInt();   
    TicketMachine ticket=new TicketMachine(cost);   
    while (true)   
    {  
    System.out.println("Menu Option:");    
    System.out.println("1.Get Price");   
    System.out.println("2. Get Balance");   
    System.out.println("3. Insert Money");   
    System.out.println("4. Print Ticket");  
    option=scan.nextInt();   
    if(option==1){   
    cost=ticket.getPrice();   
    System.out.println(cost);   
    }  
    if(option==2){   
     ticket.getBalance();   
    }   
    if(option==3){   
     int money=scan.nextInt();   
     ticket.insertMoney(money);    
    }   
    if(option==4){   
     ticket.printTicket();    
    }   
   }   
  }   
  }   

B) Class TicketMachine

  /**   
  * Ticket Machine   
  * @Author Muhammad Naufal Refadi   
  * @version date 17-09-2018   
  */    
  public class TicketMachine   
  {   
   //The price of a ticket ftr=rom this machin   
   private int price;   
   private int balance;   
   private int total;  
   private int refund;  
   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)   
   {   
    if(amount>0){  
    balance=balance+amount;  
   }  
   else{  
    System.out.println("Masukkan Uang dengan nilai yang benar");  
   }        
   }  
   public void printTicket()   
   {   
    if(balance >= price){    
    System.out.println("##################");   
    System.out.println("# The BlueJ Line");   
    System.out.println("# Ticket");   
    System.out.println("# "+price+" cents.");   
    System.out.println("##################");   
    System.out.println();   
    total=total+balance;   
    balance=0;  
   }  
   else{  
    System.out.println("Uang anda belum cukup, silahkan masukkan minimal"+(price-balance)+ " more cents");  
   }  
   }   
 }   

Dan ini adalah bagan classnya dan screenshot hasil compilenya





PBO A -Tugas 2B: Membuat Rumah

Nama; Muhammad Naufal Refadi
NRP: 05111740000097
Kelas: PBO-A

Ini adalah tugas 2B PBO saya dengan menggambar menggunakan bangunan 2D dan menggunakan bahasa Java

Berikut merupakan formatted source code saya:

a) Picture

 public class Picture   
  {   
   private Square background;   
   private Circle moon;   
   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 house2;  
   private Square window21;  
   private Square window22;  
   private Square door2;  
   private Triangle roof2;  
   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("very dark blue");  
    background.changeSize(500);  
    background.moveHorizontal(-50);   
    background.moveVertical(-50);    
    background.makeVisible();   
    moon=new Circle();   
    moon.changeColor("yellow");  
    moon.changeSize(80);  
    moon.moveHorizontal(360);   
    moon.moveVertical(-40);    
    moon.makeVisible();  
    grass=new Square();  
    grass.changeColor("green");  
    grass.changeSize(500);  
    grass.moveHorizontal(-50);   
    grass.moveVertical(230);    
    grass.makeVisible();  
    house1=new Square();  
    house1.changeColor("light green");  
    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("purple");  
    door1.changeSize(40);  
    door1.moveHorizontal(45);   
    door1.moveVertical(210);    
    door1.makeVisible();  
    roof1=new Triangle();  
    roof1.changeColor("brown");  
    roof1.changeSize(100,120);  
    roof1.moveHorizontal(65);   
    roof1.moveVertical(85);    
    roof1.makeVisible();  
    house2=new Square();  
    house2.changeColor("red");  
    house2.changeSize(150);  
    house2.moveHorizontal(200);   
    house2.moveVertical(150);    
    house2.makeVisible();  
    window21=new Square();  
    window21.changeColor("black");  
    window21.changeSize(30);  
    window21.moveHorizontal(210);   
    window21.moveVertical(175);    
    window21.makeVisible();  
    window22=new Square();  
    window22.changeColor("black");  
    window22.changeSize(30);  
    window22.moveHorizontal(315);   
    window22.moveVertical(175);    
    window22.makeVisible();  
    door2=new Square();  
    door2.changeColor("purple");  
    door2.changeSize(50);  
    door2.moveHorizontal(255);   
    door2.moveVertical(210);    
    door2.makeVisible();  
    roof2=new Triangle();  
    roof2.changeColor("brown");  
    roof2.changeSize(120,150);  
    roof2.moveHorizontal(275);   
    roof2.moveVertical(65);    
    roof2.makeVisible();  
    cloud1=new Circle();   
    cloud1.changeColor("blue");  
    cloud1.changeSize(50);  
    cloud1.moveHorizontal(200);   
    cloud1.moveVertical(-20);    
    cloud1.makeVisible();  
    cloud2=new Circle();   
    cloud2.changeColor("blue");  
    cloud2.changeSize(30);  
    cloud2.moveHorizontal(180);   
    cloud2.moveVertical(-10);    
    cloud2.makeVisible();  
    cloud5=new Circle();   
    cloud5.changeColor("blue");  
    cloud5.changeSize(30);  
    cloud5.moveHorizontal(240);   
    cloud5.moveVertical(-10);    
    cloud5.makeVisible();  
    cloud3=new Circle();  
    cloud3.changeColor("blue");  
    cloud3.changeSize(50);  
    cloud3.moveHorizontal(60);   
    cloud3.moveVertical(-20);    
    cloud3.makeVisible();  
    cloud4=new Circle();   
    cloud4.changeColor("blue");  
    cloud4.changeSize(30);  
    cloud4.moveHorizontal(40);   
    cloud4.moveVertical(-10);    
    cloud4.makeVisible();  
    cloud5=new Circle();   
    cloud5.changeColor("blue");  
    cloud5.changeSize(30);  
    cloud5.moveHorizontal(100);   
    cloud5.moveVertical(-10);    
    cloud5.makeVisible();  
   }   
  }   

b) 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: Bruce Quig  
  * @author: Michael K�lling (mik)  
  *  
  * @version 2011.07.31  
  */  
 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("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);  
     }  
   }  
 }  

c) Circle

 import java.awt.*;   
  import java.awt.geom.*;   
 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);   
    }   
   }   
 }  

d) Square

 import java.awt.*;   
  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);   
    }   
   }   
  }   

e) Triangle

 import java.awt.*;  
 /**  
  * A triangle that can be manipulated and that draws itself on a canvas.  
  *   
  * @author Michael Kolling and David J. Barnes  
  * @version 2008.03.30  
  */  
 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);  
     }  
   }  
 }  

Berikut ini adalah bagannya dan gambar hasil compile




Sunday, September 9, 2018

PBO A-TUGAS 2: Membuat Bangunan 2D

Nama; Muhammad Naufal Refadi
NRP: 05111740000097
Kelas: PBO-A

Ini adalah tugas 2 PBO saya dengan mencari luas dan keliling dari bidang 2D(lingkaran persegi,segitiga,persegi panjang,belah ketupat dan jajar genjang) dengan membuat source code menggunakan bahasa Java.

Berikut merupakan formatted source code saya:

A. Class Main

 /**  
 * @author Muhammad Naufal Refadi
 * @version 10-09-2018
 */
 class Mymain  
 {  
   public static void main()  
   {  
     Circle aCircle;  
     aCircle= new Circle();  
     aCircle.x=10;  
     aCircle.y=20;  
     aCircle.r=5;  
     double area= aCircle.area();  
     double circumf=aCircle.Circumference();  
     System.out.println("Circle:");  
     System.out.println("Radius="+aCircle.r+" Area="+area);  
     System.out.println("Radius="+aCircle.r+" Circumference= "+circumf );  
     System.out.println();  
     Square aSquare;  
     aSquare= new Square();  
     aSquare.s=10;  
     double areaSquare= aSquare.area();  
     double perimSquare=aSquare.Perimeter();  
     System.out.println("Square:");  
     System.out.println("Side="+aSquare.s+ " Area="+areaSquare);  
     System.out.println("Side="+aSquare.s+ " Perimeter="+perimSquare);  
     System.out.println();  
     Triangle aTriangle;  
     aTriangle= new Triangle();  
     aTriangle.a=10;  
     aTriangle.b=20;  
     aTriangle.c=30;  
     aTriangle.h=5;  
     double areaTriangle= aTriangle.area();  
     double perimTriangle=aTriangle.Perimeter();  
     System.out.println("Triangle:");  
     System.out.println("Side1/base="+aTriangle.a+ " Height="+aTriangle.h+" Area="+areaTriangle);  
     System.out.println("Side 1="+aTriangle.a+ " Side 2="+aTriangle.b+ " Side 3="+aTriangle.c+ " Perimeter="+perimTriangle);  
     System.out.println();  
     Rectangle aRectangle;  
     aRectangle= new Rectangle();  
     aRectangle.l=10;  
     aRectangle.w=20;  
     double areaRectangle= aRectangle.area();  
     double perimRectangle=aRectangle.Perimeter();  
     System.out.println("Rectangle:");  
     System.out.println("lenght="+aRectangle.l+ " widht="+aRectangle.w+ " Area="+areaRectangle);  
     System.out.println("lenght="+aRectangle.w+ " widht="+aRectangle.l+" Perimeter="+perimRectangle);  
     System.out.println();      
     Rhombus aRhombus;  
     aRhombus= new Rhombus();  
     aRhombus.s=10;  
     aRhombus.d1=12;  
     aRhombus.d2=16;  
     double areaRhombus= aRhombus.area();  
     double perimRhombus=aRhombus.Perimeter();  
     System.out.println("Rhombus:");  
     System.out.println("Side="+aRhombus.s+ " Diameter 1="+aRhombus.d1+ " Diameter 1="+aRhombus.d2+ " Area="+areaRhombus);  
     System.out.println("Side="+aRhombus.s+ " Perimeter="+perimRhombus);  
     System.out.println();  
     Parallelogram aParallelogram;  
     aParallelogram= new Parallelogram();  
     aParallelogram.a=16;  
     aParallelogram.b=12;  
     aParallelogram.h=13;  
     double areaParallelogram= aParallelogram.area();  
     double perimParallelogram=aParallelogram.Perimeter();  
     System.out.println("Parallelogram:");  
     System.out.println("Side 1/base="+aParallelogram.a+ " Height=" +aParallelogram.h+ " Area="+areaParallelogram);  
     System.out.println("Side 1="+aParallelogram.a+ " Side 2="+aParallelogram.b+ " Perimeter="+perimParallelogram );  
     System.out.println();  
   }  
 }  

b. Public Class Circle

 public class Circle  
 {  
   public double x,y;  
   public double r;  
   /**  
    * Constructor for objects of class Circle  
    */  
   public double Circumference()  
   {  
     // initialise instance variables  
     return 2*3.14*r;  
   }  
   public double area()  
   {  
     // put your code here  
     return 3.14*r*r;  
   }  
 }  

c. Public Class Persegi

 public class Square  
 {  
   public double x,y;  
   public double r,s;  
   public double Perimeter()  
   {  
     return 4*s;  
   }  
   public double area()  
   {  
     return s*s;  
   }  
 }  

d. Public Class Segitiga

 public class Triangle  
 {  
   public double a,b,c,h;  
   public double Perimeter()  
   {  
     return a+b+c;  
   }  
   public double area()  
   {     
     return (a*h)/2;  
   }  
 }  

e. Public Class Persegi Panjang

 public class Rectangle  
 {  
   public double x,y;  
   public double r,l,w;  
   public double Perimeter()  
   {  
     return 2*(l+w);  
   }  
   public double area()  
   {   
     return l*w;  
   }  
 }  

f. Public Class Belah Ketupat

 public class Rhombus  
 {  
   public double s,d1,d2;  
   
   public double Perimeter()  
   {  
     return 4*s;  
   }  
   public double area()  
   {  
     return (d1*d2)/2;  
   }  
 }  
g. Public Class Jajar Genjang

 public class Parallelogram  
 {  
   public double x,y;  
   public double a,b,h;  
  
   public double Perimeter()  
   {  
     return 2*(a+b);  
   }  
   public double area()  
   {  
     return a*h;  
   }  
 }  

Berikut ini adalah screenshot bagan class dan outpunya



Monday, September 3, 2018

Tugas #PBOA-Tugas1

Tugas #PBOA-Tugas1

Nama; Muhammad Naufal Refadi
NRP: 05111740000097
Kelas: PBO-A

Ini adalah tugas 1 PBO saya dengan mebuat profil saya berupa nama,kelas,alamat rumah,email,nama blog,no HP/WA, dan twitter saya menggunakan bahasa Java.

Berikut merupakan formatted source code saya:

 /**  
  * Ini adalah Biografi/profil saya  
  * @author Muhammad Naufal Refadi  
  * @version 1/03-09-2018  
  */  
 public class PBOtugas1  
 {  
   // instance variables - replace the example below with your own  
   private int x;  
   public PBOtugas1()  
   {  
     // initialise instance variables  
     x = 0;  
     System.out.println("Nama     : Muhammad Naufal Refadi");  
     System.out.println("Kelas     : PBO A");  
     System.out.println("Alamat Rumah : Perumdin PT.SG Blok D-117");  
     System.out.println("Email     : naufal.refadi@yahoo.co.id");  
     System.out.println("Blog     : http://PBORefadi.blogspot.com");  
     System.out.println("No HP/WA   : 081252987602");  
     System.out.println("Twitter    : @Starve12venom");  
   }  
 }  

Screenshot: