Selasa, 21 Maret 2017

Program BlueJ Sederhana Luas dan Keliling Bangun 2D

1).Persegi Panjang


 import java.util.Scanner;  
 public class persegi_panjang{  
 public static void main(String [] args){  
   int panjang,lebar,luas,keliling;  
   Scanner S = new Scanner (System.in);  
   System.out.print("masukan panjang=");  
     panjang=S.nextInt();  
   System.out.print("masukan lebar=");  
     lebar=S.nextInt();  
   luas=(panjang*lebar);  
   keliling=2*(panjang+lebar);  
    System.out.println("luas persegi panjang adalah="+luas);  
    System.out.println("keliling persegi panjang adalah="+keliling);  
   }  
 }  

Output:


2)Bujur Sangkar


 import java.util.Scanner;
public class persegi_panjang{
public static void main(String [] args){
    int panjang,lebar,luas,keliling;
    Scanner S = new Scanner (System.in);
    System.out.print("masukan panjang=");
        panjang=S.nextInt();
    System.out.print("masukan lebar=");
        lebar=S.nextInt();
    luas=(panjang*lebar);
    keliling=2*(panjang+lebar);
      System.out.println("luas persegi panjang adalah="+luas);
      System.out.println("keliling persegi panjang adalah="+keliling);
    }
}  

Output:







3)Segitiga


 import java.util.Scanner;  
 public class Ssegitiga{  
 public static void main(String[]args){  
   int alas,tinggi,luas;  
   Scanner F=new Scanner(System.in);  
   System.out.print("input tinggi=");  
   tinggi=F.nextInt();  
   System.out.print("input alas=");  
   alas=F.nextInt();  
     luas=(alas*tinggi)/2;  
     System.out.print("luas segitiga="+luas);  
   }  
 }  

Output:







4)Lingkaran


 import java.util.Scanner;  
 public class lingkaran  
 {  
 public static void main(String[] args)  
 {  
 Scanner input = new Scanner(System.in);  
 double phi = 3.14;  
 double r, luas,keliling;  
 System.out.println("Program Luas Lingkaran\n");  
 System.out.print("Masukkan Panjang Jari-jari : ");  
 r = input.nextDouble();  
 luas = 0.5 * phi * r * r;  
 keliling = 2 * phi * r;  
 System.out.print("Luas Lingkaran = " + (int)luas + " \nKeliling lingkaran = "+ (int)keliling);  
 }  
 }  

Output:

Senin, 06 Maret 2017

8.7-8.13

8.8 Composition

