-
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!!!
Code:
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?
}
}
Last edited by syannant; 02-05-2006 at 02:58 PM.
-
You want your printTime() method to be abstract. This is probably what your Clock class should look like:
Code:
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!
Last edited by destin; 02-05-2006 at 03:18 PM.
-
ok but when i make the extended classes do i just code in system.out.print(hr":"min":"sec); ??
-
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).
Code:
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);
}
}
-
YEAH thats right the + oh ok thanx you're really helping it come back to me thanx ill post if i have more probs
-
ok im having trouble getting the alarmTime heres what i got so far
Code:
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?
-
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:
Code:
public String toString() {
return "RadioAlarmClock[frequency=" + frequency + ",alarmTime=" + alarmTime + "]";
}
The setAlarmTime(String) is very simple:
Code:
public void setAlarmTime(String AT) {
alarmTime = AT;
}
Hope this helps.
-
thanks alot ill try it out right now
-
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
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!!!
-
 Originally Posted by syannant
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
Code:
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
Code:
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
Code:
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
Code:
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.
-
dude your freakin awesome thanks
-
arite! it works now i gotta finish up that alarm
-
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?
-
wait we already talked about that i realized but where do you get alarmTime from?
-
It's declared in the beggining of the class.
 Originally Posted by syannant
Code:
public class RadioAlarmClock extends DigitalClock
{
double radioFrequency;
String alarmTime;
// ...
Similar Threads
-
By Gary Nelson in forum .NET
Replies: 277
Last Post: 10-01-2003, 12:00 AM
-
By Robert G in forum .NET
Replies: 84
Last Post: 02-08-2001, 02:38 PM
-
By Todd B - Agendum Software in forum vb.announcements
Replies: 0
Last Post: 09-13-2000, 10:18 AM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks