Results 1 to 13 of 13

Thread: Java Methods

  1. #1
    dinosaur1993 is offline Member
    Join Date
    Mar 2014
    Posts
    11
    Rep Power
    0

    Default Java Methods

    Hi I am new to Java and would like to ask how to do this question:

    Code a Java method that accepts a String array and a String. The method should return true if the string can be found as an element of the array and
    false otherwise. Test your method by calling it from the main method which supplies its two parameters (no user input required). Use an array initialiser
    list to initialise the array you pass. Test thoroughly.

    I am trying to learn how to make a method.

    Thank you!

    Regards,
    dinosaur1993

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default Re: Java Methods

    I am trying to learn how to make a method.
    Take a look at the tutorial: Defining Methods (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,782
    Rep Power
    7

    Default Re: Java Methods

    Take it one step at a time. Try writing the method header and get it to compile. NOTE: the method will need a return statement to compile.

  4. #4
    dinosaur1993 is offline Member
    Join Date
    Mar 2014
    Posts
    11
    Rep Power
    0

    Default Re: Java Methods

    Quote Originally Posted by Junky View Post
    Take it one step at a time. Try writing the method header and get it to compile. NOTE: the method will need a return statement to compile.
    Does this look right?

    Java Code:
    public class Week4c {
    
        public static void main(String[] args) {
            String[] verbs = {"go", "do", "my", "homework" };
            printarray(verbs);
            System.out.println(returnSomething("three", "four") + " five");
        }
    
        private static void printarray(String[] words){
    
            for (String w : words) {
                System.out.printf("%s ", w);
    
            }
        }
    
        private static String returnSomething(String param1, String param2){
            String result;
    
            result = param1 + " and " + param2; //concatenation
    
            return result;
    
        }
    
    }

  5. #5
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,870
    Rep Power
    5

    Default Re: Java Methods

    What kind of response are you looking for? Only if it is technically correct, or any and all feedback also on style and naming?
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  6. #6
    dinosaur1993 is offline Member
    Join Date
    Mar 2014
    Posts
    11
    Rep Power
    0

    Default Re: Java Methods

    Does the code technically correctly answers the question stated below?

    Code a Java method that accepts a String array and a String. The method should return true if the string can be found as an element of the array and false otherwise. Test your method by calling it from the main method which supplies its two parameters (no user input required). Use an array initialiser list to initialise the array you pass. Test thoroughly.

    I am not too fussed about the naming at this stage. Thanks!

  7. #7
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    926
    Rep Power
    2

    Default Re: Java Methods

    Quote Originally Posted by dinosaur1993 View Post
    Does the code technically correctly answers the question stated below?

    Code a Java method that accepts a String array and a String. The method should return true if the string can be found as an element of the array and false otherwise. Test your method by calling it from the main method which supplies its two parameters (no user input required). Use an array initialiser list to initialise the array you pass. Test thoroughly.

    I am not too fussed about the naming at this stage. Thanks!
    No. You don't have requirement #1: a Java method that accepts a String array and a String.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2™ © 2013

  8. #8
    dinosaur1993 is offline Member
    Join Date
    Mar 2014
    Posts
    11
    Rep Power
    0

    Default Re: Java Methods

    How do you create a method that accepts a String Array and String?!

  9. #9
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    926
    Rep Power
    2

    Default Re: Java Methods

    Quote Originally Posted by dinosaur1993 View Post
    How do you create a method that accepts a String Array and String?!
    You've already created two methods that accept a String array, and a method that accepts two Strings. This is just the same.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2™ © 2013

  10. #10
    dinosaur1993 is offline Member
    Join Date
    Mar 2014
    Posts
    11
    Rep Power
    0

    Default Re: Java Methods

    How do you do it in just on method though?

  11. #11
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,870
    Rep Power
    5

    Default Re: Java Methods

    You mean to say you know how to create a method which accepts two String objects, you know how to create a method which accepts a String[] array, but you do not know how to create a method which accepts a String[] array and a String object?

    Back to the books then.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  12. #12
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    926
    Rep Power
    2

    Default Re: Java Methods

    Quote Originally Posted by dinosaur1993 View Post
    How do you do it in just on method though?
    I see a pattern here. Do you see it too?
    Java Code:
    public static void main(String[] args)
    private static void printarray(String[] words)
    private static String returnSomething(String param1, String param2)
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2™ © 2013

  13. #13
    dinosaur1993 is offline Member
    Join Date
    Mar 2014
    Posts
    11
    Rep Power
    0

Similar Threads

  1. Replies: 1
    Last Post: 07-12-2012, 11:04 PM
  2. Replies: 2
    Last Post: 07-12-2012, 11:04 PM
  3. Replies: 8
    Last Post: 04-16-2012, 11:00 PM
  4. Java Noob, trying to call methods from another methods
    By gabrielpr12 in forum New To Java
    Replies: 8
    Last Post: 11-17-2011, 09:07 PM
  5. Replies: 1
    Last Post: 10-05-2011, 12:15 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
  •