Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

# Thread: Boolean variable problem 2

1. ## Boolean variable problem 2

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

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.");

}

enter your date's fashion sense: 7
your rating is: 7
his/her parents are wealthy? y/n: y
any tips to improvise?

2. ## 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.

3. ## The Following User Says Thank You to GregBrannon For This Useful Post:

malayo (September 7th, 2013)

4. ## Re: Boolean variable problem 2

Post moved to a new thread:
1) Please do not hijack a thread started by someone else
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?

5. ## The Following User Says Thank You to jps For This Useful Post:

malayo (September 7th, 2013)

6. ## Re: Boolean variable problem 2

Originally Posted by jps
Post moved to a new thread:
1) Please do not hijack a thread started by someone else
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.

7. ## Re: Boolean variable problem 2

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
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)

8. ## 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++; ..

9. ## 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.

10. ## 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.

11. ## Re: Boolean variable problem 2

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!

12. ## Re: Boolean variable problem 2

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.

13. ## Re: Boolean variable problem 2

Originally Posted by jps
Post moved to a new thread:
1) Please do not hijack a thread started by someone else
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.

/*
* 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.");
}
}

}

14. ## Re: Boolean variable problem 2

Talk about hijacking. This thread is hijacked all over.

So it's working for you now?

Maybe not. This is a sample run I got that is not quite right.
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)

15. ## Re: Boolean variable problem 2

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.