Faruqi's Blog

Berbagi Info & Fakta Unik,Menarik,Bermanfaat

OOP Mesin ATM

OOP Mesin ATM

Hasil :
Screenshot (37).png
Screenshot (38).png

PBO : Antrian Bank




Ini adalah contoh program antrian bank
1:  import java.util.Date;  
2:  import java.text.DateFormat;  
3:  import java.text.SimpleDateFormat;  
4:  public class Antrian  
5:  {  
6:    private int antrian = 1;  
7:    private String getTanggal(){  
8:      DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy H:mm:ss");  
9:      Date date = new Date();  
10:      return dateFormat.format(date);  
11:    }  
12:    public void printTicketantrian(){  
13:      System.out.println(getTanggal());  
14:      System.out.println("Nomor Antrian Anda:");  
15:      System.out.println(antrian++);  
16:      System.out.println("Terima kasih");  
17:    }  
18:  }  
main
1:  import java.util.Scanner;  
2:  public class IntMain  
3:  {  
4:    public static int main (String args[])  
5:    {  
6:      Scanner scan = new Scanner (System.in);  
7:      int menu,tel=4;  
8:      Antrian ticket = new Antrian ();  
9:      while(true)  
10:      {  
11:      System.out.println ("1. Ambil nomor antrian");  
12:      System.out.println("2. Exit\n");  
13:      System.out.print(" ");  
14:      menu = scan.nextInt();  
15:      switch(menu)  
16:      {  
17:        case 1:  
18:        System.out.println("\n");  
19:        ticket.printTicketantrian();  
20:        if(tel==4)  
21:        {tel=1;}  
22:        else  
23:        {tel=tel+1;}  
24:        System.out.printf("nomor teller %d\n",tel);  
25:        break;  
26:        case 2:  
27:        return 0;  
28:      }  
29:    }  
30:  }  
31:  }  

outputnya

Menghitung Luas dan Keliling Bangun 2D

Menghitung Luas dan Keliling Bangun 2D

Berikut source code untuk menghitung beberapa bangun datar 2D.

- Segitiga


  public class Triangle   
  {   
   private double a;   
   private double t;   
   private String color;   
   public Triangle()   
   {   
    a = 1.0;   
    t = 1.0;   
    color = "white";   
   }   
   public Triangle(double alas)   
   {   
    a = alas;   
    t = 1.0;   
    color = "white";   
   }   
   public Triangle(double alas,String warna)   
   {   
    a = alas;   
    color = warna;   
   }   
   public Triangle(double alas,double tinggi,String warna)   
   {   
    a = alas;   
    t = tinggi;   
    color = warna;   
   }   
   public double getA()   
   {   
    return a;   
   }   
   public double getT()   
   {   
    return t;   
   }   
   public String getColor()   
   {   
    return color;   
   }   
   public double getArea()   
   {   
    return a*t/2;   
   }   
   public String toString()   
   {   
    return String.format("A %s colored triangle have an area of %f cm",this.getColor(),this.getArea());   
   }   
  }   

- Lingkaran
  import static java.lang.Math.*;   
  public class Circle   
  {   
   private double r;   
   private String color;   
   public Circle ()   
   {   
    r = 1.0;   
    color = "red";   
   }   
   public Circle (double jari)   
   {   
    r = jari;   
    color = "red";   
   }   
   public Circle ( double jari, String warna )   
   {   
    r = jari;   
    color = warna;   
   }   
   public double getArea(){   
    System.out.printf("Area of %s Circle is.. %f\n",color,PI * r * r);   
    return PI * r * r;   
   }   
   public double getRound()   
   {   
    System.out.printf("Around of %s Circle is.. %s\n",color,PI * 2 * r);   
    return PI * 2 * r;   
   }   
   public double getRadius()   
   {   
    return r;   
   }   
   public String getColor()   
   {   
    return color;   
   }   
   public String toString()   
   {   
    return String.format("A Circle with %s color has an area of %f cm",this.getColor(),this.getArea());   
   }   
  }   

