• Post Reply Bookmark Topic Watch Topic
  • New Topic

decisions without an if statement  RSS feed

 
Mike Mo
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The question states:

Clunker Motors Inc. is recalling all vehicles from model years 1995 - 1998 and 2004 - 2006. A boolean variable named norecall has been declared. Given a variable modelYear, write a statement that assigns true to noRecall if the value of modelYear does NOT fall within the two recall ranges and assigns false otherwise.
Do not use an if statement in this exercise!

since i cant use an if statement, too easy i guess?, this is what i have so far.

so given:
boolean noRecall; //this by default is false correct?
int modelYear;

also know:
if year is between 1995 and 1998 or 2004 and 2006 (x >= 1995 or x <= 1998) or (x>= 2004 or x<= 2006)
if modelYear not in those zones, then true.
if modelYear is in those zones, then false.

 

to me this is a dumb question. first why wouldnt you use an if statement? second why would you have the boolean = noRecall... if it equals just recall and is default "false" then that makes more sense. to me anyway. so i would do this...

 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where do those bang signs ! come from? What do they do? Have you been reading about de Morgan's law? How does the precedence of ! compare to that of && ?
I think the formulae you are using look incorrect, anyway.
Don't follow a boolean formula by if‑else like that. Use a straight assignment, as show in the old Sun style guide (§10.5.2). What follows is of course nonsense code.Also avoid using norecall as an identifier. It is harder to understand negatives, and it shou‍ld have a capital R in anyway. You can add () to that code to improve legibility.

