1

I have 2 classes . The one is about a 24 hour clock and the second one is a subclass of the first and is a 12 hour clock. To convert the new time in second class i need to change the value of the first class. But i can't do that. I am talking about "h" variable Specifically...

 class Clock{ public int h, m , s; public String a,b,c; public void setHour(int hour){ this.h = hour; } public void setMin(int min){ this.m = min; } public void setSec(int sec){ this.s = sec; } public void tick(){ if(h != 23){ if(m == 59 && s==59){ m = 0; s=0; h++; } else if(m != 59 && s == 59){ m++; s=0; } else if ( m != 59 && s != 59){ s++; } else if( m == 59 && s!=59){ s++; } } else if(h == 23 && m == 59 && s !=59){ s++; } else if(h == 23 && m!=59 && s == 59){ s=0; s++; m++; } else if(h == 23 && m!=59 && s!=59){ s++; } else if(h == 23 && m == 59 && s == 59){ s = 0; m =0; h = 0; } } public String toString(){ a = ""; b = ""; c = ""; if (h < 10) a = "0"; if (m <10 ) b = "0"; if (s <10) c = "0"; return a+h+":"+b+m+":"+c+s; } } class AMPMClock extends Clock{ Clock clock2 = new Clock(); public void setAMPM(boolean yes){ if(yes == true){ **clock2.h = clock2.h - 12**; } } } 
3
  • i want to change the h variable if setAMPM is true. but i wont change!! It gives me 00:00:00 Commented Jan 3, 2012 at 19:30
  • shouldn't the 24 hour clock an EXTENSION to the 12 hour clock? Commented Jan 3, 2012 at 19:31
  • 1
    You should re-design your inheritance structure: it is in severe violation of the Liskov substitution principle, since a 12-hour clock is definitely not a 24-hour clock. You should have an abstract base class Clock, from which you derive both the 12-h and 24-h clock classes. Commented Jan 3, 2012 at 19:32

4 Answers 4

1

Your AMPMClock should either just extend Clock or just use it. But you are trying to combine them both as follows:

class AMPMClock extends Clock { Clock clock2 = new Clock(); 

Either extend:

class AMPMClock extends Clock { 

Because, the public accesor methods are inherited, you can invoke getter/setter methods on this. e.g. this.getHour().

Or use it but don't extend:

class AMPMClock { Clock clock2 = new Clock(); 

Here, instead of this use clock2. e.g. clock2.getHour().

But, you need to make all the instance members private (they should not to be public). And make corresponding public getter/setter for all of them.

As suggested by dasblinkenlight:

It would be better to have an abstract class:

public abstract class BaseClock { private int h, m, s; //getter/setter } 

And two different concrete classes:

public class TwelveHourClock extends BaseClock { } 

and

public class TwentyFourHourClock extends BaseClock { } 
Sign up to request clarification or add additional context in comments.

1 Comment

yes but what should i write in BaseClock ?? because in Clock class i have written the general code .. how to change it and write in different class the 24 hour clock??
1

The tactical answer is to change click2 with "this". The strategic, bigger problem is that are not really utilizing inheritance properly.

Comments

0

You can just write

class AMPMClock extends Clock { private boolean pm = false; public void setHour(int hour) { if (hour >= 12) { hour -= 12; pm = true; } else { pm = false; } super.setHour(hour); } public void setPM(boolean pm){ this.pm = pm; } } 

The hour should be validated when it is set. The fields should all be private and you should ahve getters for the hour, mins & secs. You don't need two Clocks in AMPMClock as AMPMClock is a Clock. Having a AM/PM set to yes is just confusing.

Comments

0

It looks like you're getting confused about the difference between classes and objects.

check out: http://www.codestyle.org/java/faq-Objects.shtml#classobjectdiff

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.