In: Computer Science
Parameter Mystery. Consider the following program. List below the exact output produced by the above program (we do not claim that every line of output makes sense ;-).
public class Mystery
{ public static void main(String[] args)
{ String john = "skip";
String mary = "george";
String george = "mary";
String fun = "drive"; statement(george, mary, john);
statement(fun, "george", "work");
statement(mary, john, fun);
statement(george, "john", "dance"); }
public static void statement(String mary, String john, String fun)
{ System.out.println(john + " likes to " + fun + " with " + mary); } }
I know the output is
george likes to skip with mary george likes to work with drive skip likes to drive with george john likes to dance with mary
But I don't know why- can I get an explanation as to why???
Code:
/*
NOTE: Variables declared inside a method and
it's parameters(e.g statement(String example)) are
local
i.e they are present to use only in that method, and
their is
no problem if any other method have variables with
same name,
because they are different.
*/
public class Mystery {
/*
This method will print the value
passed to it in parameter john
then add "likes to" then the value
passed to it in fun, then add
"with" and finally the value passed
to it in mary
*/
public static void statement(String mary, String john,
String fun) {
System.out.println(john + " likes
to " + fun + " with " + mary);
}
public static void main(String[] args) {
String john = "skip";
String mary = "george";
String george = "mary";
String fun = "drive";
// I'll describe each call to the method statement()
/*
Inside the
statement
mary = value of
george i.e "mary"
john = value of
mary i.e "george"
fun = value of
john i.e "skip"
System.out.println(john + " likes to " + fun + " with " +
mary);
will print
System.out.println("george" + " likes to " + "skip" + " with " +
"mary");
*/
statement(george, mary, john);
/*
Inside the
statement
mary = value of
fun i.e "drive"
john =
"george"
fun =
"work"
System.out.println(john + " likes to " + fun + " with " +
mary);
will print
System.out.println("george" + " likes to " + "work" + " with " +
"drive");
*/
statement(fun, "george",
"work");
/*
Inside the
statement
mary = value of
mary i.e "george"
john = value of
john i.e "skip"
fun = value of
fun i.e "drive"
System.out.println(john + " likes to " + fun + " with " +
mary);
will print
System.out.println("skip" + " likes to " + "drive" + " with " +
"george");
*/
statement(mary, john, fun);
/*
Inside the
statement
mary = value of
george i.e "mary"
john =
"john"
fun =
"dance"
System.out.println(john + " likes to " + fun + " with " +
mary);
will print
System.out.println("john" + " likes to " + "dance" + " with " +
"mary");
*/
statement(george, "john",
"dance");
}
}
OUTPUT: