Results 1 to 5 of 5
  1. #1
    syntrax is offline Member
    Join Date
    Sep 2009
    Location
    http://www.kouje.com
    Posts
    16
    Rep Power
    0

    Talking 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_int
    Integer arrays are passed by:
    test_passing_array
    String variables are passed by:
    test_passing_String
    Object 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.

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    Here's a utility method you could find helpful in your tests:

    Java Code:
    absolutely final String how() {
      return "By value";
    }

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    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
    }

  4. #4
    syntrax is offline Member
    Join Date
    Sep 2009
    Location
    http://www.kouje.com
    Posts
    16
    Rep Power
    0

    Default

    Quote Originally Posted by pbrockway2 View Post
    Here's a utility method you could find helpful in your tests:

    Java Code:
    absolutely final String how() {
      return "By value";
    }
    so,, are my answers correct? according to wat ur showing they're not?

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    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

  1. Help with JAVA (Grade Book)
    By Sara_21 in forum New To Java
    Replies: 3
    Last Post: 11-30-2009, 03:45 PM
  2. beginner grade 11-need help with RaceCar Game
    By bobmasta5 in forum New To Java
    Replies: 0
    Last Post: 01-09-2009, 10:04 PM
  3. Replies: 3
    Last Post: 12-10-2008, 02:38 AM
  4. questions for 1yr exp
    By rahaman.athiq in forum Java Servlet
    Replies: 2
    Last Post: 11-26-2008, 02:13 AM
  5. Just a Few Questions
    By pringle in forum New To Java
    Replies: 21
    Last Post: 01-09-2008, 07:21 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •