Created
February 25, 2016 17:57
-
-
Save umuturan/85e12a67c8f2ff6ad64b to your computer and use it in GitHub Desktop.
Lab-03B
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.util.Arrays; | |
| import java.util.Iterator; | |
| /* | |
| *Lab01 | |
| *IntBag Class | |
| *Author: | |
| *@Umut Can Turan | |
| *21401929 | |
| *Section 06 | |
| */ | |
| public class IntBag { | |
| // properties | |
| int[] bag; | |
| // constructors | |
| public IntBag() { | |
| bag = new int[100]; // at least three space to the inside | |
| bag[0] = -1; | |
| } | |
| public IntBag(int maxAmount) { | |
| bag = new int[maxAmount + 1]; | |
| bag[0] = -1; | |
| } | |
| // methods | |
| public Iterator iterator(){ | |
| IntBag bagNew; | |
| Iterator iBag; | |
| bagNew = new IntBag(bag.length); | |
| bagNew.bag=bag; | |
| /*for(int i=0;i<bag.length && bag[i]!= 0;i++ ) | |
| { | |
| bagNew.add(bag[i]); | |
| }*/ | |
| iBag = new IntBagIterator(bagNew); | |
| return iBag; | |
| } | |
| public void add(int valToAdd) { | |
| if (valToAdd < 0) { | |
| System.out.println("please enter non-negative number!"); | |
| } else { | |
| int i; | |
| // it checks the how many value does the collection have | |
| // indicates the last index | |
| for (i = 0; bag[i] != -1; i++) { | |
| } | |
| if (i == bag.length - 1) | |
| System.out.println("bag is full!"); | |
| else { | |
| bag[i] = valToAdd; | |
| bag[i + 1] = -1; | |
| } | |
| } | |
| } | |
| public void add(int indAdd, int valToAdd) { | |
| if (valToAdd < 0) { | |
| System.out.println("please enter non-negative number!"); | |
| } else { | |
| int i; | |
| // it checks the how many value does the collection have | |
| // indicates the last index | |
| for (i = 0; bag[i] != -1; i++) { | |
| } | |
| if (i == bag.length - 1) { | |
| System.out.print("bag is full!"); | |
| } else { | |
| // to check indAdd is within bounds | |
| if (i >= indAdd) { | |
| for (; i >= indAdd; i--) { | |
| if (i != bag.length - 1) | |
| bag[i + 1] = bag[i]; | |
| } | |
| // shifts the collection by 1 to the right till the indAdd | |
| bag[indAdd] = valToAdd; | |
| } | |
| } | |
| } | |
| } | |
| public void remove(int indRem) { | |
| int i; | |
| // it checks the how many value does the collection have | |
| // indicates the last index | |
| for (i = 0; bag[i] != -1; i++) { | |
| } | |
| // to check indRem is in boundaries | |
| if (indRem <= i && indRem != bag.length - 1) { | |
| for (int x = indRem; x < i; x++) { | |
| bag[x] = bag[x + 1]; | |
| } | |
| bag[i] = 0; | |
| } else { | |
| System.out.println("invalid index is entered!"); | |
| } | |
| } | |
| public boolean contains(int valCheck) { | |
| boolean result; | |
| result = false; | |
| for (int i = 0; i < bag.length && !result; i++) { | |
| if (valCheck == bag[i]) { | |
| result = true; | |
| } | |
| } | |
| /* | |
| * if(Arrays.toString(bag).indexOf(valCheck)==-1) result=false; | |
| */ | |
| return result; | |
| } | |
| public String toString() { | |
| String result = "["; | |
| int i = 0; | |
| for (i = 0; bag[i] != -1; i++) { | |
| result = (result + bag[i] + ","); | |
| } | |
| if (bag[i] == -1) | |
| result = result + bag[i] + "]"; | |
| return result; | |
| } | |
| public int size() { | |
| int i; | |
| int size; | |
| // it checks the how many value does the collection have | |
| // indicates the last index | |
| for (i = 0; bag[i] != -1; i++) { | |
| } | |
| size = i; | |
| // size equals to the index of sentinel number | |
| return size; | |
| } | |
| public int get(int index) { | |
| return bag[index]; | |
| } | |
| public String findAll(int checkVal) { | |
| String result = ""; | |
| for (int i = 0; i < size(); i++) { | |
| if (bag[i] == checkVal) { | |
| result = result + String.valueOf(i) + ","; | |
| } | |
| } | |
| return (result); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.util.Iterator; | |
| /* | |
| *LAB03 | |
| *IntBagIterator CLASS | |
| * This class have 2 new property as well as | |
| * implementing Iterator interface | |
| *Author:@Umut Can Turan | |
| *21401929 | |
| *Section 06 | |
| */ | |
| public class IntBagIterator implements Iterator { | |
| IntBag aBag; | |
| int index; | |
| public IntBagIterator(IntBag bag) { | |
| aBag = bag; | |
| index = 0; | |
| //must be initialized to zero when an iteration begins | |
| } | |
| @Override | |
| public boolean hasNext() { | |
| /* determines whether the index value is less than the number of | |
| * elements in the set or not | |
| */ | |
| if (index < aBag.size()) | |
| return true; | |
| else | |
| return false; | |
| } | |
| @Override | |
| public Object next() { | |
| Integer box; | |
| // return the corresponding int in an Integer wrapper | |
| box = new Integer(aBag.get(index)); | |
| index++; | |
| return box; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.util.Iterator; | |
| public interface IntIterator extends Iterator { | |
| public int nextInt(); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.util.Iterator; | |
| /* | |
| *LAB03 | |
| *TestIterators CLASS | |
| * Tests the IntBagIterator class | |
| * which implements Iterator interface | |
| *Author:@Umut Can Turan | |
| *21401929 | |
| *Section 06 | |
| */ | |
| public class TestIterators { | |
| public static void main(String[] args) { | |
| // TODO Auto-generated method stub | |
| IntBag bag = new IntBag(10); | |
| for(int i=0; i<3 ;i++){ | |
| bag.add(i); | |
| } | |
| Iterator i, j; | |
| i = new IntBagIterator( bag); | |
| while ( i.hasNext() ) | |
| { | |
| System.out.println( i.next() ); | |
| //j = new IntBagIterator( bag); | |
| j = bag.iterator(); | |
| while ( j.hasNext() ) | |
| { | |
| System.out.println( "--" + j.next() ); | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment