问题
I've been having numerous problems getting this project to work correctly but I'm currently stuck on getting this class to work properly. Whats its suppose to do is take the current station from the radio class and pass it along to this class. The problem is i'm trying to select between AM and FM but every time i run it, it only displays the AM station. I don't understand why it automatically gets set to that station.
public class AutoRadioSystem
{
private Radio selectedRadio;
private AMRadio radioAM;
private FMRadio radioFM;
private XMRadio radioXM;
//is this the correct place to initialize these?
Radio amRadio = new AMRadio();
Radio fmRadio = new FMRadio();
public AutoRadioSystem()
{
//even making the selected radio FM still produces values for AM
selectedRadio = radioFM;
}
// this is where my problem currently lies and probably much more. Shouldn't it return 0.0 without any station being selected.
public double getCurrentStation()
{
if (selectedRadio == radioAM)
{
return amRadio.getCurrentStaion();
}
else if (selectedRadio == radioFM)
{
return fmRadio.getCurrentStaion();
}
return 0.0;
}
//I'm not sure if i'm setting this up correctly to switch the radio from am to fm
public void selectRadio()
{
if (selectedRadio == radioAM)
selectedRadio = radioFM;
}
public static void main (String [] args) {
AutoRadioSystem c = new AutoRadioSystem();
c.selectRadio();
double b = c.getCurrentStation();
System.out.println(b);
}
}
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()
{
currentStation = Min_Station;
}
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 class FMRadio extends Radio
{
private static final double Max_Station = 108.0;
private static final double Min_Station = 88.0;
private static final double Increment = .01;
public FMRadio()
{
currentStation = Min_Station;
}
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 = ("FM " + this.currentStation);
return message;
}
}
public abstract class Radio
{
double currentStation;
RadioSelectionBar radioSelectionBar;
public Radio()
{
}
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)
{
this.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;
}
}
回答1:
The problem is, in .getCurrentStation(), both selectedRadio & radioAM is not init and is null.
The mistake begin with:
public void selectRadio()
{
if (selectedRadio == radioAM)
{
selectedRadio = radioFM;
}
}
Here, the selectedRadio = null, so it's never get assign value.
Edit: I believe you're just begin with this, so a little more details will help.
- You make mistake when declare two field, amRadio & radioAM then init one of them and use another.
- You didn't set value to selectedRadio and compare it, this always return false
- The best place to init value for an instance is the constructor method, here is AutoRadioSystem()
You may want to change the code to like this:
private Radio selectedRadio;
public AutoRadioSystem()
{
selectedRadio = new FMRadio();
}
// To compare, using instanceOf, but better design will use enum value instead, up to you
回答2:
I think I've found the problem
You have 2 fields for each Radio
overload
private AMRadio radioAM;
...
Radio amRadio = new AMRadio();
but the one you're comparing to: radioAM
never gets instantiated, and therefore is always null.
When you call
if (selectedRadio == radioAM)
both selectedRadio
and radioAM
are null
, so of course they would be equal
unless you intend radioAM
and amRadio
to be completely different instances, than you shouldn't have 2 fields like that.
Since you're using polymorphism, you might want to use the latter one
Radio amRadio = new AMRadio();
回答3:
All properties selectedRadio, radioAM and RadioFM are null. The code in the constructor has no effect because selectedRadio = RadioFM. This means that selectedRadio its value does not change and remains zero.
Therefore selectedRadio == radioAM (null == null) in getCurrentStation is always true.This will always apply the first if-block in your method getCurrentStation and will always return the "amradio".
Caio
来源:https://stackoverflow.com/questions/18988984/logic-error-possibly-misunderstanding-in-java-assignment