-Segiempat
 public class Square   
  {   
   private double s;   
   private String color;   
   public Square()   
   {   
    s = 1.0;   
    color = "white";   
   }   
   public Square(double sisi)   
   {   
    s = sisi;   
    color = "white";   
   }   
   public Square(double sisi,String warna)   
   {   
    s = sisi;   
    color = warna;   
   }   
   public double getArea(){   
    return s * s;   
   }   
   public double getRound()   
   {   
    return 4 * s;   
   }   
   public double getSide()   
   {   
    return s;   
   }   
   public String getColor()   
   {   
    return color;   
   }   
   public String toString()   
   {   
    return String.format("The %s colored Square has an Area of %f cm",this.getColor(),this.getArea());   
   }   
  }   

- Persegi Panjang

  public class Rectangle   
  {   
   private double p;   
   private double l;   
   private String color;   
   public Rectangle()   
   {   
    p = 1.0; l = 1.0;   
    color = "white";   
   }   
   public Rectangle(double panjang,double lebar)   
   {   
    p = panjang;   
    l = lebar;   
    color = "white";   
   }   
   public Rectangle(double panjang,double lebar,String warna)   
   {   
    p = panjang;   
    l = lebar;   
    color = warna;   
   }   
   public double getP()   
   {   
    return p;   
   }   
   public double getL(){ return l; }   
   public String getColor() {return color; }   
   public double getArea ()   
   {   
    return p * l;   
   }   
   public double getRound()   
   {   
    return 2*(p+l);   
   }   
   public String toString()   
   {   
    return String.format("A %s colored rectangle with Lenght of %f and Widht of %f have an area of %f cm"   
    ,this.getColor(),this.getP(),this.getL(),this.getArea());   
   }   
  }   

main
 public class MainClass   
  {   
   public static void main()   
   {   
    Circle circle1 = new Circle(7.0,"Black");   
    Circle circle2 = new Circle(14.0);   
    Circle circle3 = new Circle();   
    System.out.println(circle1);   
    System.out.printf("A round of %f\n",circle1.getRound());   
    System.out.println(circle2);   
    System.out.printf("A round of %f\n",circle2.getRound());   
    System.out.println(circle3);   
    System.out.printf("A round of %f\n",circle3.getRound());   
    System.out.printf("\n\n");   
    System.out.printf("Now to test the Rectangle\n");   
    Rectangle persegi1 = new Rectangle(5,4);   
    Rectangle persegi2 = new Rectangle(5,3,"Merah");   
    Rectangle persegi3 = new Rectangle();   
    System.out.println(persegi1);   
    System.out.printf("A round of %f\n",persegi1.getRound());   
    System.out.println(persegi2);   
    System.out.printf("A round of %f\n",persegi2.getRound());   
    System.out.println(persegi3);   
    System.out.printf("A round of %f\n",persegi3.getRound());   
    System.out.printf("\n\n");   
    System.out.printf("Now to test the square\n");   
    Square kotak1 = new Square(10);   
    Square kotak2 = new Square(5,"hijau");   
    Square kotak3 = new Square();   
    System.out.println(kotak1);   
    System.out.printf("A round of %f\n",kotak1.getRound());   
    System.out.println(kotak2);   
    System.out.printf("A round of %f\n",kotak2.getRound());   
    System.out.println(kotak3);   
    System.out.printf("A round of %f\n",kotak3.getRound());   
    System.out.printf("\n\n");   
    System.out.printf("Now to test the triangle\n");   
    Triangle segitiga1 = new Triangle(2);   
    Triangle segitiga2 = new Triangle(6.0,3.0,"Kuning");   
    Triangle segitiga3 = new Triangle ( 10.0, "Kuning");   
    Triangle segitiga4 = new Triangle();   
    System.out.println(segitiga1);   
    System.out.println(segitiga2);   
    System.out.println(segitiga3);   
    System.out.println(segitiga4);   
    System.out.printf("\n\n");   
   }   
  }  
A Deeper Look - PBO

A Deeper Look - PBO

Ini adalah beberapa latihan kodingan java 
  • Latihan 8.7
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/** Latihan 8.7 @author (Tegar Satrio Utomo) @version (6/3/17) */
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 specific month and year");
    }    
    public String toString()
    {
        return String.format("%d/%d/%d",month,day,year);
    }
}
  • Latihan 8.8
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
/** Latihan 8.8 @author (Tegar Satrio Utomo) @version (6/3/17) */
public class Employee
{
    private String firstName; private String lastName;
    private Date birthDate; private Date hireDate;
    
