Senin, 06 Maret 2017

Latihan 8.7 - 8.13

8.7

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

8.8

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

8.9

 public class EmployeeTest  
 {  
   public static void main( String[] args)  
   {  
     date birth = new date(7,24,1949);  
     date hire = new date(3,12,1988);  
     Employee employee = new Employee ("Bob", "Blue", birth, hire);  
     System.out.println(employee);  
   }  
 }  

8.10

 public enum Book  
 {  
   JHTP("Java How to Program","2012"),  
   CHTP("C how to Program","2007"),  
   IW3HTP("Internet & World Wide Wb How to Program","2008"),  
   CPPHTP("C++ How to Program","2012"),  
   VBHTP("Visual Basic 2010 How tto Program","2011"),  
   CSHARPHTP("Visual c# 2010 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;  
   }  
 }  

8.11

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

8.12


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

8.13

 public class EmployeeTest  
 {  
   public static void main(String[]args)  
   {  
     System.out.printf("Employees before instantiation: %d\n",  
     Employee.getCount());  
     Employee e1 = new Employee("Susan","Baker");  
     Employee e2 = new Employee ("Bob","Blue");  
     System.out.println("\nEmployees after instantiation:");  
     System.out.printf("via e1.getCount(): %d\n", e1.getCount());  
     System.out.printf("via e2.getCount(): %d\n", e2.getCount());  
     System.out.printf("via Employee.getCount(): %d\n",  
     Employee.getCount());  
     System.out.printf("\nEmployee 1: %s %s\nEmployee 2: %s %s\n",  
     e1.getFirstName(), e1.getLastName(),  
     e2.getFirstName(), e2.getLastName());  
     e1 = null;  
     e2 = null;  
   }  
 }  

Mesin Tiket

Mesin Tiket

 public class TicketMachine  
 {  
   //Harga tiket dari mesin ini.  
   private int price ;  
   //jumlah uang yang dimasukkan oleh pelanggan sejauh ini.  
   private int balance;  
   //jumlah total uang yang dikumpulkan oleh mesin ini  
   private int total;  
   /*Menciptakan mesin yang mengeluarkan tiket dari harga yang diberikan.  
   * Perhatikan bahwa harga harus lebih besar dari nol, dan ada  
   * Ada pemeriksaan untuk memastikan ini.*/  
 public TicketMachine(int ticketCost)  
 {  
   price = ticketCost;  
   balance = 0;  
   total = 0;  
 }  
 //kembali ke harga tiket  
 public int getPrice()  
 {  
   return price;  
 }  
 /*  
  * Kembali jumlah uang yang sudah dimasukkan untuk  
  * Tiket berikutnya.  
  */  
 public int getBalance()  
 {  
   return balance;  
 }  
 //menerima sejumlah uang dari customer  
 public void insertMoney(int amount)  
 {  
   balance = balance + amount;  
 }  
 //print tiket  
 //memperbarui yang sudah masuk dan mengurangi saldo ke 0  
 public void printTicket()  
 {  
   //simulasi print tiket  
   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 total saldo  
   total = total + balance;  
   //mengkosongkan saldo  
   balance = 0;  
 }  
 }  

//Main

 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();  
     while(true)  
     {    
     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:  
       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;  
     }  
     }  
   }  
 }  

Senin, 27 Februari 2017

latihan 8.6

 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 argument 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());  
     }  
   }  
 }  

latihan 8.5

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

latihan 8.4

 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("%24: %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);  
   }  
 }  

laihan 8.3

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

latihan 8.2

 public class Time1Test  
 {  
   public static void main( String[] arg)  
   {  
     Time1 time = new Time1();  
     System.out.println ("The initial universal time is : ");  
     System.out.println ( time.toUniversalString());  
     System.out.println ("The initial standard time is: ");  
     System.out.println (time.toString());  
     System.out.println();  
     time.setTime( 13, 27, 6);  
     System.out.println("Universal time after setTime is: ");  
     System.out.println( time.toUniversalString());  
     System.out.println("Standard time adter 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.println ("Universal time : ");  
   System.out.println (time.toUniversalString() );  
   System.out.println ("Standard time: ");  
   System.out.println ( time.toString() );  
   }  
 }