inital commit
This commit is contained in:
		
						commit
						d1948b0e58
					
				
					 67 changed files with 5280 additions and 0 deletions
				
			
		
							
								
								
									
										3
									
								
								Lab103-VenbergGE/manifest.mf
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										3
									
								
								Lab103-VenbergGE/manifest.mf
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,3 @@
 | 
			
		|||
Manifest-Version: 1.0
 | 
			
		||||
X-COMMENT: Main-Class will be added automatically by build
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										207
									
								
								Lab103-VenbergGE/src/ArrayBag.java
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										207
									
								
								Lab103-VenbergGE/src/ArrayBag.java
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,207 @@
 | 
			
		|||
/*
 | 
			
		||||
 * 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/>.
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * @version 02/10/2021
 | 
			
		||||
 * @author toric
 | 
			
		||||
 */
 | 
			
		||||
public class ArrayBag<T> implements Bag<T>{
 | 
			
		||||
    private T[] list;
 | 
			
		||||
    private int count=0;
 | 
			
		||||
    
 | 
			
		||||
    /**
 | 
			
		||||
     * default constructor
 | 
			
		||||
     */
 | 
			
		||||
    public ArrayBag(){
 | 
			
		||||
        list = (T[]) new Object[50];
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    /**
 | 
			
		||||
     * constructs starting array based off of passed size.
 | 
			
		||||
     * @param initSize size of inital array
 | 
			
		||||
     */
 | 
			
		||||
    public ArrayBag(int initSize){
 | 
			
		||||
        list = (T[]) new Object[initSize];
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    /**
 | 
			
		||||
     * returns number of values of scores.
 | 
			
		||||
     * @return size of list
 | 
			
		||||
     */
 | 
			
		||||
    public int size(){return count;}
 | 
			
		||||
    
 | 
			
		||||
    /**
 | 
			
		||||
     * returns true if empty
 | 
			
		||||
     * @return boolean
 | 
			
		||||
     */
 | 
			
		||||
    public boolean isEmpty(){
 | 
			
		||||
        if(count==0){
 | 
			
		||||
            return true;
 | 
			
		||||
        }
 | 
			
		||||
        else{
 | 
			
		||||
            return false;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    /**
 | 
			
		||||
     * clears contents of array
 | 
			
		||||
     */
 | 
			
		||||
    public void clear(){count = 0;} //we dont need to delete the actual contents, just mark them as garbage.
 | 
			
		||||
    
 | 
			
		||||
    /**
 | 
			
		||||
     * 
 | 
			
		||||
     * @param object number you want to know frequency of
 | 
			
		||||
     * @return number of times object occurs
 | 
			
		||||
     */
 | 
			
		||||
    public int getFrequencyOf(T object){
 | 
			
		||||
        int frequency=0;
 | 
			
		||||
        for(int i=0; i<count; i++){
 | 
			
		||||
            if(list[i]==object){frequency++;}
 | 
			
		||||
        }
 | 
			
		||||
        return frequency;
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    /**
 | 
			
		||||
     * 
 | 
			
		||||
     * @param object number you want to know whether the list contains. 
 | 
			
		||||
     * @return boolean true if object is in the list.
 | 
			
		||||
     */
 | 
			
		||||
    public boolean contains(T object){
 | 
			
		||||
        for(int i=0; i<count; i++){
 | 
			
		||||
            if(list[i]==object){return true;}
 | 
			
		||||
        }
 | 
			
		||||
        return false;
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    /**
 | 
			
		||||
     * adds object to the end of the list.
 | 
			
		||||
     * @param object the number to add to the end.
 | 
			
		||||
     */
 | 
			
		||||
    public void add(T object){
 | 
			
		||||
        if(count==list.length){
 | 
			
		||||
            T tmp[] = (T[]) new Object[count*2];
 | 
			
		||||
            for(int i=0; i<count; i++){
 | 
			
		||||
                tmp[i]=list[i];
 | 
			
		||||
            }
 | 
			
		||||
            list=tmp;
 | 
			
		||||
        }
 | 
			
		||||
        list[count]=object; //since java has 0-based array, count is also the next free index.
 | 
			
		||||
        count++;
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    /**
 | 
			
		||||
     * removes item at index, shifting remaining items to the left. returns the item removed
 | 
			
		||||
     * @param int index index of item to remove
 | 
			
		||||
     */
 | 
			
		||||
    private T removeIndex(int index){ //not sure if we are suppoded to do this, but it deduplicates code.
 | 
			
		||||
        T tmp = list[index];
 | 
			
		||||
        if(index<count){
 | 
			
		||||
            for(int i=index; i<count-1; i++){
 | 
			
		||||
            list[i]=list[i+1]; //shift everything to the left.
 | 
			
		||||
            }
 | 
			
		||||
            count--;
 | 
			
		||||
            //given the object has been removed from the list, I dont think we have to worry about encapsulation.
 | 
			
		||||
            return tmp;
 | 
			
		||||
        }
 | 
			
		||||
        else{
 | 
			
		||||
            throw new ArrayIndexOutOfBoundsException();
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    /**
 | 
			
		||||
     * removes the first occurrence of object. shifts the remaining values to cover the gap.
 | 
			
		||||
     * returns null if list is empty
 | 
			
		||||
     * @param object to remove the first occurrence of.
 | 
			
		||||
     */
 | 
			
		||||
    public T remove(T object){
 | 
			
		||||
        int index=-1;
 | 
			
		||||
        for(int i=0; i<count; i++){
 | 
			
		||||
            if(list[i].equals(object)){
 | 
			
		||||
                index = i;
 | 
			
		||||
                break;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        if(index==-1){ //return null if there is no match
 | 
			
		||||
            return null;
 | 
			
		||||
        }
 | 
			
		||||
        return removeIndex(index);
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    /**
 | 
			
		||||
     * removes a random value from the list. shifts remaining values to cover the gap
 | 
			
		||||
     * returns null if list is empty
 | 
			
		||||
     */
 | 
			
		||||
    public T remove(){
 | 
			
		||||
        if(count==0){
 | 
			
		||||
            return null;
 | 
			
		||||
        }
 | 
			
		||||
        java.util.Random rand = new java.util.Random();
 | 
			
		||||
        
 | 
			
		||||
        return removeIndex(rand.nextInt(count)); // the upper bound of the nextInt function is exclusive, but since java has 0-based arrays, the max index is 1 less than count.
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    /**
 | 
			
		||||
     * gets value at i
 | 
			
		||||
     * @param i index of value to get
 | 
			
		||||
     * @return value at index i
 | 
			
		||||
     */
 | 
			
		||||
    public T get(int i){
 | 
			
		||||
        if(i<count){
 | 
			
		||||
            return list[i];
 | 
			
		||||
        }
 | 
			
		||||
        else{
 | 
			
		||||
            throw new ArrayIndexOutOfBoundsException();
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    /**
 | 
			
		||||
     * 
 | 
			
		||||
     * @param o object to compare to
 | 
			
		||||
     * @return true of objects are equal
 | 
			
		||||
     */
 | 
			
		||||
    public boolean equals(Object o){
 | 
			
		||||
        if (!(o instanceof ArrayBag)){
 | 
			
		||||
            return false;
 | 
			
		||||
        }
 | 
			
		||||
        ArrayBag s = (ArrayBag)o;
 | 
			
		||||
        //easy to check, makes sure we dont waste time doing more in depth check if we dont need to. also saves us from an indexoutofbounds.
 | 
			
		||||
        if(!(s.count==count)){
 | 
			
		||||
            return false;
 | 
			
		||||
        }
 | 
			
		||||
        
 | 
			
		||||
        for(int i=0; i<count; i++){
 | 
			
		||||
            if(!(list[i].equals(s.list[i]))){
 | 
			
		||||
                return false; //this has the happy accident of only iterating the list untill we find one inequality.
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        //if we get all the way through without discovering an inequality, they must be equal.
 | 
			
		||||
        return true;
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    /**
 | 
			
		||||
     * 
 | 
			
		||||
     * @return string representing contents of object.
 | 
			
		||||
     */
 | 
			
		||||
    public String toString(){
 | 
			
		||||
        String contents = "";
 | 
			
		||||
        for(int i=0; i<count; i++){
 | 
			
		||||
            contents =contents+":"+list[i].toString();
 | 
			
		||||
        }
 | 
			
		||||
        
 | 
			
		||||
        return getClass().getName()+'@'+count+':'+contents;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										45
									
								
								Lab103-VenbergGE/src/Bag.java
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										45
									
								
								Lab103-VenbergGE/src/Bag.java
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,45 @@
 | 
			
		|||
/*
 | 
			
		||||
 * 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/>.
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 *@date 02/10/2021
 | 
			
		||||
 * @author Gabriel Venberg
 | 
			
		||||
 */
 | 
			
		||||
public interface Bag<E> {
 | 
			
		||||
    
 | 
			
		||||
    public int size();
 | 
			
		||||
    
 | 
			
		||||
    public boolean isEmpty();
 | 
			
		||||
    
 | 
			
		||||
    public void clear();
 | 
			
		||||
    
 | 
			
		||||
    public int getFrequencyOf(E e);
 | 
			
		||||
    
 | 
			
		||||
    public boolean contains(E e);
 | 
			
		||||
    
 | 
			
		||||
    public void add(E e);
 | 
			
		||||
    
 | 
			
		||||
    public E remove(E e);
 | 
			
		||||
    
 | 
			
		||||
    public E remove();
 | 
			
		||||
    
 | 
			
		||||
    public E get(int i);
 | 
			
		||||
    
 | 
			
		||||
    public String toString();
 | 
			
		||||
    
 | 
			
		||||
    public boolean equals(Object o);
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										88
									
								
								Lab103-VenbergGE/src/Client.java
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										88
									
								
								Lab103-VenbergGE/src/Client.java
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,88 @@
 | 
			
		|||
/*
 | 
			
		||||
 * 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/>.
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 *@date 02/10/2021
 | 
			
		||||
 * @author Gabriel Venberg
 | 
			
		||||
 */
 | 
			
		||||
public class Client {
 | 
			
		||||
    public static void main(String args[]){
 | 
			
		||||
        ArrayBag mensTeam = new ArrayBag(2);
 | 
			
		||||
        
 | 
			
		||||
        //I have no clue how football works, so Im just using dumb values.
 | 
			
		||||
        mensTeam.add(new Player("name1", "position1", 1));
 | 
			
		||||
        mensTeam.add(new Player("name2", "position2", 2));
 | 
			
		||||
        mensTeam.add(new Player("name3", "position3", 3));
 | 
			
		||||
        mensTeam.add(new Player("name4", "position4", 4));
 | 
			
		||||
        mensTeam.add(new Player("name5", "position5", 5));
 | 
			
		||||
        mensTeam.add(new Player("name6", "position6", 6));
 | 
			
		||||
        mensTeam.add(new Player("name7", "position7", 7));
 | 
			
		||||
        mensTeam.add(new Player("name8", "position8", 8));
 | 
			
		||||
        
 | 
			
		||||
        System.out.println(mensTeam.toString());
 | 
			
		||||
        
 | 
			
		||||
        mensTeam.remove();
 | 
			
		||||
        
 | 
			
		||||
        System.out.println(mensTeam.toString());
 | 
			
		||||
        
 | 
			
		||||
        mensTeam.add(new Player("name9", "position9", 9));
 | 
			
		||||
        
 | 
			
		||||
        System.out.println(mensTeam.toString());
 | 
			
		||||
        
 | 
			
		||||
        mensTeam.remove(new Player("name9", "position9", 9));
 | 
			
		||||
        
 | 
			
		||||
        System.out.println(mensTeam.toString());
 | 
			
		||||
        
 | 
			
		||||
        ArrayBag courses = new ArrayBag(3);
 | 
			
		||||
        
 | 
			
		||||
        courses.add("MATH 265");
 | 
			
		||||
        courses.add("CSCI 222");
 | 
			
		||||
        courses.add("CSCI 161");
 | 
			
		||||
        
 | 
			
		||||
        System.out.println(courses.toString());
 | 
			
		||||
        
 | 
			
		||||
        courses.remove();
 | 
			
		||||
        
 | 
			
		||||
        System.out.println(courses.toString());
 | 
			
		||||
        
 | 
			
		||||
        LinkedBag womensTeam = new LinkedBag();
 | 
			
		||||
        
 | 
			
		||||
        //I have no clue how football works, so Im just using dumb values.
 | 
			
		||||
        womensTeam.add(new Player("name1", "position1", 1));
 | 
			
		||||
        womensTeam.add(new Player("name2", "position2", 2));
 | 
			
		||||
        womensTeam.add(new Player("name3", "position3", 3));
 | 
			
		||||
        womensTeam.add(new Player("name4", "position4", 4));
 | 
			
		||||
        womensTeam.add(new Player("name5", "position5", 5));
 | 
			
		||||
        womensTeam.add(new Player("name6", "position6", 6));
 | 
			
		||||
        womensTeam.add(new Player("name7", "position7", 7));
 | 
			
		||||
        womensTeam.add(new Player("name8", "position8", 8));
 | 
			
		||||
        
 | 
			
		||||
        System.out.println(womensTeam.toString());
 | 
			
		||||
        
 | 
			
		||||
        womensTeam.remove();
 | 
			
		||||
        
 | 
			
		||||
        System.out.println(womensTeam.toString());
 | 
			
		||||
        
 | 
			
		||||
        womensTeam.add(new Player("name9", "position9", 9));
 | 
			
		||||
        
 | 
			
		||||
        System.out.println(womensTeam.toString());
 | 
			
		||||
        
 | 
			
		||||
        womensTeam.remove(new Player("name9", "position9", 9));
 | 
			
		||||
        
 | 
			
		||||
        System.out.println(womensTeam.toString());
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										247
									
								
								Lab103-VenbergGE/src/LinkedBag.java
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										247
									
								
								Lab103-VenbergGE/src/LinkedBag.java
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,247 @@
 | 
			
		|||
/*
 | 
			
		||||
 * 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/>.
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 *@date 02/10/2021
 | 
			
		||||
 * @author Gabriel Venberg
 | 
			
		||||
 */
 | 
			
		||||
public class LinkedBag<T> implements Bag<T> {
 | 
			
		||||
    
 | 
			
		||||
    //dont need a count variable, linkedlist already takes care of that.
 | 
			
		||||
    private SinglyLinkedList list;
 | 
			
		||||
    
 | 
			
		||||
    public LinkedBag(){
 | 
			
		||||
        list = new SinglyLinkedList<T>();
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    public LinkedBag(int initSize){
 | 
			
		||||
        this();
 | 
			
		||||
    }
 | 
			
		||||
    /**
 | 
			
		||||
     * 
 | 
			
		||||
     * @return size of list
 | 
			
		||||
     */
 | 
			
		||||
    public int size(){return list.size();}
 | 
			
		||||
    
 | 
			
		||||
    /**
 | 
			
		||||
     * 
 | 
			
		||||
     * @return true if list is empty
 | 
			
		||||
     */
 | 
			
		||||
    public boolean isEmpty(){return list.isEmpty();}
 | 
			
		||||
    
 | 
			
		||||
    /**
 | 
			
		||||
     * emptys list of all entries
 | 
			
		||||
     */
 | 
			
		||||
    public void clear(){
 | 
			
		||||
        for(int i=0; i<list.size(); i++){
 | 
			
		||||
            list.removeFirst();
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    /*
 | 
			
		||||
    Ok, so this next bit gets MESSY. I feel like we should just add a method or two to the singlyLinkedList class.
 | 
			
		||||
    The class that my copy of the book has only contains methods for acessing/adding to the list at the tail or head,
 | 
			
		||||
    and only lets me remove from the head. This means that any object not at the head or tail is COMPLETELY INACESSABLE
 | 
			
		||||
    to me without modifying the list. So, from here on out, I will be using a second temporary stack and move items from
 | 
			
		||||
    one or the other, reading/modifying each item as its tranferred. This is wildly inneffiennt compared
 | 
			
		||||
    to if the linkedlist class just let me traverse the list, but the assignment says not to modify the list, sooo...
 | 
			
		||||
    */
 | 
			
		||||
    
 | 
			
		||||
    /**
 | 
			
		||||
     * 
 | 
			
		||||
     * @param object object to search for
 | 
			
		||||
     * @return number of times object appears in list.
 | 
			
		||||
     */
 | 
			
		||||
    public int getFrequencyOf(T object){
 | 
			
		||||
        T entry; //dont want to re-initalize this every loop.
 | 
			
		||||
        SinglyLinkedList tmp = new SinglyLinkedList<T>();
 | 
			
		||||
        int freqency=0;
 | 
			
		||||
        
 | 
			
		||||
        int size = list.size(); //If you dont do this, list.size will shrink every loop, and you will only get through half the list as i grows and list.size shrinks.
 | 
			
		||||
        for(int i=0; i<size; i++){
 | 
			
		||||
            //not sure why I had to cast this, it should return T, but the complier seems to think its returning Object.
 | 
			
		||||
            entry = (T) list.removeFirst();
 | 
			
		||||
            if(entry.equals(object)){
 | 
			
		||||
                freqency++;
 | 
			
		||||
            }
 | 
			
		||||
            //if we did addFirst, it would result in tmp being an inverted version of list.
 | 
			
		||||
            tmp.addLast(entry);
 | 
			
		||||
        }
 | 
			
		||||
        //we have shifted all of list to tmp.
 | 
			
		||||
        list = tmp;
 | 
			
		||||
        return freqency;
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    /**
 | 
			
		||||
     * 
 | 
			
		||||
     * @param object object to search for
 | 
			
		||||
     * @return true if list contains at least one of object.
 | 
			
		||||
     */
 | 
			
		||||
    public boolean contains(T object){
 | 
			
		||||
        /*
 | 
			
		||||
        so in arrayBag I used different methods for contains and frequency to save time after finding a match in contains.
 | 
			
		||||
        Why not in this method? because I cant stop halfway through. I am destructivly reading the list and reconstructing it in tmp,
 | 
			
		||||
        meaning I cant stop halfway through even if I do find a match. only thing I would be saving would be the comparison.
 | 
			
		||||
        */
 | 
			
		||||
        return getFrequencyOf(object)>0;
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    public void add(T object){
 | 
			
		||||
        list.addFirst(object);
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    /**
 | 
			
		||||
     * deletes the first instance of object from the list.
 | 
			
		||||
     * @param object object to delete
 | 
			
		||||
     * @return object that was deleted
 | 
			
		||||
     */
 | 
			
		||||
    public T remove(T object){
 | 
			
		||||
        T entry; //dont want to re-initalize this every loop.
 | 
			
		||||
        T result=null; //cant have return ending our loop early.
 | 
			
		||||
        SinglyLinkedList tmp = new SinglyLinkedList<T>();
 | 
			
		||||
        
 | 
			
		||||
        int size = list.size(); //If you dont do this, list.size will shrink every loop, and you will only get through half the list as i grows and list.size shrinks.
 | 
			
		||||
        for(int i=0; i<size; i++){
 | 
			
		||||
            entry = (T) list.removeFirst();
 | 
			
		||||
            //if the object we want to delete is found, we record it and break out of the loop BEFORE we put it on the new stack.
 | 
			
		||||
            if (entry.equals(object)){
 | 
			
		||||
                result = entry;
 | 
			
		||||
                break;
 | 
			
		||||
            }
 | 
			
		||||
            tmp.addLast(entry);
 | 
			
		||||
        }
 | 
			
		||||
        
 | 
			
		||||
        //finish the juggle the loop SHOULD do nothing if list is empty by now.
 | 
			
		||||
        size = list.size(); //If you dont do this, list.size will shrink every loop, and you will only get through half the list as i grows and list.size shrinks.
 | 
			
		||||
        for(int i=0; i<size; i++){
 | 
			
		||||
            tmp.addLast(list.removeFirst());
 | 
			
		||||
        }
 | 
			
		||||
        list=tmp;
 | 
			
		||||
        return result;
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    /**
 | 
			
		||||
     * removes a random object from the list
 | 
			
		||||
     * @return object that was deleted
 | 
			
		||||
     */
 | 
			
		||||
    public T remove(){
 | 
			
		||||
        T result=null; //cant have return ending our loop early.
 | 
			
		||||
        SinglyLinkedList tmp = new SinglyLinkedList<T>();
 | 
			
		||||
        
 | 
			
		||||
        java.util.Random rand = new java.util.Random();
 | 
			
		||||
        
 | 
			
		||||
        int indexToRemove = rand.nextInt(list.size());
 | 
			
		||||
        
 | 
			
		||||
        //juggle a bit before we delete
 | 
			
		||||
        for(int i=0; i<indexToRemove; i++){
 | 
			
		||||
            tmp.addLast(list.removeFirst());
 | 
			
		||||
        }
 | 
			
		||||
        
 | 
			
		||||
        //delete
 | 
			
		||||
        result=(T) list.removeFirst();
 | 
			
		||||
        
 | 
			
		||||
        //finish the juggle...
 | 
			
		||||
        for(int i=0; i<list.size(); i++){
 | 
			
		||||
            tmp.addLast(list.removeFirst());
 | 
			
		||||
        }
 | 
			
		||||
        
 | 
			
		||||
        list=tmp;
 | 
			
		||||
        return result;        
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    /**
 | 
			
		||||
     * retrieves an item by its index. more expensive than you think!
 | 
			
		||||
     * @param item index of item to retrieve
 | 
			
		||||
     * @return retrieved item
 | 
			
		||||
     */
 | 
			
		||||
    public T get(int item){
 | 
			
		||||
        T result = null;
 | 
			
		||||
       SinglyLinkedList tmp = new SinglyLinkedList<T>();
 | 
			
		||||
                        
 | 
			
		||||
        //juggle till we get to where we need to be
 | 
			
		||||
        for(int i=0; i<item; i++){
 | 
			
		||||
            tmp.addLast(list.removeFirst());
 | 
			
		||||
        }
 | 
			
		||||
        
 | 
			
		||||
        //record
 | 
			
		||||
        result=(T) list.removeFirst();
 | 
			
		||||
        tmp.addLast(result);
 | 
			
		||||
        
 | 
			
		||||
        //finish the juggle...
 | 
			
		||||
        for(int i=0; i<list.size(); i++){
 | 
			
		||||
            tmp.addLast(list.removeFirst());
 | 
			
		||||
        }
 | 
			
		||||
        
 | 
			
		||||
        list=tmp;//at this point Im just thankfull we can do this instead of having to juggle BACK!
 | 
			
		||||
        return result;
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    /**
 | 
			
		||||
     *
 | 
			
		||||
     * @return contents of object as string
 | 
			
		||||
     */
 | 
			
		||||
    public String toString(){
 | 
			
		||||
        String contents ="";
 | 
			
		||||
        T entry;
 | 
			
		||||
        SinglyLinkedList tmp = new SinglyLinkedList<T>();
 | 
			
		||||
        //the first record of juggling dates to between 1994 and 1781 BC, with the egyptians!
 | 
			
		||||
        int size = list.size(); //If you dont do this, list.size will shrink every loop, and you will only get through half the list as i grows and list.size shrinks.
 | 
			
		||||
        for(int i=0; i<size; i++){
 | 
			
		||||
            entry = (T) list.removeFirst();
 | 
			
		||||
            contents = contents+':'+entry.toString();
 | 
			
		||||
            tmp.addLast(entry);
 | 
			
		||||
        }
 | 
			
		||||
        list=tmp;
 | 
			
		||||
        return getClass().getName()+'@'+list.size()+':'+contents;
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    /**
 | 
			
		||||
     * compares with another object.
 | 
			
		||||
     * @param o object to compare with
 | 
			
		||||
     * @return true if objects are identical
 | 
			
		||||
     */
 | 
			
		||||
    public boolean equals(Object o){
 | 
			
		||||
        if (!(o instanceof LinkedBag)){return false;}
 | 
			
		||||
        
 | 
			
		||||
        LinkedBag l = (LinkedBag) o;
 | 
			
		||||
        if(l.size()!=list.size()){return false;}
 | 
			
		||||
        
 | 
			
		||||
        SinglyLinkedList tmp1 = new SinglyLinkedList<T>();
 | 
			
		||||
        SinglyLinkedList tmp2 = new SinglyLinkedList<T>();
 | 
			
		||||
        boolean result = true;
 | 
			
		||||
        T entry1, entry2;//need two of these, as I dont want to destroy the list passed to me, either.
 | 
			
		||||
        
 | 
			
		||||
        /*
 | 
			
		||||
        In ancient China, juggling was an art performed by some warriors.
 | 
			
		||||
        One such warrior was Xiong Yiliao, whose juggling of nine balls
 | 
			
		||||
        in front of troops on a battlefield reportedly caused the opposing troops to flee without fighting,
 | 
			
		||||
        resulting in a complete victory. (from wikipedia)
 | 
			
		||||
        */
 | 
			
		||||
        int size = list.size(); //If you dont do this, list.size will shrink every loop, and you will only get through half the list as i grows and list.size shrinks.
 | 
			
		||||
        for(int i=0; i<size; i++){
 | 
			
		||||
            entry1=(T) list.removeFirst();
 | 
			
		||||
            entry2=(T) l.list.removeFirst();
 | 
			
		||||
            //dont want to return with list items still 'in the air' (ive taken this juggling metaphor to far! help!)
 | 
			
		||||
            if(!entry1.equals(entry2)){result = false;}
 | 
			
		||||
            tmp1.addLast(entry1);
 | 
			
		||||
            tmp2.addLast(entry2);
 | 
			
		||||
        }
 | 
			
		||||
        list=tmp1;
 | 
			
		||||
        l.list=tmp2;
 | 
			
		||||
        return result;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										90
									
								
								Lab103-VenbergGE/src/Player.java
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										90
									
								
								Lab103-VenbergGE/src/Player.java
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,90 @@
 | 
			
		|||
/*
 | 
			
		||||
 * 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/>.
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 *@date 02/10/2021
 | 
			
		||||
 * @author Gabriel Venberg
 | 
			
		||||
 */
 | 
			
		||||
public class Player {
 | 
			
		||||
    private String name;
 | 
			
		||||
    private String positionPlayed; //I think positionPlayed should be a string? I know absolutly nothing about football.
 | 
			
		||||
    int jersyNumber;
 | 
			
		||||
    
 | 
			
		||||
    public Player(String name, String positionPlayed, int jersyNumber){
 | 
			
		||||
        this.name = name;
 | 
			
		||||
        this.positionPlayed = positionPlayed;
 | 
			
		||||
        this.jersyNumber = jersyNumber;
 | 
			
		||||
    }
 | 
			
		||||
    /**
 | 
			
		||||
     * 
 | 
			
		||||
     * @return name of player
 | 
			
		||||
     */
 | 
			
		||||
    public String getName(){return name;}
 | 
			
		||||
    
 | 
			
		||||
    /**
 | 
			
		||||
     * 
 | 
			
		||||
     * @return positonPlayed of player
 | 
			
		||||
     */
 | 
			
		||||
    public String getPositionPlayed(){return positionPlayed;}
 | 
			
		||||
    
 | 
			
		||||
    /**
 | 
			
		||||
     * 
 | 
			
		||||
     * @return jersyNumber of player
 | 
			
		||||
     */
 | 
			
		||||
    public int getJersyNumber(){return jersyNumber;}
 | 
			
		||||
    
 | 
			
		||||
    /**
 | 
			
		||||
     * sets name of player
 | 
			
		||||
     * @param name 
 | 
			
		||||
     */
 | 
			
		||||
    public void setName(String name){this.name=name;}
 | 
			
		||||
    
 | 
			
		||||
    /**
 | 
			
		||||
     * sets positionPlayed of player
 | 
			
		||||
     * @param positionPlayed 
 | 
			
		||||
     */
 | 
			
		||||
    public void setPositionPlayed(String positionPlayed){this.positionPlayed=positionPlayed;}
 | 
			
		||||
    
 | 
			
		||||
    /**
 | 
			
		||||
     * sets jersyNumber of player
 | 
			
		||||
     * @param jersyNumber 
 | 
			
		||||
     */
 | 
			
		||||
    public void setJersyNumber(int jersyNumber){this.jersyNumber=jersyNumber;}
 | 
			
		||||
    
 | 
			
		||||
    /**
 | 
			
		||||
     * outputs contents of object as string
 | 
			
		||||
     * @return String
 | 
			
		||||
     */
 | 
			
		||||
    public String toString(){
 | 
			
		||||
        return getClass().getName()+'@'+':'+name+':'+positionPlayed+':'+jersyNumber;
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    /**
 | 
			
		||||
     * compares with another object
 | 
			
		||||
     * @param o object to compare to
 | 
			
		||||
     * @return true if objects are identical.
 | 
			
		||||
     */
 | 
			
		||||
    public boolean equals(Object o){
 | 
			
		||||
        if (!(o instanceof Player)){return false;}
 | 
			
		||||
        
 | 
			
		||||
        Player p = (Player) o;
 | 
			
		||||
        
 | 
			
		||||
        return name.equals(p.name)&&
 | 
			
		||||
                positionPlayed.equals(p.positionPlayed)&&
 | 
			
		||||
                jersyNumber==p.jersyNumber;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										78
									
								
								Lab103-VenbergGE/src/SinglyLinkedList.java
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										78
									
								
								Lab103-VenbergGE/src/SinglyLinkedList.java
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,78 @@
 | 
			
		|||
/**
 | 
			
		||||
 *SinglyLinkedListClass
 | 
			
		||||
 * Code Fragments 3.14, 3.15
 | 
			
		||||
 * from
 | 
			
		||||
 * Data Structures & Algorithms, 6th edition
 | 
			
		||||
 * by Michael T. Goodrich, Roberto Tamassia & Michael H. Goldwasser
 | 
			
		||||
 * Wiley 2014
 | 
			
		||||
 * Transcribed by
 | 
			
		||||
 * @author Gabe Venberg
 | 
			
		||||
 */
 | 
			
		||||
public class SinglyLinkedList<E> {
 | 
			
		||||
    
 | 
			
		||||
    private static class Node<E> {
 | 
			
		||||
        private E element; //refrence to element stored at this node
 | 
			
		||||
        private Node<E> next; //refrence to subsequent node of list
 | 
			
		||||
        
 | 
			
		||||
        public Node(E e, Node<E> n){
 | 
			
		||||
            element = e;
 | 
			
		||||
            next = n;
 | 
			
		||||
        }
 | 
			
		||||
        
 | 
			
		||||
        public E getElement() {return element;}
 | 
			
		||||
        
 | 
			
		||||
        public Node<E> getNext() {return next;}
 | 
			
		||||
        
 | 
			
		||||
        public void setNext(Node<E> n) {next = n;}
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    //instance variables of SinglyLinkedList
 | 
			
		||||
    private Node<E> head = null;//head node of list
 | 
			
		||||
    private Node<E> tail = null;//last node of list
 | 
			
		||||
    private int size = 0;//number of nodes in list
 | 
			
		||||
    
 | 
			
		||||
    public SinglyLinkedList(){}//constructs an initaly empty list
 | 
			
		||||
    
 | 
			
		||||
    //access methods
 | 
			
		||||
    public int size() {return size;}
 | 
			
		||||
    
 | 
			
		||||
    public boolean isEmpty() {return size == 0;}
 | 
			
		||||
    
 | 
			
		||||
    public E first(){//returns but does not remove the first element
 | 
			
		||||
        if (size == 0) {return null;} //special case
 | 
			
		||||
        return head.getElement();
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    public E last(){//returns but does not remove last elemnt
 | 
			
		||||
        if (size ==0) {return null;}//special case
 | 
			
		||||
        return tail.getElement();
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    //update methods
 | 
			
		||||
    public void addFirst(E e){//adds element e to the front of the list
 | 
			
		||||
        head = new Node<>(e, head);//create and link a new node
 | 
			
		||||
        if (size == 0) {tail = head;}//special case, head becomes tail also
 | 
			
		||||
        size++;
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    public void addLast(E e){//adds element to end of list
 | 
			
		||||
        Node<E> newest = new Node<>(e, null);//create and link a new node
 | 
			
		||||
        if(size == 0){//special case, previously empty list
 | 
			
		||||
            head = newest;
 | 
			
		||||
        }
 | 
			
		||||
        else{
 | 
			
		||||
            tail.setNext(newest);//new node after existing tail
 | 
			
		||||
        }
 | 
			
		||||
        tail = newest;//new node becomes tail
 | 
			
		||||
        size++;
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    public E removeFirst(){//removes and returns the first element
 | 
			
		||||
        if(size == 0){return null;}//nothing to remove
 | 
			
		||||
        E answer = head.getElement();
 | 
			
		||||
        head = head.getNext();//will become null if list had only one node.
 | 
			
		||||
        size--;
 | 
			
		||||
        if(size==0){tail = null;}// special case as list is now empty
 | 
			
		||||
        return answer;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
		Reference in a new issue