Results 1 to 7 of 7
  1. #1
    lala is offline Member
    Join Date
    Oct 2010
    Posts
    80
    Rep Power
    0

    Default Write & Call User-Defined Method

    Hello All. I have to write & call a user-defined method.
    This is the beginning of the program.
    Java Code:
    import java.util.*;
    import java.lang.Math;
    
    public class CalcAge
    {
      static Scanner console = new Scanner(System.in);
      public static void main(String[] args)
      {
       int currentYear = 0, currentMonth = 0, year, month, age;
       String line;
       char again = 'y';
    		
       System.out.println("This program asks you to enter today's "		
    	 + "year in 4-digits.\n"
         + "Then it asks you to enter today's month number.\n\n"	
         + "In the next step you will be asked to enter "
         + "the 4-digit year of your birth.\n"
         + "Then you will be asked to enter the month number "
         + "of your birth.\n\n"
         + "The program will calculate and display how old you "
         + " are in years and months. \n\n");
    		  
       while (currentYear <2011 || currentMonth < 1
    	  || currentMonth >12)
    The the instructions asks me to:
    // Call the method getValue with the String Argument, "Enter today's 4-digit year: ".
    Java Code:
    currentYear =
    My question is do I write a method named "getValue"?
    Something like this:
    Java Code:
    public static int getValue (String line);
    Will someone please someone enlighten me. I see faint flickers of understanding. Enlarge my flickers. :confused:
    Thanks In Advance

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    My question is do I write a method named "getValue"?
    Yes.


    Something like this:

    Java Code:
    public static int getValue (String line);

    Before asking us, first you must check if it compiles.

    Please have a look at the tutorial on defining methods. The only difference for you is that your method will likely need to be defined static.

  3. #3
    lala is offline Member
    Join Date
    Oct 2010
    Posts
    80
    Rep Power
    0

    Default Write & Call User-Defined Method

    Errors after compile:
    currentYear = getValue; (getValue cannot be resolved)

    while (currentYear < 2011 || currentMonth < 1 || currentMonth > 12)
    currentYear = getValue;
    }
    {
    (syntax error, insert "}" to complete Block.)


    Java Code:
    while (currentYear < 2011 || currentMonth < 1 || currentMonth > 12)   currentYear = getValue; 
      }
      {   	  
      // Call the method getValue with the String Argument,
     // "Enter today's 4-digit year: ".
    
     public static  int getValue(String message)
       {
    	int value;
    	 System.out.print(message);
    	value = console.nextInt();
    	return value;
    	      	}
    }
    Thank you.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Please format your code including indentation of the braces so that it's readable. I see errors, but it's hard to know what you're intending to do based on the strange indentation. Also you'll need to post more of the code, a "wider" snapshot.

  5. #5
    lala is offline Member
    Join Date
    Oct 2010
    Posts
    80
    Rep Power
    0

    Default Write & Call User-Defined Method

    Thanks for responding. I hope my formatting is better.
    Thanks again, lala

    import java.util.*;
    import java.lang.*;

    public class CalcAge
    {
    static Scanner console = new Scanner (System.in);
    public static void main(String[] args)
    {
    int currentYear = 0, currentMonth= 0, year, month, age;
    String line;
    char again = 'y';

    System.out.println("This program asks you to enter today's "
    + "year in 4-digits.\n"
    + "Then it asks you to enter today's month number.\n\n"
    + "In the next step you will be asked to enter "
    + "the 4-digit year of your birth.\n"
    + "Then you will be asked to enter the month number "
    + "of your birth.\n\n"
    + "The program will calculate and display how old you "
    + " are in years and months. \n\n");
    while (currentYear < 2011 || currentMonth < 1 || currentMonth > 12)
    {
    currentYear = getValue; (Err: getValue cannot be resolved. )
    } (Syntax error, insert "}" to complete Block)



    // Call the method getValue with the String Argument, "Enter today's 4-digit year:".
    public static int getValue(String message)
    {
    int value;
    System.out.print(message);
    value = console.nextInt();
    return value;
    }
    }

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Each method has a signature which tells you how it must be called and what it returns, and is shown usually by the first line of the method, and getValue is no different, and here is its signature:

    Java Code:
    public static int getValue(String message)

    This tells you that the method is public -- visible everywhere, and is static -- can be called on the class and without first creating an object, but more importantly for your purposes, it tells you that the method requires that you pass a String parameter to it, and it returns an int.

    So this won't work:

    Java Code:
    currentYear = getValue;

    since you need to give a method call parenthesis, and you need to pass a String in that parenthesis. i.e.,:

    Java Code:
    currentYear = getValue("Please enter a value: ");

  7. #7
    lala is offline Member
    Join Date
    Oct 2010
    Posts
    80
    Rep Power
    0

    Default Write & Call User-Defined Method (Solved)

    Fubarable, with deepest sincerity, I thank you so very, very much.

    lala

Similar Threads

  1. draw shape with user defined values
    By cka in forum New To Java
    Replies: 4
    Last Post: 07-14-2010, 03:35 PM
  2. User Defined Method
    By overcranked in forum New To Java
    Replies: 6
    Last Post: 04-09-2010, 01:02 AM
  3. JSP with user-defined java classes
    By adammyth in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 03-05-2010, 06:13 PM
  4. RegardingSgould Enter in user defined inbox
    By Prashant.surwade in forum CLDC and MIDP
    Replies: 0
    Last Post: 09-23-2009, 01:21 PM
  5. How to call defined java class in Jsp
    By asheeshiit in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 03-04-2009, 04:21 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
  •