Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Clock {
- /**
- * @param args the command line arguments
- */
- // Fields
- private int hours;
- private int minutes;
- private int seconds;
- /**
- *
- */
- public Clock(){
- hours=0;
- minutes=0;
- seconds=0;
- }
- // Constructor
- /**
- * Sets the hours, minutes and seconds fields to the given parameters h, m
- * and s, if h is between 0 and 23, m and s are between 0 and 59
- * inclusively. If at least on of the parameters is not in the valid range,
- * the fields remains unchanged.
- *
- * @param h the hours to set
- * @param m the minutes to set
- * @param s the seconds to set
- * @throws Exception if the parameters are not in the range
- */
- public Clock(int h, int m, int s) throws Exception{
- if (h>=24 || h<0 || m>=60 || m<0 || s>=60 || s<0){
- throw new Exception("Invalid time format!");
- } else {
- hours = h; minutes = m; seconds = s;
- }
- }
- // Reset method
- /**
- * Resets all fields to 0.
- *
- */
- public void resetToMidnight() {
- hours = 0; minutes = 0; seconds = 0;
- }
- // Check if morning method
- /**
- * Shows is it morning by returning boolean.
- *
- * @return
- */
- public boolean isMorning() {
- return (hours>=5 && hours<12);
- }
- // Advance one second method
- /**
- * Does one tick.
- *
- */
- public void tick() {
- seconds++;
- if (seconds >= 60) {
- seconds = 0;
- // need to increment mins, etc.
- minutes++;
- if (minutes>=60){
- minutes=0;
- hours++;
- if (hours>=24){
- hours=0;
- }
- }
- }
- }
- /**
- * Shows the fields in time format.
- *
- * @return String
- */
- public String toString(){
- String sh, sm, ss;
- if (hours<10){
- sh="0"+hours;
- } else {
- sh=""+hours;
- }
- if (minutes<10){
- sm="0"+minutes;
- } else {
- sm=""+minutes;
- }
- if (seconds<10){
- ss="0"+seconds;
- } else {
- ss=""+seconds;
- }
- return sh+":"+sm+":"+ss;
- }
- /**
- * Returns hors field value.
- *
- * @return the hours
- */
- public int getHours() {
- return hours;
- }
- /**
- * Sets the hours field to the hours, if the hours is in range from 0 to 23
- * inclusively. If the hours parameter is not in the range, the hours field
- * remains unchanged.
- *
- * @param hours the hours to set
- * @throws Exception is hours is not in the range
- */
- public void setHours(int hours) throws Exception{
- if (hours>=24 || hours<0){
- throw new Exception("Invalid hours format!");
- }
- this.hours = hours;
- }
- /**
- * Gets the value of minutes field.
- *
- * @return the minutes
- */
- public int getMinutes() {
- return minutes;
- }
- /**
- * Sets the minutes field to the minutes, if the parameter minutes is in
- * range from 0 to 59 inclusively. If the minutes parameter is not in the
- * range, the minutes field remains unchanged.
- *
- * @param minutes the minutes to set
- * @throws Exception if minutes is not in the range
- */
- public void setMinutes(int minutes) throws Exception{
- if (minutes>=60 || minutes<0){
- throw new Exception("Invalid minutes format!");
- }
- this.minutes = minutes;
- }
- /**
- * @return the seconds
- */
- public int getSeconds() {
- return seconds;
- }
- /**
- * Sets the seconds field to the seconds, if the parameter seconds is in
- * range from 0 to 59 inclusively. If the seconds parameter is not in the
- * range, the seconds field remains unchanged.
- *
- * @param seconds the seconds to set
- * @throws Exception if seconds is not in the range
- */
- public void setSeconds(int seconds) throws Exception{
- if (seconds>=24 || seconds<0){
- throw new Exception("Invalid seconds format!");
- }
- this.seconds = seconds;
- }
- }
- public class testClock {
- public static void main(String[] args){
- Clock myClock = new Clock();
- try{
- myClock=new Clock(24, 13, 30);
- } catch (Exception ex) {
- System.out.println(ex.getMessage());
- }
- System.out.println(myClock);
- try{
- myClock.setHours(24);
- } catch (Exception ex){
- System.out.println(ex.getMessage());
- }
- System.out.println(myClock);
- try{
- myClock.setMinutes(60);
- } catch (Exception ex){
- System.out.println(ex.getMessage());
- }
- System.out.println(myClock);
- try{
- myClock.setSeconds(60);
- } catch (Exception ex){
- System.out.println(ex.getMessage());
- }
- System.out.println(myClock);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment