Senin, 20 Maret 2017

Program Untuk Menghitung Luas dan Keliling Bangun Datar 2 Dimensi

Halo, pada kesempatan kali ini saya ingin berbagi mengenai cara meghitung luas dan keliling bangun datar 2 dimensi.

  • Layang-layang
import java.util.Scanner;
public class layanglayang{
    public static void main (String [] args)
    {
        Scanner nilai = new Scanner (System.in);
        double keliling, luas;
        System.out.println("Menghitung Luas Layang-Layang");
        {
            int d1, d2;
            System.out.println("Diagonal 1 = ");
            d1 = nilai.nextInt();
            System.out.println("Diagonal 2 = ");
            d2 = nilai.nextInt();
            luas = d1*d2/2;
            System.out.println("Luas layang-layang = "+luas);
        }
        System.out.println("\nMenghitung Keliling Layang-Layang");
        {
            int sisiA, sisiB;
            System.out.println("Sisi A = ");
            sisiA = nilai.nextInt();
            System.out.println("Sisi B = ");
            sisiB = nilai.nextInt();
            keliling = 2*sisiA + 2*sisiB;
            System.out.println("Keliling layang-layang = "+keliling);
        }
    }
}

  • Bujung Sangkar

Senin, 06 Maret 2017

Classes and Object: A Deeper Look II

8.7. Date Class Declaration


/**
 * Date
 * Ivan Fadhila
 * PBO
 */
public class Date
{
    private int month;
    private int day;
    private int year;
    
    private static final int[] daysPerMonth =
        {0,31,28,31,30,31,30,31,31,30,31,30,31};
        
   public Date(int theMonth, int theDay, int theYear)
   {
       month = checkMonth( theMonth ); 
       year = theYear;
       day = checkDay (theDay);
      
       System.out.printf(
        "Date object constructor for date %s\n", this);
   }
   
   private int CheckMonth(int testMonth)
   {
       if (testMonth > 0 && testMonth <=12)
        return testMonth;
       else
        throw new IllegalArgumentException ("month must be 1-12");
   }
   
   private int checkDay(int testDay)
   {
       if (testDay > 0 && testDay <= daysPerMonth [month])
        return testDay;
        
       if(month == 2 && testDay == 29 && (year % 400 == 0 ||
        (year % 4 == 0 && year % 100 != 0 )))
        return testDay;
        
       throw new IllegalArgumentException(
        "day out-of-range for the specified month and year");
   }
   
   public String toString()
   {
       return String.format("%d/%d/%d", month, day, year);
   }
}

Rabu, 01 Maret 2017

Studi Kasus Ticket Machine

Ticket Machine adalah sebuah mesin seperti ATM, yang berfungsi melayani penjualan tiket kereta api dari satu tujuan ke tujuan yang lain. Di dalam Ticket Machine ada sebuah program atau perangkat lunak yang mengatur harga tiket di tiap tujuan, mengatur kembalian uang, dan juga mencetak receipt sebagai bukti pembelian tiket.

Untuk menunjang pembelajaran, berikut saya lampirkan kode program dari Ticket Machine:

1. Class TicketMachine

/**
 * TicketMachine
 * Ivan Fadhila
 * PBO
 */
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;
   
   public TicketMachine (int ticketCost)
   {
       price=ticketCost;
       balance=0;
       total=0;
   }
   
   public int getPrice()
   {
       return price;
   }
   
   public int getBalance()
   {
       return balance;
   }
   
   public void insertMoney(int amount)
   {
       balance=balance+amount;
   }
   
   public void printTicket()
   {
        // 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;
   }
}