A TangoDancer has a name and may have a partner who is also a TangoDancer. If TangoDancer ‘A’ chooses TangoDancer ‘B’ to be a partner, the operation succeeds if neither ‘A’ nor ‘B’ already has a partner, and A is not B. Write the choosePartner method and additional methods as desired so that the following interactions work:
> TangoDancer rita = new TangoDancer(“senorita rita”);
> TangoDancer tony = new TangoDancer(“mr tony”);
> rita.getName()
“senorita rita”
> rita.hasAPartner()
false
> tony.hasAPartner()
false
> rita.choosePartner(tony)
true
> rita.hasAPartner()
true
> rita.getPartner().getName()
“mr tony”
> tony.hasAPartner()
true
> tony.getPartner().getName()
“senorita rita”
> tony.choosePartner(rita)
false
> TangoDancer elana = new TangoDancer(“ms elana”);
> elana.choosePartner(elana)
false
/* TangoDancer Class */
public class TangoDancer{
private TangoDancer partner;
public TangoDancer(String name){this.name=name; partner = null; }
public String getName(){ return name; }
public TangoDancer getPartner(){ return partner; }
public boolean hasAPartner(){ return partner != null; }
public ? choosePartner(?){………}
}
TangoDancer.java
/** * TangoDancer Class */ public class TangoDancer { private String name; private TangoDancer partner; public TangoDancer(String name){ this.name = name; partner = null; } public String getName(){ return name; } public TangoDancer getPartner(){ return partner; } public boolean hasAPartner(){ return partner != null; } public boolean choosePartner(TangoDancer name){ if (name == this){ return false; } if (partner != null){ return false; } if (name.hasAPartner()){ return false; } partner = name; name.setPartner(this); return true; } public void setPartner(TangoDancer partner){ this.partner = partner; } public static void main(String[] args) { TangoDancer rita = new TangoDancer("senorita rita"); TangoDancer tony = new TangoDancer("mr tony"); System.out.println(rita.getName()); System.out.println(rita.hasAPartner()); System.out.println(tony.hasAPartner()); System.out.println(rita.choosePartner(tony)); System.out.println(rita.hasAPartner()); System.out.println(rita.getPartner().getName()); System.out.println(tony.hasAPartner()); System.out.println(tony.getPartner().getName()); System.out.println(tony.choosePartner(rita)); TangoDancer elana = new TangoDancer("ms elana"); System.out.println(elana.choosePartner(elana)); } }
Yorum yapabilmek için giriş yapmalısınız.