Ask Me Help Desk

Ask Me Help Desk (https://www.askmehelpdesk.com/forum.php)
-   Java (https://www.askmehelpdesk.com/forumdisplay.php?f=440)
-   -   Need help into passing the object into the method and make it work (https://www.askmehelpdesk.com/showthread.php?t=706028)

  • Sep 30, 2012, 07:25 PM
    oneoderja
    Need help into passing the object into the method and make it work
    Define a class called ‘twoInt’ with two public integer fields a and b
    In the constructor of ‘twoInt’, initialize the a and b to be 1 and 2
    Create an instance ‘mytwoInt’ in the main method
    Write a method ‘swap’ (not the method of the class ‘twoInt’) and pass the above instance ‘mytwoInt’ into the method, swap two a and b in the method
    Print out a and b in the main method
    System.out.print(object.a + ‘ ‘ + object.b);
    Swap(object);
    System.out.print(object.a + ‘ ‘ + object.b);

    My Code:

    public class TwoInt
    {
    public static void main (String [] args)
    {
    int a;
    int b;

    TwoInt myTwoInt = new TwoInt();

    System.out.println(myTwoInt.a + " " + myTwoInt.b);

    }

    public TwoInt()
    {
    a = 1;
    b = 2;
    }

    public void swap (myTwoInt int a, int b)
    {
    int tmp = a;

    a = b;
    b = tmp;

    System.out.println(myTwoInt.a + " " + myTwoInt.b);

    }

  • All times are GMT -7. The time now is 05:08 AM.