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

Thread: Simple Java Program - hasTeen [HELP]

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    14
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Simple Java Program - hasTeen [HELP]

    I am trying to create a program that does the following:
    "We'll say that a number is "teen" if it is in the range 13..19 inclusive. Given 3 int values, return true if 1 or more is teen.

    My code:
    public class hasTeen
    {
       public static void main()
       {       
            System.out.println(" hasTeen(13, 20, 10) =" + hasTeen(13, 20, 10));
            System.out.println(" hasTeen(20, 19, 10) =" + hasTeen(20, 19, 10));
            System.out.println(" hasTeen(20, 10, 13)=" + hasTeen(20, 10, 13));
            System.out.println("\n---end hasTeen---\n");
        }
     
        public static boolean hasTeen(int a, int b, int c)
        {
        if ( a >= 13 && a<= 19 || b >= 11 && b<= 19 || c >= 13 || c<= 19)
        {
            return false;
        }
        else
        {
        return true;
        }
    }
    }

    And when I run it they all come up false. Here's the original problem:
    hasTeen(13, 20, 10) = true
    hasTeen(20, 19, 10) = true
    hasTeen(20, 10, 13) = true

    I get false for all of these. What's wrong with my code?


  2. #2
    Junior Member
    Join Date
    Oct 2011
    Posts
    20
    Thanks
    1
    Thanked 4 Times in 4 Posts

    Default Re: Simple Java Program - hasTeen [HELP]

    You put "return false" in the wrong position, where the "return true" should be. Try inverting them like this:

    public static boolean hasTeen(int a, int b, int c)
        {
        if ( a >= 13 && a<= 19 || b >= 11 && b<= 19 || c >= 13 || c<= 19)
        {
            return true;
        }
        else
        {
        return false;
        }

  3. #3
    Junior Member
    Join Date
    Oct 2011
    Posts
    14
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Simple Java Program - hasTeen [HELP]

    Quote Originally Posted by Henry518 View Post
    You put "return false" in the wrong position, where the "return true" should be. Try inverting them like this:

    public static boolean hasTeen(int a, int b, int c)
        {
        if ( a >= 13 && a<= 19 || b >= 11 && b<= 19 || c >= 13 || c<= 19)
        {
            return true;
        }
        else
        {
        return false;
        }
    Yes, they now return true, but when I input 3 numbers that aren't "teen", like (1, 4, 3), it still comes out as true, when it should be false as none of those numbers are "teen".
    Error:

  4. #4
    Junior Member
    Join Date
    Oct 2011
    Posts
    20
    Thanks
    1
    Thanked 4 Times in 4 Posts

    Default Re: Simple Java Program - hasTeen [HELP]

    Alright, true... I found the problem:

    public static boolean hasTeen(int a, int b, int c)
    {
    if ( a >= 13 && a<= 19 || b >= 11 && b<= 19 || c >= 13 || c<= 19)
    {
    return true;
    }
    else
    {
    return false;
    }

    Fixing that, it should look like this:

    public static boolean hasTeen(int a, int b, int c)
        {
        if ( a >= 13 && a<= 19 || b >= 13 && b<= 19 || c >= 13 && c<= 19)
        {
            return true;
        }
        else
        {
        return false;
        }

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

    HeroFlame (October 23rd, 2011)

  6. #5
    Junior Member
    Join Date
    Oct 2011
    Posts
    14
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Simple Java Program - hasTeen [HELP]

    Quote Originally Posted by Henry518 View Post
    Alright, true... I found the problem:

    public static boolean hasTeen(int a, int b, int c)
    {
    if ( a >= 13 && a<= 19 || b >= 11 && b<= 19 || c >= 13 || c<= 19)
    {
    return true;
    }
    else
    {
    return false;
    }

    Fixing that, it should look like this:

    public static boolean hasTeen(int a, int b, int c)
        {
        if ( a >= 13 && a<= 19 || b >= 13 && b<= 19 || c >= 13 && c<= 19)
        {
            return true;
        }
        else
        {
        return false;
        }
    Thanks so much!!

  7. #6
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Simple Java Program - hasTeen [HELP]

    Quote Originally Posted by Henry518 View Post
    Alright, true... I found the problem:

    public static boolean hasTeen(int a, int b, int c)
    {
    if ( a >= 13 && a<= 19 || b >= 11 && b<= 19 || c >= 13 || c<= 19)
    {
    return true;
    }
    else
    {
    return false;
    }

    Fixing that, it should look like this:

    public static boolean hasTeen(int a, int b, int c)
        {
        if ( a >= 13 && a<= 19 || b >= 13 && b<= 19 || c >= 13 && c<= 19)
        {
            return true;
        }
        else
        {
        return false;
        }
    It's good to help but don't spoon feed. Try to provide hints.
    THanks

  8. The Following User Says Thank You to Mr.777 For This Useful Post:

    Henry518 (October 24th, 2011)

  9. #7
    Junior Member
    Join Date
    Oct 2011
    Posts
    20
    Thanks
    1
    Thanked 4 Times in 4 Posts

    Default Re: Simple Java Program - hasTeen [HELP]

    Quote Originally Posted by Mr.777 View Post
    It's good to help but don't spoon feed. Try to provide hints.
    THanks
    Thanks for the suggestion, i'll try to keep that in mind. ^^

Similar Threads

  1. Simple Java Program
    By Robbiep in forum What's Wrong With My Code?
    Replies: 6
    Last Post: October 24th, 2011, 07:27 AM
  2. how to give inputs to my simple java program
    By pokuri in forum What's Wrong With My Code?
    Replies: 11
    Last Post: January 8th, 2011, 07:36 PM
  3. Need simple JAVA program fixing ASAP
    By theviper in forum Paid Java Projects
    Replies: 1
    Last Post: April 14th, 2010, 10:59 AM
  4. PLEASE HELP!!!! simple java program...
    By parvez07 in forum Object Oriented Programming
    Replies: 5
    Last Post: August 26th, 2009, 06:38 AM
  5. help with simple java program
    By parvez07 in forum Java Theory & Questions
    Replies: 4
    Last Post: August 25th, 2009, 07:19 AM