c# - Creating a "Car" class with Accelerate method, and associate Accel and Decel buttons on form with created class -
taking c#.net class , have been introduced new concept of using class form. it's online class , instructor has given instructions, unclear, @ least me. unfortunately, responses students questions vague , basically, in nut shell, told figure out, here. below instructor gave us.
=========================================================================
uml: model: string, currentspeed: decimal, topspeed: decimal, , accelerate (change speed: decimal)
accelerate adds changespeed (which can positive, negative or 0) currentspeed. currentspeed can never exceed topspeed or less 0 e.g. if currentspeed 10 , dchangespeed -20, currentspeed becomes 0
general requirements:
- top speed combo box filled numbers 60, 70, 80 …. 200 (at run time)
- change speed combo box filled numbers 1, 2, 3 ….200 (at run time)
- user must select combo box (cannot enter own numbers)
button action requirements:
accelerate same model, increases current speed change speed new model, sets current speed change speed (current speed can’t exceed top speed)
decelerate same model, decreases current speed change speed new model, sets current speed 0 (current speed can’t go below 0)
uml diagram followed exactly. work (calculations, etc) done in class, not form.
=========================================================================
ok, question is, should use deceleration method, along accelerate in class? i'm assuming each button (decel, accel) need own method in class. think i'm overly making complicated, answer 1 of question asked instructor. guess i'm totally stumped how reference class, accelerate form.
so far, came with, know wrong, beginners class i'm told , support form instructor not there other relying on chapter classes.
public class car { // backing fields (instance variables) private string _model; private int _currentspeed; private int _topspeed; public int speedresult; // set parameters public car() { this.model = ""; this.currentspeed = 0; this.topspeed = 0; } // constructor public car(string model, int currentspeed, int topspeed) { _model = model; _currentspeed = currentspeed; _topspeed = topspeed; } // model property public string model { { return _model; } set { _model = value; } } // current speed property public int currentspeed { { return _currentspeed; } set { _currentspeed = value; } } // top speed property public int topspeed { { return _topspeed; } set { _topspeed = value; } } // methods public int accelerate() { speedresult = (topspeed + currentspeed); return speedresult; } // public void clear() { model = ""; currentspeed = 0; topspeed = 0; } }
form:
public partial class carspeedform : form { private car _mycar; private int speedresult; public carspeedform() { _mycar = new car(); initializecomponent(); } private void carspeedform_load(object sender, eventargs e) { // loads amount combobox values 60 200 in increments of 10 (int = 60; <= 200; += 10) topspeedcombobox.items.add(i); // loads amount combobox values 1 200 in increments of 1 (int = 1; <= 200; += 1) changespeedcombobox.items.add(i); } private void getcardata() { try { _mycar.model = carmodeltextbox.text; _mycar.topspeed = int.parse(topspeedcombobox.text); _mycar.currentspeed = int.parse(changespeedcombobox.text); } catch { messagebox.show(string.concat("please enter valid model , speed(s) vehicle.","\r\n")); } } private void accelbutton_click(object sender, eventargs e) { getcardata(); speedresultlabel.text = speedresult.tostring("n1"); carresultlabel.text = string.format("your car new car."); } private void decelbutton_click(object sender, eventargs e) { getcardata(); speedresultlabel.text = speedresult.tostring("n1"); carresultlabel.text = string.format("your car new car."); } private void clearbutton_click(object sender, eventargs e) { //clear textbox, combobox, labels carmodeltextbox.clear(); carmodeltextbox.focus(); topspeedcombobox.selectedindex = -1; changespeedcombobox.selectedindex = -1; speedresultlabel.text = ""; carresultlabel.text = ""; } private void exitbutton_click(object sender, eventargs e) { //close application this.close(); } }
reading description wrote, looks have concept down. form, assume selecting car type? if case, want define model(car) behavior in car class. example, have done, want define accessors selected cars data , accelerate , decelerate methods. way inside view(carspeedform) can call perspective method when corresponding button pressed. i.e. if user presses accelerate button on form, call accelerate method of instance of car. hope helps. you're not far off.
you should use button , input controls store data in your_mycar instances. example
public partial class carform : form { private car thecar; private bool modelchanged; public carform() { thecar = new car(); initializecomponent(); loadchangespeed_cb(); loadtopspeed_cb(); modelchanged = false; } private void loadchangespeed_cb() { (decimal = 1; <= 200; i++) { changespeed_cb.items.add(i); } } private void loadtopspeed_cb() { for(decimal = 60; <= 200; i+=10) { topspeed_cb.items.add(i); } } private void accel_b_click(object sender, eventargs e) { if(modelchanged) { thecar.currentspeed = thecar.changespeed; modelchanged = false; } else { var si = changespeed_cb.selecteditem; if (si == null) { return; } thecar.accelerate((decimal)si); } } private void decel_b_click(object sender, eventargs e) { if(modelchanged) { thecar.currentspeed = 0; modelchanged = false; return; } else { var si = changespeed_cb.selecteditem; if (si == null) { return; } thecar.accelerate(-(decimal)si); } } private void topspeed_cb_selectedindexchanged(object sender, eventargs e) { var si = topspeed_cb.selecteditem; if(si == null) { return; } thecar.topspeed = (decimal)si; } private void changespeed_cb_selectedindexchanged(object sender, eventargs e) { var si = changespeed_cb.selecteditem; if (si == null) { return; } thecar.changespeed = (decimal)changespeed_cb.selecteditem; }
Comments
Post a Comment