Question

In: Computer Science

Parameter Mystery. Consider the following program. List below the exact output produced by the above program...

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???

Solutions

Expert Solution

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:


Related Solutions

PROBLEM: Write a C program that will produce the EXACT output shown below. Using initialization lists,...
PROBLEM: Write a C program that will produce the EXACT output shown below. Using initialization lists, create 5 one-dimensional arrays, one for each of these: hold the names of the people running in the election (see note below) hold the names of the subdivision (see note below) hold the vote counts in the Aberdeen subdivision for each candidate hold the vote counts in the Brock subdivision for each candidate hold the vote counts in the Sahali subdivision for each candidate...
1. Consider the program below. a. What would the output be in a language with static...
1. Consider the program below. a. What would the output be in a language with static scoping rules? Explain your answer. b. What would the output be in a language with dynamic scoping rules? Explain your answer. int x; //global int main() {    x = 11;    fun1();    fun2(); } void fun1() {    int x = 19;    fun3(); } void fun2() {    int x = 4;    fun3(); } void fun3() {    print(x); }
Create the following java program with class list that outputs: //output List Empty List Empty List...
Create the following java program with class list that outputs: //output List Empty List Empty List Empty Item not found Item not found Item not found Original list Do or do not. There is no try. Sorted Original List Do There do is no not. or try. Front is Do Rear is try. Count is 8 Is There present? true Is Dog present? false List with junk junk Do or moremorejunk do not. There is no try. morejunk Count is...
Write a  program in c++ using a map to create the following output. Here is the list...
Write a  program in c++ using a map to create the following output. Here is the list of students: 100: Tom Lee 101: Joe Jones 102: Kim Adams 103: Bob Thomas 104: Linda Lee Enter a student an ID to get a student's name: ID:  103 students[103] - Bob Thomas # of students: 5 Delete a student (Y or N)?  Y Enter ID of student to be deleted:  103 Here is the list of students after the delete: 100: Tom Lee 101: Joe Jones...
Which of the types listed below can be the type of a function output parameter? unsigned...
Which of the types listed below can be the type of a function output parameter? unsigned long * double unsigned long char * Void Consider this function that takes a fraction (with arguments for numerator and denominator) and inverts it in place: void invert_fraction(type num, type denom) {    int tmp = *num;    if (*num == 0 || *denom == 0) {        fprintf(stderr, "ERROR: cannot invert %d/%d.\n", *num, *denom);        exit(1);     }     *num = *denom;    ...
Consider a country whose output can be produced with 2 inputs (capital and labor). The output...
Consider a country whose output can be produced with 2 inputs (capital and labor). The output per worker/capita production function is given by y=k1/2, where y represents output per worker/capita and k is capital per worker/capita.  Assume the fraction of output saved/invested is (the savings rate) s = 25%, the population growth rate is 0%, the depreciation rate δ=5%, the level of technology is constant at A=1 and the assumptions of the Solow model hold. What are the steady state levels...
Consider the following data: equilibrium price = $21, quantity of output produced = 1,000 units, average...
Consider the following data: equilibrium price = $21, quantity of output produced = 1,000 units, average total cost = $13, and average variable cost $9. Given this information, total revenue is ___________, total cost is _____________, and total fixed cost is ______________.
Consider the following data: equilibrium price = $15, quantity of output produced = 800 units, average...
Consider the following data: equilibrium price = $15, quantity of output produced = 800 units, average total cost = $13, and average variable cost $9. Given this information, total revenue is ___________, total cost is _____________, and total fixed cost is ______________. $9,000; $8,000; $6,000 $12,000; $10,400; $3,200 $1,200; $1,040; $320 $12,000; $10,400; $7,200
Given the following code, what is output by the method call, mystery(6 * 8)? public static...
Given the following code, what is output by the method call, mystery(6 * 8)? public static void mystery (int x[]) {     System.out.println("A"); } public static void mystery (int x) {     System.out.println("B"); } public static void mystery (String x) {     System.out.println("C"); } A B C CA CB Which of the following is true about overloaded methods? Java cannot use a method's return type to tell two overloaded methods apart. Java cannot use a method's parameters to tell two overloaded methods apart....
Consider a market in which all output is produced by two firms, A and B. The...
Consider a market in which all output is produced by two firms, A and B. The market inverse demand curve is given by where a is 260 and b is 1. Both firms have a constant marginal cost equal to 3. a) Find the Stackelberg equilibrium outputs for firms A and B, the equilibrium market price, and the equilibrium profit for each firm, on the assumption that firm A is the leader and firm B is the follower. b) Repeat...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT