Need help with java project please!!
I might be able to help
have u worked with java before?
yes I have
okay, awesome!
so i've basically completed the program except for the last step
those are the instructions
need help with the second step
hmm can i get the pictures
what pictures?
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.
*Listings
thats the step i need help with
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); } }
thats my code
hmmm not sure ive only done really basic things i might know someone who can help
Wait, so you really just need to swap the values of 2 variables right?
okay,thanks anyway
yes
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.
yes, but then it says
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.
no
So you just need to transfer the swapping into the main() instead of swap() right?
im not sure, thats why i came for help haha, not really clear on what i need to do. sorry haha
what happens when you run it
@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?
Join our real-time social learning platform and learn together with your friends!