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!