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


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 13 of 13

Thread: Boolean variable problem 2

  1. #1
    Junior Member
    Join Date
    Sep 2012
    Posts
    20
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Thumbs down 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?
    Last edited by jps; September 2nd, 2013 at 04:37 AM. Reason: added assignment instructions


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default 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. #3
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default 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. #4
    Member
    Join Date
    Sep 2013
    Posts
    102
    Thanks
    38
    Thanked 0 Times in 0 Posts

    Default Re: Boolean variable problem 2

    Quote Originally Posted by jps View Post
    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. #5
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Boolean variable problem 2

    Quote Originally Posted by KAJLogic View Post
    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 View Post
    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. #6
    Member
    Join Date
    Sep 2013
    Posts
    102
    Thanks
    38
    Thanked 0 Times in 0 Posts

    Default 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. #7
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default 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.
    Improving the world one idiot at a time!

  10. #8
    Member
    Join Date
    Sep 2013
    Posts
    102
    Thanks
    38
    Thanked 0 Times in 0 Posts

    Default 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. #9
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Boolean variable problem 2

    Quote Originally Posted by KAJLogic View Post
    I have noticed increased performance via simple tests on already created programs
    I reiterate: I doubt that very much. Provide proof!
    Improving the world one idiot at a time!

  12. #10
    Member
    Join Date
    Sep 2013
    Posts
    102
    Thanks
    38
    Thanked 0 Times in 0 Posts

    Default Re: Boolean variable problem 2

    Quote Originally Posted by Junky View Post
    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. #11
    Junior Member
    Join Date
    Sep 2012
    Posts
    20
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Boolean variable problem 2

    Quote Originally Posted by jps View Post
    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. #12
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default 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. #13
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Boolean variable problem 2

    Quote Originally Posted by malayo View Post
    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.

Similar Threads

  1. Boolean variable problem
    By ghostheadx in forum What's Wrong With My Code?
    Replies: 1
    Last Post: August 29th, 2013, 06:54 PM
  2. Boolean problem
    By RepersGhost in forum What's Wrong With My Code?
    Replies: 7
    Last Post: March 8th, 2013, 07:05 AM
  3. boolean problem.
    By Leonardo1143 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 13th, 2013, 05:32 PM
  4. Boolean Method Test Problem
    By Khadafi in forum Java Theory & Questions
    Replies: 4
    Last Post: January 13th, 2012, 11:07 PM
  5. Problem printing a two dimensional boolean array
    By sallyB in forum What's Wrong With My Code?
    Replies: 10
    Last Post: June 23rd, 2011, 03:56 PM