inital commit
This commit is contained in:
		
						commit
						d1948b0e58
					
				
					 67 changed files with 5280 additions and 0 deletions
				
			
		
							
								
								
									
										3
									
								
								Lab106-VenbergGE/manifest.mf
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										3
									
								
								Lab106-VenbergGE/manifest.mf
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,3 @@
 | 
			
		|||
Manifest-Version: 1.0
 | 
			
		||||
X-COMMENT: Main-Class will be added automatically by build
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										93
									
								
								Lab106-VenbergGE/src/Client.java
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										93
									
								
								Lab106-VenbergGE/src/Client.java
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,93 @@
 | 
			
		|||
/*
 | 
			
		||||
 * Copyright (C) 2021 Gabriel Venberg
 | 
			
		||||
 *
 | 
			
		||||
 * This program is free software: you can redistribute it and/or modify
 | 
			
		||||
 * it under the terms of the GNU General Public License as published by
 | 
			
		||||
 * the Free Software Foundation, either version 3 of the License, or
 | 
			
		||||
 * (at your option) any later version.
 | 
			
		||||
 *
 | 
			
		||||
 * This program is distributed in the hope that it will be useful,
 | 
			
		||||
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
			
		||||
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
			
		||||
 * GNU General Public License for more details.
 | 
			
		||||
 *
 | 
			
		||||
 * You should have received a copy of the GNU General Public License
 | 
			
		||||
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 *
 | 
			
		||||
 * @author Gabriel Venberg
 | 
			
		||||
 */
 | 
			
		||||
public class Client {
 | 
			
		||||
 | 
			
