Kamis, 27 Oktober 2016

Program Display Jam

          Hi semua, bertemu lagi nih dengan saya Sidqi, mahasiswa Teknik Informatika ITS yang saat ini berada di semester 3 hehe. Pada kesempatan kali ini saya ingin mengupload sekaligus berbagi ilmu mengenai Program Display Jam. Program ini dibuat untuk memenuhi tugas Pemrograman Berorientasi Objek (PBO) Kelas D.

          Langsung saja, inti dari program ini yaitu untuk menampilkan (mendisplay) suatu jam yang terdiri dari satuan jam dan menit. Untuk satuan jam memiliki nilai maksimal 23 dan satuan menit memiliki nilai maksimal 59. Problem untuk mendisplay Jam ini dibantu oleh beberapa subproblem yang merupakan suatu kelas-kelas. Kelas-kelas tersebut yaitu NumberDisplay, ClockDisplay, dan TestClockDisplay.

          Hubungannya seperti berikut :



Source codenya (created by Format My SourceCode for Blogging) :
public class NumberDisplay
{
private int limit;
private int value;
/**
* Constructor for objects of class NumberDisplay
*/
public NumberDisplay(int rollOverLimit)
{
limit = rollOverLimit;
value = 0;
}
/**
* Return the current value.
*/
public int getValue()
{
return value;
}
/**
* Set the value of the display to the new specified
* value. If the new value is less than zero or over the
* limit, do nothing.
*/
public void setValue(int replacementValue)
{
if((replacementValue >= 0) &&
(replacementValue < limit)) {
value = replacementValue;
}
}
/**
* Return the display value (that is, the current value
* as a two-digit String. If the value is less than ten,
* it will be padded with a leading zero).
*/
public String getDisplayValue()
{
if(value < 10) {
return "0" + value;
}
else {
return "" + value;
}
}
/**
* Increment the display value by one, rolling over to zero if
* the limit is reached.
* 
*/
public void increment()
{
value = (value + 1) % limit;
}
}
public class ClockDisplay
{
private NumberDisplay hours;
private NumberDisplay minutes;
private String displayString; // simulates the actual display
/**
* Constructor for ClockDisplay objects. This constructor
* creates a new clock set at 00:00.
*/
public ClockDisplay()
{
hours = new NumberDisplay(24);
minutes = new NumberDisplay(60);
updateDisplay();
}
/**
* Constructor for ClockDisplay objects. This constructor
* creates a new clock set at the time specified by the
* parameters.
*/
public ClockDisplay(int hour, int minute)
{
hours = new NumberDisplay(24);
minutes = new NumberDisplay(60);
setTime(hour, minute);
}
/**
* This method should get called once every minute - it
* makes the clock display go one minute forward.
*/
public void timeTick()
{
minutes.increment();
if(minutes.getValue() == 0) { // it just rolled over!
hours.increment();
}
updateDisplay();
}
/**
* Set the time of the display to the specified hour and
* minute.
*/
public void setTime(int hour, int minute)
{
hours.setValue(hour);
minutes.setValue(minute);
updateDisplay();
}
/**
* Return the current time of this display in the format
* HH:MM.
*/
public String getTime()
{
return displayString;
}
/**
* Update the internal string that represents the
* display.
*/
private void updateDisplay()
{
displayString = hours.getDisplayValue() + ":" +
minutes.getDisplayValue();
}
}
public class TestClockDisplay
{
 
    public TestClockDisplay()
    {
    }
 
    public void test()
    {
        ClockDisplay clock = new ClockDisplay();
       
        clock.setTime(19,04);
        System.out.println(clock.getTime());
       
        clock.setTime(04,52);
        System.out.println(clock.getTime());
    }
}
Nah, kelas yang pertama yaiut NumberDisplay, berikut screen capture source codenya :


Lalu, kelas yang kedua yaitu ClockDisplay, berikut source codenya :


Dan yang terakhir yaitu kelas TestClockDisplay. Berikut source codenya :

Lalu ketiga itu akan saling berinteraksi dan setelah dicompile akan tampak jam yang terdiri dari jam dan menit. (Sebagai contoh saya menginput jam 19:04 dan 04:52. Berikut hasil setelah dicompile :)

Nama : R. Sidqi Tri Priwi
NRP  : 5115100153
Kelas : PBO D
Read More..

Kamis, 13 Oktober 2016

Program Ticket Machine

          Pagi, bagaimana kabarnya semua? Semoga sehat selalu ya :)

          Pada kesempatan kali ini saya akan mempublish mengenai tugas kuliah saya yang implementasinya sangat dibutuhkan di kehidupan sehari-hari.

          Ya, program pada tugas kali ini yaitu "Ticket Machine". Seperti namanya, Ticket Machine merupakan mesin yang berfungsi untuk melayani penjualan tiket. Baik itu tiket kereta api, pesawat, atau yang lainnya. Langsung saja, berikut kodingannya menggunakan Java di BlueJ.

