当我去运行它时,它返回的值是currentStation加上增量值的两倍。而不仅仅是currentStation加上增量值。我无法弄清楚它是否在AutoRadioSystem或Radio类中出现问题。
public class AutoRadioSystem
{
private Radio selectedRadio;
private AMRadio radioAM = new AMRadio();
private FMRadio radioFM = new FMRadio();
private XMRadio radioXM = new XMRadio();
public AutoRadioSystem()
{
selectedRadio = new AMRadio();
}
public double getCurrentStation()
{
if (selectedRadio.equals(radioAM))
{
return radioAM.getCurrentStaion();
}
else if (selectedRadio.equals(radioFM))
{
return radioFM.getCurrentStaion();
}
else if (selectedRadio.equals(radioXM))
{
return radioXM.getCurrentStaion();
}
return 0.0;
}
public void selectRadio()
{
if (selectedRadio.equals(radioAM))
selectedRadio = radioFM;
else if (selectedRadio.equals(radioFM))
selectedRadio = radioXM;
else if (selectedRadio.equals(radioXM))
selectedRadio = radioAM;
}
public void upCategory()
{
double catUp = radioXM.getCurrentStaion();
catUp += 10;
if (catUp > 199.0)
{
catUp = 1;
radioXM.setCurrentStation(catUp);
}
radioXM.setCurrentStation(catUp);
}
public void up()
{
if (selectedRadio.equals(radioAM))
{
double stationUp = radioAM.getCurrentStaion();
stationUp += radioAM.getIncrement();
radioAM.setCurrentStation(stationUp);
// System.out.println(stationUp );
}
}
public boolean equals (Object o)
{
if (o == null)
return false;
if (! (o instanceof AutoRadioSystem))
return false;
AutoRadioSystem other = (AutoRadioSystem) o;
return this.selectedRadio == other.selectedRadio;
}
public static void main (String [] args) {
AutoRadioSystem c = new AutoRadioSystem();
c.selectRadio();
double b = c.getCurrentStation();
System.out.println(b);
c.selectRadio();
double d = c.getCurrentStation();
System.out.println(d);
c.upCategory();
double f = c.getCurrentStation();
System.out.println(f);
c.selectRadio();
double e = c.getCurrentStation();
System.out.println(e);
c.up();
double g = c.getCurrentStation();
System.out.println(g);
c.up();
double t = c.getCurrentStation();
System.out.println(t);
}
}
附加代码
public class AMRadio extends Radio
{
private static final double Max_Station = 1605;
private static final double Min_Station = 535;
private static final double Increment = 10;
public AMRadio()
{
}
public double getMax_Station()
{
return this.Max_Station;
}
public double getMin_Station()
{
return this.Min_Station;
}
public double getIncrement()
{
return this.Increment;
}
public String toString()
{
String message = ("AM " + this.currentStation);
return message;
}
}
public abstract class Radio
{
double currentStation;
RadioSelectionBar radioSelectionBar;
public Radio()
{
this.currentStation = getMin_Station();
}
public abstract double getMax_Station();
public abstract double getMin_Station();
public abstract double getIncrement();
public void up()
{
}
public void down()
{
}
public double getCurrentStaion()
{
return this.currentStation;
}
public void setCurrentStation(double freq)
{
currentStation += freq;
}
public void setStation(int buttonNumber, double station)
{
}
public double getStation(int buttonNumber)
{
return 0.0;
}
public String toString()
{
String message = ("" + currentStation);
return message;
}
public boolean equals (Object o)
{
if (o == null)
return false;
if (! (o instanceof Radio))
return false;
Radio other = (Radio) o;
return this.currentStation == other.currentStation;
}
答案 0 :(得分:2)
问题在于方法:
public void setCurrentStation(double freq)
{
// This is equal to currentStation = currentStation + freq
currentStation += freq;
}
应该是
public void setCurrentStation(double freq)
{
currentStation = freq;
}
因为您已经在currentStation
方法中添加了up
。
只是建议,与问题无关:
Radio
作为属性以及此类的其他三个实现。我认为你应该考虑使用'instance of`运算符。