[edit]If the question says to use noRecall,
  • 1: It is a poorly‑formulated question, and,
  • 2: You shou‍ld tell us where the question comes from, to avoid copyright problems.
  •  
    Carey Brown
    Saloon Keeper
    Posts: 3328
    46
    Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I'm not advocating changing this
    but because of operator precedence, you could re-write this without the parens and it will be evaluated exactly the same.
    Order of precedence (highest to lowest): '>=', '&&', '||'

    Over time, all programmers should become familiar with operator precedence till it becomes second nature. The exception is the use of bit-wise operators where the use of parens is encouraged. When I see parens I suspect that the programmer is specifically trying to over-ride operator precedence and I have to study it extra carefully to see if that is the case. (...IMHO, but lots of others have the same opinion)

    https://docs.oracle.com/javase/tutorial/java/nutsandbolts/operators.html
    http://www.cs.bilkent.edu.tr/~guvenir/courses/CS101/op_precedence.html

    Interesting that the above two links don't mention the 'new' operator as this next one does
    http://introcs.cs.princeton.edu/java/11precedence/
     
    Mike Mo
    Ranch Hand
    Posts: 40
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Campbell Ritchie wrote:Where do those bang signs ! come from? What do they do? Have you been reading about de Morgan's law? How does the precedence of ! compare to that of && ?
    I think the formulae you are using look incorrect, anyway.
    Don't follow a boolean formula by if‑else like that. Use a straight assignment, as show in the old Sun style guide (§10.5.2). What follows is of course nonsense code.Also avoid using norecall as an identifier. It is harder to understand negatives, and it shou‍ld have a capital R in anyway. You can add () to that code to improve legibility.

    [edit]If the question says to use noRecall,
  • 1: It is a poorly‑formulated question, and,
  • 2: You shou‍ld tell us where the question comes from, to avoid copyright problems.


  • ! is negative or "not"...or so I was told. the previous online questions were correct using the !, this one has given me trouble however. I didn't capitalize the R in norecall bc they didn't and I assumed there was a reason for that. maybe not however. all the variables are pre declared as the question stated, so no need to declare them again. also I completely agree that this question format is complete shit. Nobody would ever do this b/c logically its ignorant to say it this way. we don't answer a question by saying "Hey ill not not see you in 5 in" lol so why would you do that in a program. makes it hard to understand which makes it hard to do whatever you want to do with it. I guess I'm not sure how my logic is wrong. iv read it a few times and no clue why it wouldn't work properly.

    (x >= 1995 or x <= 1998) or (x>= 2004 or x<= 2006)  <---- this should have been...
    (x >= 1995 and x <= 1998) or (x>= 2004 and x<= 2006).

    the or needing to be an and wasn't in my code so wouldn't have caused the problem.


     
    Mike Mo
    Ranch Hand
    Posts: 40
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    the correct answer they wanted was


    is there a reason they say: (year <= variable) then say "&&" followed by (variable <= year). are signs facing the same way an important thing to do? or does it matter either way?

    thanks for the help!

    also notice what I was missing was noRecall or "norecall" as they had needed to be set to the whole thing. so I added norecall = to the beginning and it "fixed" it.
     
    Campbell Ritchie
    Marshal
    Posts: 56570
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    And did you notice that both the Java™ Tutorials and Bilkent use the wrong names for && || ? Did you notice that Bilkent has i++ associating to the right and Princeton has it associating to the left? The Java™ Tutorials keep quiet about associating direction.

    If you go through the JLS, which I warn against because it is difficult to read, it clearly calls && and || conditional AND/OR, not logical, which is an overloading of the bitwise operators & |. If you go through the grammar, you will find that a postfix expression includes the definition post increment expression, and that is defined as postfix&bsp;expression ++ That means Princeton is correct to call it a left‑associative operator, even though I believe it is impossible to use ++ or -- twice, so it is of no practical significance.
     
    Fred Kleinschmidt
    Bartender
    Posts: 571
    9
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Another way would be to put all of the recall years in a List<Integer>, then simply use:

    This has the advantage that if new years are added to the List later on, the rest of the code remains unchanged.
     
    Junilu Lacar
    Sheriff
    Posts: 11494
    180
    Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Fred Kleinschmidt wrote:Another way would be to put all of the recall years in a List<Integer>, then simply use:

    This has the advantage that if new years are added to the List later on, the rest of the code remains unchanged.

    +1 - I like the way you think, Fred.

    It might also be possible to use a List<Predicate<Integer>> and the removeIf() default method.
     
    Junilu Lacar
    Sheriff
    Posts: 11494
    180
    Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Mike Mo wrote:
    ! is negative or "not"...or so I was told. the previous online questions were correct using the !, this one has given me trouble however. I didn't capitalize the R in norecall bc they didn't and I assumed there was a reason for that. maybe not however. all the variables are pre declared as the question stated, so no need to declare them again. also I completely agree that this question format is complete shit. Nobody would ever do this b/c logically its ignorant to say it this way. we don't answer a question by saying "Hey ill not not see you in 5 in" lol so why would you do that in a program. makes it hard to understand which makes it hard to do whatever you want to do with it. I guess I'm not sure how my logic is wrong. iv read it a few times and no clue why it wouldn't work properly.

    (x >= 1995 or x <= 1998) or (x>= 2004 or x<= 2006)  <---- this should have been...
    (x >= 1995 and x <= 1998) or (x>= 2004 and x<= 2006).

    the or needing to be an and wasn't in my code so wouldn't have caused the problem.

    Apart from perhaps the poor (non)use of capitalization in the name norecall, I don't think the question is that bad. I've seen worse. I can tell you what the goals of this exercise are:

    1. See if the student understands how to use conditional operators !, &&, and ||
    2. See if the student can formulate an appropriate boolean expression
    3. See if the student understands how assigning boolean expressions to boolean variables work.
    4. See if the student understands De Morgan's Laws for prepositional logic and boolean algebra.

    Knowing that these are the goals of this exercise, can you see where you failed to meet them?
     
    Mike Mo
    Ranch Hand
    Posts: 40
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Junilu Lacar wrote:
    Fred Kleinschmidt wrote:Another way would be to put all of the recall years in a List<Integer>, then simply use:

    This has the advantage that if new years are added to the List later on, the rest of the code remains unchanged.

    +1 - I like the way you think, Fred.

    It might also be possible to use a List<Predicate<Integer>> and the removeIf() default method.


    awesome response. i havent learned all of that yet but i can see what you mean. does a list work like a stack where you can only take values the top or can you pluck a value out from anywhere?
     
    Junilu Lacar
    Sheriff
    Posts: 11494
    180
    Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Mike Mo wrote:the correct answer they wanted was


    This is where I think they would be wrong: to expect this exact answer and accept only this answer as the correct one.  There is more than one way you can formulate a correct boolean expression based on transformation rules defined by DeMorgan's Laws.

    Here's another correct answer:

    There is no need for parentheses in this expression because of operator precedence rules.
     
    Junilu Lacar
    Sheriff
    Posts: 11494
    180
    Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Mike Mo wrote:awesome response. i havent learned all of that yet but i can see what you mean. does a list work like a stack where you can only take values the top or can you pluck a value out from anywhere?

    No. The list works like, well, a list.   ¯\_(ツ)_/¯

    In this case, you put all the years that are being recalled and if the modelYear is on that list (which is what the contains() method basically reports back), then that car is being recalled. Negating that result would mean that it's not being recalled.

    It's easier to understand if you program it like this:

     
    Campbell Ritchie
    Marshal
    Posts: 56570
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Junilu Lacar wrote:. . . Here's another correct answer:
    . . . .
    Junilu, I wanted to post almost exactly the same, and you beat me to it!

    That is not another correct answer, but a better answer, for three reasons:-
  • 1: It uses fewer operators than the other solutions.
  • 2: Is uses only one relational operator, <. There is nothing actually wrong with <= and >= but < and > are easier to understand.
  • 3: It doesn't use any negation operators.
  •  
    Consider Paul's rocket mass heater.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!