    public Employee(String first, String last, Date dateOfBirth, Date dateOfHire)
    {
        firstName=first; lastName=last;
        birthDate=dateOfBirth; hireDate=dateOfHire;
    }
    
    public String toString()
    {
        return String.format("%s, %s Hired: %s Birthday: %s",lastName,firstName,hireDate,birthDate);
    }
}
  • Latihan 8.9
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
/**Latihan 8.9 @author (Tegar Satrio Utomo) @version (6/3/17) */
public class EmployeeTest
{
    public static void main(String [] args)
    {
        Date Birth=new Date (7,24,1949);
        Date Hire= new Date(3,12,1968);
        Employee employee= new Employee ("Bob","Blue",Birth,Hire);
        
        System.out.println(employee);
    }
}
  • Latihan 8.10
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/** Latihan 8.10 @author (tegar satrio utomo) @version (6/3/17) */
public enum Book
{
    JHTP("Java How to Program","2012"),
    CHTP("C How to Program","2007"),
    IW3HTP("Internet & World Wide Web How to Program","2008"),
    CPPHTP("C++ How to Program","2012"),
    VBHTPC("Visual Basic How to Program","2011"),
    CSHARPHTP("Visual C# How to Program","2011");
    
    private final String title;
    private final String copyrightYear; 
    
    Book(String BookTitle, String year)
    {
        title=BookTitle;
        copyrightYear=year;
    }
    
    public String getTitle()
    {
        return title;
    }
    
    public String getCopyrightYear()
    {
        return copyrightYear;
    }
}
  • Latihan 8.11
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
/** Latihan 8.11 @author (Tegar Satrio Utomo) @version (6/3/17) */
import java.util.EnumSet;
public class EnumTest
{
   public static void main(String[] args)
   {
       System.out.println("All Books\n");
       for(Book book : Book.values() )
            System.out.printf("%-10s%-45s%s\n",book,book.getTitle(),book.getCopyrightYear());
       
       System.out.println("\nDisplay a range of enum constants:\n");
       
       for(Book book : EnumSet.range(Book.JHTP,Book.CPPHTP))
       System.out.printf("%-10s%-45s%s\n",book,book.getTitle(),book.getCopyrightYear());
   }
}
  • Latihan 8.12

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/** Latihan 8.12 @author (Tegar Satrio Utomo) @version (6/3/17) */
public class Employee
{
    private String firstName;
    private String lastName;
    private static int count= 0;
    
    public Employee(String first, String last)
    {
        firstName = first;
        lastName = last;
        
        ++count;
        System.out.printf("Employee constructor: %s %s; count = %d\n",firstName,lastName,count);
    }
    
    public String getFirstName()
    {
        return firstName;
    }
    
    public String getLastName()
    {
        return lastName;
    }
    
    public static int getCount()
    {
        return count;
    }
}
  • Latihan 8.13

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
/** latihan 8.13 @author (Tegar Satrio Utomo) @version (6/3/17) */
public class EmployeeTest
{
   public static void main(String[] args)
   {
       System.out.printf("Employees before instatiation: %d\n",Employee.getCount());
   
       Employee e1 = new Employee("Susan","Baker");
   Employee e2 = new Employee("Bob","Blue");
   
   System.out.println("\nEmployees after instantion: ");
   System.out.printf("via e1.getCount(): %d\n",e1.getCount());
   System.out.printf("via e2.gerCount(): %d\n",e2.getCount());
   System.out.printf("via employee.getCount(): %d\n",Employee.getCount());
   
   System.out.printf("Employee 1: %s %s\nEmployee 2: %s %s\n"
   ,e1.getFirstName(),e1.getLastName(),e2.getFirstName(),e2.getLastName());
   
   e1=null;e2=null;   
   }
}
PBO - Ticket Machine

PBO - Ticket Machine

