Results 1 to 5 of 5
Thread: Grade my questions, lol
- 12-16-2009, 03:20 AM #1
Member
- Join Date
- Sep 2009
- Location
- http://www.kouje.com
- Posts
- 16
- Rep Power
- 0
Grade my questions, lol
Can u guys just help me makes sure that these questions are answered correctly? thx
*edit*
I didn't write this program, it was provided by the instructor.
Integer variables are passed by:test_passing_intInteger arrays are passed by:test_passing_arrayString variables are passed by:test_passing_StringObject variables are passed by:test_passing_object
Java Code:public class Assignment8_B { public static void main(String[] args) { int int_test = 10; int int_array[] = {100, 200, 300}; String str_test = "CIS 150"; MyClass object_test = new MyClass("Carlos", 15); System.out.println("int_test BEFORE method call...\t\t" + int_test); test_passing_int(int_test); System.out.println("int_test AFTER method call...\t\t" + int_test); print_array(int_array, "Int_array BEFORE method call...\t"); test_passing_array(int_array); print_array(int_array, "int_array AFTER method call.....\t"); test_passing_array(int_array); System.out.println("str_test BEFORE method call...\t\t" + str_test); test_passing_String(str_test); System.out.println("str_test AFTER method call...\t\t" + str_test); System.out.println("object_test BEFORE method call...\t" + object_test.toString()); test_passing_object(object_test); System.out.println("object_test AFTER method call...\t" + 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] + "\t"); 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 = "CIS150"; } public static void test_passing_object(MyClass _obj) { _obj.setNameHours("Pam\t", 16); } }
Last edited by syntrax; 12-16-2009 at 03:49 AM.
- 12-16-2009, 03:33 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,716
- Rep Power
- 19
Here's a utility method you could find helpful in your tests:
Java Code:absolutely final String how() { return "By value"; }
- 12-16-2009, 03:39 AM #3
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,716
- Rep Power
- 19
More seriously, why are your test_passing_string() and test_passing_object() methods so different?
The string one shows how to test how the method calling is done: pass a string parameter, within the method change the value of the parameter, check the value that the caller sees afterwards. This same strategy should be followed for object or you won't be comparing apples with apples.
Java Code:public static void test_passing_object(MyClass _obj) { _obj = new MyClass("Pam\t", 16); // or whatever assigns a reference to a new MyClass }
- 12-16-2009, 03:48 AM #4
Member
- Join Date
- Sep 2009
- Location
- http://www.kouje.com
- Posts
- 16
- Rep Power
- 0
- 12-16-2009, 07:03 AM #5
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,716
- Rep Power
- 19
What answers? You've posted only your instructor's code.
My point was simply that two of the methods (test_passing_int() and test_passing_string()) change the value assigned to the parameter while the other two (test_passing_array() and test_passing_object()) do not. It seemed to me that this was a bit odd if the methods were to act as a test that compared the behaviour of the four types. And I asked why they were different.
Similar Threads
-
Help with JAVA (Grade Book)
By Sara_21 in forum New To JavaReplies: 3Last Post: 11-30-2009, 03:45 PM -
beginner grade 11-need help with RaceCar Game
By bobmasta5 in forum New To JavaReplies: 0Last Post: 01-09-2009, 10:04 PM -
beginner grade 11-need help with a math code involving returning values from a method
By bobmasta5 in forum New To JavaReplies: 3Last Post: 12-10-2008, 02:38 AM -
questions for 1yr exp
By rahaman.athiq in forum Java ServletReplies: 2Last Post: 11-26-2008, 02:13 AM -
Just a Few Questions
By pringle in forum New To JavaReplies: 21Last Post: 01-09-2008, 07:21 PM
Bookmarks