Ask your own question, for FREE!
Mathematics 13 Online
OpenStudy (yumi):

Need help with java project please!!

OpenStudy (lxdrpr):

I might be able to help

OpenStudy (yumi):

have u worked with java before?

OpenStudy (lxdrpr):

yes I have

OpenStudy (yumi):

okay, awesome!

OpenStudy (yumi):

so i've basically completed the program except for the last step

OpenStudy (yumi):

those are the instructions

OpenStudy (yumi):

need help with the second step

OpenStudy (lxdrpr):

hmm can i get the pictures

OpenStudy (yumi):

what pictures?

OpenStudy (yumi):

modify the main method so after the call to the swap method the actual values of num1 and num2 are swapped in main and show this in the output. Your output will have one additional line after the listing on page 213 that shows the values of the two numbers in main after they are swapped. See new output line below in blue text. Before invoking the swap method, num1 is 1 and num2 is 2 Inside the swap method Before swapping n1 is 1 n2 is 2 After swapping n1 is 2 n2 is 1 After invoking the swap method, num1 is 1 and num2 is 2 After swapping the numbers in main method, num1 is 2 and num2 is 1.

OpenStudy (lxdrpr):

*Listings

OpenStudy (yumi):

thats the step i need help with

OpenStudy (yumi):

public class Chpt6_Tutorial { public static void main(String[] args) { // Declare and initialize variables int num1 = 1; int num2 = 2; System.out.println("Before invoking the swap method, num1 is " + num1 + " and num2 is " + num2); // Invoke the swap method to attempt to swap two variables swap(num1, num2); System.out.println("After invoking the swap method, num1 is " + num1 + " and num2 is " + num2); } /** Swap two variables */ public static void swap(int n1, int n2){ System.out.println("\tInside the swap method"); System.out.println("\t\tBefore swapping, n1 is " + n1 + " and n2 is " + n2); //Swap n1 with n2 int temp = n1; n1 = n2; n2 = temp; System.out.println("\t\tAfter swapping, n1 is " + n1 + " and n2 is " + n2); } }

OpenStudy (yumi):

thats my code

OpenStudy (lxdrpr):

hmmm not sure ive only done really basic things i might know someone who can help

OpenStudy (mhchen):

Wait, so you really just need to swap the values of 2 variables right?

OpenStudy (yumi):

okay,thanks anyway

OpenStudy (yumi):

yes

OpenStudy (mhchen):

Don't you have it here: //Swap n1 with n2 int temp = n1; n1 = n2; n2 = temp; You stored n1 into a temporary location. Then made n1 = n2 Then used n2 = the temporary location.

OpenStudy (yumi):

yes, but then it says

OpenStudy (yumi):

modify the main method so after the call to the swap method the actual values of num1 and num2 are swapped in main and show this in the output. Your output will have one additional line after the listing on page 213 that shows the values of the two numbers in main after they are swapped. See new output line below in blue text. Before invoking the swap method, num1 is 1 and num2 is 2 Inside the swap method Before swapping n1 is 1 n2 is 2 After swapping n1 is 2 n2 is 1 After invoking the swap method, num1 is 1 and num2 is 2 After swapping the numbers in main method, num1 is 2 and num2 is 1.

OpenStudy (yumi):

no

OpenStudy (mhchen):

So you just need to transfer the swapping into the main() instead of swap() right?

OpenStudy (yumi):

im not sure, thats why i came for help haha, not really clear on what i need to do. sorry haha

OpenStudy (opti):

what happens when you run it

OpenStudy (mathmate):

@yumi Have you learned about call by reference and call by value? Java passes every everything by value. So if you call swap(a,b), in the method, you're just swapping copies of a and b. Is that where you have a problem?

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!