Clock project
hey guys im not very good with java, and the new semester just started and im having alot of trouble with our 1st assignment, what we need to do is make an abstract class clock, which i have completed all except the printTime method, where my troubles begin. I also have to make 2 sub-classes DigitalClock where the time is printed in the format hr:min:sec, and AnalogClock hr,min,sec. I dont even know where to begin on those. Lastly i have to make 2 more subclasses for an alarm for both the dig and analog heres my code for the abstract class any help you guys can give will be extremely appreciated!!!
public abstract class Clock
{
public int hours, minutes, seconds;
public int getSec()
{
return seconds;
}
public int getMin()
{
return minutes;
}
public int getHour()
{
return hours;
}
public void setTime(int hr, int min, int sec)
{
if (0 <= hours || hours < 24)
hr = hours;
else hr = 0;
if (0 <=minutes || minutes < 60)
min = minutes;
else
min = 0;
if (0 <= seconds || seconds < 60)
sec = seconds;
else
sec = 0;
}
public void printTime (int hr, int min, int sec)
{
//what do i do here?
}
}
[1469 byte] By [
syannant] at [2007-11-11 7:56:11]

# 1 Re: Clock project
You want your printTime() method to be abstract. This is probably what your Clock class should look like:
public abstract class Clock {
protected int hour;
protected int minute;
protected int second;
public Clock(int hour, int minute, int second) {
setTime(hour, minute, second);
}
public void setTime(int hour, int minute, int second) {
setHour(hour);
setMinute(minute);
setSecond(second);
}
public void setHour(int hour) {
if (hour < 0 || hour > 12) {
throw new IllegalArgumentException();
}
this.hour = hour;
}
public void setMinute(int minute) {
if (minute < 0 || minute > 60) {
throw new IllegalArgumentException();
}
this.minute = minute;
}
public void setSecond(int second) {
if (second < 0 || second > 60) {
throw new IllegalArgumentException();
}
this.second = second;
}
public int getSecond() { return second; }
public int getMinute() { return minute; }
public int getHour() { return hour; }
abstract public void printTime();
}
Now you need to make DigitalClock and AnalogClock and make them extend Clock. They will need to implement the printTime() method. In each class you can customize how it prints the time. Post back if you need more help.
Good luck! :)
destin at 2007-11-11 22:37:00 >

# 2 Re: Clock project
ok but when i make the extended classes do i just code in system.out.print(hr":"min":"sec); ??
# 3 Re: Clock project
No. That won't compile. This will: System.out.print(hour + ":" + minute + ":" + second);. And you only want to do this in DigitalClock (read the instructions).
public class DigitalClock extends Clock {
public DigitalClock(int hour, int minute, int second) {
super(hour, minute, second);
}
public void printTime() {
System.out.println(hour + ":" + minute + ":" + second);
}
}
destin at 2007-11-11 22:39:04 >

# 4 Re: Clock project
YEAH thats right the + oh ok thanx you're really helping it come back to me thanx ill post if i have more probs
# 5 Re: Clock project
ok im having trouble getting the alarmTime heres what i got so far
public class RadioAlarmClock extends DigitalClock
{
double radioFrequency;
String alarmTime;
public RadioAlarmClock (int hours, int minutes, int seconds)
{
super(hours, minutes, seconds);
}
public double getRF()
{
return radioFrequency;
}
public String getAT()
{
return alarmTime;
}
public void setRF(double RF)
{
if (85.5 <= radioFrequency || radioFrequency > 106.8 )
RF = radioFrequency;
else RF = 100.0;
}
public DigitalClock toString;
public void setAT(String AT)
{
}
}
what should i do to set the AT, and also is that correct when i convert DigitalClock toString? or should something else be converted toString? or is that even the right type of alarmTime?
# 6 Re: Clock project
The toString() method overrides the toString() method in the Object class. Basically, it returns a String version of the class. Yours might look like this:
public String toString() {
return "RadioAlarmClock[frequency=" + frequency + ",alarmTime=" + alarmTime + "]";
}
The setAlarmTime(String) is very simple:
public void setAlarmTime(String AT) {
alarmTime = AT;
}
Hope this helps.
destin at 2007-11-11 22:42:07 >

