Sei sulla pagina 1di 2

Methods

WS #2 Answers (2016):

1.) Parameter Mystery1 Output:

int a = 4, b = 7, c = -2; -4 + 12 = 11
mystery(a, b, c); 2 + 8 = 1
mystery(c, 3, a); 0 + 10 = 5
mystery(a + b, b + c, c + a);

//formal parameters in method header:
public static void mystery(int c, int a, int b)

2.) Parameter Mystery2 Output:

String a = “king”; a two and a queen beats a king
String b = “two”; a queen and a queen beats a b
String c = “queen”; a two and a king beats a five
String two = “five”; a king and a two beats a queen
a queen and a two beats a five
sentence (a, b, c);
sentence(“b”, c, c);
sentence(two, “two”, a);
sentence(c, a, b);
sentence(two, “queen”, b);

//formal parameters in method header:


public static void sentence(String y, String z, String x)


3. ParameterMystery3 Output:

String major = “fred”; Many a student in the computer of fred
String fred = “computer”;
String computer = “department”; Many a computer in the department of major
String department = “student”; Many a department in the honor of fred
String student = “major”;
Many a baz in the bar of foo
sentence(major, fred, department); Many a major in the department of computer
sentence(student, computer, fred);
sentence(“fred”, “honor”, computer);

sentence(“foo”, “bar”, “baz”);
sentence(fred, computer, student);

//formal parameters in method header:
public static void sentence(String major, String fred, String foo) {









4. Distance formula example:

public static double distanceFormula(int x1, int y1, int x2, int y2)
{
 double dist = Math.sqrt((Math.pow((x2 - x1), 2)) +
(Math.pow((y2 - y1), 2)));

  return dist;
}

5. Vertical letters example:

public static void vertical(String str)


{
for(int i = 0; i < str.length(); i++)
{
System.out.println(str.charAt(i));
}
}

Potrebbero piacerti anche