jeudi 8 janvier 2015

How do I test a method in java with objects I created? (Helpful title suggestions are welcome.)

I am going through a class to learn about JAVA...


I was told to add a method (void swapNames(Greeter other)) that swaps the names of this greeter and another. Then to create two objects in the Greeter class and use the swapNames method to swap their names.


I started with ..



public class Greeter {

public Greeter(String aName){
name = aName;
}


public String sayHello(){
return "Hello, " + name + "!";
}

private static String name;

public static void swapNames(Greeter other){

String aux = name;
name = other.getName();
other.setName(aux);
}

private void setName(String aux) {
Greeter.name = aux;
}

private String getName() {
return name;
}

}


But I am unsure how to test my code.


I have a tester class with a main method, and I have created two object instances of Greeter..



Greeter nameGreeter = new Greeter("John Smith");
Greeter nameGreeter = new Greeter("Jane Doe");


I'm not sure where to go with this though.


Aucun commentaire:

Enregistrer un commentaire