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);

Need help
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 (TwoInt myTwoInt)
    {
        int tmp = a;

        a = b;
        b = tmp;

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

    }
}

Recommended Answers

All 8 Replies

  public TwoInt()
    {
        //a = 1;
        //b = 2;
        //add this

        swab(a,b);

    }

if it did not work, try
myTwoInt.swab(a,b) instead of swab(a,b);

let me know if this works

No that did not work, the output should give 1 and 2 and then a and b shouel swap using that swap method and passing the instance object into the method

public class TwoInt
{

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


        TwoInt myTwoInt = new TwoInt();

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

    }

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

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

        a = b;
        b = tmp;

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

    }
}

Try this, copy paste

sorry, I will post the correct answer one minute please

public class TwoInt
{

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


        TwoInt myTwoInt = new TwoInt();

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

    }

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

    public void swap (TwoInt myTwoInt)
    {
        int tmp = myTwoInt.a;

        a = myTwoInt.b;
        b = tmp;

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

    }
}

This should work

Thanks man, genius. Do you mind if i have ur email? i want you to help me with another one

Just post a new and I will work with you,

Here is the other Bro

Define an integer array ‘arr’ with size 2 in the main method and initialize the two elements in ‘arr’ to be 1 and 2
Write a method ‘swap’ and pass the ‘arr’ into the method, swap two elements of the ‘arr’ in the method
Print out all elements in ‘arr’ in the main method
System.out.print(arr[0] + ‘ ‘ + arr[1]);
Swap(arr);
System.out.print(arr[0] + ‘ ‘ + arr[1]);

public class ArrayInt
{
    public static void main (String[] args)
    {
        int [] arr = new int [2];

        arr [0]  = 1;
        arr [1] = 2;

        System.out.prinln(arr[0] + " " + arr[1]);
    }

    public void swap (int [] arr)
    {
        int tmp = myTwoInt.a;
        a = myTwoInt.b;
        b = tmp;
    System.out.println(arr[0] + " " + arr[1]);
    }
}
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.