		||||
    private static boolean isPrime(int n) {
 | 
			
		||||
        for (int i = 2; i < n / 2; i++) {
 | 
			
		||||
            if (n % i == 0) {
 | 
			
		||||
                return false;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        return true;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public static void main(String[] args) {
 | 
			
		||||
 | 
			
		||||
        LuckyNumberList luckyNumbers = new LuckyNumberList();
 | 
			
		||||
 | 
			
		||||
        luckyNumbers.addLuckyNumber(new LuckyNumber("alice"));
 | 
			
		||||
        luckyNumbers.addLuckyNumber(new LuckyNumber("bob"));
 | 
			
		||||
        luckyNumbers.addLuckyNumber(new LuckyNumber("bill"));
 | 
			
		||||
        luckyNumbers.addLuckyNumber(new LuckyNumber("jill"));
 | 
			
		||||
        luckyNumbers.addLuckyNumber(new LuckyNumber("gabe"));
 | 
			
		||||
        luckyNumbers.addLuckyNumber(new LuckyNumber("erica"));
 | 
			
		||||
        luckyNumbers.addLuckyNumber(new LuckyNumber("albrich"));
 | 
			
		||||
        luckyNumbers.addLuckyNumber(new LuckyNumber("orianna"));
 | 
			
		||||
        luckyNumbers.addLuckyNumber(new LuckyNumber("maritt"));
 | 
			
		||||
        luckyNumbers.addLuckyNumber(new LuckyNumber("joe"));
 | 
			
		||||
 | 
			
		||||
        Iterator<Position<LuckyNumber>> iterator;
 | 
			
		||||
        String format = "%1$10s, %2$10d %3$10s %4$10s %n";
 | 
			
		||||
        
 | 
			
		||||
        iterator = luckyNumbers.positions().iterator();
 | 
			
		||||
        System.out.println("full iterator:");
 | 
			
		||||
        while (iterator.hasNext()) {
 | 
			
		||||
            LuckyNumber n = iterator.next().getElement();
 | 
			
		||||
            String even = "odd";
 | 
			
		||||
            String prime = "not prime";
 | 
			
		||||
            if (n.getLuckyNumber() % 2 == 0) {
 | 
			
		||||
                even = "even";
 | 
			
		||||
            } else if (isPrime(n.getLuckyNumber())) {
 | 
			
		||||
                prime = "prime";
 | 
			
		||||
            }
 | 
			
		||||
            System.out.printf(format, n.getName(), n.getLuckyNumber(), even, prime);
 | 
			
		||||
        }
 | 
			
		||||
        
 | 
			
		||||
        System.out.println("even iterator:");
 | 
			
		||||
        iterator = luckyNumbers.EvenPositions().iterator();
 | 
			
		||||
        while (iterator.hasNext()) {
 | 
			
		||||
            LuckyNumber n = iterator.next().getElement();
 | 
			
		||||
            String even = "odd";
 | 
			
		||||
            String prime = "not prime";
 | 
			
		||||
            if (n.getLuckyNumber() % 2 == 0) {
 | 
			
		||||
                even = "even";
 | 
			
		||||
            } else if (isPrime(n.getLuckyNumber())) {
 | 
			
		||||
                prime = "prime";
 | 
			
		||||
            }
 | 
			
		||||
            System.out.printf(format, n.getName(), n.getLuckyNumber(), even, prime);
 | 
			
		||||
        }
 | 
			
		||||
        
 | 
			
		||||
        System.out.println("prime iterator");
 | 
			
		||||
        iterator = luckyNumbers.PrimePositions().iterator();
 | 
			
		||||
        while (iterator.hasNext()) {
 | 
			
		||||
            LuckyNumber n = iterator.next().getElement();
 | 
			
		||||
            String even = "odd";
 | 
			
		||||
            String prime = "not prime";
 | 
			
		||||
            if (n.getLuckyNumber() % 2 == 0) {
 | 
			
		||||
                even = "even";
 | 
			
		||||
            } else if (isPrime(n.getLuckyNumber())) {
 | 
			
		||||
                prime = "prime";
 | 
			
		||||
            }
 | 
			
		||||
            System.out.printf(format, n.getName(), n.getLuckyNumber(), even, prime);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										10
									
								
								Lab106-VenbergGE/src/Iterable.java
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										10
									
								
								Lab106-VenbergGE/src/Iterable.java
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,10 @@
 | 
			
		|||
/**
 | 
			
		||||
 * Data Structures & Algorithms 6th Edition
 | 
			
		||||
 * Goodrick, Tamassia, Goldwasser
 | 
			
		||||
 * Section 7.4.1
 | 
			
		||||
 */
 | 
			
		||||
public interface Iterable<E> {
 | 
			
		||||
    
 | 
			
		||||
    Iterator<E> iterator( ); // Returns an iterator of the elements in the collection
 | 
			
		||||
   
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										20
									
								
								Lab106-VenbergGE/src/Iterator.java
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										20
									
								
								Lab106-VenbergGE/src/Iterator.java
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,20 @@
 | 
			
		|||
/**
 | 
			
		||||
 * Data Structures & Algorithms 6th Edition
 | 
			
		||||
 * Goodrick, Tamassia, Goldwasser
 | 
			
		||||
 * Section 7.4
 | 
			
		||||
 */
 | 
			
		||||
public interface Iterator<E> {
 | 
			
		||||
    
 | 
			
		||||
    boolean hasNext( ); // Returns true if there is at least one additional 
 | 
			
		||||
                        // element in the sequence, and false otherwise.
 | 
			
		||||
    
 | 
			
		||||
    E next( );          // Returns the next element in the sequence.
 | 
			
		||||
    
 | 
			
		||||
    void remove( ) throws IllegalStateException;     
 | 
			
		||||
                        // Removes from the collection the element returned by
 | 
			
		||||
                        // the most recent call to next( ).  Throws an 
 | 
			
		||||
                        // IllegalStateException if next has not yet been called,
 | 
			
		||||
                        // or if remove was already called since the most recent
 | 
			
		||||
                        // call to next.
 | 
			
		||||
    
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										224
									
								
								Lab106-VenbergGE/src/LinkedPositionalList.java
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										224
									
								
								Lab106-VenbergGE/src/LinkedPositionalList.java
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,224 @@
 | 
			
		|||
 | 
			
		||||
import java.util.NoSuchElementException;
 | 
			
		||||
 | 
			
		||||
/*
 | 
			
		||||
 * Data Structures & Algorithms 6th Edition 
 | 
			
		||||
 * Goodrich, Tamassia, Goldwasser
 | 
			
		||||
 * Code Fragments 7.9-12 and 7.14
 | 
			
		||||
 * 
 | 
			
		||||
 * An implementation of the linkedpositionallist class
 | 
			
		||||
 */
 | 
			
		||||
/**
 | 
			
		||||
 *
 | 
			
		||||
 * @author Gabriel Venberg
 | 
			
		||||
 */
 | 
			
		||||
public class LinkedPositionalList<E> implements PositionalList<E>{
 | 
			
		||||
    
 | 
			
		||||
    //nested node class
 | 
			
		||||
    private static class Node<E> implements Position<E>{
 | 
			
		||||
        //refrence to the element stored at this node
 | 
			
		||||
        private E element;
 | 
			
		||||
        //refrence to the previous node on the list
 | 
			
		||||
        private Node<E> prev;
 | 
			
		||||
        //refrence to the next node on the list
 | 
			
		||||
        private Node<E> next;
 | 
			
		||||
        public Node (E e, Node<E> p, Node<E> n){
 | 
			
		||||
            element = e;
 | 
			
		||||
            prev = p;
 | 
			
		||||
            next = n;
 | 
			
		||||
        }
 | 
			
		||||
        
 | 
			
		||||
        public E getElement() throws IllegalStateException{
 | 
			
		||||
            //convention for defunct node
 | 
			
		||||
            if(next==null){throw new IllegalStateException("position no longer valid");}
 | 
			
		||||
            return element;
 | 
			
		||||
        }
 | 
			
		||||
        
 | 
			
		||||
        public Node<E> getPrev(){
 | 
			
		||||
            return prev;
 | 
			
		||||
        }
 | 
			
		||||
        
 | 
			
		||||
        public Node<E> getNext(){
 | 
			
		||||
            return next;
 | 
			
		||||
        }
 | 
			
		||||
        
 | 
			
		||||
        public void setElement(E e){
 | 
			
		||||
            element = e;
 | 
			
		||||
        }
 | 
			
		||||
        
 | 
			
		||||
        public void setPrev(Node<E> p){
 | 
			
		||||
            prev=p;
 | 
			
		||||
        }
 | 
			
		||||
        
 | 
			
		||||
        public void setNext(Node<E> n){
 | 
			
		||||
            next=n;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    //end of nested node class.
 | 
			
		||||
    
 | 
			
		||||
    //nested positoniterator class
 | 
			
		||||
    private class PositionIterator implements Iterator<Position<E>>{
 | 
			
		||||
        //position of next element to report
 | 
			
		||||
        private Position<E> cursor = first();
 | 
			
		||||
        
 | 
			
		||||
        //position of last reported element
 | 
			
		||||
        private Position<E> recent = null;
 | 
			
		||||
        
 | 
			
		||||
        /**tests whether the iterator has a next object*/
 | 
			
		||||
        public boolean hasNext(){return (cursor!=null);}
 | 
			
		||||
        
 | 
			
		||||
        /**returns the next position in the iterator*/
 | 
			
		||||
        public Position<E> next()throws NoSuchElementException{
 | 
			
		||||
            if(cursor==null){throw new NoSuchElementException("nothing left");}
 | 
			
		||||
            //element at this position might later be removed
 | 
			
		||||
            recent = cursor;
 | 
			
		||||
            cursor = after(cursor);
 | 
			
		||||
            return recent;
 | 
			
		||||
        }
 | 
			
		||||
        
 | 
			
		||||
        /**removes the element removed by most recent call to next*/
 | 
			
		||||
        public void remove()throws IllegalStateException{
 | 
			
		||||
            if(recent==null){throw new IllegalStateException("nothing to remove");}
 | 
			
		||||
            //remove from outer list
 | 
			
		||||
            LinkedPositionalList.this.remove(recent);
 | 
			
		||||
            //do not remove again untill next is called
 | 
			
		||||
            recent = null;
 | 
			
		||||
        }
 | 
			
		||||
        //end of nested positionIterator class.
 | 
			
		||||
        
 | 
			
		||||
        //nested positioniterable class
 | 
			
		||||
        private class PositionIterable implements Iterable<Position<E>>{
 | 
			
		||||
            public Iterator<Position<E>> iterator(){return new PositionIterator();}
 | 
			
		||||
        }
 | 
			
		||||
        //end of positioniterable class
 | 
			
		||||
        
 | 
			
		||||
        //retursn an iterable representation of the lists positions*/
 | 
			
		||||
        public Iterable<Position<E>> positions(){
 | 
			
		||||
            return new PositionIterable();
 | 
			
		||||
        }
 | 
			
		||||
        
 | 
			
		||||
        //nested elementIterator class
 | 
			
		||||
        /*this class adapts the iteration produced by positions to return elements*/
 | 
			
		||||
        private class ElementIterator implements Iterator<E>{
 | 
			
		||||
            Iterator<Position<E>> posIterator = new PositionIterator();
 | 
			
		||||
            public boolean hasNext(){return posIterator.hasNext();}
 | 
			
		||||
            //return element
 | 
			
		||||
            public E next(){return posIterator.next().getElement();}
 | 
			
		||||
            public void remove(){posIterator.remove();}
 | 
			
		||||
        }
 | 
			
		||||
        
 | 
			
		||||
        public Iterator<E> iterator(){return new ElementIterator();}
 | 
			
		||||
    }
 | 
			
		||||
        
 | 
			
		||||
    //instance variables of LinkedPositonalList
 | 
			
		||||
    //header sentinel
 | 
			
		||||
    private Node<E> header;
 | 
			
		||||
    //trailer sentinel
 | 
			
		||||
    private Node<E> trailer;
 | 
			
		||||
    //number of elements in list
 | 
			
		||||
    private int size=0;
 | 
			
		||||
    
 | 
			
		||||
    /**constructs a new empty list*/
 | 
			
		||||
    public LinkedPositionalList(){
 | 
			
		||||
        header = new Node<>(null, null, null);
 | 
			
		||||
        trailer = new Node<>(null, header, null);
 | 
			
		||||
        header.setNext(trailer);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    //private utilities
 | 
			
		||||
    /**validates the position and returns it as a node*/
 | 
			
		||||
    private Node<E> validate(Position<E> p) throws IllegalArgumentException{
 | 
			
		||||
        if(!(p instanceof Node)) throw new IllegalArgumentException("invalid p");
 | 
			
		||||
        //safe cast
 | 
			
		||||
        Node<E> node = (Node<E>) p;
 | 
			
		||||
        //convention for defunct node
 | 
			
		||||
        if(node.getNext()==null){throw new IllegalArgumentException("p is no longer in the list");}
 | 
			
		||||
        return node;
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    /**returns the given node as a position (or null, if it is a sentinel)*/
 | 
			
		||||
    private Position<E> position(Node<E> node){
 | 
			
		||||
        //dont expose user to sentinels
 | 
			
		||||
        if(node==header||node==trailer){
 | 
			
		||||
            return null;
 | 
			
		||||
        }
 | 
			
		||||
        return node;
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    //nested position
 | 
			
		||||
    //public accessor methods
 | 
			
		||||
    /**returns the number of elements in the linked list*/
 | 
			
		||||
    public int size(){return size;}
 | 
			
		||||
    
 | 
			
		||||
    /** tests whether the linked list is empty*/
 | 
			
		||||
    public boolean isEmpty(){return size==0;}
 | 
			
		||||
    
 | 
			
		||||
    /**returns the first postion in the linked list (or null, if empty)*/
 | 
			
		||||
    public Position<E> first(){return position(header.getNext());}
 | 
			
		||||
    
 | 
			
		||||
    /**returns the last postion in the linked list (or null, if empty)*/
 | 
			
		||||
    public Position<E> last(){return position(header.getNext());}
 | 
			
		||||
    
 | 
			
		||||
    /**returns the postion immediately before position p (or null, if p is first*/
 | 
			
		||||
    public Position<E> before(Position<E> p) throws IllegalArgumentException{
 | 
			
		||||
        Node<E> node = validate(p);
 | 
			
		||||
        return position(node.getPrev());
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    public Position<E> after(Position<E> p) throws IllegalArgumentException{
 | 
			
		||||
        Node<E> node = validate(p);
 | 
			
		||||
        return position(node.getNext());
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    //private utilites
 | 
			
		||||
    private Position<E> addBetween(E e, Node<E> pred, Node<E> succ){
 | 
			
		||||
        //create and link a new node
 | 
			
		||||
        Node<E> newest = new Node<>(e, pred, succ);
 | 
			
		||||
        pred.setNext(newest);
 | 
			
		||||
        succ.setPrev(newest);
 | 
			
		||||
        size++;
 | 
			
		||||
        return newest;
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    //public update methods
 | 
			
		||||
    /**inserts element e at the front of the linked list and returns its new position*/
 | 
			
		||||
    public Position<E> addFirst(E e){return addBetween(e, header, header.getNext());}
 | 
			
		||||
    
 | 
			
		||||
    /**inserts element e at the back fo the linked list and returns its new postiton*/
 | 
			
		||||
    public Position<E> addLast(E e){return addBetween(e, trailer.getPrev(), trailer);}
 | 
			
		||||
    
 | 
			
		||||
    /**inserts element e immediately before position p, and returns its new position*/
 | 
			
		||||
    public Position<E> addBefore(Position<E> p, E e) throws IllegalArgumentException{
 | 
			
		||||
        Node<E> node = validate(p);
 | 
			
		||||
        return addBetween(e, node.getPrev(), node);
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    /**inserts element e immediately after position p, and returns its new position*/
 | 
			
		||||
    public Position<E> addAfter(Position<E> p, E e){
 | 
			
		||||
        Node<E> node = validate(p);
 | 
			
		||||
        return addBetween(e, node, node.getNext());
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    /**replaces the element stored at position p and returns the old element*/
 | 
			
		||||
    public E set(Position<E> p, E e) throws IllegalArgumentException{
 | 
			
		||||
        Node<E> node = validate(p);
 | 
			
		||||
        E answer = node.getElement();
 | 
			
		||||
        node.setElement(e);
 | 
			
		||||
        return answer;
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    /**removes the element stored at Position p and returns it (invalidating p)*/
 | 
			
		||||
    public E remove(Position<E> p) throws IllegalArgumentException{
 | 
			
		||||
        Node<E> node = validate(p);
 | 
			
		||||
        Node<E> predecessor = node.getPrev();
 | 
			
		||||
        Node<E> succsessor = node.getNext();
 | 
			
		||||
        predecessor.setNext(succsessor);
 | 
			
		||||
        succsessor.setPrev(predecessor);
 | 
			
		||||
        size--;
 | 
			
		||||
        E answer = node.getElement();
 | 
			
		||||
        node.setElement(null);
 | 
			
		||||
        node.setNext(null);
 | 
			
		||||
        node.setPrev(null);
 | 
			
		||||
        return answer;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										48
									
								
								Lab106-VenbergGE/src/LuckyNumber.java
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										48
									
								
								Lab106-VenbergGE/src/LuckyNumber.java
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,48 @@
 | 
			
		|||
/*
 | 
			
		||||
 * Copyright (C) 2021 Gabriel Venberg
 | 
			
		||||
 *
 | 
			
		||||
 * This program is free software: you can redistribute it and/or modify
 | 
			
		||||
 * it under the terms of the GNU General Public License as published by
 | 
			
		||||
 * the Free Software Foundation, either version 3 of the License, or
 | 
			
		||||
 * (at your option) any later version.
 | 
			
		||||
 *
 | 
			
		||||
 * This program is distributed in the hope that it will be useful,
 | 
			
		||||
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
			
		||||
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
			
		||||
 * GNU General Public License for more details.
 | 
			
		||||
 *
 | 
			
		||||
 * You should have received a copy of the GNU General Public License
 | 
			
		||||
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 *
 | 
			
		||||
 * @author Gabriel Venberg
 | 
			
		||||
 */
 | 
			
		||||
import java.util.Random;
 | 
			
		||||
public class LuckyNumber {
 | 
			
		||||
    private String name;
 | 
			
		||||
    private int luckyNumber;
 | 
			
		||||
    
 | 
			
		||||
    Random rand = new Random();
 | 
			
		||||
    
 | 
			
		||||
    public LuckyNumber(String name){
 | 
			
		||||
        this.name = name;
 | 
			
		||||
        luckyNumber = rand.nextInt(10);
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    public String getName(){return name;}
 | 
			
		||||
    
 | 
			
		||||
    public int getLuckyNumber(){return luckyNumber;}
 | 
			
		||||
    
 | 
			
		||||
    public boolean equals(Object o){
 | 
			
		||||
        if (!(o instanceof LuckyNumber)){return false;}
 | 
			
		||||
        LuckyNumber l = (LuckyNumber) o;
 | 
			
		||||
        return name.equals(l.name)
 | 
			
		||||
                && luckyNumber == l.luckyNumber;
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    public String toString(){
 | 
			
		||||
        return getClass().getName() + ':'+name+';'+luckyNumber;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										143
									
								
								Lab106-VenbergGE/src/LuckyNumberList.java
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										143
									
								
								Lab106-VenbergGE/src/LuckyNumberList.java
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,143 @@
 | 
			
		|||
/*
 | 
			
		||||
 * Copyright (C) 2021 Gabriel Venberg
 | 
			
		||||
 *
 | 
			
		||||
 * This program is free software: you can redistribute it and/or modify
 | 
			
		||||
 * it under the terms of the GNU General Public License as published by
 | 
			
		||||
 * the Free Software Foundation, either version 3 of the License, or
 | 
			
		||||
 * (at your option) any later version.
 | 
			
		||||
 *
 | 
			
		||||
 * This program is distributed in the hope that it will be useful,
 | 
			
		||||
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
			
		||||
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
			
		||||
 * GNU General Public License for more details.
 | 
			
		||||
 *
 | 
			
		||||
 * You should have received a copy of the GNU General Public License
 | 
			
		||||
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
import java.util.NoSuchElementException;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 *
 | 
			
		||||
 * @author Gabriel Venberg
 | 
			
		||||
 */
 | 
			
		||||
public class LuckyNumberList {
 | 
			
		||||
    private LinkedPositionalList<LuckyNumber> luckyNumbers;
 | 
			
		||||
    
 | 
			
		||||
    public LuckyNumberList(){
 | 
			
		||||
        luckyNumbers = new LinkedPositionalList<LuckyNumber>();
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    public void addLuckyNumber(LuckyNumber l){
 | 
			
		||||
        luckyNumbers.addLast(l);
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    private class positionIterator implements Iterator<Position<LuckyNumber>>{
 | 
			
		||||
        private Position<LuckyNumber> cursor = luckyNumbers.first();
 | 
			
		||||
        private Position<LuckyNumber> recent = null;
 | 
			
		||||
        
 | 
			
		||||
        public boolean hasNext(){return(cursor!=null);}
 | 
			
		||||
        
 | 
			
		||||
        public Position<LuckyNumber> next() throws NoSuchElementException {
 | 
			
		||||
            if(cursor==null){throw new NoSuchElementException("nothing left");}
 | 
			
		||||
            recent = cursor;
 | 
			
		||||
            cursor = luckyNumbers.after(cursor);
 | 
			
		||||
            return recent;
 | 
			
		||||
        }
 | 
			
		||||
        public void remove() throws IllegalStateException{
 | 
			
		||||
            if(recent==null){throw new IllegalStateException("nothing to remove");}
 | 
			
		||||
            luckyNumbers.remove(recent);
 | 
			
		||||
            recent=null;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    private class positionIterable implements Iterable<Position<LuckyNumber>>{
 | 
			
		||||
        public Iterator<Position<LuckyNumber>> iterator(){return new positionIterator();}
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    public Iterable<Position<LuckyNumber>> positions(){
 | 
			
		||||
        return new positionIterable();
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    private class EvenPositionIterator implements Iterator<Position<LuckyNumber>>{
 | 
			
		||||
        private Position<LuckyNumber> cursor = luckyNumbers.first();
 | 
			
		||||
        private Position<LuckyNumber> recent = null;
 | 
			
		||||
        
 | 
			
		||||
        public boolean hasNext(){return(cursor!=null);}
 | 
			
		||||
        
 | 
			
		||||
        public Position<LuckyNumber> next() throws NoSuchElementException {
 | 
			
		||||
            if(cursor==null){throw new NoSuchElementException("nothing left");}
 | 
			
		||||
            if(recent==null){
 | 
			
		||||
                while((cursor != null) && !(cursor.getElement().getLuckyNumber()%2==0)){
 | 
			
		||||
                    cursor = luckyNumbers.after(cursor);
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
            recent = cursor;
 | 
			
		||||
            cursor = luckyNumbers.after(cursor);
 | 
			
		||||
            //advance cursor to next even number
 | 
			
		||||
            while((cursor != null) && !(cursor.getElement().getLuckyNumber()%2==0)){
 | 
			
		||||
                cursor = luckyNumbers.after(cursor);
 | 
			
		||||
            }
 | 
			
		||||
         return recent;
 | 
			
		||||
        }
 | 
			
		||||
        public void remove() throws IllegalStateException{
 | 
			
		||||
            if(recent==null){throw new IllegalStateException("nothing to remove");}
 | 
			
		||||
            luckyNumbers.remove(recent);
 | 
			
		||||
            recent=null;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    private class EvenPositionIterable implements Iterable<Position<LuckyNumber>>{
 | 
			
		||||
        public Iterator<Position<LuckyNumber>> iterator(){return new EvenPositionIterator();}
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    public Iterable<Position<LuckyNumber>> EvenPositions(){
 | 
			
		||||
        return new EvenPositionIterable();
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    //utility class
 | 
			
		||||
    private boolean isPrime(int n){
 | 
			
		||||
        for(int i=2; i<n/2;i++){
 | 
			
		||||
            if(n%i==0){return false;}
 | 
			
		||||
        }
 | 
			
		||||
        return true;
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    private class PrimePositionIterator implements Iterator<Position<LuckyNumber>>{
 | 
			
		||||
        private Position<LuckyNumber> cursor = luckyNumbers.first();
 | 
			
		||||
        private Position<LuckyNumber> recent = null;
 | 
			
		||||
        
 | 
			
		||||
        public boolean hasNext(){return(cursor!=null);}
 | 
			
		||||
        
 | 
			
		||||
        public Position<LuckyNumber> next() throws NoSuchElementException {
 | 
			
		||||
            if(cursor==null){throw new NoSuchElementException("nothing left");}
 | 
			
		||||
            if(recent==null){
 | 
			
		||||
                while(cursor != null && !isPrime(cursor.getElement().getLuckyNumber())){
 | 
			
		||||
                    cursor = luckyNumbers.after(cursor);
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
            recent = cursor;
 | 
			
		||||
            cursor = luckyNumbers.after(cursor);
 | 
			
		||||
            //advance cursor to next even number
 | 
			
		||||
            while(cursor != null && !isPrime(cursor.getElement().getLuckyNumber())){
 | 
			
		||||
                cursor = luckyNumbers.after(cursor);
 | 
			
		||||
            }
 | 
			
		||||
         return recent;
 | 
			
		||||
        }
 | 
			
		||||
        public void remove() throws IllegalStateException{
 | 
			
		||||
            if(recent==null){throw new IllegalStateException("nothing to remove");}
 | 
			
		||||
            luckyNumbers.remove(recent);
 | 
			
		||||
            recent=null;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    private class PrimePositionIterable implements Iterable<Position<LuckyNumber>>{
 | 
			
		||||
        public Iterator<Position<LuckyNumber>> iterator(){return new PrimePositionIterator();}
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    public Iterable<Position<LuckyNumber>> PrimePositions(){
 | 
			
		||||
        return new PrimePositionIterable();
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										21
									
								
								Lab106-VenbergGE/src/Position.java
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										21
									
								
								Lab106-VenbergGE/src/Position.java
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,21 @@
 | 
			
		|||
/*
 | 
			
		||||
 * Data Structures & Algorithms 6th Edition 
 | 
			
		||||
 * Goodrich, Tamassia, Goldwasser
 | 
			
		||||
 * Code Fragment 7.7
 | 
			
		||||
 * 
 | 
			
		||||
 * An implementation of the position interface
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 *
 | 
			
		||||
 * @author Gabriel Venberg
 | 
			
		||||
 */
 | 
			
		||||
public interface Position<E> {
 | 
			
		||||
    /**
 | 
			
		||||
     * Returns the element stored at this position
 | 
			
		||||
     * 
 | 
			
		||||
     * @return the stored element
 | 
			
		||||
     * @throws IllegalStateException if position no longer valid.
 | 
			
		||||
     */
 | 
			
		||||
    E getElement() throws IllegalStateException;
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										50
									
								
								Lab106-VenbergGE/src/PositionalList.java
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										50
									
								
								Lab106-VenbergGE/src/PositionalList.java
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,50 @@
 | 
			
		|||
/*
 | 
			
		||||
 * Data Structures & Algorithms 6th Edition 
 | 
			
		||||
 * Goodrich, Tamassia, Goldwasser
 | 
			
		||||
 * Code Fragment 7.8
 | 
			
		||||
 * 
 | 
			
		||||
 * An implementation of the positionalList interface
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 *
 | 
			
		||||
 * @author Gabriel Venberg
 | 
			
		||||
 */
 | 
			
		||||
public interface PositionalList<E> {
 | 
			
		||||
    
 | 
			
		||||
    /** returns the number of elements in the list*/
 | 
			
		||||
    int size();
 | 
			
		||||
    
 | 
			
		||||
    /**tests whether the list is empty*/
 | 
			
		||||
    boolean isEmpty();
 | 
			
		||||
    
 | 
			
		||||
    /**returns the first position in the list (or null if empty)*/
 | 
			
		||||
    Position<E> first();
 | 
			
		||||
    
 | 
			
		||||
    /**returns the last position in the list (or null if empty)*/
 | 
			
		||||
    Position<E> last();
 | 
			
		||||
    
 | 
			
		||||
    /**returns the position immediately before position p (or null, if p is first)*/
 | 
			
		||||
    Position<E> before(Position<E> p) throws IllegalArgumentException;
 | 
			
		||||
    
 | 
			
		||||
    /**returns the position immediately after position p (or null, if p is last)*/
 | 
			
		||||
    Position<E> after(Position<E> p) throws IllegalArgumentException;
 | 
			
		||||
    
 | 
			
		||||
    /**inserts element e at the front of the list and returns its new position*/
 | 
			
		||||
    Position<E> addFirst(E e);
 | 
			
		||||
    
 | 
			
		||||
    /**inserts element e at the back of the list and returns its new position*/
 | 
			
		||||
    Position<E> addLast(E e);
 | 
			
		||||
    
 | 
			
		||||
    /**inserts element e immediately before position p and returns its new position*/
 | 
			
		||||
    Position<E> addBefore(Position<E> p, E e) throws IllegalArgumentException;
 | 
			
		||||
    
 | 
			
		||||
    /**inserts element e immediately after Position p and returns its new position*/
 | 
			
		||||
    Position<E> addAfter(Position<E> p, E e) throws IllegalArgumentException;
 | 
			
		||||
    
 | 
			
		||||
    /**replaces the element stored at position p and returns the replaced element*/
 | 
			
		||||
    E set(Position<E> p, E e) throws IllegalArgumentException;
 | 
			
		||||
    
 | 
			
		||||
    /**removes the element stored at position p and returns it (invalidating p)*/
 | 
			
		||||
    E remove(Position<E> p) throws IllegalArgumentException;
 | 
			
		||||
}
 | 
			
		||||
		Reference in a new issue