Both files run, you have to answers the following questions with the reason you
ID: 3627532 • Letter: B
Question
Both files run, you have to answers the following questions with the reason you chose an answer:o Integer variables are passed by ____________
o Integer arrays are passed by ____________
o String variables are passed by ____________ (You will need to research the String class
to explain how String objects are passed into methods!)
o Object variables are passed by ____________
public class MyClass
{ String name;
int hours;
public MyClass(String _name, int _hours)
{ name = _name;
hours = _hours;
}
public void setNameHours(String _name, int _hours)
{ name = _name;
hours = _hours;
}
public String toString()
{ return "Name = " + name + " hours = " + hours;
}
}
public class Assignment3_2
{
public static void main(String args[])
{ int int_test = 10;
int int_array[] = {100,200,300};
String str_test = "CIS150";
MyClass object_test = new MyClass("Maya", 15);
System.out.println(" int_test before method call..." + int_test);
test_passing_int(int_test);
System.out.println("int_test after method call..." + int_test);
print_array(int_array, " int_array before method call..");
test_passing_array(int_array);
print_array(int_array, "int_array after method call..");
test_passing_array(int_array);
System.out.println(" str_test before method call..." + str_test);
test_passing_String(str_test);
System.out.println("str_test after method call..." + str_test);
System.out.println(" object_test before method call..." + object_test.toString());
test_passing_object(object_test);
System.out.println("object_test after method call..." + object_test.toString());
}
public static void test_passing_int(int _i)
{ _i *= 2;
}
public static void print_array(int _arr[], String msg)
{ System.out.print(msg);
for(int i=0; i < _arr.length; i++)
System.out.print(_arr[i] + " ");
System.out.println();
}
public static void test_passing_array(int _arr[])
{ for(int i=0; i < _arr.length; i++)
_arr[i] *= 2;
}
public static void test_passing_String(String _str)
{ _str = "CIS250";
}
public static void test_passing_object(MyClass _obj)
{ _obj.setNameHours("Pam", 16);
}
}
Explanation / Answer
o Integer variables are passed by Pass-By-Value, so just a copy is given to the method (because ints are a primitive type) and the original value remains unchanged. This is evidenced by int_test. o Integer arrays are passed by Pass-By-Reference (because an array is just a group of addresses, the address of array[0] is passed), so a method is editing the actual array. This is evidenced by int_array. o String variables are passed by Pass-By-Reference (because an actual String object is created for every String), BUT String objects are immutable, so the method does not change the actual String (as seen in str_test). o Object variables are passed by Pass-By-Reference, so the address of the object is passed and the method is editing the real object (as seen in object_test).
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.