Member Avatar for 9w43
9w43

Hello, I have a class named Time and when I run it I can enter the time, increase the hours and minutes, the minutes and hours will stored in their fields.

I have another class called TimeTest, when this class is running, it needs to display the test results of my Time class, test results are basically what time I entered and what time it is. I need to return the results on terminal.

Like the time is etc
Time was increased x timees

I used ++ increment on hours and minutes when increasing
Here is my time class code

How can I code this! Can you put it in a simple form, thanks

/**
 * Handles time(hour:minutes) 
 * If given hour/minutes outside appropriate range, sets to 0
 * @author A.A.Marczyk
 * @version 22/11/06
 */

    // Fields being declared
    // Task 1.1
    public class Time
    {
        private static int hour;
        private static int minute;
        public static int displayTime;
        public static String names = null;
    
        // Constrcuter resets objects
        // Task 1.2
        public Time()
        {
            hour = 0;
            minute = 0;
            displayTime = 0;
        }
    
        // Mutator for hour field
        // Task 1.3
        private void setHour (int newHour)
        {
            if (hour < 23)
            {
                hour = newHour;
            }
            else 
            { 
                hour = 0;
            }
            if (hour > 23)
            {
                hour = 0;
            }
            else
            {
                
            }
        }

        // Mutator to set minute
        // Task 1.4
        private void setMinute (int newMinute)
        {
            if (newMinute <59)
            {
                minute = newMinute;
            }
            else
            {
                minute = 0;
            }
            
            if (newMinute >59)
            {
                minute = 0;
            }
            else
            {

            }
        }
  
        // Method for reseting hours
        // Task 1.5
        public void setTime (int newTimeHour, int newTimeMinute)
        {
            setHour (newTimeHour);
            setMinute (newTimeMinute);
        }
                
        // Constructor for time
        // Task 1.6
        public void increaseTime ()
        {
            
            if (hour < 23)
            {
                hour++;
            }
            else
            {
                hour = 0;
            }
            if (minute < 59)
            {
                minute++;
            }
            else
            {
                minute = 0;
            }
        }

        //Accessor to display time in correct format 
        // There are some new local variables declared
        // Task 1.7
        public String getAsString ()
        {
            int zero;
            char doubleDot;
            zero = 0;
            doubleDot = ':';
            displayTime = hour + minute;
            names = "Time is " + hour + minute;
            
            if ((hour <10) && (minute <10))
            {
                return "0" + hour + doubleDot + zero + minute;
            }
             if ((hour >10) && (minute <10))
            {
                return "" + hour + doubleDot + zero + minute;
            }
             if ((hour <10) && (minute >10))
            {
                return "0" + hour + doubleDot + minute;
            }
            else
            {
                return " " + hour + doubleDot + minute;
            }
        }   

 
    
    // These methods required ONLY for Task 6
    public int getElapsedTime(Time that)
    {
        int thisTime = this.getTotalMinutes();
        int thatTime = that.getTotalMinutes();
        if (thisTime > thatTime)
        {
            return thisTime - thatTime;
        }
        return thatTime - thisTime;
    }
    
    private int getTotalMinutes()
    {
        return -1;  // you must amend this code in task 6 of Cwk 3
    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.