Berikut adalah code java untuk Ticket Machine yang saya pelajari di kelas PBO

 public class TicketMachine  
 {  
   private int price;  
   private int balance;  
   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()  
   {  
     System.out.println("@@@@@@@@@@@@@@@@@@");  
     System.out.println("@ The BlueJ Line @");  
     System.out.println("@ Ticket     @");  
     System.out.println("" + price + " cents.");  
     System.out.println();  
     total = total + balance;  
     balance = 0;  
   }  
 }  

 import java.util.Scanner;  
 public class IntMain  
 {  
    public static int main()  
       {  
         Scanner scan = new Scanner(System.in);  
         int cost,menu;  
         System.out.println("Masukkan harga tiket \n");  
         cost=scan.nextInt();  
         TicketMachine ticket=new TicketMachine(cost);  
         while (true){  
           System.out.println("1. Get Price");  
           System.out.println("2. Get Balance");  
           System.out.println("3. Insert Money");  
           System.out.println("4. Print Ticket");  
           System.out.println("5. EKZIT");  
           menu=scan.nextInt();  
         switch(menu)  
         {  
           case 1:  
           cost=ticket.getPrice();  
           System.out.println(cost);  
           break;  
           case 2:  
           System.out.println(ticket.getBalance());  
           break;  
           case 3:  
           int money=scan.nextInt();  
           ticket.insertMoney(money);  
           break;  
           case 4:  
           ticket.printTicket();  
           break;  
           case 5:  
           return 0;  
         }  
       }  
     }  
   }  


PBO Program Chapter 8 Class and Objects

PBO Program Chapter 8 Class and Objects

8.1 Time1.java


