# Boolean variable problem 2

• September 2nd, 2013, 03:50 AM
malayo
Boolean variable problem 2
Quote:

problem:

When you go on a date, you rate your date's fashion sense on a scale from 0 to 10. As a general rule, if the rating is 8 or more, then you will consider going out again with the same person. However, if your date's parents are wealthy, you will consider going out again with him or her if the rating is 7 or more.

If the int r contains your date's rating from 0 to 10 and the boolean w contains true if and only if your date's parents are wealthy, write code that will set d to true if and only if you will consider going on another date with the same person.
hi, i decided to try this assignment for fun and stuck with last condition

Code :

```import java.util.Scanner; public class dating { public static void main(String[] args) { System.out.print("enter your date's fashion sense: "); Scanner userInput = new Scanner(System.in); String r = userInput.next(); int rate = Integer.parseInt(r); System.out.println("your rating is: " + r); int pass = 8; if (rate >= pass) { System.out.println("you will consider going out with your date again.");   } if (rate == 7) { System.out.print("his/her parents are wealthy? y/n: "); String d = userInput.next(); if (d == "y") { System.out.println("you will consider going out with your date again."); } } else System.out.print("you will not consider going out with your date again.");       }```

Quote:

enter your date's fashion sense: 7
his/her parents are wealthy? y/n: y
any tips to improvise?
• September 2nd, 2013, 04:49 AM
GregBrannon
Re: Boolean variable problem 2
I suggest you obtain both r (fashion sense rating) and w (parent's wealthy?) and then process the logic to determine if you will call that date again.
• September 2nd, 2013, 04:49 AM
jps
Re: Boolean variable problem 2
Post moved to a new thread:
2) Posting your version of a solution (incomplete/incorrect or not) is considered spoonfeeding.

Class names should begin with an upper case letter
The scanner should be closed
if/else statements should be followed by {...} even if they only encompass a single statement, even though it is not required
Use .equals to compare strings instead of ==
When a value of 8 or more is entered, the else statement is executed, why?
• September 2nd, 2013, 04:08 PM
KAJLogic
Re: Boolean variable problem 2
Quote:

Originally Posted by jps
Post moved to a new thread:
2) Posting your version of a solution (incomplete/incorrect or not) is considered spoonfeeding.

Class names should begin with an upper case letter
The scanner should be closed
if/else statements should be followed by {...} even if they only encompass a single statement, even though it is not required
Use .equals to compare strings instead of ==
When a value of 8 or more is entered, the else statement is executed, why?

I am curious as to why you think omitting the brackets for single execution statements is not good? I have definitely noticed an increase in efficiency with a large enough volume.
• September 2nd, 2013, 06:10 PM
jps
Re: Boolean variable problem 2
Quote:

Originally Posted by KAJLogic
I am curious as to why you think omitting the brackets for single execution statements is not good?

To help prevent errors in the future maintenance of the code
Quote:

Originally Posted by KAJLogic
I have definitely noticed an increase in efficiency with a large enough volume.

The time saved in fewer bugs surely outweighs the time it takes to type two characters (or less considering a decent IDE)
• September 2nd, 2013, 06:22 PM
KAJLogic
Re: Boolean variable problem 2
I only continue my inquiry because you are answering just to aid us, so I figure if it will further help me you wouldn't mind. When I said efficiency I meant in terms of execution I have relieved some redundancies in the bytecode by not using these brackets. What errors have you ran into specifically? If I may take a leap, I think you are talking about staying safe due to improper use? As in the removal of the brackets is fine, but as these things go if you make it a habit eventually you'll begin using it improperly by accident: everyone has forgot to add semi-colons before compile; the difference here being semi-colons is a compile time error where-as if you make omitting the brackets habit you will have a (potentially) perplexing run time error. Is this what you mean?

P.S: I am tedious though I still use ++variable; instead of variable++; ..
• September 2nd, 2013, 08:11 PM
Junky
Re: Boolean variable problem 2
I highly doubt that placing or not placing braces around an if statement will affect performance.

The main issue with leaving braces out is that at a later time when more code is added to the if statement the programmer forgets to add the braces. Even though the code is correctly indented and looks fine it does not execute correctly.

IMO, placing braces around the code makes it easier to read as well. You can clearly see what is or isn't included in the if statement.
• September 2nd, 2013, 08:30 PM
KAJLogic
Re: Boolean variable problem 2
Yes I believe I summarized everything you have put in this post in my initial one. And to answer to your doubts I have noticed increased performance via simple tests on already created programs, however I cannot say with any degree of certainty that it does. I think this can then be summed up to if you responsible then using them isn't an issue. I do understand that probably only 5-10% of the programmers out there are this able, but about 100% of them will assume they are, so I suppose you should put out there never use them, and if you are that 5-10 you will know.
• September 2nd, 2013, 10:12 PM
Junky
Re: Boolean variable problem 2
Quote:

Originally Posted by KAJLogic
I have noticed increased performance via simple tests on already created programs

I reiterate: I doubt that very much. Provide proof!
• September 3rd, 2013, 06:25 AM
KAJLogic
Re: Boolean variable problem 2
Quote:

Originally Posted by Junky
I reiterate: I doubt that very much. Provide proof!

It seems you are right, although I fee inclined to dig deeper. After an initial test it seems as though bytecode does not confront this issue. If your curious I'll post my specific results and update this thread whist I continue to test it.
• September 7th, 2013, 06:31 AM
malayo
Re: Boolean variable problem 2
Quote:

Originally Posted by jps
Post moved to a new thread:
2) Posting your version of a solution (incomplete/incorrect or not) is considered spoonfeeding.

Class names should begin with an upper case letter
The scanner should be closed
if/else statements should be followed by {...} even if they only encompass a single statement, even though it is not required
Use .equals to compare strings instead of ==
When a value of 8 or more is entered, the else statement is executed, why?

my bad, updated code as suggested.

Code java:

```/* * rate your date's fashion sense on a scale from 0 to 10 * if the rating is 8 or more, then you will consider going out again * if your date's parents are wealthy, you will consider going out again with him or her * if the rating is 7 or more */ import java.util.Scanner; public class Dating { public static void main(String[] args) { System.out.print("enter your date's fashion sense: "); Scanner userInput = new Scanner(System.in); int r = userInput.nextInt();   System.out.print("his/her parents are wealthy? y/n: "); String d = userInput.next();   int pass = 8;   if (r >= pass) { System.out.println("you rated " + r + " and will consider going out with your date again."); }   if (r == 7) { if (d.equals("y")) { System.out.println("your date's parents are rich and you will consider going out with your date again."); } } else { System.out.println("you will not consider having another date."); } }   }```
• September 7th, 2013, 07:02 AM
GregBrannon
Re: Boolean variable problem 2

So it's working for you now?

Maybe not. This is a sample run I got that is not quite right.
Code java:

```enter your date's fashion sense: 8 his/her parents are wealthy? y/n: n you rated 8 and will consider going out with your date again. you will not consider having another date.```
To improve and simplify the logic, I suggest the following flow:
Get date's fashion sense rating, 0 - 10: Done.
Get date's parent's wealthy, true/false: Done.
Set pass value, 8 if not wealthy, 7 if wealthy: Not Done.
Run the logic based on the pass value: Not Done (modify existing if statements)
• September 7th, 2013, 08:10 AM
jps
Re: Boolean variable problem 2
Quote:

Originally Posted by malayo
my bad, updated code as suggested.

Looks like the scanner is not closed.
Other than that the syntax is looking okay.

Fix that and then on to fixing the functionality. With this limited set of possible values (0-10), it should be easy enough to test with each valid value to be sure it works.