You are on page 1of 5

Implementing Iterator import java.util.

*; public interface Queue{ public boolean add(Object value); public Object remove(); }

class LinkedQueue implements Queue{ private Node head; private Node tail; private int numberOfElements; private class Node{ private Object data; private Node next; private Node(Object object, Node next) { this.data=object; this.next=next; } public Object getData() { return data; } public void setNext(Node next) { this.next=next; } public Node getNext() { return next; } } public boolean add(Object value){ Node node=new Node(value,null); if (tail==null){ tail=head=node; } else{ tail.setNext(node); tail=node; } numberOfElements++; return true; } public Object remove() { if(head==null)

add(new Integer(2)). cursor=cursor.add(new Integer(1)).iterator().getData(). } Object object=cursor.iterator1. return value.return null. return object. } private class QueueIterator implements Iterator { private Node cursor.getNext(). } public void remove() { } } public static void main(String args[]) { Collection collection=new LinkedList(). } public boolean hasNext() { return cursor!=null. collection. Object value=head.hasNext().getData(). } public Object next() { if(!hasNext()) { return null. } numberOfElements--.getNext(). } public Iterator iterator() { return new QueueIterator(). public QueueIterator() { cursor=head. for(Iterator iterator1=collection. collection. if(head==null) { tail=null. head=head.) { .

} } System.out.) { Integer int2=(Integer)iterator2.iterator().hasNext().iterator2.println(count). for(Iterator iterator2=collection.next().out.equals(int2)) { count++. int count=0. if(int1. System.next(). } } } .println(int1).Integer int1=(Integer)iterator1.

public void moveBy (int dx.x = 0. p2.} . System.show () . p1 = SinglePoint. System.print ("Points are ") .print (" and ") . y = 0.} public int y () {return y.out.in.out.out. private SinglePoint () {System.} public void show () { System. p2.show () . p2 = SinglePoint. System. } } } . try { System. static int z=10 . class SinglePoint { int x.println(z)."). } } class Root { public static void main (String [] args) { SinglePoint p1. p1.getInstance () .IOException.read().out.getInstance () . } public int x () {return x. public static SinglePoint getInstance () {System. int dy) { x = x + dx .println("Hello"). y.Implementing Singleton pattern import java. } catch (IOException e) { return.io.println('\n' + "Press <Enter>.return instance.out. y = y + dy .moveBy (7. p1.out. 8) .print ("(" + x + ".} private static SinglePoint instance = new SinglePoint () . " + y + ")") .

getWidth() . SquarePegAdapter spa. } } } . System.getWidth() + " by " + ((amount < 0) ? 0 : amount) + " amount" ). spa. } } class RoundHole { private int radius.out. public RoundHole( int r ) { radius = r.out.setWidth( sp.out. i++) { spa = new SquarePegAdapter( (double) i ). for (int i=6.println( "reducing SquarePeg " + sp.println( " width is now " + sp. } public int getRadius() { return radius. public SquarePegAdapter( double w ) { sp = new SquarePeg( w ).amount ). i < 10. } public void makeFit( RoundHole rh ) { double amount = sp.Implementing Adapter class SquarePeg { private double width.sqrt(2) ).sqrt(2). } } } class AdapterDemoSquarePeg { public static void main( String[] args ) { RoundHole rh = new RoundHole( 5 ). } } class SquarePegAdapter { private SquarePeg sp.getWidth() . public SquarePeg( double w ) { width = w. System.rh. System.makeFit( rh ). if (amount > 0) { sp.getRadius() * Math.getWidth() ). } public double getWidth() { return width.println( "RoundHole: max SquarePeg is " + r * Math. } public void setWidth( double w ) { width = w.