Trying to write an addClock method for a Java assignment

时间:2016-10-20 13:18:39

标签: java

I'm in my first semester learning Java and I'm struggling. For our assignment, we're writing code that is supposed to increment a clock by one second, decrement a clock by one second, and add two clocks together. I think I've got the first two sections to work, but I'm having trouble with addClock. Here's what I have so far and I'm just not sure where to go or what's wrong with what I've got:

public class Clock
{
    //Instance variables
    private int hours;
    private int minutes;
    private int seconds;
    private int clock;
    private int totalSeconds;

    //Method to convert from totalSeconds to hours, minutes, and seconds
    public void convertSeconds(int totalSeconds)
    {
        hours = (totalSeconds / 3600) % 24;
        minutes = (totalSeconds % 3600) / 60;
        seconds = totalSeconds % 60;
    }

    //Method to convert from hours, minutes, and seconds to totalSeconds
    private int getTotalSeconds() 
    {
        int hoursInSeconds = hours * 60 * 60;
        int minutesInSeconds = minutes * 60;
        return hoursInSeconds + minutesInSeconds + seconds;
    }

    //Constructor 1: Initialize time to 0
    public Clock()
    {
        hours = 0;
        minutes = 0;
        seconds = 0;
    }

    //Constructor 2: Value of time in seconds and convert seconds to hours, minutes, and seconds
    public Clock(int totalSeconds)
    {
        convertSeconds(totalSeconds);
    }

    //Constructor 3: Passes three values for hours, minutes, and seconds
    public Clock(int hours, int minutes, int seconds)
    {
        hours = hours;
        minutes = minutes;
        seconds = seconds;
    }

    //Instance method tick() to increment a Clock object by one second
    public void tick()
    {
        convertSeconds(getTotalSeconds() + 1);
    }

    //Instance method tickDown() to decrement a Clock object by one second
    public void tickDown()
    {
        convertSeconds(getTotalSeconds() - 1);
    }

    //Accessor/getter method: getHours
    public int getHours()
    {
        return hours;
    }

    //Accessor/getter method: getMinutes
    public int getMinutes()
    {
        return minutes;
    }

    //Accessor/getter method: getSeconds
    public int getSeconds()
    {
        return seconds;
    }

    //Mutator/setter method: setHours
    public void setHours()
    {
        this.hours = hours;
    }

    //Mutator/setter method: setMinutes
    public void setMinutes()
    {
        this.minutes = minutes;
    }

    //Mutator/setter method: setSeconds
    public void setSeconds()
    {
        this.seconds = seconds;
    }

    //Mutator/setter method: setClock
    public void setClock(int totalSeconds)
    {
        this.clock = totalSeconds;
    }

    //Instance method addClock() to give the sum of two clock objects
    public void addClock(Clock clock2)
    {
        convertSeconds(clock2.getTotalSeconds() + this.getTotalSeconds());
    }

    //Main  method to test the functionality of objects
    public static void main(String[] args) 
    {
        Clock clock1 = new Clock();
        Clock clock2 = new Clock();

        int clock = 12699;
        int clock2 = 3600;

        clock1.convertSeconds(totalSeconds);
        System.out.println("Number of hours: " + clock1.getHours());
        System.out.println("Number of minutes: " + clock1.getMinutes());
        System.out.println("Number of seconds: " + clock1.getSeconds());

        clock1.tick();
        System.out.println("Number of hours: " + clock1.getHours());
        System.out.println("Number of minutes: " + clock1.getMinutes());
        System.out.println("Number of seconds: " + clock1.getSeconds());

        clock1.tickDown();
        System.out.println("Number of hours: " + clock1.getHours());
        System.out.println("Number of minutes: " + clock1.getMinutes());
        System.out.println("Number of seconds: " + clock1.getSeconds());

        clock1.addClock(clock2);    
        System.out.println("After adding the two clocks, number of hours: " + clock1.getHours());
        System.out.println("After adding the two clocks, number of minutes: " + clock1.getMinutes());
        System.out.println("After adding the two clocks, number of seconds: " + clock1.getSeconds());
    }
}

Any suggestions, advice, or help would be greatly appreciated!

3 个答案:

答案 0 :(得分:0)

You need to assign the result to this.totalSeconds

答案 1 :(得分:0)

您的代码是否已编译?

错误将在这里:

Clock clock1 = new Clock();
-> Clock clock2 = new Clock();

int clock = 12699;
-> int clock2 = 3600;

clock2被宣告2次。

答案 2 :(得分:0)

我认为您的主要问题来自于存储两个时间表示的事实:一个表示totalSeconds而另一个表示hours,{{1 }和minutes。这是相当痛苦的,因为当另一个表示更新时,你必须更新一个表示。

我建议只存储seconds,其他3个可以根据这个来推断。

只有one main constructor这是一个很好的做法,任何其他构造函数都必须调用这个。它有助于使您的代码更易于理解和维护。

以下是您可以结束的代码类型(因为它是一项任务,如果您需要,我允许您实施TODOS):

totalSeconds