Java - method PairUtil.swap? Implement a static generic method PairUtil.swap who
ID: 3646684 • Letter: J
Question
Java - method PairUtil.swap? Implement a static generic method PairUtil.swap whose parameter is a Pair object, using the generic class declared in Section 17.2. The method should return a new pair, with the first and second element swapped. The Pair class from 17.2 looks like: public class Pair { private T first; private S second; /** * Constructs a pair containing two given elements * @param firstElement the first element * @param secondElement the second element */ public Pair(T firstElement, S secondElement) { first = firstElement; second = secondElement; } /** Gets the first element of this pair. * @return the first element */ public T getFirst() { return first; } /** * Gets the second element of this pair. * @return the second element */ public S getSecond() { return second; } public String toString() { return "(" + first + ", " + second + ")"; } } And I know the driver class is supposed to look like this: public class PairDemo { public static void main(String[] args) { Pair p = new Pair(Explanation / Answer
public class Cell implements $Parametric { static $TD $cl; static{ $cl=createTD(new $TD[]{ $TDManager.register(String.class)}); } public static $TD createTD($TD[] params) { $TD t=$TDManager.register(Cell.class,params); if (t.checkNew()){ t.father=$TD.objTD; t.friends=$TD.voidTDs; } return t; } public $TD getTD() { return $td;} private $TD $td; private Object t; Cell($TD $td,Object t){ this.$td=$td; this.t=t; } void set(Object t){ this.t=t;} Object get(){ return t;} void setIfPossible(Object o){ if ($td.params[0].isInstance(o)) t=o; } Cell getCString(){ return new Cell($cl,""+t); } } public class Client { static $TD[] $t=new $TD[4]; static{ $t[0]=$TDManager.register(Integer.class); //Integer $t[1]=Cell.createTD(new $TD[]{$t[0]}); //Cell $t[2]=$TDManager.register(String.class); //String $t[3]=Pair.createTD(new $TD[]{$t[0],$t[2]});//Pair } public static void main(java.lang.String[] args) { Cell c1=new Cell($t[1],null); // new Cell(null); c1.set(new Integer(1)); Integer I=(Integer)c1.get(); c1.setIfPossible("2"); Cell c2=c1.getCString(); Pair p=new Pair($t[3],I,c2.get()); //new Pair(I,c2,get()); Object o=p.getReverse(); c1=$t[1].isInstance(o)?(Cell)o:null; // o instanceof Cell?... } } public class Pair extends Cell { public static $TD createTD($TD[] params) { $TD t=$TDManager.register(Pair.class,params); if (t.checkNew()){ t.father=Cell.createTD(new $TD[]{params[0]}); t.friends=new $TD[1]; t.friends[0]=Pair.createTD( new $TD[]{params[1],params[0]}); } return t; } public $TD getTD() {return $td;} private $TD $td; Object s; public Pair($TD $td,Object r,Object s) { super($td.father,r); this.$td=$td; this.s=s; } public void setSecond(Object s) { this.s=s; } public Object getSecond() { return s;} public Pair getReverse() { return new Pair($td.friends[0],s,get()); } }Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.