• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to set up boolean in user menu?  RSS feed

 
Jack Smithnson
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey guys, I'm trying to set up a boolean flag so that the user must enter 'M' (Mark the student answers) before they are able to choose 'S' and 'Q'. If they choose these, there should be a message saying "Please mark the answers before inputting statistics". The rest of the options should always be available for the user but I'm not sure how to set up a boolean so that the user must choose 'M' before choosing 'S' and 'Q'. Anyone know how to do this? Here is my code so far:

 
Les Morgan
Rancher
Posts: 779
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Under the 'M' case you set your Boolean flag, then you check for it under the 'S' and 'Q' cases. You need some condition to reset your Boolean flag too.
 
Campbell Ritchie
Marshal
Posts: 56533
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch
 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Les Morgan wrote:Under the 'M' case you set your Boolean flag, then you check for it under the 'S' and 'Q' cases. You need some condition to reset your Boolean flag too.

I'd advise against that. The information for determining whether marking has been done thereby making S and Q valid choices is most probably in the Quiz_Marker class. Therefore, any control around invoking the behaviors related to S and Q should be in the Quiz_Marker class.

I suggest that you add a method in Quiz_Marker that you can query from the Menu class.  Say public boolean markingDone(). That way, any change of state in "marking done" can also be performed inside the Quiz_Marker class as appropriate.  Prefer telling objects to do things (via method calls) and let them manage their own state internally as a result of their response rather than asking objects to report their state and then manipulating that state externally via setters.
 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By the way, Java naming convention discourages using underscores to separate words in class or variable names. Underscores are used in constant names. For variables and methods, names start with lowercase and the rest is camelcase. Type names (classes, interfaces, enums) start with a capital letter followed by camelcase to make words stand out.  So, QuizMarker instead of Quiz_Marker and MarkerMenu instead of Marker_Menu.

Also, the method names corAnsPrint(), stuAnsPrint(), etc. are very poorly chosen. There's no need to abbreviate names like that; it only makes your code more difficult to read. Method names should also be verb phrases, so printCorrectAnswers(), printStudentAnswers(), printStudentStats() are preferable names over what you have now.
 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Letting the QuizMarker class manage its own state allows you to write code like this:

Nothing in the external code explicitly manipulates the state of the quizMarker object.

It's like the difference between writing code like this:

versus

In the first version, the Person object manages its own "clean" state. In the second version, external code manipulates the "clean" state. The first version is more encapsulated and easier to manage.
 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One final point: Using a boolean variable to externally track the state of an object would lead you to write code that creates a lot of problems down the road:

Now the object's "clean" state is totally separated from it and managed by some external entity. This breaks the object's encapsulation and if the same state is needed elsewhere, you'd probably have to duplicate a lot of the code that tracks and manages the state.
 
Piet Souris
Master Rancher
Posts: 2042
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Given that this code runs from the command line, adding and removing options from a menu is okay. In a GUI environment, it is better to show all of the menu always. The options that are not valid (like the S and Q when the marking has not ben done yet) are then greyed out.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!