Logic error possibly misunderstanding in java assignment -
i've been having numerous problems getting project work correctly i'm stuck on getting class work properly. whats suppose take current station radio class , pass along class. problem i'm trying select between , fm every time run it, displays station. don't understand why automatically gets set station.
public class autoradiosystem { private radio selectedradio; private amradio radioam; private fmradio radiofm; private xmradio radioxm; //is correct place initialize these? radio amradio = new amradio(); radio fmradio = new fmradio(); public autoradiosystem() { //even making selected radio fm still produces values selectedradio = radiofm; } // problem lies , more. shouldn't return 0.0 without 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 correctly switch radio 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; } }
the problem is, in .getcurrentstation(), both selectedradio & radioam not init , null.
the mistake begin with:
public void selectradio() { if (selectedradio == radioam) { selectedradio = radiofm; } }
here, selectedradio = null, it's never assign value.
edit: believe you're begin this, little more details help.
- you make mistake when declare 2 field, amradio & radioam init 1 of them , use another.
- you didn't set value selectedradio , compare it, return false
- the best place init value instance constructor method, here autoradiosystem()
you may want change code this:
private radio selectedradio; public autoradiosystem() { selectedradio = new fmradio(); } // compare, using instanceof, better design use enum value instead,
Comments
Post a Comment