/**  
 public class Time1  
 {  
   private int hour;  
   private int minute;  
   private int second;  
   public void setTime(int h, int m, int s)  
   {  
     if ( ( h >= 0 && h< 24 ) && ( m >= 0 && m< 60 ) &&   
     ( s >= 0 && s < 60 ) )  
     {  
       hour = h;  
       minute = m;  
       second = s;  
     }  
     else  
       throw new IllegalArgumentException(  
         "hour, minute and/or second was out of range" );  
   }  
   public String toUniversalString()  
   {  
     return String.format( "%02d:%02d:%02d", hour, minute, second );  
   }  
   public String toString()  
   {  
   return String.format( "%d:%02d:%02d %s", ( ( hour == 0 || hour == 12) ? 12  
   : hour % 12 ), minute , second, ( hour < 12 ? "AM" : "PM" ) );  
   }  
 } 

8.2 Time1Test.java


 public class Time1Test  
 {  
   public static void main( String[] args )  
   {  
     Time1 time = new Time1();  
     System.out.print( "The initial universal time is: ");  
     System.out.println( time.toUniversalString() );  
     System.out.print( "The initial standard time is: ");  
     System.out.println( time.toString() );  
     System.out.println();  
     time.setTime( 13, 27, 6 );  
     System.out.print( "Universal time after setTime is: ");  
     System.out.println( time.toUniversalString() );  
     System.out.print( "Standard time after setTime is: " );  
     System.out.println( time.toString() );  
     System.out.println();  
     try  
     {  
       time.setTime( 99, 99, 99 );  
     }  
     catch (IllegalArgumentException e )  
     {  
       System.out.printf( "Exception: %s\n\n", e.getMessage() );  
     }  
     System.out.println( "After attempting invalid settings:");  
     System.out.print( "Universal time: ");  
     System.out.println( time.toUniversalString() );  
     System.out.print( "Standar time: ");  
     System.out.println( time.toString() );  
   }  
 } 


Output :
 

8.3 MemberAccessTest.java


 public class MemberAccessTest  
 {  
   public static void main( String[] args )  
   {  
     Time1 time = new Time1();  
     time.hour = 7;   
     time.minute = 15;   
     time.second = 30;   
   }  
 } 


8.4 ThisTest.java

 public class ThisTest  
 {  
   public static void main( String[] args )  
   {  
     SimpleTime time = new SimpleTime( 15, 30, 19 );  
     System.out.println( time.buildString() );  
   }  
 }  
 class SimpleTime  
 {  
   private int hour;  
   private int minute;  
   private int second;  
   public SimpleTime( int hour, int minute, int second )  
   {  
     this.hour = hour;  
     this.minute = minute;  
     this.second = second;  
   }  
   public String buildString()  
   {  
     return String.format( "%24s: %s\n%24s: %s",  
       "this.toUniversalString()", this.toUniversalString(),  
       "toUniversalString()", toUniversalString() );  
   }  
   public String toUniversalString()  
   {  
     return String.format( "%02d:%02d:%02d",  
       this.hour, this.minute, this.second );  
   }  
 } 


Output :
 






8.5 Time2.java

 public class Time2  
 {  
   private int hour;  
   private int minute;  
   private int second;  
   public Time2()  
   {  
     this( 0, 0, 0 );  
   }  
   public Time2( int h )  
   {  
     this( h, 0, 0 );  
   }  
   public Time2 ( int h, int m )  
   {  
     this( h, m, 0 );  
   }  
   public Time2( int h, int m, int s )  
   {  
     setTime( h, m, s );  
   }  
   public Time2( Time2 time )  
   {  
     this( time.getHour(), time.getMinute(), time.getSecond() );  
   }  
   public void setTime( int h, int m, int s )  
   {  
     setHour( h );  
     setMinute( m );  
     setSecond( s );  
   }  
   public void setHour( int h )  
   {  
     if ( h >= 0 && h < 24 )  
       hour = h;  
     else  
       throw new IllegalArgumentException( "hour must be 0-23" );  
   }  
   public void setMinute( int m )  
   {  
     if ( m >= 0 && m < 60 )  
       minute = m;  
     else  
       throw new IllegalArgumentException( "minute must be 0-59" );  
   }  
   public void setSecond( int s )  
   {  
     if ( s >= 0 && s < 60 )  
       second = ( ( s >= 0 && s < 60 ) ? s : 0 );  
     else  
       throw new IllegalArgumentException( "second must be 0-59" );  
   }  
   public int getHour()  
   {  
     return hour;  
   }  
   public int getMinute()  
   {  
     return minute;  
   }  
   public int getSecond()  
   {  
     return second;  
   }  
   public String toUniversalString()  
   {  
     return String.format(   
       "%02d:%02d:%02d", getHour(), getMinute(), getSecond() );  
   }  
   public String toString()  
   {  
     return String.format( "%d:%02d:%02d %s",  
       ( (getHour() == 0 || getHour() == 12 ) ? 12 : getHour() % 12 ),  
       getMinute(), getSecond(), ( getHour() < 12 ? "AM" : "PM" ) );  
   }  
 } 


8.6 Time2Test.java

 public class Time2Test  
 {  
   public static void main( String[] args )  
   {  
     Time2 t1 = new Time2();   
     Time2 t2 = new Time2( 2 );   
     Time2 t3 = new Time2( 21, 34 );   
     Time2 t4 = new Time2( 12, 25, 42 );   
     Time2 t5 = new Time2( t4 );  
     System.out.println( "Constructed with:" );  
     System.out.println( "t1: all arguments defaulted" );  
     System.out.printf( " %s\n", t1.toUniversalString() );  
     System.out.printf( " %s\n", t1.toString() );  
     System.out.println(  
       "t2: hour specified; minute and second defaulted" );  
     System.out.printf( " %s\n", t2.toUniversalString() );  
     System.out.printf( " %s\n", t2.toString() );  
     System.out.println(  
     "t3: hour and minute specified; second defaulted" );  
     System.out.printf( " %s\n", t3.toUniversalString() );  
     System.out.printf( " %s\n", t3.toString() );  
     System.out.println( "t4: hour, minute and second specified" );  
     System.out.printf( " %s\n", t4.toUniversalString() );  
     System.out.printf( " %s\n", t4.toString() );  
     System.out.println( "t5: Time2 object t4 specified" );  
     System.out.printf( " %s\n", t5.toUniversalString() );  
     System.out.printf( " %s\n", t5.toString() );  
     try  
     {  
      Time2 t6 = new Time2( 27, 74, 99 );   
     }   
     catch ( IllegalArgumentException e )  
     {  
       System.out.printf( "\nException while initializing t6: %s\n", e.getMessage() );  
     }   
   }   
 } 


Output :

Konsep Objek (PBO)

Konsep Objek (PBO)

Pemrograman berorientasi objek (Inggris: object-oriented programming disingkat OOP) merupakan paradigma pemrograman yang berorientasikan kepada objek. Semua data dan fungsi di dalam paradigma ini dibungkus dalam kelas-kelas atau objek-objek. Bandingkan dengan logika pemrograman terstruktur. Setiap objek dapat menerima pesan, memproses data, dan mengirim pesan ke objek lainnya.