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...
Java Program. Please read carefully. i need the exact same output as below. if you unable...
Java Program. Please read carefully. i need the exact same output as below. if you unable to write the code according to the question, please dont do it. thanks To the HighArray class in the highArray.java program (Listing 2.3), add the following methods: 1.      getMax() that returns the value of the highest key (value) in the array without removing it from the array, or –1 if the array is empty. 2.      removeMax() that removes the item with the highest key from the...
Do a walk-through of the following program segments and determine, in each case, the exact output:...
Do a walk-through of the following program segments and determine, in each case, the exact output: a) int i, j; for (i = 1; i <= 5; i++) { for (j = 1; j <= 5; j++) cout << setw (3) << i; cout << endl; } b) int i, j; for (i = 1; i <= 5; i++) { for (j = (i + 1; j <= 5; j++) cout << setw(5) << j; cout << endl; c) int...
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...
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...
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 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
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT