first commit

This commit is contained in:
Jose Caban
2025-06-07 01:59:34 -04:00
commit 388ac241f0
3558 changed files with 9116289 additions and 0 deletions

32
CS1322/t3/Animal.java Normal file
View File

@@ -0,0 +1,32 @@
public class Animal{
public String name;
public static int animalCount=0; //A count of all the animals created
public Animal(){
this("Bob");
}
public Animal(String name){
this.name=name;
animalCount++;
}
public void eat(){
System.out.println("Yummy");
}
public void speak(){
System.out.println("I am an Animal. My name is "+ this.name);
}
public String toString(){
return "I am an animal named " + this.name;
}
}

25
CS1322/t3/Cat.java Normal file
View File

@@ -0,0 +1,25 @@
//test
public class Cat extends Animal{
public Cat(String name){
this.name = name;
}
public Cat(){
}
public void speak(){
System.out.println("Meow. My name is " + this.name);
}
public void nap(){
System.out.println("Taking a nap. Zzzzzz");
}
public String toString(){
return "I am an cat named " + this.name;
}
}

24
CS1322/t3/Dog.java Normal file
View File

@@ -0,0 +1,24 @@
public class Dog extends Animal{
public Dog(String name){
this.name = name;
}
public Dog(){
}
public void speak(){
System.out.println("Woof. My name is "+this.name);
}
public void play(){
System.out.println("I am playing.");
}
public String toString(){
return "I am a dog named " + this.name;
}
}

132
CS1322/t3/LLNode.java Normal file
View File

@@ -0,0 +1,132 @@
/**
* <PRE>
* LLNode.java
*
* Revisions: 1.0 Sep. 21, 2002
* Created the LLNode class
* 1.1 Sep. 21, 2002
* Finished class
*
* </PRE>
*
* Collaboration Statement:
* I worked on the homework assignment alone, using only
* course materials.
*
* Created with JCreatorLE, some indents are off when viewed through notepad
* or EMACS
*
* @author <A HREF="mailto:gtg184g@mail.gatech.edu">Jose Manuel Caban</A>
* @version Version 1.1, Sep. 21, 2002
*/
public class LLNode {
/**
*holds the node data
*/
private Object data;
/**
*holds the location of the next node
*/
private LLNode next;
//nothing to debug that this will help with,
//but its in here for good measure
private static final boolean bDebug = false;
////////////////
//Constructors//
////////////////
/**
*Defaul LLNode Constructor
*both next and data are initialized to null
*/
public LLNode(){
setNext(null);
setData(null);
}
/**
*LLNode(Object)
*next initialized to null
*@param o the value to be given to data
*/
public LLNode(Object o){
setNext(null);
setData(o);
}
/**
*LLNode(Object,LLNode)
*@param o, the value of data
*@param next, the value of next
*/
public LLNode(Object o, LLNode next){
setNext(next);
setData(o);
}
///////////////////////
//Accessors/Modifiers//
///////////////////////
/**
*Accessor for data
*@return value of data
*/
public Object getData(){
return data;
}
/**
*Modifier for data
*@param the value for data
*/
public void setData(Object data){
this.data = data;
}
/**
*Accessor for next
*@return the next node
*/
public LLNode getNext(){
return next;
}
/**
*Modifier for next
*@param the new next node
*/
public void setNext(LLNode next){
this.next = next;
}
///////////
//Methods//
///////////
public String toString(){
return (data.toString());
}
/***********************************************/
/**
* Debugging main for class LLNode.
* This method will rigorously test my code.
*
* <br><br>
* @param args a String array of command line arguments.
*/
public static void main(String[] args) {
Integer bob = new Integer(5);
LLNode node = new LLNode(bob);
System.out.println(node);
}// end of main(String[] args)
}// end of class LLNode

425
CS1322/t3/LinkedList.java Normal file
View File

