1 Replies - 745 Views - Last Post: 15 May 2014 - 10:12 PM Rate Topic: -----

#1 smalld01   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 70
  • Joined: 25-February 14

MVC help?

Posted 15 May 2014 - 06:03 PM

For my CS class we have to create a Light Switch MVC. Here are the basics about the problem:
Write a class named LightSwitchModelthatmodels a lightswitch.  A lightswitch is eitherONorOFF.  The
LightSwitchModel will have a default constructor(the switch isOFF) and the followingmethods:
o public boolean isOn() // returns true if the switch is ON and false otherwise 
o public boolean isOff() // returns true if the switch is OFF and false otherwise 
o public boolean flip() // if the switch is ON it becomes OFF otherwise it is ON 

Write a class named LightSwitchView that is the view of a LightSwitchModel.  The view is a rectangle that 
contains within it a single rectangle.  If the model is ON then the inner rectangle is located in the top ½ of the 
view, otherwise the inner rectangle is located in the lower ½ of the view.  Include the following methods: 
o public LightSwitchView(int x, int y, int w, int h, LightSwitchModel m) 
o public void modelChanged() 

Write a controller that creates 10 LightSwitchModels and stores them in an array.  The controller then 
creates a view for each model and shows them as a row of views within a single window.  Also add a text‐
field and a button.  Whenever the button is pressed, the light‐switch that corresponds to the number in the 
text‐field is flipped.


When I am creating the buttons and text field, do I make those in the controller or the view?

Is This A Good Question/Topic? 0
  • +

Replies To: MVC help?

#2 Flukeshot   User is offline

  • A little too OCD
  • member icon

Reputation: 417
  • View blog
  • Posts: 1,030
  • Joined: 14-November 12

Re: MVC help?

Posted 15 May 2014 - 10:12 PM

Anything GUI-related (including buttons) go in the view class. Anything related to storing data goes into your model class and anything to do with changing the data or the gui components goes into your controller class.

The main reason for the MVC pattern is to decouple your GUI from your main program. So the first step would be to create a program that does not require a GUI, then build one that can send the signals through the controller to 'read/write' data from the model.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1