Advertisement
thenewboston

Java Programming Tutorial - 40 - Set and Get Methods

Aug 22nd, 2014
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.92 KB | None | 0 0
  1. public class tuna {
  2.    private int hour;
  3.    private int minute;
  4.    private int second;
  5.    
  6.    public tuna(){
  7.       this(0,0,0);
  8.    }
  9.    public tuna(int h){
  10.       this(h,0,0);
  11.    }
  12.    public tuna(int h, int m){
  13.       this(h,m,0);
  14.    }
  15.    public tuna(int h, int m, int s){
  16.       setTime(h,m,s);
  17.    }
  18.    public void setTime(int h, int m, int s){
  19.       setHour(h);
  20.       setMinute(m);
  21.       setSecond(s);
  22.    }
  23.    public void setHour(int h){
  24.       hour = ((h>=0 && h<24)?h:0);
  25.    }
  26.    public void setMinute(int m){
  27.       minute = ((m>=0 && m<60)?m:0);
  28.    }
  29.    public void setSecond(int s){
  30.       second = ((s>=0 && s<60)?s:0);
  31.    }
  32.    public int getHour(){
  33.       return hour;
  34.    }
  35.    public int getMinute(){
  36.       return minute;
  37.    }
  38.    public int getSecond(){
  39.       return second;
  40.    }
  41.    public String toMilitary(){
  42.       return String.format("%02d:%02d:%02d", getHour(), getMinute(), getSecond());
  43.    }
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement