Clock project


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Page 1 of 2 12 LastLast
Results 1 to 15 of 20

Thread: Clock project

  1. #1
    Join Date
    Dec 2005
    Posts
    27

    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.

  2. #2
    Join Date
    Dec 2005
    Location
    New Jersey
    Posts
    290
    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.

  3. #3
    Join Date
    Dec 2005
    Posts
    27
    ok but when i make the extended classes do i just code in system.out.print(hr":"min":"sec); ??

  4. #4
    Join Date
    Dec 2005
    Location
    New Jersey
    Posts
    290
    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);
        }
    }

  5. #5
    Join Date
    Dec 2005
    Posts
    27
    YEAH thats right the + oh ok thanx you're really helping it come back to me thanx ill post if i have more probs

  6. #6
    Join Date
    Dec 2005
    Posts
    27
    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?

  7. #7
    Join Date
    Dec 2005
    Location
    New Jersey
    Posts
    290
    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.

  8. #8
    Join Date
    Dec 2005
    Posts
    27
    thanks alot ill try it out right now

  9. #9
    Join Date
    Dec 2005
    Posts
    27
    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!!!

  10. #10
    Join Date
    Dec 2005
    Location
    New Jersey
    Posts
    290
    Quote 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.

  11. #11
    Join Date
    Dec 2005
    Posts
    27
    dude your freakin awesome thanks

  12. #12
    Join Date
    Dec 2005
    Posts
    27
    arite! it works now i gotta finish up that alarm

  13. #13
    Join Date
    Dec 2005
    Posts
    27
    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?

  14. #14
    Join Date
    Dec 2005
    Posts
    27
    wait we already talked about that i realized but where do you get alarmTime from?

  15. #15
    Join Date
    Dec 2005
    Location
    New Jersey
    Posts
    290
    It's declared in the beggining of the class.
    Quote Originally Posted by syannant
    Code:
    public class RadioAlarmClock extends DigitalClock
    {
    	double radioFrequency;
    	String alarmTime;
    	// ...

Similar Threads

  1. Control Arrays in VB.NET
    By Gary Nelson in forum .NET
    Replies: 277
    Last Post: 10-01-2003, 12:00 AM
  2. Good Editorial by Russell Jones
    By Robert G in forum .NET
    Replies: 84
    Last Post: 02-08-2001, 02:38 PM
  3. 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
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center