@@ -0,0 +1,425 @@
/**
* <PRE>
* LinkedList.java
*
* Revisions: 1.0 Sep. 21, 2002
* Created the LinkedList class
* 1.1 Sep. 26, 2002
* Compiled, Finished, Commented
*
* </PRE>
*
* Collaboration Statement:
* I worked on the homework assignment alone, using only
* course materials.
*
* Created with JCreatorLE, some indents are off when viewed through notepad
* or EMACS
*
* @author <A HREF="mailto:gtg184g@mail.gatech.edu">Jose Manuel Caban</A>
* @version Version 1.1, Sep. 26, 2002
*/
public class LinkedList {
/**
*Front of the list
*/
private LLNode head;
/**
*Back of the List
*/
private LLNode tail;
//there's actually something to debug this time!
private static final boolean bDebug = false;
////////////////
//Constructors//
////////////////
/**
*Default constructor for LinkedList
*sets head and tail to null
*/
public LinkedList(){
head = null;
tail = null;
}
///////////
//Methods//
///////////
/**
*Add node to front of list
*@param o, the data to be stored in the node
*/
public void addToFront(Object o){
LLNode node = new LLNode(o,head);
if(bDebug){System.out.print("Adding to Front:");}
head = node;
if(tail == null){
this.tail = node;
}
if(bDebug){System.out.println("Done.");}
}
/**
*Add node to back of list
*@param o, the data to be stored in the node
*/
public void addToBack(Object o){
LLNode node = new LLNode(o);
if(bDebug){System.out.print("Adding to Back:");}
if(tail != null){
tail.setNext(node);
}
tail = node;
if(head == null){
head = node;
}
if(bDebug){System.out.println("Done.");}
}
/**
*@return the data stored by the first node
*/
public Object getFirst(){
if(head != null){
return head.getData();
}
else {
return null;
}
}
/**
*@return the data stored by the last node
*/
public Object getLast(){
if(tail != null){
return tail.getData();
}
else {
return null;
}
}
/**
*@return the data stored by the first node, destroy first node
*/
public Object removeFromFront(){
if(head == null){
return null;
}
if(bDebug){System.out.print("Removing from Front:");}
Object temp = head.getData();
head = head.getNext();
if(head == null){
tail = null;
}
if(bDebug){System.out.println("Done.");}
return temp;
}
/**
*@return the data stored by the last node, destroy the last node
*/
public Object removeFromBack(){
if(tail == null){
return null;
}
if(bDebug){System.out.print("Removing from Back:");}
Object temp = tail.getData();
LLNode current = head;
if(current != tail){
//find second to last node
while(current.getNext() != tail){
current = current.getNext();
}
tail = current;
tail.setNext(null);
}
else {
tail = null;
}
if(tail == null){
head = null;
}
if(bDebug){System.out.println("Done.");}
return temp;
}
/**
*@return the number of nodes in the LinkedList
*/
public int size(){
if(head == null){
return 0;
}
if(head == tail){
return 1;
}
int count=0;
LLNode current = head;
while(current!= null){
current = current.getNext();
count++;
}
return (count);
}
/**
*Turn the LinkedList into an Array
*@return array containing all the data held by the LinkedList
*/
public Object[] asArray(){
Object[] array = new Object[this.size()];
if(head == null){
return array;
}
LLNode current = head;
int i=0;
while(current != null){
array[i] = current.getData();
i++;
current = current.getNext();
}
return array;
}
/**
*temp method to output list on screen
*meant for debugging to circumvent the array method
*/
public void listOut(){
LLNode current = head;
while(current != null){
System.out.println(current.getData());
current = current.getNext();
}
}
/**
*Find the equality of the LinkedList
*@param LL, the list to be compared to
*@return the equality value
*/
public boolean equals(Object obj){
if(!(obj instanceof LinkedList)){
return false;
}
LinkedList LL = (LinkedList)obj;
LLNode thisCurrent = this.head;
LLNode temp = LL.head;
if(this.size() != LL.size()){
return false;
}
while(thisCurrent != null){
if(!(thisCurrent.getData().equals(temp.getData()))){
return false;
}
thisCurrent = thisCurrent.getNext();
temp = temp.getNext();
}
return true;
}
/**
* Change standard string to output to be used for this specific program
* @return phrase as string
*/
public String toString(){
String ReallyLong;
ReallyLong = "[ ";
LLNode current = head;
while(current != null){
ReallyLong += current.getData().toString();
if(current.getNext() != null){
ReallyLong += ", ";
}
current = current.getNext();
}
ReallyLong += "]";
return ReallyLong;
}
/**
*Check if there is an object in the list matching o
*@return true if such an object exists
*/
public boolean contains(Object o){
LLNode current = head;
while(current != null){
if(current.getData().equals(o)){
return true;
}
current = current.getNext();
}
return false;
}
/**
*Method meant for debugging
*clears the list
*/
public void clear(){
head = null;
tail = null;
}
/*********************************************************/
/**
* Debugging main for class LinkedList.
* This method will rigorously test my code.
*
* <br><br>
* @param args a String array of command line arguments.
*/
public static void main(String[] args) {
LinkedList LL = new LinkedList();
Object[] array;
LinkedList LL2 = new LinkedList();
System.out.println("test 1");
LL.addToFront("2");
LL.addToFront("1");
LL.addToBack("3");
LL.addToBack("4");
LL.listOut();
array = LL.asArray();
for(int i=0; i<array.length; i++){
System.out.println(array[i]);
}
System.out.println("test 2");
LL.removeFromFront();
LL.removeFromBack();
LL.removeFromBack();
array = LL.asArray();
for(int i=0; i<array.length; i++){
System.out.println(array[i]);
}
System.out.println(LL.equals(LL2));
LL.clear();
LL.addToFront("1");
LL.addToFront("2");
LL.addToBack("3");
LL2.addToFront("1");
LL2.addToFront("2");
LL2.addToBack("3");
System.out.println(LL2.equals(LL));
LL.clear();
LL2.clear();
LL.addToBack("2");
LL.addToFront(new Integer(1));
LL2.addToBack("2");
System.out.println(LL.equals(LL2));
LL2.addToFront(new Integer(1));
System.out.println(LL2.equals(LL));
LL2.removeFromBack();
System.out.println(LL2.equals(LL));
System.out.println(LL.equals(LL));
LL.clear();
System.out.println(LL.equals(LL2));
//test what autograder finds as wrong
//431, 653, 95, 201, 331, 326, 627, 38, 284, 350, 960, 36
LL.clear();
LL2.clear();
LL.addToBack(new Integer(36));
LL.addToFront(new Integer(960));
LL.addToFront(new Integer(350));
LL.addToFront(new Integer(284));
LL.addToFront(new Integer(38));
LL.addToFront(new Integer(627));
LL.addToFront(new Integer(326));
LL.addToFront(new Integer(331));
LL.addToFront(new Integer(201));
LL.addToFront(new Integer(95));
LL.addToFront(new Integer(653));
LL.addToFront(new Integer(431));
System.out.println(LL.equals(LL));
System.out.println(LL.equals(LL2));
LL2 = LL;
System.out.println(LL.equals(LL2));
//Holy crap this is a long debug main!
LL2.clear();
LL2.addToFront(new Integer(960));
LL2.addToFront(new Integer(350));
LL2.addToFront(new Integer(284));
LL2.addToFront(new Integer(38));
LL2.addToFront(new Integer(627));
LL2.addToFront(new Integer(326));
LL2.addToFront(new Integer(331));
LL2.addToFront(new Integer(201));
LL2.addToFront(new Integer(95));
LL2.addToFront(new Integer(653));
LL2.addToFront(new Integer(431));
System.out.println(LL.equals(LL2));
}// end of main(String[] args)
}// end of class LinkedList

