Tugas-1 PBO

 1. Time Class Case Study

Dalam kasus ini terdapat dua class, yaitu Time1 dan Time1Test, dimana kelas Time1 menunjukan waktu, sementara Time1Test merupakan kelas pengaplikasiannya.

 Berikut merupakan source code Time1 :

/**
 * Fig 8.1 : Time1.java
 * Time1 class declaration maintains the time in 24 hour.
 * @author (Sabrina Lydia S)
 * @version (11/10/2020)
 */
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"));
    }
}
 


Berikut merupakan source code Time1Test :

/**
 * Fig 8.2 : Time1Test.java
 * Time1 object used in an application.
 * @author (Sabrina Lydia S)
 * @version (11/10/2020)
 */
 public class Time1Test
 {
     public static void main( String[] args )
     {
         // create and initialize a Time1 object
         Time1 time = new Time1(); // invokes Time1 constructor
         // output string representations of the time
         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(); // output a blank line
 
         // change time and output updated time
        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(); // output a blank line
 
        // attempt to set time with invalid values
        try
        {
            time.setTime( 99, 99, 99 ); // all values out of range
 
        } // end try
        catch ( IllegalArgumentException e )
        {
            System.out.printf( "Exception: %s\n\n", e.getMessage() );
        } // end catch
 
        // display time after attempt to set invalid values
        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() );
    } // end main
 } // end class Time1Test


Berikut merupakan outputnya :






2. Controlling Access to Member

Anggota private tidak dapat diakses di luar kelas.

Berikut merupakan source code MemberAccessTest :


/**
 * Private members of class Time1 are not accesible.
 *
 * @author (Sabrina Lydia S)
 * @version (11/10/2020)
 */
public class MemberAccessTest
{
   public static void main (String[] args)
   {
       //create and intialize Time1 object
       Time1 time = new Time1(); //create and intialize Time1 object
       
       time.hour = 7;   //Error : hour has private acces in Time1
       time.minute = 15;//Error : minute has private access in Time1
       time.second = 30;//Error : second has private access in Time1
   }//end main    
}


Berikut yang terjadi saat di compile :



3. Reffering to Current Object's Members with This Reference 

Program ini digunakan secara implisit dan eksplisit untuk merujuk kepada anggota dari sebuah objek dengan menggunakan kata kunci "this".

Berikut merupakan source code ThisTest :

/**
 * This used implicity and expicitly to refer to members of ab object
 *
 * @author (Sabrina Lydia S)
 * @version (11/10/2020)
 */
public class ThisTest
{
    public static void main(String[] args)
    {
        SimpleTime time = new SimpleTime(15,30,19);
        System.out.println(time.buildString() );
    }//end main
}
class SimpleTime
    {
        private int hour;   //0-23
        private int minute;  //0-59
        private int second; //0-59
        
        //if the constructor uses parameter names identical to
        //instance variables names the "this" reference is
        //required to distinguish between teh names
        
        public SimpleTime (int hour, int minute, int second)
        {
            this.hour = hour;
            this.minute = minute;
            this.second = second;
        }//end simpleTime constructor
        
        //use explicit and implicit "this" to call toUniversalString
        public String buildString()
        {
            return String.format("%24s: %s\n%24s: %s",
            "this.toUniversalString()",this.toUniversalString(),
            "toUniversalString()",toUniversalString() );
        }//end method buildString
            
        //convert to string in universal time format(HH:MM:XX)
        public String toUniversalString(){
            //"this" is not required here ro access instance variables,
            //because method does not have local variables woth same 
            //names as instance variables
            return String.format("%02d:%02d:%02d", this.hour,this.minute,this.second );
        }//end method toUniversalString
    }//end class simpleTime


Berikut merupakan outputnya :



4.Overloaded Constractors

Constructor bisa dideklarasikan untuk menentukan bagaimana objek dari suatu kelas dapat diinisialisasikan, dan di sini dibuat suatu class yang terdiri dari beberapa overloaded constractors.

Berikut merupakan source code Time2 :

/**
 * Time Class declaration with overloaded constructors.
 *
 * @author (Sabrina Lydia S)
 * @version (11/10/2020)
 */
public class Time2
{
    private int hour;   //0-23
    private int minute; // 0-59
    private int second; //0-59
   
    //Time2 no-argument constructor:
    //initailizes each instancevariable to zero
    public Time2()
    {
        this (0,0,0); //onvoke Time2 constructor with three arguments
    }//end time2 no-argument constructor
   
    //Time2 constructor : hour supplied, minute and second defaulted to 0
    public Time2( int h )
    {
        this(h,0,0);//invoke Time2 constructor with three arguments
    }//end Time2 one argument constructor
   
    //Time2 constructor : hour and minute supplied, second defaulted to 0
    public Time2(int h, int m)
    {
        this(h,m,0);//invoke Time2 constructor with three arguments
    }//end Time2 two argument constructor
   
    //Time2 constructor : hour,minute, and second supplied
    public Time2(int h,int m,int s)
    {
        setTime(h,m,s);//invoke setTime to validate time
    }//end time2 three argument constructor
   
    //Time2 constructor : another Time2 object supplied
    public Time2(Time2 time)
    {
        //invoke time2 three argument consructor
        this (time.getHour(), time.getMinute(), time.getSecond() );
    } //end Time2 constructor with a Time2 object argument
   
   
    //Set Methods
    //Set a new time value using universal time;
    //validate the data
   
    public void setTime (int h, int m, int s)
    {
        setHour( h );     //set the hour
        setMinute( m );   //set the minute
        setSecond( s );   //set the second  
    }//end method setMinute
   
   
    public void setHour(int h)
    {
        if(h>=0 && h<24)
            hour = h;
        else
            throw new IllegalArgumentException("hour must be 0-23");
    }//end method set hour
   
    public void setMinute(int m)
    {
        if(m>=0 &&m<60)
            minute = m;
        else
            throw new IllegalArgumentException("minute must be 0-59");
    }//end method setMinute
   
    //validate and set second
    public void setSecond(int s)
    {
        if (s>=0 && s<60)
            second = ((s>=0 && s<60)? s:0);
           
        else
            throw new IllegalArgumentException("second must 0-59");
    } //end method setSecond
   
    //get method
    //get hour value
    public int getHour()
    {
        return hour;
    }//end method getHour
   
    //get minute value
    public int getMinute()
    {
        return minute;
    }//end method get minute
   
    //det second value
    public int getSecond()
    {
        return second;
    }//end method get second
   
    //convert to String in universal time format (HH:MM:SS)
    public String toUniversalString()
    {
        return String.format("%02d:%02d:%02d",getHour(), getMinute(), getSecond());
    } //end method toUniversalString
   
   
    //convert to String in standard in standard time format (H:MM::SS AM or PM)
    public String toString(){
        return String.format("%d:%02d:%02d: %s",
        ((getHour() == 0 || getHour() ==12)? 12 : getHour() %12),
        getMinute(),getSecond(),(getHour() <12 ? "AM" : "PM" ));
    } //end method toString
   
}//end class Time2


Berikut merupakan source code Time2Test :

<?php
     echo"Saya Sedang Makan";
     echo"Saya Sedang Minum";
?>


Berikut merupakan outputnya :



Komentar

Postingan populer dari blog ini

Ticket Machine

Login Panel

Space Invaders