I have the following program to write: given the TimeAT class:

The time class uses two integers, one to store the hours and one to store the minutes of a given point in time.

Write a Clock class which uses TimeAT class objects to store the current time and an alarm time. The Clock class should have 3 constructors:

  • A no-arg constructor
  • A constructor which takes one TimeAT object as an argument and sets the current time
  • A constructor which takes two times objects as arguments and sets both times

The Clock class should also have the following methods:

  • setTime – takes a TimeAT object as an argument and sets the current time
  • setAlarm – takes a TimeAT object as an argument and sets the alarm time
  • getTime – returns the current time in the form of a TimeAT object
  • getAlarm – returns the alarm time in the form of a TimeAT object
  • toString – returns a string which contains both the current time and the alarm time

Write a driver program to test your Clock class. The driver program should allow the user to enter both the current time and the alarm time. It should create one or more Clock objects to demonstrate usage of the constructors, and it should print the Clock objects.

I have written the Clock class, but am confused about how to set up the constructors... I also started the driver and tested the TimeAT class which returns. Here is what I have so far if anyone can offer me some assistance with the constructors I would appreciate it.... thanks!

This is the given TimeAT class:

public class TimeAT 
{

    private int hours;
    private int minutes;

    // Assigns 12pm to the time.
    public TimeAT()
    {
        hours = 12;
        minutes = 0;
    }

    public TimeAT(int h, int m)
    {
        if(h >= 0 && h <= 24)
            hours = h;
        else
            hours = 12;
        if(m >= 0 && m < 60)
            minutes = m;
        else
            minutes = 0;
    }

    public boolean setHours(int h)
    {
        if(h >= 0 && h <= 24)
        {
               hours = h;
               return true;
        }
        return false;
    }

    public boolean setMinutes(int m)
    {
        if(m >= 0 && m < 60)
        {
               minutes = m;
               return true;
        }
        return false;
    }

    public int getHours()
    {
        return hours;
    }

    public int getMinutes()
    {
        return minutes;
    }

    public String toString()
    {
        String str = hours + ":" + minutes ;
        if(minutes == 0)
            str = str + "0";


        return str;

    }
}

This is my Clock class so far:

public class Clock 
{


    private int time;
    private int alarm;


    // no arg constructor-- wasnt sure what to set them equal to
    public Clock()
    {
        time = 0;
        alarm = 0;


    }

    // Constructor 2
    public Clock(int t, int a)
    {
        time = t;
        alarm = a;

    }


    //Constructor 3--- Do not understand passing multiple objects

    public Clock()
    {


    }


    //setTime and setAlarm method---X

    public void set(int t, int a)
    {
        time = t;
        alarm = a;
    }

//getTime---X

public int getTime()
{
    return time;
}

//getAlarm---X

public int getAlarm()
{
    return alarm;
}


//toString---X
public String toString()
{
    String str = "Current time: " + time
            + "\nAlarm time: " + alarm;

    return str;
}

This is the driver that I have created... the first part returns correctly, but the second does not...

public class ClockDemo {



    public static void main(String[] args)
    {

       TimeAT myTime = new TimeAT(4,50);

       System.out.println(myTime);

       Clock myClock = new Clock(3,50);
       System.out.println(myClock);

    }


}

Thank you for any help I receive!

Write a Clock class which uses TimeAT class objects to store the current time and an alarm time. The Clock class should have 3 constructors:
- A no-arg constructor
- A constructor which takes one TimeAT object as an argument and sets the current time
- A constructor which takes two times objects as arguments and sets both times

class Clock {
 private TimeAT currentTime=null;
 private TimeAT alarm=null;

 public Clock () {
   currentTime = new TimeAT();
   alarm = new TimeAT();
 }

  public Clock (TimeAT currentTime, TimeAT alarm) {
   this.currentTime = currentTime;
   this.alarm = alarm;
 }

  public Clock (TimeAT currentTime) {
   this.currentTime = currentTime;
   this.alarm = new TimeAT();
 }

}

Now write get, set methods with the right arguments and the right return types according to the above. If you are not familiar with the 'this' keyword then you cal also do this:

public Clock (TimeAT curr, TimeAT al) {
   currentTime = curr;
   alarm = al;
 }

no offence, but I don't think you really got the point

in neither of your constructors should be an int as parameter

your task is to use a TimeAT object (which you're not doing)

so: you'll have a class that hase:

private TimeAT time = null; // or new TimeAT(); if you like
private TimeAT alarm = null;

and another class which has the next constructors:
1. no arg

public Clock(){
 // either you do nothing, or set the times to default values
}

2. 1 TimeAT object, which sets the time

public Clock(TimeAT newTime){
  // you set the time 
}

3. 2 TimeAT objects, for time and alarm

public Clock(TimeAT time, TimeAT alarm){
// set the values for time and alarm
}

I haven't looked at the rest of your code and assignment (yet) since I'm at work myself, might have the time tonight. but if you go and use int's in your constructors, just because the TimeAT object contains variables of the type int, I can tell you now you'll fail on this assignment

Thank you Javaadict that makes so much more sense to me... I understand that I was not using the Time class... I also do not understand the driver program that tests the clock Class... I am unsure about how to have the user input the current time as well as the alarm time....


Thank you so much for your assistance.

BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));

System.out.println("Enter value:");
String input = keyboard.readLine();

System.out.println("Value entered: "+input);

int i = Integer.parseInt(input); //if an integer was given

You might want to check the Scanner class. I don't really use but I have seen a lot of people use it in this forum.

Scanner sc = new Scanner(System.in);
int i = sc.nextInt();

As you can see it is simpler

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.