Class Date
  // Fig. 8.7: Date.java  
  // Date class declaration.  
 public class Date  
  {  
  private int month; // 1-12  
  private int day; // 1-31 based on month  
  private int year; // any year  
  private static final int[] daysPerMonth = // days in each month  
  { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };  
  // constructor: call checkMonth to confirm proper value for month;  
  // call checkDay to confirm proper value for day  
  public Date( int theMonth, int theDay, int theYear )  
  {  
  month = checkMonth( theMonth ); // validate month  
  year = theYear; // could validate year  
  day = checkDay( theDay ); // validate day  
  System.out.printf(  
  "Date object constructor for date %s\n", this );  
  } // end Date constructor  
  // utility method to confirm proper month value  
  private int checkMonth( int testMonth )  
  {  
  if ( testMonth > 0 && testMonth <= 12 ) // validate month  
 return testMonth;  
  else // month is invalid  
  throw new IllegalArgumentException( "month must be 1-12" );  
  } // end method checkMonth  
  // utility method to confirm proper day value based on month and year  
  private int checkDay( int testDay )  
  {  
  // check if day in range for month  
  if ( testDay > 0 && testDay <= daysPerMonth[ month ] )  
  return testDay;  
  // check for leap year  
  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" );  
  } // end method checkDay  
  // return a String of the form month/day/year  
  public String toString()  
  {  
  return String.format( "%d/%d/%d", month, day, year );  
 } // end method toS  

Class Employee
 // Fig. 8.8: Employee.java  
  // Employee class with references to other objects.  
 public class Employee  
 {  
 private String firstName;  
 private String lastName;  
  // constructor to initialize name, birth date and hire date  
  public Employee( String first, String last, Date dateOfBirth,  
  Date dateOfHire )  
  {  
  firstName = first;  
  lastName = last;  
  birthDate = dateOfBirth;  
  hireDate = dateOfHire;  
  } // end Employee constructor  
  // convert Employee to String format  
  public String toString()  
  {  
  return String.format( "%s, %s Hired: %s Birthday: %s",  
  lastName, firstName, hireDate, birthDate );  
 } // end method toString  
 } // end class Employee  

Class EmployeeTest
 // Fig. 8.9: EmployeeTest.java  
  // Composition demonstration.  
 public class EmployeeTest  
  {  
  public static void main( String[] args )  
  {  
  Date birth = new Date( 7, 24, 1949 );  
  Date hire = new Date( 3, 12, 1988 );  
  } // end main  
  } // end class EmployeeTesst  

8.9 Enumerations
 // Fig. 8.10: Book.java  
  // Declaring an enum type with constructor and explicit instance fields  
  // and accessors for these fields  
 public enum Book  
  {  
 // instance fields  
  private final String title; // book title  
  private final String copyrightYear; // copyright year  
 Fig. 8.10 | Declaring an enum type with constructor and explicit instance fields and accessors  
 for these fields. (Part 1 of 2.)  
 // declare constants of enum type  
 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" ),  
 VBHTP( "Visual Basic 2010 How to Program", "2011" ),  
 CSHARPHTP( "Visual C# 2010 How to Program", "2011" );  
  // enum constructor  
  Book( String bookTitle, String year )  
  {  
  title = bookTitle;  
  copyrightYear = year;  
  } // end enum Book constructor  
  // accessor for field title  
  public String getTitle()  
  {  
  return title;  
  } // end method getTitle  
  // accessor for field copyrightYear  
  public String getCopyrightYear()  
  {  
  return copyrightYear;  
  } // end method getCopyrightYear  
  } // end enum Book  


 // Fig. 8.11: EnumTest.java  
  // Testing enum type Book.  
  import java.util.EnumSet;  
 public class EnumTest  
  {  
  public static void main( String[] args )  
  {  
  System.out.println( "All books:\n" );  
 // print all books in enum Book  
  for ( )  
  System.out.printf( "%-10s%-45s%s\n", book,  
  , );  
  System.out.println( "\nDisplay a range of enum constants:\n" );  
  // print first four books  
  for ( Book book : )  
  System.out.printf( "%-10s%-45s%s\n", book,  
  , );  
  } // end main  
  } // end class EnumTest  

Senin, 27 Februari 2017

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.

Berikut Source Code-nya:

  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;   
  }   
 }  

Output:

Chapter 8 Classes and Objects

8.1

source code:

 /**   
  * Program Kelas Time1   
  * Deklarasi kelas Time1 mengelola waktu dalam format 24 jam    
  * Nama file : Time1.java   
  */   
  public class Time1   
  {   
   private int hour;    
   private int minute;    
   private int second;    
   //set a new time value using universal time;   
   //throw an exception if the hour, minute, or second is invalid   
   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");   
   }//end method setTime   
   //convert to String in universal-time format (HH:MM:SS)   
   public String toUniversalString()   
   {   
    return String.format ("%02d:%02d:%02d", hour, minute, second);   
   }//end method toUniversalString   
   //convert to String in standart-time format (H:MM:SS AM or PM)   
   public String toString ()   
   {   
    return String.format ("%d:%02d:%02d %s",    
     ( ( hour == 0 || hour == 12) ? 12 : hour % 12),   
     minute, second, (hour < 12 ? "AM" : "PM") );   
   }//end method toString   
  }   

8.2
source code:

  /**   
  * Program Kelas Time1Test   
  * Objek Time1 digunakan dalam aplikasi   
  * nama file : 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("Standard time: ");   
    System.out.println( time.toString() );   
   }   
  }   

Output:


8.3
source code:
 /**   
  * Program Kelas MemberAccessTest   
  * Anggota kelas private Time1 tidak dapat diakses   
  * Nama file : MemberAccessTest.java   
  */   
 public class MemberAccessTest  
 {  
   public static void main(String[] args)  
   {  
     Time1 time = new Time1();  
     time.hour = 7; //error: has private access  
     time.minute = 15; //error: has private access  
     time.second = 30; //error: has private access  
   }  
 }  

8.4
source code:
 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("Standard time: ");   
    System.out.println( time.toString() );   
   }   
  }   

Output:



8.5
source code:

 /**   
  * Program Kelas Time2   
  * Deklarasi kelas Time2 dengan konstuktor overload   
  * Nama file : 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
source code:

 /**   
  * Program Kelas Time2Test   
  * Konstruktor overload digunakan untuk menginisialisasi objek Time2   
  * Nama file : 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: