Question: (a) Given this class definition, what would be printed out by the code System.out.println(new Friends(2))? Use text and/or diagrams to show how you derive the

 (a) Given this class definition, what would be printed out by

(a) Given this class definition, what would be printed out by the code System.out.println(new Friends(2))? Use text and/or diagrams to show how you derive the answer.

(b) Produce an English description of the replace method, including an explanation of the effect achieved by use of the % operator. Suggest an alternative way of achieving this effect.

(c) Given fr is the object constructed by new Friends(3), what would be returned by the call fr.replace(fr.man)? Use text and/or diagrams to show how you derived your answer.

1. Consider the following Java program, consisting of the class Friends. Notice this is defined to have one constructor and two methods, one named replace, and one name toStrina.. public class Friends - String man, woman; String[] names = {"xi", "m "yu ", "dave", "anne", "johny", "olive"}; Jen" public Friends(int k) f for (int i = 0; | k) man - names[i]; woman = names [i+11 breaki F f public String replace(String name) for (int i = 0; i names. length; 1++) if (names[i].equals (name) ) i name = names [ ( i+2 ) % names. length ] ; break; return name; public String toString() s String s = man + " is friends with " + woman; return s

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!