From 0857874b423e35c01c1de4ebd6c19e53a68f76f8 Mon Sep 17 00:00:00 2001 From: Yuhuang Hu Date: Wed, 29 Feb 2012 18:19:04 +0800 Subject: [PATCH] lab 2 --- lab_answer/lab2/Time24.java | 104 ++++++++++++++++++++++++++++++++++ lab_answer/lab2/TimeCard.java | 20 +++++++ 2 files changed, 124 insertions(+) create mode 100644 lab_answer/lab2/Time24.java create mode 100644 lab_answer/lab2/TimeCard.java diff --git a/lab_answer/lab2/Time24.java b/lab_answer/lab2/Time24.java new file mode 100644 index 0000000..4fe09ce --- /dev/null +++ b/lab_answer/lab2/Time24.java @@ -0,0 +1,104 @@ +import java.text.DecimalFormat; +import java.util.Scanner; + + +public class Time24 { + private int hour; + private int minute; + + + private void normalizeTime() + { + int extraHours=minute/60; + + minute%=60; + hour=(hour+extraHours%24); + } + + public Time24() + { + hour=0; + minute=0; + } + + public Time24(int h, int m) + { + hour=h; + minute=m; + normalizeTime(); + } + + public void addTime(int m) + { + if (m<0) + { + throw new IllegalArgumentException("Time24.setTime: "+ "argument must be a positive integer"); + } + + minute+=m; + normalizeTime(); + } + + public Time24 interval(Time24 t) + { + int currTime=hour*60+minute; + int tTime=t.hour*60+t.minute; + + if (tTime