PBOooo

1.Class NumberDisplay
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. public class NumberDisplay
  2. {
  3.     private int limit;
  4.     private int value;
  5.    
  6.     public NumberDisplay(int rollOverLimit)
  7.     {
  8.         limit = rollOverLimit;
  9.         value = 0;
  10.     }
  11.     public int getValue()
  12.     {
  13.         return value;
  14.     }
  15.    
  16.    
  17.    
  18.     public String getDisplayValue()
  19.     {
  20.         if(value < 10)
  21.         {
  22.             return "0" + value;
  23.         }
  24.         else
  25.         {
  26.             return "" + value;
  27.         }
  28.     }
  29.    public void setValue(int replacementValue)
  30.     {
  31.         if((replacementValue >= 0) && (replacementValue < limit))
  32.         {
  33.             value = replacementValue;
  34.         }
  35.     }
  36.     public void increment()
  37.     {
  38.         value = (value + 1) % limit;
  39.     }
  40. }
2.Class ClockDisplay
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. public class ClockDisplay {
  2.    private NumberDisplay hours;
  3.    private NumberDisplay minutes;
  4.    private String displayString;
  5.    
  6.    public ClockDisplay()
  7.    {
  8.        hours = new NumberDisplay(24);
  9.        minutes = new NumberDisplay(60);
  10.        updateDisplay();
  11.    
  12.    }
  13.    
  14.    public ClockDisplay(int hour, int minute)
  15.    {
  16.        hours = new NumberDisplay(24);
  17.        minutes = new NumberDisplay(60);
  18.        setTime(hour, minute);
  19.        
  20.    }
  21.    
  22.    public void timeTick()
  23.    {
  24.        minutes.increment();
  25.        if(minutes.getValue() == 0)
  26.        {
  27.            hours.increment();
  28.            
  29.        }
  30.        updateDisplay();
  31.    }
  32.    
  33.    public void setTime(int hour, int minute)
  34.    {
  35.        hours.setValue(hour);
  36.        minutes.setValue(minute);
  37.        updateDisplay();
  38.    }
  39.    
  40.    public String getTime()
  41.    {
  42.        return displayString;
  43.    }
  44.    
  45.    private void updateDisplay()
  46.    {
  47.        displayString = hours.getDisplayValue() + ":" + minutes.getDisplayValue();
  48.    }
  49.    
  50. }
3.Class TestClockDisplay
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. public class TestClockDisplay
  2. {
  3.     public TestClockDisplay()
  4.     {
  5.     }
  6.     public void test()
  7.     {
  8.      
  9.         ClockDisplay clock = new ClockDisplay();
  10.        
  11.         clock.setTime(21,59);
  12.         System.out.println(clock.getTime());
  13.        
  14.        
  15.         clock.setTime(12,39);
  16.         System.out.println(clock.getTime());
  17.        
  18.         clock.setTime(14,89);
  19.         System.out.println(clock.getTime());
  20.        
  21.        
  22.      
  23.     }
  24. }
Bentuk Hubungan antar Class Output Program Siswa Class student
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. public class Student
  2. {
  3.     // instance variables - replace the example below with your own
  4.     private String nama = "Aguel";
  5.     private double grade = 4;
  6.    
  7.     public String getName(){
  8.         return nama;
  9.     }
  10.    
  11.     public void printGrade(){
  12.         System.out.println(grade);
  13.     }
  14. }
class siswa
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. public class Siswa {
  2.     public static void main() {
  3.         Student b = new Student();
  4.         String nama;
  5.         nama = b.getName();
  6.         System.out.println(nama);
  7.         b.printGrade();
  8.     }
  9. }

Komentar

Postingan Populer