Domanda

I've been working on this assignment for a while and I've hit a wall that I can't figure a way around. The goal of the assignment can be read here, to save me the trouble of typing or copying it.

http://imgur.com/be5YVst

I can't figure out the output portion of the assignment. Because the class had to be set up in such a specific way, and not just to perform a specific function. Even though the assignment says nothing about a toString() or similar method, I started to write one, but realized that I couldn't use the fields, hour, minute, and second because they're out of scope. If I tried to put them in the TimeTest program instead, I won't have permission because they're private. (They HAVE to be private.)

How can I get these converted units to be printed out in the form hour:minute:second?

Below is my current code:

public class Time 
{
    private long hour;
    private long minute;
    private long second;

    public Time()
        {
            long compTime = System.currentTimeMillis();
            this.setTime(compTime);
        }

    public Time(long hour, long minute, long second)
        {
            this.hour = hour;
            this.minute = minute;
            this.second = second;
        }

    public Time(long time)
        {
            this.setTime(time);
        }

    //three get methods
    public long getHour()
        {
            return this.hour;
        }

    public long getMinute()
        {
            return this.minute;
        }

    public long getSecond()
        {
            return this.second;
        }
    //setTime method
    public void setTime(long elapseTime)
        {
            this.hour = (elapseTime/36000000);
            this.minute = ((elapseTime - (this.hour * 36000000))/60000);
            this.second = ((elapseTime - (this.minute * 60000))/1000);
        }

    public String printFormat()
        {
        //it wouldn't let me name it toString() so I named it printFormat() instead.
        }
}
È stato utile?

Soluzione

try

@Override
public String toString() {

    // create a string from your fields

}

Altri suggerimenti

Try this,

 class Time 
    {
        private long hour;
        private long minute;
        private long second;

        public Time()
        {
            long compTime = System.currentTimeMillis();
            this.setTime(compTime);
        }

        public Time(long hour, long minute, long second)
        {
            this.hour = hour;
            this.minute = minute;
            this.second = second;
        }

        public Time(long time)
        {
            this.setTime(time);
        }

        //three get methods
        public long getHour()
        {
            return this.hour;
        }

        public long getMinute()
        {
            return this.minute;
        }

        public long getSecond()
        {
            return this.second;
        }
        //setTime method
        public void setTime(long elapseTime)
        {
            this.hour = (elapseTime/36000000);
            this.minute = ((elapseTime - (this.hour * 36000000))/60000);
            this.second = ((elapseTime - (this.minute * 60000))/1000);
        }

        public String printFormat()
        {
            return hour + ":" + minute + ":" + second;
        }
    }
    public class TimeTest
    {
        public static void main(String[] args)
        {
            System.out.println(new Time().printFormat());
        }
    }
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top