Question

java help!im a littleconfused with this swap. Could someone please explain the steps. possibly with a...

java help!im a littleconfused with this swap. Could someone please explain the steps. possibly with a memory diagram? thanks!!

public class tricky {

  
   public static void tricky1(Point arg1, Point arg2) {
    arg1.x = 100;
    arg1.y = 100;
    Point temp = arg1;
    arg1 = arg2;
    arg2 = temp;
}

public static void main(String [] args) {
    Point pnt1 = new Point(0,0);
    Point pnt2 = new Point(0,0);
    System.out.println("X: " + pnt1.x + " Y: " +pnt1.y);
    System.out.println("X: " + pnt2.x + " Y: " +pnt2.y);
    System.out.println(" "); tricky1(pnt1,pnt2);
    System.out.println("X: " + pnt1.x + " Y:" + pnt1.y);
    System.out.println("X: " + pnt2.x + " Y: " +pnt2.y);
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

//You didn't specified the Point class. Probably, it is like reading 2 variables like x-axis and y-axis.
public class tricky {
  
public static void tricky1(Point arg1, Point arg2) //Input two points arg1, and arg2, and then updates the point arg1 to (100,100).
//Then it will exchange the values of arg1 and arg2.
//This means arg2 is the point (100,100), and arg1 will be the second point arg2.
{
arg1.x = 100;   //arg1.x is assigned with 100.
arg1.y = 100; //arg1.y is assigned with 100.
Point temp = arg1; //Values of arg1 are copied to temp.
arg1 = arg2; //Copies the values of arg2 to arg1. Don't know what the values of arg2 as of now.
arg2 = temp; //Copies the values of temp to arg2.
}
public static void main(String [] args) {
Point pnt1 = new Point(0,0); //pnt1 is a point at origin. (0,0).
Point pnt2 = new Point(0,0); //pnt2 is a point at origin. (0,0).
System.out.println("X: " + pnt1.x + " Y: " +pnt1.y);
//Displays a message: "X: 0 Y: 0"
System.out.println("X: " + pnt2.x + " Y: " +pnt2.y);
//Displays a message: "X: 0 Y: 0"
System.out.println(" "); //Prints a space.
//Now applies the function tricky as described in the function.
tricky1(pnt1,pnt2);
//After the application, now, pnt1 is a point at (0,0), and pnt2 is a point at (100,100).
System.out.println("X: " + pnt1.x + " Y:" + pnt1.y);
//Displays a message: "X: 100 Y: 100"
System.out.println("X: " + pnt2.x + " Y: " +pnt2.y);
//Displays a message: "X: 0 Y: 0"
}
}

Add a comment
Know the answer?
Add Answer to:
java help!im a littleconfused with this swap. Could someone please explain the steps. possibly with a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I do not understand why the swap methods are not doing anything for the variable x...

    I do not understand why the swap methods are not doing anything for the variable x and y in the main method. When are variables affected by the other methods and when they do not get modified by the other methods? (The answer is A). Thank you so much in advance, 24. What does the following code print? public class Swapper [ public static void main (String[] args) I int [ ] x = int [ ] y = {4,5,6,7,8};...

  • Java will be printed 10. can you explain step by step why? public class WhatsPrinted2 {...

    Java will be printed 10. can you explain step by step why? public class WhatsPrinted2 { public static void whatHappens(int A[]) { int []B = new int[A.length]; for (int i=0; i<A.length; i++) { B[i]=A[i]*2; } A=B; } public static void main(String args[]) { int A[] = {10,20,30}; whatHappens(A); System.out.println(A[0]); } } will print 10. explain how it's works. Thanks public class WhatsPrinted3 { public static int[] whatHappens(int A[]) { int []B = new int[A.length]; for (int i=0; i<A.length; i++) {...

  • What display is produced by the execution of this JAVA program if we make the following...

    What display is produced by the execution of this JAVA program if we make the following call f(0,0). Please explain your answer public class function {    static void f(int x){        System.out.println(1);    }    static void f(double x){        System.out.println(2);    }    static void f(char x){        System.out.println(3);    }    static void f(long x){        System.out.println(4);    }    public static void main(String[] args) {       } }

  • easy question but i am confused. someone help Consider the following Java classes: class OuterClass {...

    easy question but i am confused. someone help Consider the following Java classes: class OuterClass { static class InnerClass { public void inner Method({ System.out.println("This is my inner class"); public static void outer Method{ System.out.println("This is my outer class"); public class OuterClass Test{ public static void main(String args[]) { Complete the class OuterClass Test to produce as output the two strings in class OuterClass. What are the outputs of your test class?

  • I need help with my IM (instant messaging) java program, I created the Server, Client, and...

    I need help with my IM (instant messaging) java program, I created the Server, Client, and Message class. I somehow can't get both the server and client to message to each other. I am at a roadblock. Here is the question below. Create an IM (instant messaging) java program so that client and server communicate via serialized objects (e.g. of type Message). Each Message object encapsulates the name of the sender and the response typed by the sender. You may...

  • In JAVA Thank You What is the exact output produced by running the method test? /**...

    In JAVA Thank You What is the exact output produced by running the method test? /** * TestSwap.java * Demonstrates parameter passing involving arrays and integers, * scope of variables, flow of control, and overloaded methods. */ public class TestSwap { public void swap (int x, int y) { int temp; temp = x; x = y; y = temp; System.out.println("Inside swap version 1:"); System.out.println("x = " + x); System.out.println("y = " + y); } public void swap (int[] a,...

  • 12. Predict the output generated at the marked println lines in the following program. The program...

    12. Predict the output generated at the marked println lines in the following program. The program makes use of the class Employee that is also given. Please enter your answers in the space provided below the code. public class Employee { private String name; private double salary; public Employee(String name, double salary) { this.name = name; this.salary = salary; } public String getName() { return name; } public double getSalary() { return salary; } public void raiseSalary(double percent) { double...

  • please evaluate the following code. this is JAVA a. class Car { public int i =...

    please evaluate the following code. this is JAVA a. class Car { public int i = 3; public Car(int i) { this.i = i; } } ... Car x = new Car(7), y = new Car(5); x = y; y.i = 9; System.out.println(x.i); b. class Driver { public static void main(String[] args) { int[] x = {5, 2, 3, 6, 5}; int n = x.length; for (int j = n-2; j > 0; j--) x[j] = x[j-1]; for (int j...

  • Java i know that result will be 415. Can you explain how it works. step by...

    Java i know that result will be 415. Can you explain how it works. step by step please. thanks public static void whatsPrinted(int A[]) { for (int i=1; i<A.length; i++) { A[i]=A[i-1]*2+1; } } public static void main(String args[]) { int A[] = {12,3,8,9,7,11}; whatsPrinted(A); System.out.println(A[A.length-1]); }

  • help me with a question in java: class A { int i = 10; } class...

    help me with a question in java: class A { int i = 10; } class B extends A { int i = 20; } public class Boo { public static void main(String[] args) { A a = new B(); System.out.println(a.i); } }

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT