218 lines
4.5 KiB
Java
218 lines
4.5 KiB
Java
public class Test3{
|
|
|
|
static LinkedList bob = new LinkedList();
|
|
|
|
public boolean contains(String s, char c){
|
|
boolean truthitude = false;
|
|
|
|
if(s.charAt(0) == c){
|
|
return true;
|
|
}
|
|
|
|
if(s.length() > 1){
|
|
truthitude = contains(s.substring(1),c);
|
|
}
|
|
|
|
return truthitude;
|
|
}
|
|
|
|
public String sameWithout(String s, char c){
|
|
String string = s;
|
|
|
|
if(s.length() > 1){
|
|
string = s.charAt(0) + sameWithout(s.substring(1),c);
|
|
}
|
|
|
|
if(s.charAt(0) == c && s.length() == 1){
|
|
string = "";
|
|
}
|
|
|
|
else if(s.charAt(0) == c){
|
|
string = s.substring(1);
|
|
}
|
|
|
|
return string;
|
|
|
|
}
|
|
|
|
public boolean isAll(String s, char c){
|
|
boolean truth = true;
|
|
|
|
if(s.length() > 1){
|
|
truth = isAll(s.substring(1),c);
|
|
}
|
|
|
|
if(!truth){
|
|
return false;
|
|
}
|
|
|
|
if(s.length() < 1){
|
|
return true;
|
|
}
|
|
|
|
if(s.charAt(0) == c){
|
|
return true;
|
|
}
|
|
else{
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public String replaceOccurancesOf(String s, char from, char to){
|
|
String string = s;
|
|
|
|
if(s == ""){
|
|
return "";
|
|
}
|
|
|
|
if(s.length() > 1){
|
|
string = s.charAt(0) + replaceOccurancesOf(s.substring(1),from,to);
|
|
}
|
|
|
|
if(s.charAt(0) == from){
|
|
string = to + string.substring(1);
|
|
}
|
|
return string;
|
|
}
|
|
|
|
public int firstIndexOf(String s, char c){
|
|
int index = -1;
|
|
|
|
if(s == ""){
|
|
return index;
|
|
}
|
|
|
|
if(s.charAt(0) == c){
|
|
return 0;
|
|
}
|
|
|
|
if(s.length() > 1){
|
|
index = firstIndexOf(s.substring(1),c);
|
|
}
|
|
if(index != -1){
|
|
index++;
|
|
}
|
|
return (index);
|
|
}
|
|
|
|
public int countOccurancesOf(String s, char c){
|
|
int count = 0;
|
|
|
|
if(s == ""){
|
|
return 0;
|
|
}
|
|
|
|
if(s.length() > 1){
|
|
count += countOccurancesOf(s.substring(1),c);
|
|
}
|
|
|
|
if(s.charAt(0) == c){
|
|
count++;
|
|
}
|
|
return count;
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void main(String args[]){
|
|
Test3 test = new Test3();
|
|
|
|
//test contains
|
|
System.out.println(test.contains("abc",'a'));
|
|
System.out.println(test.contains("ab c",'b'));
|
|
System.out.println(test.contains("a-bc",'c'));
|
|
|
|
System.out.println(test.contains("abc",'d'));
|
|
|
|
|
|
//test sameWithout
|
|
System.out.println();
|
|
System.out.println(test.sameWithout("abc",'a'));
|
|
System.out.println(test.sameWithout("abc",'b'));
|
|
System.out.println(test.sameWithout("abc",'c'));
|
|
|
|
//test isAll
|
|
System.out.println();
|
|
System.out.println(test.isAll("aaa",'b'));
|
|
System.out.println(test.isAll("aaa",'a'));
|
|
System.out.println(test.isAll("aba",'b'));
|
|
System.out.println(test.isAll("",'a'));
|
|
|
|
//test replaceOcc
|
|
System.out.println();
|
|
System.out.println(test.replaceOccurancesOf("aabaabaa",'b','a'));
|
|
System.out.println(test.replaceOccurancesOf("bbbbbbbb",'b','a'));
|
|
System.out.println(test.replaceOccurancesOf("",'b','a'));
|
|
|
|
//test firstIndexOf
|
|
System.out.println();
|
|
System.out.println(test.firstIndexOf("",'a'));
|
|
System.out.println(test.firstIndexOf("aaaaaa",'b'));
|
|
System.out.println(test.firstIndexOf("babababa",'a'));
|
|
System.out.println(test.firstIndexOf("babababa",'b'));
|
|
System.out.println(test.firstIndexOf("aaaaaaab",'b'));
|
|
System.out.println(test.firstIndexOf("aaaaaaba",'b'));
|
|
|
|
//test counters
|
|
System.out.println();
|
|
System.out.println(test.countOccurancesOf("",'a'));
|
|
System.out.println(test.countOccurancesOf("aaa",'a'));
|
|
System.out.println(test.countOccurancesOf("aba",'b'));
|
|
|
|
//which is valid
|
|
System.out.println();
|
|
//can go down but not up
|
|
//Dog d=new Animal();
|
|
|
|
Animal a=new Dog();
|
|
|
|
Cat c=new Cat();
|
|
|
|
//Dog d=new Cat();
|
|
|
|
a=new Dog();
|
|
c=new Cat();
|
|
a=c;
|
|
|
|
System.out.print("Expect Yummy: \n Actually: ");
|
|
Dog d=new Dog();
|
|
d.eat();
|
|
|
|
a=new Cat();
|
|
((Cat)a).nap();
|
|
|
|
System.out.print("Expect I am Playing: \n Actually: ");
|
|
a=new Dog();
|
|
((Dog)a).play();
|
|
/*
|
|
a=new Dog();
|
|
((Cat)a).speak();
|
|
*/
|
|
|
|
bob.addToFront(new Integer(5));
|
|
bob.addToFront(new Integer(6));
|
|
|
|
bob.addToFront(new Integer(7));
|
|
|
|
bob.addToFront(new Integer(9));
|
|
bob.addToFront(new String("bob"));
|
|
|
|
System.out.println();
|
|
d = new Dog("Fido");
|
|
c = new Cat("Fluffy");
|
|
a = new Animal();
|
|
|
|
System.out.println(d);
|
|
System.out.println(d.animalCount);
|
|
|
|
System.out.println(c);
|
|
System.out.println(c.animalCount);
|
|
|
|
System.out.println(a);
|
|
System.out.println(a.animalCount);
|
|
|
|
a=new Cat();
|
|
|
|
}
|
|
} |