Using the time class (24hr clock)

// jamesprogram.java
class jamesprogram {

	public static void main (String[] args){
		
		// make a cat object to we can use the methods
		// within the cat class
		cat catObject = new cat();
		
		System.out.println(catObject.militaryTime());
		catObject.makeTime(14,15,20);		
		System.out.println(catObject.militaryTime());
		
	}
}
//cat.java
public class cat {

	private int hour;
	private int minute;
	private int second;
	
	public void makeTime (int h, int m, int s){
		// check data inputed is right
		hour = ((h>=0 && h<24)? h : 0);
		minute = ((m>=0 && m<60)? m : 0);
		second = ((s>=0 && s<60)? s : 0);
	}
	
	// display through military time 12 to 24
	public String militaryTime(){
		// display first one to two decimal places
		// add a colon, display second one to two
		// decimal places
		return String.format("%02d:%02d:%02d", hour, minute, second);
	}
}

Outputs:

00:00:00
14:15:20

Leave a Reply