# 7 Re: Clock project
thanks alot ill try it out right now
# 8 Re: Clock project
man u are really awesome and i absolutly hate to keep bothering you but one more so sorry
i almost have the test working but it just it prints all 0s for everything and im not sure how to test the radio alarm clock
heres the code
import java.util.*;
public class Tester
{
static Scanner console = new Scanner(System.in);
public static void main(String[]args)
{
Clock DigitalClock = new DigitalClock(5, 4, 30);
Clock AnalogClock = new AnalogClock(6, 7, 36);
System.out.print("Digital Clock = ");
DigitalClock.printTime();
System.out.println();
DigitalClock.setTime(5, 4, 30);
System.out.print("Analog Clock = ");
AnalogClock.printTime();
System.out.println();
AnalogClock.setTime(6, 7, 36);
}
}
again thank you so much!!!
# 9 Re: Clock project
man u are really awesome and i absolutly hate to keep bothering you but one more so sorry
You're not bothering me. I'm helping you. No one is forcing me to reply.
As for your problem, there's nothing wrong with the class you posted. There's something wrong w/ your other classes.
Here's how I did them. Compare them to yours and track down the bug:
Clock.java
public abstract class Clock {
protected int hour;
protected int minute;
protected int second;
public Clock(int hour, int minute, int second) {
setTime(hour, minute, second);
}
public void setTime(int hour, int minute, int second) {
setHour(hour);
setMinute(minute);
setSecond(second);
}
public void setHour(int hour) {
if (hour < 0 || hour > 12) {
throw new IllegalArgumentException();
}
this.hour = hour;
}
public void setMinute(int minute) {
if (minute < 0 || minute > 60) {
throw new IllegalArgumentException();
}
this.minute = minute;
}
public void setSecond(int second) {
if (second < 0 || second > 60) {
throw new IllegalArgumentException();
}
this.second = second;
}
public int getSecond() { return second; }
public int getMinute() { return minute; }
public int getHour() { return hour; }
abstract public void printTime();
}
DigitalClock.java
public class DigitalClock extends Clock {
public DigitalClock(int hour, int minute, int second) {
super(hour, minute, second);
}
public void printTime() {
System.out.println(hour + ":" + minute + ":" + second);
}
}
AnalogClock.java
public class AnalogClock extends Clock {
public AnalogClock(int hour, int minute, int second) {
super(hour, minute, second);
}
public void printTime() {
System.out.println(hour + "," + minute + "," + second);
}
}
ClockTester.java
public class ClassTester {
public static void main(String[]args) {
Clock digitalClock = new DigitalClock(5, 4, 30);
Clock analogClock = new AnalogClock(6, 7, 36);
System.out.print("Digital Clock = ");
digitalClock.printTime();
System.out.print("Analog Clock = ");
analogClock.printTime();
}
}
Hope this helps.
destin at 2007-11-11 22:45:00 >

# 10 Re: Clock project
dude your freakin awesome thanks
# 11 Re: Clock project
arite! it works now i gotta finish up that alarm
# 12 Re: Clock project
ok heres my next question - for the alarm class - the alarmTime has to be String correct? if so how do i set the Alarm time?
# 13 Re: Clock project
wait we already talked about that i realized but where do you get alarmTime from?
# 14 Re: Clock project
It's declared in the beggining of the class.
public class RadioAlarmClock extends DigitalClock
{
double radioFrequency;
String alarmTime;
// ...
destin at 2007-11-11 22:50:14 >

# 15 Re: Clock project
ok everything works cept for setting the radio frequency it just prints 0.0
public class RadioAlarmClock extends DigitalClock
{
public double radioFrequency;
public String alarmTime;
public RadioAlarmClock(double radioFrequency, int hours, int minutes, int seconds)
{
super(hours, minutes, seconds);
}
public void setRF(double radioFrequency)
{
if (85.5 <= radioFrequency || radioFrequency > 108.0)
this.radioFrequency = radioFrequency;
else this.radioFrequency = 100;
}
public double getRF()
{
return radioFrequency;
}
public void setAlarmTime(String AT)
{
alarmTime = AT;
}
public void printAlarm()
{
System.out.print(radioFrequency + " at " + hours + ":" + minutes + ":" + seconds);
System.out.println();
}
}
# 16 Re: Clock project
any1 have any ideas??
# 17 Re: Clock project
Look in your constructor. You accept an argument for radioFrequency, but don't use it. You probably want something like setRF(radioFrequency);.
destin at 2007-11-11 22:53:13 >

# 18 Re: Clock project
thanks alot again if that works thisll be done!
# 19 Re: Clock project
thank you so much destin it works!!!!
Thread CLOSED