Results 1 to 12 of 12
Like Tree2Likes
  • 1 Post By JosAH
  • 1 Post By JosAH

Thread: The Switch Statement

  1. #1
    Everyman's Avatar
    Everyman is offline Member
    Join Date
    Feb 2012
    Location
    Everywhere
    Posts
    26
    Rep Power
    0

    Default The Switch Statement

    I have been stuck on this problem for 30 minutes:

    " Write the definition of a method printAttitude , which has an int parameter and returns nothing. The method prints a message to standard output depending on the value of its parameter.

    • If the parameter equals 1 , the method prints disagree
    • If the parameter equals 2 , the method prints no opinion
    • If the parameter equals 3 , the method prints agree
    • In the case of other values, the method does nothing.



    Each message is printed on a line by itself."


    This is the code that I have assembled:

    Java Code:
    switch (numValues)
    {
     case 1
       System.out.println("disagree");
       break;
    
    case 2
      System.out.println("no opinion");
      break;
    
    case 3
      System.out.println("agree");
      break;
    
    default:
    }
    Every time I submit it to the compiler on MyProgrammingLab, it tells me that I have to "write the definition of the printAttitude method" or something like that. I have no idea how to do this and I looked at the API site and it looked kinda confusing... Could someone please lend a hand to this poor noob?

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,433
    Blog Entries
    7
    Rep Power
    20

    Default Re: The Switch Statement

    Where is your printAttitude method? All we can see is a single switch statement ...

    kind regards,

    Jos
    noobplus likes this.
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    noobplus's Avatar
    noobplus is offline Senior Member
    Join Date
    Feb 2012
    Location
    chennai,south of India(south east of asia)
    Posts
    232
    Rep Power
    3

    Default Re: The Switch Statement

    Quote Originally Posted by Everyman View Post
    Each message is printed on a line by itself.
    System.out.println("-text-");
    prints the stuffs in a new line, and also skips to next line for successor statements..
    for example,
    Java Code:
    System.out.println("1st line");
    System.out.print("2nd line");
    System.out.print("3rd line");
    output is
    Java Code:
    1st line
    2nd line3rd line
    note that
    System.out.print();
    requires a boolean or a string as an argument

    regards
    dhilip

  4. #4
    Everyman's Avatar
    Everyman is offline Member
    Join Date
    Feb 2012
    Location
    Everywhere
    Posts
    26
    Rep Power
    0

    Default Re: The Switch Statement

    Quote Originally Posted by JosAH View Post
    Where is your printAttitude method? All we can see is a single switch statement ...

    kind regards,

    Jos
    That's what I don't understand. I have tried the public static int printAttitude and similar things but they didn't work. Was that the right track? Is there something that I am missing? I don't know...

  5. #5
    noobplus's Avatar
    noobplus is offline Senior Member
    Join Date
    Feb 2012
    Location
    chennai,south of India(south east of asia)
    Posts
    232
    Rep Power
    3

    Default Re: The Switch Statement

    Quote Originally Posted by Everyman View Post
    int printAttitude
    Jos knows the answer for this. wait for the answer

    regards
    dhilip

  6. #6
    Everyman's Avatar
    Everyman is offline Member
    Join Date
    Feb 2012
    Location
    Everywhere
    Posts
    26
    Rep Power
    0

    Default Re: The Switch Statement

    Well, thats my bell.

  7. #7
    noobplus's Avatar
    noobplus is offline Senior Member
    Join Date
    Feb 2012
    Location
    chennai,south of India(south east of asia)
    Posts
    232
    Rep Power
    3

    Default Re: The Switch Statement

    Quote Originally Posted by Everyman View Post
    public static int printAttitude
    public static int printAttitude() returns an int.. but the code should not return any int's .. so
    public static void printAttitude() should be the method

    regards
    dhilip

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,433
    Blog Entries
    7
    Rep Power
    20

    Default Re: The Switch Statement

    Quote Originally Posted by Everyman View Post
    That's what I don't understand. I have tried the public static int printAttitude and similar things but they didn't work. Was that the right track? Is there something that I am missing? I don't know...
    Do you know how to define a method? Look at your main( ... ) method for a static method that doesn't return anything. Your method should take a single int parameter.

    kind regards,

    Jos
    noobplus likes this.
    cenosillicaphobia: the fear for an empty beer glass

  9. #9
    Everyman's Avatar
    Everyman is offline Member
    Join Date
    Feb 2012
    Location
    Everywhere
    Posts
    26
    Rep Power
    0

    Default Re: The Switch Statement

    This is kinda old, but I never successfully figured this out. Would the code look something like this:

    Java Code:
    public static void printAttitude(int);
    
    {
    case 1:
       System.out.println("disagree");
       break;
    
    case 2:
      System.out.println("no opinion");
      break;
    
    case 3:
      System.out.println("agree");
      break;
    }
    I feel like that "method does nothing" part is throwing me off...

  10. #10
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,926
    Rep Power
    8

    Default Re: The Switch Statement

    That's not the correct syntax for defining a method, taking a parameter, or for setting up a switch statement. They are all covered in the basic tutorials.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  11. #11
    Everyman's Avatar
    Everyman is offline Member
    Join Date
    Feb 2012
    Location
    Everywhere
    Posts
    26
    Rep Power
    0

    Default Re: The Switch Statement

    Quote Originally Posted by KevinWorkman View Post
    That's not the correct syntax for defining a method, taking a parameter, or for setting up a switch statement. They are all covered in the basic tutorials.
    Like this, right?

    Java Code:
    public          static       void        printAttitude      (    int      x    )
    {
    if          (           x       ==        1          )
    {
    System.out.println          (           "disagree"           )           ;
    }
    if            (         x         ==        2             )
    {
    System.out.println        (       "no opinion"           )         ;
    }
    if           (           x          ==         3         )
    {
    System.out.println            (           "agree"            )           ;
    }
    }

  12. #12
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,931
    Rep Power
    19

    Default Re: The Switch Statement

    Well (barring the appearance of a huuuge amount of spaces there) the method signature is OK now, but you no longer have a switch statement in there.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Replies: 2
    Last Post: 03-26-2012, 04:27 PM
  2. switch statement
    By droidus in forum New To Java
    Replies: 2
    Last Post: 09-21-2011, 09:54 AM
  3. help with switch statement
    By java__beginner in forum New To Java
    Replies: 4
    Last Post: 03-19-2009, 02:22 PM
  4. Demonstration of the switch statement
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-23-2008, 08:08 PM
  5. Switch Statement Help
    By bluegreen7hi in forum New To Java
    Replies: 6
    Last Post: 02-06-2008, 05:16 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •