Alright for those of you who do not know AP Comp Sci is taught in Java so i figured i would post all of my Labs that I still have lying around just to show what it is like. I am pretty far ahead so yeah here they are.
This is a purse class that contains some commands -
help displays the commands
reverse reverses the coins order
add adds a coin
transfer moves coins between purses
same? checks if contents are same and in same order
samecoins? checks for same coins in any order
empty removes all coins from purse
exit exit
Purse class:
import java.util.ArrayList;
public class Purse
{
public Purse()
{
}
public boolean sameContents(Purse other)
{
boolean is_same=true;
int sizeOther=other.getSize();
int sizeMain=coins.size();
if(sizeMain!=sizeOther)
return false;
for(int i=0;i<sizeMain;i++)
{
String main=coins.get(i);
String otherCurr=other.getCoin(i);
if(main.equals(otherCurr))
is_same=true;
else
{
is_same=false;
break;
}
}
return is_same;
}
public boolean sameCoins(Purse main, Purse other)
{
boolean is_same=true;
int sizeOther=other.getSize();
int sizeMain=coins.size();
if(sizeMain!=sizeOther)
return false;
Index mainindex = new Index(main);
Index otherindex = new Index(other);
for(int i=0;i<mainindex.indexString.size();i++)
{
String current=mainindex.indexString.get(i);
int numberof=mainindex.indexNum.get(i);
if(otherindex.indexString.contains(current))
{
int indexnum=otherindex.indexNum.get(otherindex.indexString.indexOf(current));
if(numberof==indexnum)
{
is_same=true;
}else
{
is_same=false;
break;
}
}else
{
is_same=false;
break;
}
}
return is_same;
}
public void addCoin(String coin)
{
String coinName = coin;
coins.add(coinName);
}
public void getString()
{
System.out.printf("Purse[");
int max=coins.size();
for(int i=0;i<max;i++)
{
System.out.printf("%s", coins.get(i));
if(i!=coins.size()-1)
System.out.printf(",");
}
System.out.printf("]%n");
}
public void reverse()
{
String arr[]= new String[coins.size()];
int max=coins.size();
int maxind=max-1;
for(int i=0;i<max;i++)
{
String current=coins.get(0);
coins.remove(0);
arr[maxind-i]=current;
}
for(int i=0;i<max;i++)
{
coins.add(arr[i]);
}
}
public void transfer(Purse other)
{
int max=other.getSize();
for(int i=0;i<max;i++)
{
coins.add(other.getCoin(i));
}
}
public String getCoin(int index)
{
return coins.get(index);
}
public int getSize()
{
return coins.size();
}
public void emptyPurse()
{
coins.clear();
}
private ArrayList<String> coins = new ArrayList<String>();
}
Index Class:
import java.util.ArrayList;
public class Index
{
public Index(Purse target)
{
ArrayList<String> targetCpy = new ArrayList<String>();
for(int i=0;i<target.getSize();i++)
{
String current=target.getCoin(i);
targetCpy.add(current);
}
for(int i=0;i<targetCpy.size();)
{
String current=targetCpy.get(i);
indexString.add(current);
targetCpy.remove(i);
indexNum.add(1);
for(int h=0;h<targetCpy.size();h++)
{
String test=targetCpy.get(h);
if(current.equals(test))
{
indexNum.set(indexString.size()-1, indexNum.get(indexString.size()-1)+1);
targetCpy.remove(h);
h--;
}
}
}
}
public ArrayList<String> indexString = new ArrayList<String>();
public ArrayList<Integer> indexNum = new ArrayList<Integer>();
}
PurseTester class:
import java.util.Scanner;
public class PurseTester
{
public static void main(String args[])
{
Scanner input = new Scanner(System.in);
Purse mypurse = new Purse();
Purse extrapurse = new Purse();
extrapurse.addCoin("Dime");
extrapurse.addCoin("Nickel");
extrapurse.addCoin("Dime");
extrapurse.addCoin("Dime");
extrapurse.addCoin("Nickel");
boolean is_running=true;
do
{
System.out.printf("Enter help to list commands: ");
String coinname = input.nextLine();
System.out.printf("%s%n", coinname);
switch(coinname)
{
case "exit": is_running=false;
break;
case "reverse": mypurse.reverse();
break;
case "print": mypurse.getString();
break;
case "add":
System.out.printf("Enter coin name: ");
coinname = input.nextLine();
mypurse.addCoin(coinname);
break;
case "transfer": mypurse.transfer(extrapurse);
break;
case "same?": System.out.printf("%s%n", mypurse.sameContents(extrapurse));
break;
case "samecoins?": System.out.printf("%s%n", mypurse.sameCoins(mypurse, extrapurse));
break;
case "empty": mypurse.emptyPurse();
break;
case "help":
System.out.printf("help: print this%nreverse: reverse the order of the coins%nprint: print the coins%nadd: add a coin%ntransfer: take the other purse and move its contents into the current purse%n");
System.out.printf("same?: check of the contents of the purses are exactly the same%nsamecoins?: checks if the two purses have the same coins but not necessarily in the same order%nempty: removes all coins from the purse %nexit: exit the program%n");
break;
default: System.out.printf("Invalid command!");
break;
}
}while(is_running);
}
}
7 More Labs to add...