i missed lecture today and this is what they did in class. im not sure how to complete this. hope you all can help me out.

class Clock
{
// Declare fields of the class
// 0 <= hours < 24, 0 <= minutes < 60, 0 <= seconds < 60
private int hours, minutes, seconds;
Clock(int hh, int mm, int ss) //constructor
{
}
//increase time by sec seconds
public void incrementSeconds(int sec)
{
}
//increase time by min minutes
public void incrementMinutes(int min)
{
}
//increase time by hh hours,
//if hours reach 24, simply wrap around to 0.
public void incrementHours(int hh)
{
}
public void addTime(Clock C) //add C into the clock
{
}
//print time in hours:minutes:seconds am(or pm) format
public void printTime()
{
}
}
class ClockDemo
{
public static void main(String [] args)
{
//write each statement for each operation below
(create Clock object C1 with h:m:s = 0:0:0)
(create second Clock object C2 with h:m:s=12:35:59)
(add C2 into C1)
(print C1)
(print C2)
(increase clock C1 by 1 seconds)
(print C1)
(increase clock C1 by 100 minutes)
(print C1)
(increase clock C1 by 10 hours)
(print C1)
(print C2)
}
}

Dani AI

Generated

This thread asks for a simple, robust implementation of a Java Clock class (hours 0..23, minutes 0..59, seconds 0..59). posted the skeleton; correctly explained the required steps and reminded about Java case sensitivity (class/constructor names are exact). A concise, dependable strategy is to keep a single private normalize() helper that carries seconds -> minutes -> hours and wraps hours modulo 24. All mutators then update fields and call normalize().

Key points to implement (in plain terms):

  • Constructor: assign hh/mm/ss and call normalize(); validate non-negative inputs.
  • incrementSeconds/minutes: add the amount and normalize so carries happen automatically.
  • incrementHours: add hours and wrap with modulo 24 (no carry beyond day).
  • addTime(Clock c): add c's fields to this instance and normalize.
  • printTime: convert internal 24-hour state into 12-hour output with am/pm and zero-padded minutes/seconds (0:00:00 -> 12:00:00 am, 12:00:00 -> 12:00:00 pm).

Example implementation (keeps behavior predictable and easy to test):

public class Clock {
    private int hours, minutes, seconds;

    public Clock(int hh, int mm, int ss) {
        if (hh < 0 || mm < 0 || ss < 0) throw new IllegalArgumentException("Negative time not allowed");
        this.hours = hh;
        this.minutes = mm;
        this.seconds = ss;
        normalize();
    }

    private void normalize() {
        if (seconds >= 60) { minutes += seconds / 60; seconds %= 60; }
        if (minutes >= 60) { hours += minutes / 60; minutes %= 60; }
        hours = ((hours % 24) + 24) % 24;
    }

    public void incrementSeconds(int sec) {
        if (sec < 0) throw new IllegalArgumentException("sec must be >= 0");
        seconds += sec; normalize();
    }

    public void incrementMinutes(int min) {
        if (min < 0) throw new IllegalArgumentException("min must be >= 0");
        minutes += min; normalize();
    }

    public void incrementHours(int hh) {
        if (hh < 0) throw new IllegalArgumentException("hh must be >= 0");
        hours = (hours + hh) % 24;
    }

    public void addTime(Clock c) {
        if (c == null) return;
        seconds += c.seconds; minutes += c.minutes; hours += c.hours; normalize();
    }

    public void printTime() {
        int displayHour = hours % 12; if (displayHour == 0) displayHour = 12;
        String ampm = (hours < 12) ? "am" : "pm";
        System.out.printf("%d:%02d:%02d %s%n", displayHour, minutes, seconds, ampm);
    }
}

Troubleshooting notes: common mistakes are forgetting to carry seconds/minutes, wrapping hours to 12 instead of 24, and using the wrong constructor name case. Test boundary cases explicitly (23:59:59 + 1s, 11:59:59 + 1s, 12:00:00) to confirm normalize() and printTime() behave as expected. This complements the earlier guidance from and the syntax reminder from .

Recommended Answers

All 6 Replies

So "what they did in class" was to assign a lot of blank methods that you are supposed to implement. You would've heard the teacher's spiel about doing your own work, too, except you weren't there for that either. My point here is this: do your own work. We're here to help people learn, not to do people's work. Oh, and btw: you also didn't follow the forum rules by posting code tags, and you also did not ask any questions.

ive heard the spiel of doing my own work, copying code is the same as plagiarism. im not asking anyone to completely solve this and give me the code... i dont understand what this is even asking. looking at that it doesnt make any since to me.

i missed lecture today and this is what they did in class. im not sure how to complete this. hope you all can help me out.

My guess is that you missed the lecture because you were held over in Engrish?

This is your assignment:

Complete the Clock class. You have been supplied with a nice template for which (as mentioned above in another response) you need to supply the implementation code for the mutator and accessor methods. You only have 3 states to mess with, so it should not take a lot of effort.

In addition, you need to complete the ClockDemo class which will demonstrate the following:

1. Creating 2 different instances of the class Clock.
(create Clock object C1 with h:m:s = 0:0:0)
(create second Clock object C2 with h:m:s=12:35:59)

2. Adding the value of the second class to the first and printing both using the printTime() method.
(add C2 into C1)
(print C1)
(print C2)

3. Modifying the first instance values 3 different times, printing the result after each modification.
(increase clock C1 by 1 seconds)
(print C1)
(increase clock C1 by 100 minutes)
(print C1)
(increase clock C1 by 10 hours)
(print C1)

4. Just for kicks, calling the printTime() method of the second object one more time.
(print C2)

Hopefully this provides the clarity you were asking for.

yes thanks, that helps me understand it a little better. im still not really figuring out how to call the functions properly but i guess ill just mess with it a little more and see what i can do.

yes thanks, that helps me understand it a little better. im still not really figuring out how to call the functions properly but i guess ill just mess with it a little more and see what i can do.

Here is a resource that you can look at for an example of how to create your implementations of your Clock class, and how to call the methods (functions) of it.

http://www.javacoffeebreak.com/java102/java102.html

It will most certainly help you to understand classes enough for your assignment. Read the entire document, and study the design of the class 'Account' structure. It has a similar type of structure to your clock Class, with some hints in it as to how you would deal with modifying your states (variables) in the class. It also contains examples of creating objects and how to call the methods of the instance.

FYI in the link that Patrick gave you, the ideas are good so far as I've seen, but the syntax would not compile. You cannot create a new 'Account' Object by saying account myaccount = new account(). You must say Account account = new Account(); (or whatever the constructor is). Java is case sensitive. And these are the "official" Java tutorials on class, objects, etc and they are excellent. So if you get through the tutorial Patrick gave you and still don't get it, try out the java sun one.

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.