Reputation: 13
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**;
}
}
}
Upvotes: 1
Views: 1669
Reputation: 51030
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 { }
Upvotes: 1
Reputation: 923
The tactical answer is to change click2 with "this". The strategic, bigger problem is that are not really utilizing inheritance properly.
Upvotes: 1
Reputation: 67
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
Upvotes: 0
Reputation: 533432
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.
Upvotes: 0