218
CS1322/t3/Test3.java Normal file
View File

@@ -0,0 +1,218 @@
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();
}
}

271
CS1322/t3/test3.txt Normal file
View File

@@ -0,0 +1,271 @@
Test Review Questions
--------------------
String and Recursion
--------------------
You may only use these methods from String class for problems 1-6
o public char charAt(int index);
o public String substring(int beginIndex);
o public boolean equals(Object o);
Implement the following methods
1) public boolean contains(String s, char c);
/*This method should return true if the given string contains the given
character and false otherwise. Implement this problem using RECURSION only.*/
2) public String sameWithout(String s, char c);
/*This method should return the String given with all the occurrences of the
given char taken out. exp.
sameWithout("abc", 'b'); should return "ac".
Implement this problem using RECURSION only.*/
3) public boolean isAll(String s, char c);
/*This method should return true is all characters in s are equal to c and
false if and only if s contains some letter other than c. For example,
isAll("", 'b') would return true, isAll("aaaaa",'a') would return true, and
isAll("aaabaaa", 'a') would return false.
You must use RECURSION only to implement this method.*/
4) public String replaceOccurancesOf(String s, char from, char to);
/*This method should return a string that is the same as s, except with all
occurances of the character from replaced by the character to.
You must use RECURSION only to implement this method. */
5) public int firstIndexOf(String s, char c);
/*This method should return the index of the first occurence of c in the
String s, or -1 if c does not appear in s. For example, firstIndex("", 'x')
should return -1, firstIndexOf("abcde", 'c') should return 2 and
firstIndexOf("qwexy", 'd') should return -1.
You must use RECURSION only to implement this method.*/
6) public int countOccurancesOf(String s, char from, char to);
/*This method should return a count of the number of times the character c
appears in s. For example, countOccurencesOf("",'b') should return 0,
countOccurencesOf("abceabaa",'a') should return 4 and
countOccurencesOf("abcdefq",'q') should return 1.
You must use RECURSION only to implement this method.*/
Refer to the following inheritance structure for problems 8, 11, and 15:
public class Animal{
public String name;
public static animalCount=0; //A count of all the animals created
public Animal(){
this("Bob");
}
public Animal(String name){
this.name=name;
animalCount++;
}
public void eat(){
System.out.println("Yummy");
}
public void speak(){
System.out.println("I am an Animal. My name is "+ this.name);
}
public String toString(){
return "I am an animal named " + this.name;
}
}
public class Dog extends Animal{
public void speak(){
System.out.println("Woof. My name is "+this.name);
}
public void play(){
System.out.println("I am playing.");
}
public String toString(){
return "I am a dog named " + this.name;
}
}
public class Cat extends Animal{
public void speak(){
System.out.println("Meow. My name is " + this.name);
}
public void nap(){
System.out.println("Taking a nap. Zzzzzz");
}
public String toString(){
return "I am an cat named " + this.name;
}
}
------------
Polymorphism
------------
7) Why is Polymorphism useful?
8) Using the above classes, which of the following code segements is valid?
a) Dog d=new Animal();
b) Animal a=new Dog();
c) Cat c=new Cat();
d) Dog d=new Cat();
e) Animal a=new Dog();
Cat c=new Cat();
a=c;
---------------
Dynamic Binding
---------------
9) What is Dynamic Binding?
10) How does Dynamic Binding relate to Polymorphism?
11) Using the above classes what would be the output for the following
pieces of code:
a) Dog d=new Dog();
dog.eat();
b) Animal a=new Dog();
a.play();
c) Animal a=new Dog();
a.speak();
d) Dog d=new Dog();
((Animal)d).speak();
e) Animal a=new Cat();
((Cat)a).nap();
f) Animal a=new Dog();
((Cat)a).speak();
------------------
Static Vs Instance
------------------
12) What is the difference between an instance variable/method and a static
variable/method?
13) When would you want to use an instance variable?
14) When would you want to use a static variable?
15) Consider the following code:
Dog d=new Dog("Fido");
Cat c=new Cat("Fluffy");
Animal 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);
16) What would be the output of the above code?
17) What would be the output of the above code if we made the name variable
static? if we made the animalCount variable an instance variable?
-----------
Linked List
-----------
18) What are linked lists?
19) Why would you use one? What are some advantages over other data structures
we have learned about?
For problems 20-24, assume that you have a standard (as in p2) LLNode
implementation and write the following methods from the LinkedList class:
20) public void removeAllRec(Object data);
/*This method will remove the all occurances of this object found in the list.
You must use RECURSION only for this method*/
21) public void removeAllItr(Object data);
/*This method will remove the all occurances of this object found in the list.
You must use ITERATION only for this method*/
22) public boolean find(Object data);
/*This method should return true if the data is found in the list and
false otherwise*/
23) public int size();
/*this method should return the number of nodes in the list*/
24) Assume that you are given a typical LLNode, and a LinkedList class,
but it is possible that the LinkedList might end up wihth a loop in it,
rather than terminating at null. One algorithm to find out if there is a
loop is to have two references, and advanced one by one step and the other
by two steps repeatedly. if one ever reaches null, there is no loop,
if they both reference the same node [besides at the very start], there is
a loop.
Implement the method
public boolean hasALoop()
/*This method determines if a loop exists in the LinkedList.*/
-------
Stacks
-------
25) What is a stack?
36) Give an real life example of a stack.
27) How would this relate to a linked list? What restrictions would there
if you used a linked list to implement a stack?
-----
Queue
-----
28) Whats is a queue?
29) Give a real life example of a queue.
30) How would this relate to a linked list? What restrictions would there
if you used a linked list to implement a queue?