Monday, February 24, 2014

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);

}

}


No comments:

Post a Comment