Create a gist now

Instantly share code, notes, and snippets.

package examples.git;
public class Circle {
private int x;
private int y;
public Circle (int x, int y) {
this.x = x;
this.y = y;
}
public void printCoordinates() {
System.out.println("coordinates: x = " + this.x + " y = " + this.y);
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
}
package examples.git;
public class CircleMover {
//the reference to the object circle is passed by value and cannot be changed!!
/* moves the circle to x + deltaX, y + deltaY */
public void moveCircle(Circle circle, int deltaX, int deltaY) {
circle.setX(circle.getX() + deltaX);
circle.setY(circle.getY() + deltaY);
//code that tries to assign a new reference to circle
circle = new Circle(0, 0);
circle = null;
}
}
package examples.git.tests;
import examples.git.*;
import junit.framework.TestCase;
public class ReferenceValueTest extends TestCase {
public void testModifiedString() {
String originalString = "first string";
int originalNumber = 10;
StringModifier test = new StringModifier(originalString, originalNumber);
String beforeMethodOne = test.string + " " + test.number;
System.out.println(beforeMethodOne);
StringModifier.methodOne(test.string, test.number);
String afterMethodOne = test.string + " " + test.number;
System.out.println(afterMethodOne);
assertEquals(afterMethodOne, beforeMethodOne);
}
public void testMoveCircle() {
int origin = 0;
Circle myCircle = new Circle(origin, origin);
int delta = 10;
myCircle.printCoordinates();
CircleMover circleMover = new CircleMover();
circleMover.moveCircle(myCircle, delta, delta);
myCircle.printCoordinates();
assertEquals(origin + delta, myCircle.getX());
assertEquals(origin + delta, myCircle.getY());
}
}
package examples.git;
public class StringModifier {
public String string;
public int number;
public StringModifier(String string, int number) {
this.string = string;
this.number = number;
}
public static void methodOne(String s, int number) {
s = "string modified by methodOne";
number = 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment