Write a Java program that contains the following three overl
Write a Java program that contains the following three overloaded methods, and then you can invoke them one by one to see the difference on both input and output.
Sample code:
import javax.swing.JOptionPane;
public class OverLoadedMethodSample
{
public static void main(String args[])
{
int feelGoodOrNot = 0; // 0 stands for unsatisfied, and 1 means happy
int timeToGreet = 5;
String greetingWords = “Happy Holidays!”;
System.out.println(“Now let’s see the first version of seasonal greetings!\ ”); SeasonalGreeting(timeToGreet);
System.out.println();
System.out.println(“How about the second one?\ ”);
SeasonalGreeting(timeToGreet, greetingWords);
System.out.println();
System.out.println(“Here comes the best one so far”);
feelGoodOrNot = SeasonalGreeting();
if (feelGoodOrNot == 1){
System.out.println(“It seems that you are happy for the last version!”);
}
else if (feelGoodOrNot == 0){
System.out.println(“Let’s try that version again!”);
}
else{
System.out.println(“There must be some problem in my last method.”);
}
}
public static void SeasonalGreeting(int counter)
{
//The first version just takes in the counter and print out the FIXED greeting
}
public static void SeasonalGreeting(int counter, String wordsToSay)
{
//The second version takes both counter and your customized greeting words
}
public static int SeasonalGreeting()
//The third version does NOT have any parameter passed in, and you should use
//JOptionPane.showInputDialog to ask the user their name, then greeting them
//according to their name, and finally ask them if they are happy or not. If they
//are, then return 1; if they do not feel satisfied to this method, then return 0.
}
Solution
import static java.lang.System.exit;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class OverLoadedMethodSample {
public static void main(String args[]) {
int feelGoodOrNot = 0; // 0 stands for unsatisfied, and 1 means happy
int timeToGreet = 5;
String greetingWords = \"Happy Holidays!\";
System.out.println(\"Now let’s see the first version of seasonal greetings!\ \");
SeasonalGreeting(timeToGreet);
System.out.println();
System.out.println(\"How about the second one?\ \");
SeasonalGreeting(timeToGreet, greetingWords);
System.out.println();
System.out.println(\"Here comes the best one so far\");
feelGoodOrNot = SeasonalGreeting();
switch (feelGoodOrNot) {
case 1:
System.out.println(\"It seems that you are happy for the last version!\");
break;
case 0:
System.out.println(\"Let’s try that version again!\");
break;
default:
System.out.println(\"There must be some problem in my last method.\");
break;
}
exit(0);
}
/**
*
* @param counter
*/
public static void SeasonalGreeting(int counter) {
System.out.println(\"Happy Birth Day!\");
}
/**
*
* @param counter
* @param wordsToSay
*/
public static void SeasonalGreeting(int counter, String wordsToSay) {
System.out.println(wordsToSay);
}
/**
*
* @return
*/
public static int SeasonalGreeting() {
JFrame frame = new JFrame(\"InputDialog Example #1\");
// prompt the user to enter their name
String name = JOptionPane.showInputDialog(frame, \"What\'s your name?\");
JOptionPane.showMessageDialog(frame, \"Happy birthday : \'\" + name + \"\'.\");
int response = JOptionPane.showConfirmDialog(null, \"are you happy Sir?\", \"Confirm\",
JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
if (response == JOptionPane.NO_OPTION) {
return 0;
}
if (response == JOptionPane.YES_OPTION) {
return 1;
}
return -1;
}
}