Source codenya (created by Format My SourceCode for Blogging) :
 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)  
 {  
  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)  
 {  
   balance = balance + amount;  
 }  
 /**  
 * Print a ticket.  
 * Update the total collected and  
 * reduce the balance to zero.  
 */  
 public void printTicket()  
 {  
   // 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 balance.  
   total = total + balance;  
   // Clear the balance.  
   balance = 0;  
 }  
 }  
 //Main  
 import java.util.Scanner;  
 public class IntMain  
 {  
 public static void main(String args[])  
 {  
  Scanner scan= new Scanner(System.in);  
  int cost,menu;  
  System.out.println(“Masukkan harga tiket \n”);  
  cost=scan.nextInt();  
  TicketMachine ticket=new TicketMachine(cost);System.out.println(“1. Get Price”);  
  System.out.println(“2. Get Balance”);  
  System.out.println(“3. Insert Money”);  
  System.out.println(“4. Print Ticket”);  
  menu=scan.nextInt();  
  switch(menu)  
  {  
   case 1:  
   cost=ticket.getPrice();  
   System.out.println(cost);  
   break;  
   case 2:  
   ticket.getBalance();  
   break;  
   case 3:  
   int money=scan.nextInt();  
   ticket.insertMoney(money);  
   break;  
   case 4:  
   ticket.printTicket();  
   break;  
  }  
  }  
 }  
                                                                                                                                                                   

            Dan setelah di compile program tersebut akan berjalan seperti pada gambar berikut : 


Read More..

Kamis, 06 Oktober 2016

3 Program Java Sederhana

          Hi Selamat Pagi semua :)

          Pada kesempatan kali ini saya akan memposting mengenai 3 Program Java Sederhana yang merupakan Tugas Mata Kuliah yang saya ambil (Pemrograman Berorientasi Objek Kelas D).

           Langsung saja, program pertama yaitu Perhitungan Aritmatika.

          Program ini sangatlah sederhana, saya memasukan 2 input, panjang 10 dan lebar 5, berikut source codenya :

Source codenya (created by Format My SourceCode for Blogging) :
/*
  Program       : Perhitungan Aritmatika
  Author        : R. Sidqi Tri Priwi
  Date          : 7 Oct 2016
 */

 

public class PerhitunganAritmatika {
       public static void main(String[] args) {
            
              // deklarasi variabel
              double panjang = 10;
              double lebar = 5;
              double jumlah;
              double kurang;
              double kali;
              double bagi;
              double modulus;
            
              //proses operasi
              jumlah = panjang + lebar; // operasi Penjumlahan
              kurang = panjang - lebar; // operasi Pengurangan
              kali   = panjang * lebar; // operasi Perkalian
              bagi   = panjang / lebar; // Operasi Pembagian
              modulus = panjang % lebar; //operasi Modulus
            
              //mencetak hasil
              System.out.println("Hasil Penjumlahan    : " + jumlah);
              System.out.println("Hasil Pengurangan    : " + kurang);
              System.out.println("Hasil Perkalian      : " + kali);
              System.out.println("Hasil Pembagian      : " + bagi);
              System.out.println("Hasil Modulus : " + modulus);
       }

}
           Dan berikut output saat sudah dicompile :
           Lalu yang kedua adalah program Menghitung Nilai Rata-rata

          Inputnya terdiri dari 3 nilai ujian, lalu program ini berfungsi untung menghitung rata-ratanya. Berikut source codenya :


Source codenya (created by Format My SourceCode for Blogging) :
/*
  Program       : Menghitung Nilai Rata-rata
  Author        : R. Sidqi Tri Priwi
  Date          : 7 Oct 2016
 */

 
import java.util.Scanner;
public class scanner_angka {

       /**
        * @param args
        */
       public static void main(String[] args) {
              int nilai1, nilai2, nilai3;
              double hasil;
            
              Scanner DataIn = new Scanner(System.in);
              System.out.print("Nilai Ujian Ke-1 : ");
              nilai1 = DataIn.nextInt();
            
              System.out.print("Nilai Ujian Ke-2 : ");
              nilai2 = DataIn.nextInt();
            
              System.out.print("Nilai Ujian Ke-3 : ");
              nilai3 = DataIn.nextInt();
            
              hasil = (nilai1+nilai2+nilai3)/3;
            
              System.out.println("Nilai Rata-Rata      : " + hasil);
       }

}
  
           Dan setelah dicompile akan mengeluarkan output sebagai berikut :

          Dan program terakhir yang saya buat dan pelajari minggu ini yaitu program Looping.
          Berikut source codenya. Looping dimulai dari 10 sampai 1.



Source codenya (created by Format My SourceCode for Blogging) :
/*
  Program       : Looping 
  Author        : R. Sidqi Tri Priwi
  Date          : 7 Oct 2016
 */

class Looping {
    public static void main(String args[]){
         for(int i=10; i>1; i--){
              System.out.println("The value of i is: "+i);
         }
    }
}
   

  Dan outputnya :

sumber :
Perhitungan Aritmatika (http://www.infomugi.com/2015/06/contoh-program-java-sederhana.html)
Menghitung Nilai Rata-rata (http://www.infomugi.com/2015/06/contoh-program-java-sederhana.html)
Looping (http://beginnersbook.com/2015/03/for-loop-in-java-with-example/) 

R. Sidqi Tri Priwi
5115100153
PBO D

Read More..
Related Posts Plugin for WordPress, Blogger...