Results 1 to 7 of 7
Like Tree3Likes
  • 1 Post By milovan
  • 1 Post By kneitzel
  • 1 Post By kneitzel

Thread: java Method overloading

  1. #1
    danielki is offline Member
    Join Date
    Feb 2014
    Posts
    16
    Rep Power
    0

    Default java Method overloading

    Hello, I am trying to do the following exercise but I keep getting errors, can any one help me
    what my mistake is thanks!

    The class Overloading below asks for two names and prints three different greetings. Your task is to write the class methods missing from the class declaration. Methods print the greetings as shown in the example print.

    Hint:The names and parameter types of the needed methods can be checked from the main method because all methods are called there. This exercise also does not require you to copy the source code below to the return field. The method declarations will suffice.
    Example output
    Type in the first name: John
    Type in the second name: Doe

    **********
    Hi!
    **********
    Hi, John
    **********
    Hi, John and Doe
    **********

    Java Code:
    import java.util.Scanner;
    
    public class Overloading {
    
        public static void main(String[] args) {
    
            String firstName, secondName;
    
            Scanner reader = new Scanner(System.in);
    
     
    
            System.out.print("Type in the first name: ");
    
            firstName = reader.nextLine();
    
     
    
            System.out.print("Type in the second name: ");
    
            secondName = reader.nextLine();            
    
     
    
            System.out.println("\n**********");
    
            greet();
    
            System.out.println("**********");
    
     
    
            greet(firstName);
    
            System.out.println("**********");
    
     
    
            greet(firstName, secondName);
    
            System.out.println("**********");
    
        }
    
     
    
        //  the needed greeting methods
      private static void greet(){
        	System.out.println("Hi!");
        }
         
        private static void greet(string firstName){
       
        System.out.println("Hi, "+firstName);
        	
        	
        }
          
        private static void greet(string firstName, string secondName){
        
        	System.out.println("Hi, "+firstName+" and "+secondName);
        	
        }
    
    }

  2. #2
    milovan is offline Senior Member
    Join Date
    Jan 2011
    Location
    Belgrade, Serbia
    Posts
    301
    Rep Power
    4

    Default Re: java Method overloading

    What errors you receive?
    danielki likes this.

  3. #3
    danielki is offline Member
    Join Date
    Feb 2014
    Posts
    16
    Rep Power
    0

    Default Re: java Method overloading

    It is a compiler error and say:
    Overloading.java:36: cannot find symbol

    symbol : class string

    location: class Overloading

    private static void greet(string firstName){

    ^

    Overloading.java:43: cannot find symbol

    symbol : class string

    location: class Overloading

    private static void greet(string firstName, string secondName){

    ^

    Overloading.java:43: cannot find symbol

    symbol : class string

    location: class Overloading

    private static void greet(string firstName, string secondName){

    ^

    3 errors

  4. #4
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Posts
    447
    Rep Power
    1

    Default Re: java Method overloading

    Hi,

    you should be aware that "string" is not "String". (So java knows a class String but not a class string. That is the error message telling you.)

    So you should change the string to String for the parameters of your functions.

    With kind regards,

    Konrad
    danielki likes this.

  5. #5
    danielki is offline Member
    Join Date
    Feb 2014
    Posts
    16
    Rep Power
    0

    Default Re: java Method overloading

    Hi Konrad,
    you are right but that is not my code, my task is to write a partial code(the 3 methods) I only feed the software the 3 methods. do you see any thing
    wrong with my methods?

    // the needed greeting methods
    private static void greet(){
    System.out.println("Hi!");
    }

    private static void greet(string firstName){

    System.out.println("Hi, "+firstName);


    }

    private static void greet(string firstName, string secondName){

    System.out.println("Hi, "+firstName+" and "+secondName);

    }

    }

  6. #6
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Posts
    447
    Rep Power
    1

    Default Re: java Method overloading

    Hi,

    what I meant is that you wrote string and not String. Just change the definitions of your methods:
    private static void greet(String firstName, String secondName){
    and
    private static void greet(String firstName){

    So
    String is a known class.
    string is an unknown symbol

    With kind regards,

    Konrad
    danielki likes this.

  7. #7
    danielki is offline Member
    Join Date
    Feb 2014
    Posts
    16
    Rep Power
    0

    Default Re: java Method overloading

    Hi Konrad,
    Thank you it worked now fine.

Similar Threads

  1. Method Overloading
    By allaudin in forum New To Java
    Replies: 1
    Last Post: 02-15-2014, 10:07 PM
  2. overloading an equals method
    By tonmachine in forum New To Java
    Replies: 5
    Last Post: 03-17-2013, 02:26 AM
  3. Method Overloading - Doubt
    By vidya lakshman in forum New To Java
    Replies: 4
    Last Post: 02-01-2011, 07:47 AM
  4. Method Overloading Verification
    By lala in forum New To Java
    Replies: 22
    Last Post: 11-30-2010, 06:32 AM
  5. method overloading and arrays.
    By glopez09 in forum New To Java
    Replies: 5
    Last Post: 12-08-2009, 04:02 PM

Posting Permissions

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