From 7a6e5c1b31491cc9538e9611768ad006e4b40816 Mon Sep 17 00:00:00 2001 From: boris Date: Sun, 21 Jan 2024 00:27:23 +0000 Subject: [PATCH] vault backup: 2024-01-21 00:27:23 --- .obsidian/workspace.json | 27 +-- .../Week 1/workshop/delete.sh | 10 + .../Week 1/workshop/script1.sh | 14 ++ .../Week 1/workshop/script2.sh | 9 + .../Week 1/workshop/script3.sh | 6 + .../Week 1/workshop/testdir/test.md | 0 .../Week 1/workshop/testdir/test.txt | 0 .../Week 1/workshop/wastebasket/test.txt | 0 .../Week 1/Week 1 Database Systems.md | 23 ++- .../java-apps/HelloWorld/HelloWorldApp.class | Bin 0 -> 432 bytes .../java-apps/HelloWorld/HelloWorldApp.java | 13 ++ .../Week 1/java-apps/HelloWorld/start.sh | 2 + .../Week 1/workshop/package.bluej | 40 ++++ .../Week 1/zoo-io-v1/Animal.class | Bin 0 -> 787 bytes .../Week 1/zoo-io-v1/Animal.ctxt | 14 ++ .../Week 1/zoo-io-v1/Animal.java | 59 ++++++ .../Programming 2/Week 1/zoo-io-v1/README.TXT | 12 ++ .../Programming 2/Week 1/zoo-io-v1/Zoo.class | Bin 0 -> 4129 bytes .../Programming 2/Week 1/zoo-io-v1/Zoo.ctxt | 35 ++++ .../Programming 2/Week 1/zoo-io-v1/Zoo.java | 192 +++++++++++++++++ .../Week 1/zoo-io-v1/animal_data.txt | 10 + .../Week 1/zoo-io-v1/new_animals.txt | 3 + .../Week 1/zoo-io-v1/package.bluej | 3 + .../Week 1/zoo-io-v2/Animal.class | Bin 0 -> 787 bytes .../Week 1/zoo-io-v2/Animal.ctxt | 14 ++ .../Week 1/zoo-io-v2/Animal.java | 59 ++++++ .../Programming 2/Week 1/zoo-io-v2/README.TXT | 12 ++ .../Programming 2/Week 1/zoo-io-v2/Zoo.class | Bin 0 -> 4254 bytes .../Programming 2/Week 1/zoo-io-v2/Zoo.ctxt | 35 ++++ .../Programming 2/Week 1/zoo-io-v2/Zoo.java | 194 ++++++++++++++++++ .../Week 1/zoo-io-v2/animal_data.txt | 10 + .../Week 1/zoo-io-v2/new_animals.txt | 3 + .../Week 1/zoo-io-v2/package.bluej | 36 ++++ .../Week 1/zoo-io-v3/Animal.class | Bin 0 -> 787 bytes .../Week 1/zoo-io-v3/Animal.ctxt | 14 ++ .../Week 1/zoo-io-v3/Animal.java | 59 ++++++ .../Programming 2/Week 1/zoo-io-v3/README.TXT | 12 ++ .../Programming 2/Week 1/zoo-io-v3/Zoo.class | Bin 0 -> 4254 bytes .../Programming 2/Week 1/zoo-io-v3/Zoo.ctxt | 35 ++++ .../Programming 2/Week 1/zoo-io-v3/Zoo.java | 194 ++++++++++++++++++ .../Week 1/zoo-io-v3/ZooApp.class | Bin 0 -> 636 bytes .../Week 1/zoo-io-v3/ZooApp.ctxt | 5 + .../Week 1/zoo-io-v3/ZooApp.java | 22 ++ .../Week 1/zoo-io-v3/animal_data.txt | 10 + .../Week 1/zoo-io-v3/new_animals.txt | 3 + .../Week 1/zoo-io-v3/package.bluej | 51 +++++ 46 files changed, 1226 insertions(+), 14 deletions(-) create mode 100755 Semester 2/Computer Systems Internals & Linux/Week 1/workshop/delete.sh create mode 100755 Semester 2/Computer Systems Internals & Linux/Week 1/workshop/script1.sh create mode 100755 Semester 2/Computer Systems Internals & Linux/Week 1/workshop/script2.sh create mode 100755 Semester 2/Computer Systems Internals & Linux/Week 1/workshop/script3.sh create mode 100644 Semester 2/Computer Systems Internals & Linux/Week 1/workshop/testdir/test.md create mode 100644 Semester 2/Computer Systems Internals & Linux/Week 1/workshop/testdir/test.txt create mode 100644 Semester 2/Computer Systems Internals & Linux/Week 1/workshop/wastebasket/test.txt create mode 100644 Semester 2/Programming 2/Week 1/java-apps/HelloWorld/HelloWorldApp.class create mode 100644 Semester 2/Programming 2/Week 1/java-apps/HelloWorld/HelloWorldApp.java create mode 100755 Semester 2/Programming 2/Week 1/java-apps/HelloWorld/start.sh create mode 100644 Semester 2/Programming 2/Week 1/workshop/package.bluej create mode 100644 Semester 2/Programming 2/Week 1/zoo-io-v1/Animal.class create mode 100644 Semester 2/Programming 2/Week 1/zoo-io-v1/Animal.ctxt create mode 100644 Semester 2/Programming 2/Week 1/zoo-io-v1/Animal.java create mode 100644 Semester 2/Programming 2/Week 1/zoo-io-v1/README.TXT create mode 100644 Semester 2/Programming 2/Week 1/zoo-io-v1/Zoo.class create mode 100644 Semester 2/Programming 2/Week 1/zoo-io-v1/Zoo.ctxt create mode 100644 Semester 2/Programming 2/Week 1/zoo-io-v1/Zoo.java create mode 100644 Semester 2/Programming 2/Week 1/zoo-io-v1/animal_data.txt create mode 100644 Semester 2/Programming 2/Week 1/zoo-io-v1/new_animals.txt create mode 100644 Semester 2/Programming 2/Week 1/zoo-io-v1/package.bluej create mode 100644 Semester 2/Programming 2/Week 1/zoo-io-v2/Animal.class create mode 100644 Semester 2/Programming 2/Week 1/zoo-io-v2/Animal.ctxt create mode 100644 Semester 2/Programming 2/Week 1/zoo-io-v2/Animal.java create mode 100644 Semester 2/Programming 2/Week 1/zoo-io-v2/README.TXT create mode 100644 Semester 2/Programming 2/Week 1/zoo-io-v2/Zoo.class create mode 100644 Semester 2/Programming 2/Week 1/zoo-io-v2/Zoo.ctxt create mode 100644 Semester 2/Programming 2/Week 1/zoo-io-v2/Zoo.java create mode 100644 Semester 2/Programming 2/Week 1/zoo-io-v2/animal_data.txt create mode 100644 Semester 2/Programming 2/Week 1/zoo-io-v2/new_animals.txt create mode 100644 Semester 2/Programming 2/Week 1/zoo-io-v2/package.bluej create mode 100644 Semester 2/Programming 2/Week 1/zoo-io-v3/Animal.class create mode 100644 Semester 2/Programming 2/Week 1/zoo-io-v3/Animal.ctxt create mode 100644 Semester 2/Programming 2/Week 1/zoo-io-v3/Animal.java create mode 100644 Semester 2/Programming 2/Week 1/zoo-io-v3/README.TXT create mode 100644 Semester 2/Programming 2/Week 1/zoo-io-v3/Zoo.class create mode 100644 Semester 2/Programming 2/Week 1/zoo-io-v3/Zoo.ctxt create mode 100644 Semester 2/Programming 2/Week 1/zoo-io-v3/Zoo.java create mode 100644 Semester 2/Programming 2/Week 1/zoo-io-v3/ZooApp.class create mode 100644 Semester 2/Programming 2/Week 1/zoo-io-v3/ZooApp.ctxt create mode 100644 Semester 2/Programming 2/Week 1/zoo-io-v3/ZooApp.java create mode 100644 Semester 2/Programming 2/Week 1/zoo-io-v3/animal_data.txt create mode 100644 Semester 2/Programming 2/Week 1/zoo-io-v3/new_animals.txt create mode 100644 Semester 2/Programming 2/Week 1/zoo-io-v3/package.bluej diff --git a/.obsidian/workspace.json b/.obsidian/workspace.json index d536cb0..71cc981 100644 --- a/.obsidian/workspace.json +++ b/.obsidian/workspace.json @@ -175,27 +175,29 @@ "switcher:Open quick switcher": false } }, - "active": "671043c113c40804", + "active": "13f4c5e19c9ab3c8", "lastOpenFiles": [ + "Semester 2/Programming 2/Week 1/java-apps/HelloWorld/HelloWorldApp.class", + "Semester 2/Programming 2/Week 1/java-apps/HelloWorld/start.sh", + "Semester 2/Programming 2/Week 1/java-apps/HelloWorld/HelloWorldApp.java", + "Semester 2/Programming 2/Week 1/java-apps/HelloWorld", + "Semester 2/Programming 2/Week 1/java-apps", + "Semester 2/Programming 2/Week 1/zoo-io-v3/ZooApp.java#", + "Semester 2/Programming 2/Week 1/zoo-io-v3/__SHELL31.class", + "Semester 2/Programming 2/Week 1/zoo-io-v3/__SHELL31$1.class", + "Semester 2/Programming 2/Week 1/zoo-io-v3/__SHELL31.java", + "Semester 2/Programming 2/Week 1/zoo-io-v3/__SHELL30.class", + "Semester 2/Programming 2/Week 1/zoo-io-v3/__SHELL30$1.class", + "Semester 2/Computer Systems Internals & Linux/Week 1/workshop/testdir/test.md", + "Semester 2/Computer Systems Internals & Linux/Week 1/Week 1 Computer Systems Internals.md", "Semester 1/Database Systems/Week 11/Week 11 Database Systems.md", "Semester 1/Database Systems/Week 10/Week 10 Database Systems.md", "Semester 2/Database Systems/Week 1/Week 1 Database Systems.md", "Semester 1/Database Systems/Assignment/Querying Database.md", - "Semester 2/Database Systems/Week 1", - "Semester 2/Computer Systems Internals & Linux/Week 1/Week 1 Computer Systems Internals.md", - "Semester 2/Database Systems", "README.md", "Semester 2/Programming 2/Week 1/Week 1 Programming 2.md", - "Semester 2/Computer Systems Internals & Linux/Week 1", - "Semester 2/Programming 2/Week 1", - "Semester 2/Computer Systems Internals & Linux", - "Semester 2/Programming 2", - "Semester 2", - "Semester 1", "2023-10-31.md", "2024-01-15.md", - "Semester 1/Web Development and HCI/Assignment.odt", - "Web Development and HCI/lu11370hmnm.tmp", "Semester 1/Web Development and HCI/Assignment 1/src/images/proxmox-03.jpg", "Semester 1/Web Development and HCI/Assignment 1/src/images/proxmox-04.jpg", "Web Development and HCI/Assignment 1/src/images/Screenshot from 2023-12-07 18-29-51.png", @@ -221,7 +223,6 @@ "Semester 1/Computer Systems Internals & Linux/Coursework 1/Task 5.md", "Semester 1/Computer Systems Internals & Linux/Coursework 1/Task 3.md", "Semester 1/Computer Systems Internals & Linux/Coursework 1/Task 1.md", - "Semester 1/Computer Systems Internals & Linux/Coursework 1/Task 2.md", "Database Systems/Untitled.canvas", "Untitled 1.canvas", "Untitled.canvas" diff --git a/Semester 2/Computer Systems Internals & Linux/Week 1/workshop/delete.sh b/Semester 2/Computer Systems Internals & Linux/Week 1/workshop/delete.sh new file mode 100755 index 0000000..028a30c --- /dev/null +++ b/Semester 2/Computer Systems Internals & Linux/Week 1/workshop/delete.sh @@ -0,0 +1,10 @@ +#!/bin/bash +if [ $# -eq 1 ] && [ -f $1 ]; then + mv $1 ./wastebasket/$1 + echo "file $1 moved to ./wastebasket/$1" +elif [ $# -eq 1] && [ -d $1 ]; then + mv -R $1 ./wastebasket/$1 + echo "directory $1 moved to ./wastebasket/$1" +else + echo "More than one file supplied" +fi diff --git a/Semester 2/Computer Systems Internals & Linux/Week 1/workshop/script1.sh b/Semester 2/Computer Systems Internals & Linux/Week 1/workshop/script1.sh new file mode 100755 index 0000000..8ddeff1 --- /dev/null +++ b/Semester 2/Computer Systems Internals & Linux/Week 1/workshop/script1.sh @@ -0,0 +1,14 @@ +#!/bin/bash +echo -n Enter num: +read num +if [ "$num" = "" ]; then + echo No input +elif echo $num | grep -Eq '[^0-9]'; then + echo Not an integer +elif [ $num -gt 5 ]; then + echo Greater than 5 +elif [ $num -eq 5 ]; then + echo Equal to 5 +else + echo Less than 5 +fi diff --git a/Semester 2/Computer Systems Internals & Linux/Week 1/workshop/script2.sh b/Semester 2/Computer Systems Internals & Linux/Week 1/workshop/script2.sh new file mode 100755 index 0000000..5b52938 --- /dev/null +++ b/Semester 2/Computer Systems Internals & Linux/Week 1/workshop/script2.sh @@ -0,0 +1,9 @@ +#!/bin/bash +for file in ~/*; do #For every item in the user's home directory. + if [ -d "$file" ]; then + echo $file is a directory + elif [ -f "$file" ]; then + lines=$(cat "$file" | wc -l) + echo $file is a file with $lines lines + fi +done diff --git a/Semester 2/Computer Systems Internals & Linux/Week 1/workshop/script3.sh b/Semester 2/Computer Systems Internals & Linux/Week 1/workshop/script3.sh new file mode 100755 index 0000000..596b201 --- /dev/null +++ b/Semester 2/Computer Systems Internals & Linux/Week 1/workshop/script3.sh @@ -0,0 +1,6 @@ +#!/bin/bash +if [ $# -eq 2 ]; then + echo "2 arguments (\"$1\" and \"$2\")" +else + echo Please give me 2 arguments +fi diff --git a/Semester 2/Computer Systems Internals & Linux/Week 1/workshop/testdir/test.md b/Semester 2/Computer Systems Internals & Linux/Week 1/workshop/testdir/test.md new file mode 100644 index 0000000..e69de29 diff --git a/Semester 2/Computer Systems Internals & Linux/Week 1/workshop/testdir/test.txt b/Semester 2/Computer Systems Internals & Linux/Week 1/workshop/testdir/test.txt new file mode 100644 index 0000000..e69de29 diff --git a/Semester 2/Computer Systems Internals & Linux/Week 1/workshop/wastebasket/test.txt b/Semester 2/Computer Systems Internals & Linux/Week 1/workshop/wastebasket/test.txt new file mode 100644 index 0000000..e69de29 diff --git a/Semester 2/Database Systems/Week 1/Week 1 Database Systems.md b/Semester 2/Database Systems/Week 1/Week 1 Database Systems.md index f82b146..447fcc2 100644 --- a/Semester 2/Database Systems/Week 1/Week 1 Database Systems.md +++ b/Semester 2/Database Systems/Week 1/Week 1 Database Systems.md @@ -1,7 +1,10 @@ -### [Relational Algebra - Recap](obsidian://open?vault=Computer%20Science%20Year%201&file=Semester%201%2FDatabase%20Systems%2FWeek%2010%2FWeek%2010) +### [Relational Algebra - Recap](obsidian://open?vault=Computer%20Science%20Year%201&file=Semester%201%2FDatabase%20Systems%2FWeek%2010%2FWeek%2010%20Database%20Systems) - High level & procedural - Defines how to build relations +- SELECT represented by pi ( projection ) +- FROM represented by sigma ( selection ) +- WHERE represented by x ( cartesian product ) #### Data Manipulation Language @@ -14,3 +17,21 @@ - Operations work on one or more relations to define another relation without changing the original - Both operands and results are relations, output from one operation can become the input for another operation. - This allows nested expressions. + +#### Theta Join + +R theta(f) S defines a relation that contains tuples satisfying F from the Cartesian product of two relations R and S. +Takes the Cartesian product of two relations r and s than selects the rows which satisfy the condition F. +R theta(f) S is the same as sigma(f) (R x S ). +F may contain comparison operators to form a condition. + +#### Query Processing +The activities involved in parsing, validating, optimising and executing a query. +Purpose is to transform a query written in a high level language into correct and efficient strategy in low level language. Also to execute strategy to retrieve data. + +#### Common Optimisation Rules +- Begin with initial query tree for SQL +- Move SELECT operations down the tree +- Apply more restrictive SELECT operations first ( eg. equalities before range queries ) +- Replace Cartesian products followed by selection with theta joins ( eg. *sigma(f) ( RxS )* -> *R theta(f) S* ) +- Move PROJECT operations down the query tree ( add project operations as inputs to theta joins ) \ No newline at end of file diff --git a/Semester 2/Programming 2/Week 1/java-apps/HelloWorld/HelloWorldApp.class b/Semester 2/Programming 2/Week 1/java-apps/HelloWorld/HelloWorldApp.class new file mode 100644 index 0000000000000000000000000000000000000000..d933ad6d71eda56447034155e7c962faf20c72fe GIT binary patch literal 432 zcmZvZ%SyvQ6o&tkUM7jDt*Ncof?X6+7xn?Ff(YWGs8Fd3S7|$xDY+37!N<~tf(swO zhZ4`kg%moAbD8=7kHeYopU*DG5KOCCaBskYH2;!rMI1T1q z8x3p;xDJ}wCN$T$2qMvpAY!cd$1MhAsEQWD{zRZns1?|PdXXfA@?|`ig!)KD@+J?b zGQIbw0pFBEUooofJ+5OB>~Wl@GkK-7*j=HX=<(1+irM6T@+SS||HQjkk2NMC#*7!xHiJ~a4H(}c%qJ1Il9+h*DFyENgF zVB!z(M;X7FEo$0kl5=Lx%y&6we*J#<3E(aE0t6_{V;(O}c;%xUz(dJ|jXWwQZ~EBs z@mj#Y8mf*~R|3VxrM#B)NXET-Gfi~dI}z|=*;fL&cRJSTdx5>`^sDL0ev8P{L6^uv zL&xfD)NiZgt87QC6dQw%j9N0$<~%NX>4m0;eB(o``!b?(&{Jvi(c5x$e_Dm+`R2?T z%%_9QGl9*)DCwwA+K4S=d`G6W5KplkVgX@@CkO?ePd5Bspecies\ a\ String\ specifying\ the\n\ type\ of\ animal\n\ @param\ \ name\ a\ String\ specifying\ the\ \n\ name\ of\ animal\n +comment1.params= +comment1.target=java.lang.String\ getSpecies() +comment1.text=\n\ Returns\ the\ species\ of\ the\ Animal\ object\n\ \n\ @return\ \ \ the\ species\ of\ animal,\ as\ a\ String\n +comment2.params= +comment2.target=java.lang.String\ getName() +comment2.text=\n\ Returns\ the\ name\ of\ the\ Animal\ object\n\ \n\ @return\ \ \ the\ name\ of\ animal,\ as\ a\ String\n +comment3.params= +comment3.target=java.lang.String\ toString() +comment3.text=\n\ Returns\ a\ string\ representing\ the\ Animal\ object.\ \ For\ a\ \n\ lion\ with\ name\ Leo\ it\ will\ return\ the\ String\ "Leo,\ a\ lion"\n\ \n\ @return\ \ \ a\ String\ representation\ of\ the\ animal\n +numComments=4 diff --git a/Semester 2/Programming 2/Week 1/zoo-io-v1/Animal.java b/Semester 2/Programming 2/Week 1/zoo-io-v1/Animal.java new file mode 100644 index 0000000..6f76a2d --- /dev/null +++ b/Semester 2/Programming 2/Week 1/zoo-io-v1/Animal.java @@ -0,0 +1,59 @@ +/** + * class Animal simulates an animal and stores + * the species and name. + * + * @author D Newton + * @version Version 1, 1 November 2010 + */ +public class Animal +{ + // species of the animal e.g. lion, tiger + private String species; + // name of the animal e.g. Leo, Tommy + private String name; + + /** + * Create an animal of the specified species and with the specified name + * + * @param species a String specifying the + * type of animal + * @param name a String specifying the + * name of animal + */ + public Animal(String species, String name) + { + this.species = species; + this.name = name; + } + + /** + * Returns the species of the Animal object + * + * @return the species of animal, as a String + */ + public String getSpecies() + { + return species; + } + + /** + * Returns the name of the Animal object + * + * @return the name of animal, as a String + */ + public String getName() + { + return name; + } + + /** + * Returns a string representing the Animal object. For a + * lion with name Leo it will return the String "Leo, a lion" + * + * @return a String representation of the animal + */ + public String toString() + { + return name + ", a " + species; + } +} \ No newline at end of file diff --git a/Semester 2/Programming 2/Week 1/zoo-io-v1/README.TXT b/Semester 2/Programming 2/Week 1/zoo-io-v1/README.TXT new file mode 100644 index 0000000..2bea2dd --- /dev/null +++ b/Semester 2/Programming 2/Week 1/zoo-io-v1/README.TXT @@ -0,0 +1,12 @@ +------------------------------------------------------------------------ +This is the project README file. Here, you should describe your project. +Tell the reader (someone who does not know anything about this project) +all he/she needs to know. The comments should usually include at least: +------------------------------------------------------------------------ + +PROJECT TITLE: +PURPOSE OF PROJECT: +VERSION or DATE: +HOW TO START THIS PROJECT: +AUTHORS: +USER INSTRUCTIONS: diff --git a/Semester 2/Programming 2/Week 1/zoo-io-v1/Zoo.class b/Semester 2/Programming 2/Week 1/zoo-io-v1/Zoo.class new file mode 100644 index 0000000000000000000000000000000000000000..efe85b42455322aed0d0194403f989dfeebde30e GIT binary patch literal 4129 zcmaJ^`F|AU9eyU+%}kad2_peRI06Y00wF52KoYQkL}QnPk`xHN7_vh$xS3gZXMtc3 zTcg$kwO$ml)lxOKwTBiqLM&Qa+e&YH*wfzE5B&q$kCr~)nb{*W^@n`ldEa^8=lwq4 zUfIg++(}M7*hQ~B~M#IS%s_5 ztg|sp;&~Nci(x&^$;t~FUX;nhV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ir}`8D&stk<();cRV}1cKc*<-7yen~>eHN0nio@C0*#2hwA=YJcLyo= zVFCSE!uy+90-Kn;Vp2DUUKzN&gs6(LH()5WBr#mbgF{!v*{1pvm{)YKI?Tm_UE`P` z{x6&+;`>;o3Dgs<5eG`HZz{yOspR?y%Ia0#Z5IOd5B8Y^`WQQ;YQR`zG)!P|4oiY* zqtR44U20rbI$dTomrj=(toq{F6~@ZaX{)iSblPaNl};}=t|*;eX{-*W#R6Z&(gMn1W=uZ3U4gU&OMh)otFb7Db;nWDGH?pf$X@EnIM%lsjm6apS*;jHW2>>WxT?yk zI*yrgP?L!^e&G?}81ZX+BiYd>%D^oYo?9`WY?k77wBZi^2X`l1>Mn}mQP$nvY^Qt3 z`CdlgKHP-+aWfw9Ikyn2jgoY|-*9UvnMuY++I%dG!+aLY2pp!}<*JBTu#)1FG~--U zyn*>DZg>-|D$*)4wYC33Wf@<41*LnHkQLnAKZ*4ND<;ssEsR=EYZ$e4dhgCicZuqr zC#o>*oWxba^c~wKv1x#{y}73~hb_mkn8VfW5#L7rzNw4WMS}VrzwkQOw-y;)MlQf9 z+S3$}34T@1U=hyJ?@6Zkd91}bs^<$nvyHrW5x+w>l39d1s{K?(#g_uJ&H}U5Ecy@8 fX9F$xFh0Wk2uEEw8Y*s5@zJI!3_r%VWw`mj_Bo=E literal 0 HcmV?d00001 diff --git a/Semester 2/Programming 2/Week 1/zoo-io-v1/Zoo.ctxt b/Semester 2/Programming 2/Week 1/zoo-io-v1/Zoo.ctxt new file mode 100644 index 0000000..8c75b75 --- /dev/null +++ b/Semester 2/Programming 2/Week 1/zoo-io-v1/Zoo.ctxt @@ -0,0 +1,35 @@ +#BlueJ class context +comment0.params= +comment0.target=Zoo() +comment0.text=\n\ Create\ an\ "empty"\ zoo\n +comment1.params=fileName +comment1.target=Zoo(java.lang.String) +comment1.text=\n\ Create\ a\ zoo\ and\ populate\ it\ using\ data\ from\ a\ text\ file\n +comment10.params=fileName +comment10.target=void\ readAnimalData(java.lang.String) +comment10.text=\n\ Reads\ animal\ data\ from\ a\ file\ and\ adds\ corresponding\ animals\ to\ the\ zoo\n\n\ @param\ \ \ fileName\ a\ String,\ the\ name\ of\ the\ \n\ \ \ \ \ \ \ \ \ \ text\ file\ in\ which\ the\ data\ is\ stored.\n\ \n\ @throws\ \ FileNotFoundException\n +comment2.params=animal +comment2.target=void\ storeAnimal(Animal) +comment2.text=\n\ Adds\ an\ animal\ to\ the\ zoo\n\n\ @param\ \ \ animal\ an\ Animal\ object,\ the\ animal\ to\ be\ added\n +comment3.params=listPosition +comment3.target=void\ showAnimal(int) +comment3.text=\n\ Shows\ an\ animal\ by\ printing\ it's\ details.\ \ This\ includes\n\ it's\ position\ in\ the\ collection.\n\n\ @param\ \ listPosition\ the\ position\ of\ the\ animal\n +comment4.params= +comment4.target=int\ numberOfAnimals() +comment4.text=\n\ Returns\ how\ many\ animals\ are\ stored\ in\ the\ collection\n\n\ @return\ \ \ the\ number\ of\ animals\ in\ the\ collection\n +comment5.params= +comment5.target=void\ showAllAnimals() +comment5.text=\n\ Displays\ all\ the\ animals\ in\ the\ collection\n\n +comment6.params=listPosition +comment6.target=void\ removeAnimal(int) +comment6.text=\n\ Remove\ an\ animal\ from\ the\ collection\n\n\ @param\ \ listPosition\ the\ position\ of\ the\ animal\n +comment7.params= +comment7.target=void\ populate() +comment7.text=\n\ Adds\ a\ pre-defined\ set\ of\ animals\ to\ the\ current\ collection\n\n +comment8.params= +comment8.target=void\ countAnimals() +comment8.text=\n\ Counts\ the\ number\ of\ lions,\ tigers\ and\ elephants\n\n +comment9.params=fileName +comment9.target=void\ writeAnimalData(java.lang.String) +comment9.text=\n\ Writes\ animal\ data\ to\ a\ file\n\n\ @param\ \ \ fileName\ a\ String,\ the\ name\ of\ the\ \n\ \ \ \ \ \ \ \ \ \ text\ file\ in\ which\ the\ data\ will\ be\ stored.\n\ \n\ @throws\ \ FileNotFoundException\n +numComments=11 diff --git a/Semester 2/Programming 2/Week 1/zoo-io-v1/Zoo.java b/Semester 2/Programming 2/Week 1/zoo-io-v1/Zoo.java new file mode 100644 index 0000000..bff3c90 --- /dev/null +++ b/Semester 2/Programming 2/Week 1/zoo-io-v1/Zoo.java @@ -0,0 +1,192 @@ +import java.util.*; +import java.io.*; +/** + * class Zoo simulates storing animals in a collection. + * + * @author D Newton + * */ +public class Zoo +{ + private ArrayList animalCollection; + private int lionCount; + private int tigerCount; + private int elephantCount; + private int otherCount; + + /** + * Create an "empty" zoo + */ + public Zoo() + { + animalCollection = new ArrayList(); + lionCount = 0; + tigerCount = 0; + elephantCount = 0; + otherCount = 0; + } + + /** + * Create a zoo and populate it using data from a text file + */ + public Zoo(String fileName) throws FileNotFoundException + { + this(); + readAnimalData(fileName); + } + + /** + * Adds an animal to the zoo + * + * @param animal an Animal object, the animal to be added + */ + public void storeAnimal(Animal animal) + { + animalCollection.add(animal); + } + + /** + * Shows an animal by printing it's details. This includes + * it's position in the collection. + * + * @param listPosition the position of the animal + */ + public void showAnimal(int listPosition) + { + Animal animal; + if( listPosition < animalCollection.size() ) + { + animal = animalCollection.get(listPosition); + System.out.println("Position " + listPosition + ": " + animal); + } + } + + /** + * Returns how many animals are stored in the collection + * + * @return the number of animals in the collection + */ + public int numberOfAnimals() + { + return animalCollection.size(); + } + + /** + * Displays all the animals in the collection + * + */ + public void showAllAnimals() + { + System.out.println("Zoo"); + System.out.println("==="); + + int listPosition = 0; + while( listPositionlistPosition the position of the animal + */ + public void removeAnimal(int listPosition) + { + if( listPosition>=0 && listPosition it = animalCollection.iterator(); + while( it.hasNext() ) + { + Animal currentAnimal = it.next(); + String species = currentAnimal.getSpecies(); + if( species.equalsIgnoreCase("lion") ) + lionCount++; + else if( species.equalsIgnoreCase("tiger") ) + tigerCount++; + else if( species.equalsIgnoreCase("elephant") ) + elephantCount++; + else + otherCount++; + } + } + + /** + * Writes animal data to a file + * + * @param fileName a String, the name of the + * text file in which the data will be stored. + * + * @throws FileNotFoundException + */ + public void writeAnimalData(String fileName) throws FileNotFoundException + { + PrintWriter pWriter = new PrintWriter(fileName); + + for(Animal a: animalCollection) + { + String lineOfOutput = a.getName() + " " + a.getSpecies(); + pWriter.println(lineOfOutput); + } + pWriter.close(); + } + + /** + * Reads animal data from a file and adds corresponding animals to the zoo + * + * @param fileName a String, the name of the + * text file in which the data is stored. + * + * @throws FileNotFoundException + */ + public void readAnimalData(String fileName) throws FileNotFoundException + { + File dataFile = new File(fileName); + Scanner scanner = new Scanner(dataFile); + + while( scanner.hasNext() ) + { + String name = scanner.next(); + String species = scanner.next(); + storeAnimal( new Animal(species, name) ); + } + + scanner.close(); + } +} \ No newline at end of file diff --git a/Semester 2/Programming 2/Week 1/zoo-io-v1/animal_data.txt b/Semester 2/Programming 2/Week 1/zoo-io-v1/animal_data.txt new file mode 100644 index 0000000..3739046 --- /dev/null +++ b/Semester 2/Programming 2/Week 1/zoo-io-v1/animal_data.txt @@ -0,0 +1,10 @@ +Leo lion +Tommy tiger +Ollie elephant +Roland rat +Rudolph reindeer +Lenny Lion +Nellie Elephant +Tessa tiger +Hetty eLephant +Leslie LION diff --git a/Semester 2/Programming 2/Week 1/zoo-io-v1/new_animals.txt b/Semester 2/Programming 2/Week 1/zoo-io-v1/new_animals.txt new file mode 100644 index 0000000..95f7a14 --- /dev/null +++ b/Semester 2/Programming 2/Week 1/zoo-io-v1/new_animals.txt @@ -0,0 +1,3 @@ +Bobby bear +Andy aardvark +Polly parrot diff --git a/Semester 2/Programming 2/Week 1/zoo-io-v1/package.bluej b/Semester 2/Programming 2/Week 1/zoo-io-v1/package.bluej new file mode 100644 index 0000000..e6cc546 --- /dev/null +++ b/Semester 2/Programming 2/Week 1/zoo-io-v1/package.bluej @@ -0,0 +1,3 @@ +#BlueJ package file +#Thu Jan 18 14:27:41 GMT 2024 +project.charset=UTF-8 diff --git a/Semester 2/Programming 2/Week 1/zoo-io-v2/Animal.class b/Semester 2/Programming 2/Week 1/zoo-io-v2/Animal.class new file mode 100644 index 0000000000000000000000000000000000000000..fab6eb43466c8bbd4832fa35315edf5d6ee5ea17 GIT binary patch literal 787 zcmZuuTTc@~6#iy+>6T@+SS||HQjkk2NMC#*7!xHiJ~a4H(}c%qJ1Il9+h*DFyENgF zVB!z(M;X7FEo$0kl5=Lx%y&6we*J#<3E(aE0t6_{V;(O}c;%xUz(dJ|jXWwQZ~EBs z@mj#Y8mf*~R|3VxrM#B)NXET-Gfi~dI}z|=*;fL&cRJSTdx5>`^sDL0ev8P{L6^uv zL&xfD)NiZgt87QC6dQw%j9N0$<~%NX>4m0;eB(o``!b?(&{Jvi(c5x$e_Dm+`R2?T z%%_9QGl9*)DCwwA+K4S=d`G6W5KplkVgX@@CkO?ePd5Bspecies\ a\ String\ specifying\ the\n\ type\ of\ animal\n\ @param\ \ name\ a\ String\ specifying\ the\ \n\ name\ of\ animal\n +comment1.params= +comment1.target=java.lang.String\ getSpecies() +comment1.text=\n\ Returns\ the\ species\ of\ the\ Animal\ object\n\ \n\ @return\ \ \ the\ species\ of\ animal,\ as\ a\ String\n +comment2.params= +comment2.target=java.lang.String\ getName() +comment2.text=\n\ Returns\ the\ name\ of\ the\ Animal\ object\n\ \n\ @return\ \ \ the\ name\ of\ animal,\ as\ a\ String\n +comment3.params= +comment3.target=java.lang.String\ toString() +comment3.text=\n\ Returns\ a\ string\ representing\ the\ Animal\ object.\ \ For\ a\ \n\ lion\ with\ name\ Leo\ it\ will\ return\ the\ String\ "Leo,\ a\ lion"\n\ \n\ @return\ \ \ a\ String\ representation\ of\ the\ animal\n +numComments=4 diff --git a/Semester 2/Programming 2/Week 1/zoo-io-v2/Animal.java b/Semester 2/Programming 2/Week 1/zoo-io-v2/Animal.java new file mode 100644 index 0000000..6f76a2d --- /dev/null +++ b/Semester 2/Programming 2/Week 1/zoo-io-v2/Animal.java @@ -0,0 +1,59 @@ +/** + * class Animal simulates an animal and stores + * the species and name. + * + * @author D Newton + * @version Version 1, 1 November 2010 + */ +public class Animal +{ + // species of the animal e.g. lion, tiger + private String species; + // name of the animal e.g. Leo, Tommy + private String name; + + /** + * Create an animal of the specified species and with the specified name + * + * @param species a String specifying the + * type of animal + * @param name a String specifying the + * name of animal + */ + public Animal(String species, String name) + { + this.species = species; + this.name = name; + } + + /** + * Returns the species of the Animal object + * + * @return the species of animal, as a String + */ + public String getSpecies() + { + return species; + } + + /** + * Returns the name of the Animal object + * + * @return the name of animal, as a String + */ + public String getName() + { + return name; + } + + /** + * Returns a string representing the Animal object. For a + * lion with name Leo it will return the String "Leo, a lion" + * + * @return a String representation of the animal + */ + public String toString() + { + return name + ", a " + species; + } +} \ No newline at end of file diff --git a/Semester 2/Programming 2/Week 1/zoo-io-v2/README.TXT b/Semester 2/Programming 2/Week 1/zoo-io-v2/README.TXT new file mode 100644 index 0000000..2bea2dd --- /dev/null +++ b/Semester 2/Programming 2/Week 1/zoo-io-v2/README.TXT @@ -0,0 +1,12 @@ +------------------------------------------------------------------------ +This is the project README file. Here, you should describe your project. +Tell the reader (someone who does not know anything about this project) +all he/she needs to know. The comments should usually include at least: +------------------------------------------------------------------------ + +PROJECT TITLE: +PURPOSE OF PROJECT: +VERSION or DATE: +HOW TO START THIS PROJECT: +AUTHORS: +USER INSTRUCTIONS: diff --git a/Semester 2/Programming 2/Week 1/zoo-io-v2/Zoo.class b/Semester 2/Programming 2/Week 1/zoo-io-v2/Zoo.class new file mode 100644 index 0000000000000000000000000000000000000000..ec021b2cf0d30f9d69b8bb86a8ca629508979b67 GIT binary patch literal 4254 zcmaJ^d3+RS8GdH7o0%*_2wMUTfszuA5E4QuT1W_$D>QXUkVIm@YKPq+89Le7?#^;q zty)^_L0fArVo$Mcs@9_wmbPgrsCd+)iuZx{Rka>}>aP`_cV_koM*rCNJLdb|_kG^u znc0{A{oF|a%khsWuEs$XABZA^gH`Cpts-s{al42+L>!7z929Y05t>H5oKC9t#8lKYdw1&@XIHuvahP;Lo8lDkV&rZT~7!&cl z=z1ZFlXy|ZsVG+BwCsFA!xv@opolN2I1|N{IIZH#QLMpNG<;RYu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43{Zrdct! zmKb7n^i;-~94l6pz*aXCQvCr%89o1M39AQl7GYipaV|7!?4{hykLWFg+=tn;IG4$3 zqCacsM=7am1FvFOS_V`_=rzQYT7tN)n1>Wy6=&zw-;bFk_of87m_0CxN#g${6GVIm zt2BvvhOQC2%FNFz#(7?u`7lEDDs9`vK)u2~lRzJ4hg1*7=ENFK;L<$i`pd@Hyz=Gz z*n;xq!dO%Ja#4&`U%I6J~jV&o(Hpbe@mrG-pl`ogYF87zE0$;+?0y2b~Wiz@g z^Bh)THz{Hd+i)){XCLK$9K^eDA8shCkHf|K7%9M3E@yp6!5)~14g8v09$)+{-k{(J z3{KvtKw5&Oe??DgY)&34j-aM_@E9WDYsn|0SlJqDEbUgxZsjN%TVwM}yQ=J}qnIog zHCbq*=N}M^VXwAV6CHgb4ctuPxdpR`WUc)UXfjXoVY4Za?9A>dt(zu>!&u~}B%-_j!Ce1hlm9Jrz z3ioxis>rF>U0eH4RE79>Pm$@)Qe`C%_m5%a;NlZ#-xNfxr!|P$3Qqi}wTA;5sUyjB zu(M*TC}eSk-)8NRmbyqDoulW^Fu&7gYbk0csVP$ zi@xk7%m_~{WFaKHLPVvzobXc zpXJl5N+6dp2f#6cJWfu@^F{Lv8u-t70mew^&tn;0AhDjrM!d-EpW=(~vag&MzOUDJ^WfileName\ a\ String,\ the\ name\ of\ the\ \n\ \ \ \ \ \ \ \ \ \ text\ file\ in\ which\ the\ data\ is\ stored.\n\ \n\ @throws\ \ FileNotFoundException\n +comment2.params=animal +comment2.target=void\ storeAnimal(Animal) +comment2.text=\n\ Adds\ an\ animal\ to\ the\ zoo\n\n\ @param\ \ \ animal\ an\ Animal\ object,\ the\ animal\ to\ be\ added\n +comment3.params=listPosition +comment3.target=void\ showAnimal(int) +comment3.text=\n\ Shows\ an\ animal\ by\ printing\ it's\ details.\ \ This\ includes\n\ it's\ position\ in\ the\ collection.\n\n\ @param\ \ listPosition\ the\ position\ of\ the\ animal\n +comment4.params= +comment4.target=int\ numberOfAnimals() +comment4.text=\n\ Returns\ how\ many\ animals\ are\ stored\ in\ the\ collection\n\n\ @return\ \ \ the\ number\ of\ animals\ in\ the\ collection\n +comment5.params= +comment5.target=void\ showAllAnimals() +comment5.text=\n\ Displays\ all\ the\ animals\ in\ the\ collection\n\n +comment6.params=listPosition +comment6.target=void\ removeAnimal(int) +comment6.text=\n\ Remove\ an\ animal\ from\ the\ collection\n\n\ @param\ \ listPosition\ the\ position\ of\ the\ animal\n +comment7.params= +comment7.target=void\ populate() +comment7.text=\n\ Adds\ a\ pre-defined\ set\ of\ animals\ to\ the\ current\ collection\n\n +comment8.params= +comment8.target=void\ countAnimals() +comment8.text=\n\ Counts\ the\ number\ of\ lions,\ tigers\ and\ elephants\n\n +comment9.params=fileName +comment9.target=void\ writeAnimalData(java.lang.String) +comment9.text=\n\ Writes\ animal\ data\ to\ a\ file\n\n\ @param\ \ \ fileName\ a\ String,\ the\ name\ of\ the\ \n\ \ \ \ \ \ \ \ \ \ text\ file\ in\ which\ the\ data\ will\ be\ stored.\n\ \n\ @throws\ \ FileNotFoundException\n +numComments=11 diff --git a/Semester 2/Programming 2/Week 1/zoo-io-v2/Zoo.java b/Semester 2/Programming 2/Week 1/zoo-io-v2/Zoo.java new file mode 100644 index 0000000..e5f6a99 --- /dev/null +++ b/Semester 2/Programming 2/Week 1/zoo-io-v2/Zoo.java @@ -0,0 +1,194 @@ +import java.util.*; +import java.io.*; +/** + * class Zoo simulates storing animals in a collection. + * + * @author D Newton + * */ +public class Zoo +{ + private ArrayList animalCollection; + private int lionCount; + private int tigerCount; + private int elephantCount; + private int otherCount; + + /** + * Create an "empty" zoo + */ + public Zoo() + { + animalCollection = new ArrayList(); + lionCount = 0; + tigerCount = 0; + elephantCount = 0; + otherCount = 0; + } + + /** + * Create a zoo and populate it using data from a text file + */ + public Zoo(String fileName) throws FileNotFoundException + { + this(); + readAnimalData(fileName); + } + + /** + * Adds an animal to the zoo + * + * @param animal an Animal object, the animal to be added + */ + public void storeAnimal(Animal animal) + { + animalCollection.add(animal); + } + + /** + * Shows an animal by printing it's details. This includes + * it's position in the collection. + * + * @param listPosition the position of the animal + */ + public void showAnimal(int listPosition) + { + Animal animal; + if( listPosition < animalCollection.size() ) + { + animal = animalCollection.get(listPosition); + System.out.println("Position " + listPosition + ": " + animal); + } + } + + /** + * Returns how many animals are stored in the collection + * + * @return the number of animals in the collection + */ + public int numberOfAnimals() + { + return animalCollection.size(); + } + + /** + * Displays all the animals in the collection + * + */ + public void showAllAnimals() + { + System.out.println("Zoo"); + System.out.println("==="); + + int listPosition = 0; + while( listPositionlistPosition the position of the animal + */ + public void removeAnimal(int listPosition) + { + if( listPosition>=0 && listPosition it = animalCollection.iterator(); + while( it.hasNext() ) + { + Animal currentAnimal = it.next(); + String species = currentAnimal.getSpecies(); + if( species.equalsIgnoreCase("lion") ) + lionCount++; + else if( species.equalsIgnoreCase("tiger") ) + tigerCount++; + else if( species.equalsIgnoreCase("elephant") ) + elephantCount++; + else + otherCount++; + } + } + + /** + * Writes animal data to a file + * + * @param fileName a String, the name of the + * text file in which the data will be stored. + * + * @throws FileNotFoundException + */ + public void writeAnimalData(String fileName) throws FileNotFoundException + { + PrintWriter pWriter = new PrintWriter(fileName); + + for(Animal a: animalCollection) + { + String lineOfOutput = a.getName() + "," + a.getSpecies(); + pWriter.println(lineOfOutput); + } + pWriter.close(); + } + + /** + * Reads animal data from a file and adds corresponding animals to the zoo + * + * @param fileName a String, the name of the + * text file in which the data is stored. + * + * @throws FileNotFoundException + */ + public void readAnimalData(String fileName) throws FileNotFoundException + { + File dataFile = new File(fileName); + Scanner scanner = new Scanner(dataFile); + scanner.useDelimiter("[,\n]"); + + while( scanner.hasNext() ) + { + String name = scanner.next(); + name.trim(); + String species = scanner.next(); + species.trim(); + storeAnimal( new Animal(species, name) ); + } + scanner.close(); + } +} \ No newline at end of file diff --git a/Semester 2/Programming 2/Week 1/zoo-io-v2/animal_data.txt b/Semester 2/Programming 2/Week 1/zoo-io-v2/animal_data.txt new file mode 100644 index 0000000..077e4e6 --- /dev/null +++ b/Semester 2/Programming 2/Week 1/zoo-io-v2/animal_data.txt @@ -0,0 +1,10 @@ +Leo, Lion +Tommy, Tiger +Ollie, Elephant +Roland, Rat +Rudolph, Reindeer +Lenny, Lion +Nellie, Elephant +Tessa, Tiger +Hetty, Elephant +Leslie, Lion diff --git a/Semester 2/Programming 2/Week 1/zoo-io-v2/new_animals.txt b/Semester 2/Programming 2/Week 1/zoo-io-v2/new_animals.txt new file mode 100644 index 0000000..0207d8e --- /dev/null +++ b/Semester 2/Programming 2/Week 1/zoo-io-v2/new_animals.txt @@ -0,0 +1,3 @@ +Bobby, Bear +Andy, Aardvark +Polly, African Grey Parrot diff --git a/Semester 2/Programming 2/Week 1/zoo-io-v2/package.bluej b/Semester 2/Programming 2/Week 1/zoo-io-v2/package.bluej new file mode 100644 index 0000000..db83f6c --- /dev/null +++ b/Semester 2/Programming 2/Week 1/zoo-io-v2/package.bluej @@ -0,0 +1,36 @@ +#BlueJ package file +dependency1.from=Zoo +dependency1.to=Animal +dependency1.type=UsesDependency +objectbench.height=76 +objectbench.width=686 +package.editor.height=400 +package.editor.width=560 +package.editor.x=774 +package.editor.y=277 +package.numDependencies=1 +package.numTargets=2 +package.showExtends=true +package.showUses=true +project.charset=UTF-8 +target1.height=50 +target1.name=Animal +target1.showInterface=false +target1.type=ClassTarget +target1.typeParameters= +target1.width=80 +target1.x=90 +target1.y=10 +target2.editor.height=700 +target2.editor.width=900 +target2.editor.x=814 +target2.editor.y=297 +target2.height=50 +target2.name=Zoo +target2.naviview.expanded=true +target2.showInterface=false +target2.type=ClassTarget +target2.typeParameters= +target2.width=80 +target2.x=180 +target2.y=10 diff --git a/Semester 2/Programming 2/Week 1/zoo-io-v3/Animal.class b/Semester 2/Programming 2/Week 1/zoo-io-v3/Animal.class new file mode 100644 index 0000000000000000000000000000000000000000..fab6eb43466c8bbd4832fa35315edf5d6ee5ea17 GIT binary patch literal 787 zcmZuuTTc@~6#iy+>6T@+SS||HQjkk2NMC#*7!xHiJ~a4H(}c%qJ1Il9+h*DFyENgF zVB!z(M;X7FEo$0kl5=Lx%y&6we*J#<3E(aE0t6_{V;(O}c;%xUz(dJ|jXWwQZ~EBs z@mj#Y8mf*~R|3VxrM#B)NXET-Gfi~dI}z|=*;fL&cRJSTdx5>`^sDL0ev8P{L6^uv zL&xfD)NiZgt87QC6dQw%j9N0$<~%NX>4m0;eB(o``!b?(&{Jvi(c5x$e_Dm+`R2?T z%%_9QGl9*)DCwwA+K4S=d`G6W5KplkVgX@@CkO?ePd5Bspecies\ a\ String\ specifying\ the\n\ type\ of\ animal\n\ @param\ \ name\ a\ String\ specifying\ the\ \n\ name\ of\ animal\n +comment1.params= +comment1.target=java.lang.String\ getSpecies() +comment1.text=\n\ Returns\ the\ species\ of\ the\ Animal\ object\n\ \n\ @return\ \ \ the\ species\ of\ animal,\ as\ a\ String\n +comment2.params= +comment2.target=java.lang.String\ getName() +comment2.text=\n\ Returns\ the\ name\ of\ the\ Animal\ object\n\ \n\ @return\ \ \ the\ name\ of\ animal,\ as\ a\ String\n +comment3.params= +comment3.target=java.lang.String\ toString() +comment3.text=\n\ Returns\ a\ string\ representing\ the\ Animal\ object.\ \ For\ a\ \n\ lion\ with\ name\ Leo\ it\ will\ return\ the\ String\ "Leo,\ a\ lion"\n\ \n\ @return\ \ \ a\ String\ representation\ of\ the\ animal\n +numComments=4 diff --git a/Semester 2/Programming 2/Week 1/zoo-io-v3/Animal.java b/Semester 2/Programming 2/Week 1/zoo-io-v3/Animal.java new file mode 100644 index 0000000..6f76a2d --- /dev/null +++ b/Semester 2/Programming 2/Week 1/zoo-io-v3/Animal.java @@ -0,0 +1,59 @@ +/** + * class Animal simulates an animal and stores + * the species and name. + * + * @author D Newton + * @version Version 1, 1 November 2010 + */ +public class Animal +{ + // species of the animal e.g. lion, tiger + private String species; + // name of the animal e.g. Leo, Tommy + private String name; + + /** + * Create an animal of the specified species and with the specified name + * + * @param species a String specifying the + * type of animal + * @param name a String specifying the + * name of animal + */ + public Animal(String species, String name) + { + this.species = species; + this.name = name; + } + + /** + * Returns the species of the Animal object + * + * @return the species of animal, as a String + */ + public String getSpecies() + { + return species; + } + + /** + * Returns the name of the Animal object + * + * @return the name of animal, as a String + */ + public String getName() + { + return name; + } + + /** + * Returns a string representing the Animal object. For a + * lion with name Leo it will return the String "Leo, a lion" + * + * @return a String representation of the animal + */ + public String toString() + { + return name + ", a " + species; + } +} \ No newline at end of file diff --git a/Semester 2/Programming 2/Week 1/zoo-io-v3/README.TXT b/Semester 2/Programming 2/Week 1/zoo-io-v3/README.TXT new file mode 100644 index 0000000..2bea2dd --- /dev/null +++ b/Semester 2/Programming 2/Week 1/zoo-io-v3/README.TXT @@ -0,0 +1,12 @@ +------------------------------------------------------------------------ +This is the project README file. Here, you should describe your project. +Tell the reader (someone who does not know anything about this project) +all he/she needs to know. The comments should usually include at least: +------------------------------------------------------------------------ + +PROJECT TITLE: +PURPOSE OF PROJECT: +VERSION or DATE: +HOW TO START THIS PROJECT: +AUTHORS: +USER INSTRUCTIONS: diff --git a/Semester 2/Programming 2/Week 1/zoo-io-v3/Zoo.class b/Semester 2/Programming 2/Week 1/zoo-io-v3/Zoo.class new file mode 100644 index 0000000000000000000000000000000000000000..ec021b2cf0d30f9d69b8bb86a8ca629508979b67 GIT binary patch literal 4254 zcmaJ^d3+RS8GdH7o0%*_2wMUTfszuA5E4QuT1W_$D>QXUkVIm@YKPq+89Le7?#^;q zty)^_L0fArVo$Mcs@9_wmbPgrsCd+)iuZx{Rka>}>aP`_cV_koM*rCNJLdb|_kG^u znc0{A{oF|a%khsWuEs$XABZA^gH`Cpts-s{al42+L>!7z929Y05t>H5oKC9t#8lKYdw1&@XIHuvahP;Lo8lDkV&rZT~7!&cl z=z1ZFlXy|ZsVG+BwCsFA!xv@opolN2I1|N{IIZH#QLMpNG<;RYuZb8F@scR`y3Ba? zAr;?HP-CR55yM{R*tVH)EhnX*HonW)ZM5ZF%Whkf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LWFg+=tn;IG4$3 zqCacsM=7am1FvFOS_V`_=rzQYT7tN)n1>Wy6=&zw-;bFk_of87m_0CxN#g${6GVIm zt2BvvhOQC2%FNFz#(7?u`7lEDDs9`vK)u2~lRzJ4hg1*7=ENFK;L<$i`pd@Hyz=Gz z*n;xq!dO%Ja#4&`U%I6J~jV&o(Hpbe@mrG-pl`ogYF87zE0$;+?0y2b~Wiz@g z^Bh)THz{Hd+i)){XCLK$9K^eDA8shCkHf|K7%9M3E@yp6!5)~14g8v09$)+{-k{(J z3{KvtKw5&Oe??DgY)&34j-aM_@E9WDYsn|0SlJqDEbUgxZsjN%TVwM}yQ=J}qnIog zHCbq*=N}M^VXwAV6CHgb4ctuPxdpR`WUc)UXfjXoVY4Za?9A>dt(zu>!&u~}B%-_j!Ce1hlm9Jrz z3ioxis>rF>U0eH4RE79>Pm$@)Qe`C%_m5%a;NlZ#-xNfxr!|P$3Qqi}wTA;5sUyjB zu(M*TC}eSk-)8NRmbyqDoulW^Fu&7gYbk0csVP$ zi@xk7%m_~{WFaKHLPVvzobXc zpXJl5N+6dp2f#6cJWfu@^F{Lv8u-t70mew^&tn;0AhDjrM!d-EpW=(~vag&MzOUDJ^WfileName\ a\ String,\ the\ name\ of\ the\ \n\ \ \ \ \ \ \ \ \ \ text\ file\ in\ which\ the\ data\ is\ stored.\n\ \n\ @throws\ \ FileNotFoundException\n +comment2.params=animal +comment2.target=void\ storeAnimal(Animal) +comment2.text=\n\ Adds\ an\ animal\ to\ the\ zoo\n\n\ @param\ \ \ animal\ an\ Animal\ object,\ the\ animal\ to\ be\ added\n +comment3.params=listPosition +comment3.target=void\ showAnimal(int) +comment3.text=\n\ Shows\ an\ animal\ by\ printing\ it's\ details.\ \ This\ includes\n\ it's\ position\ in\ the\ collection.\n\n\ @param\ \ listPosition\ the\ position\ of\ the\ animal\n +comment4.params= +comment4.target=int\ numberOfAnimals() +comment4.text=\n\ Returns\ how\ many\ animals\ are\ stored\ in\ the\ collection\n\n\ @return\ \ \ the\ number\ of\ animals\ in\ the\ collection\n +comment5.params= +comment5.target=void\ showAllAnimals() +comment5.text=\n\ Displays\ all\ the\ animals\ in\ the\ collection\n\n +comment6.params=listPosition +comment6.target=void\ removeAnimal(int) +comment6.text=\n\ Remove\ an\ animal\ from\ the\ collection\n\n\ @param\ \ listPosition\ the\ position\ of\ the\ animal\n +comment7.params= +comment7.target=void\ populate() +comment7.text=\n\ Adds\ a\ pre-defined\ set\ of\ animals\ to\ the\ current\ collection\n\n +comment8.params= +comment8.target=void\ countAnimals() +comment8.text=\n\ Counts\ the\ number\ of\ lions,\ tigers\ and\ elephants\n\n +comment9.params=fileName +comment9.target=void\ writeAnimalData(java.lang.String) +comment9.text=\n\ Writes\ animal\ data\ to\ a\ file\n\n\ @param\ \ \ fileName\ a\ String,\ the\ name\ of\ the\ \n\ \ \ \ \ \ \ \ \ \ text\ file\ in\ which\ the\ data\ will\ be\ stored.\n\ \n\ @throws\ \ FileNotFoundException\n +numComments=11 diff --git a/Semester 2/Programming 2/Week 1/zoo-io-v3/Zoo.java b/Semester 2/Programming 2/Week 1/zoo-io-v3/Zoo.java new file mode 100644 index 0000000..e5f6a99 --- /dev/null +++ b/Semester 2/Programming 2/Week 1/zoo-io-v3/Zoo.java @@ -0,0 +1,194 @@ +import java.util.*; +import java.io.*; +/** + * class Zoo simulates storing animals in a collection. + * + * @author D Newton + * */ +public class Zoo +{ + private ArrayList animalCollection; + private int lionCount; + private int tigerCount; + private int elephantCount; + private int otherCount; + + /** + * Create an "empty" zoo + */ + public Zoo() + { + animalCollection = new ArrayList(); + lionCount = 0; + tigerCount = 0; + elephantCount = 0; + otherCount = 0; + } + + /** + * Create a zoo and populate it using data from a text file + */ + public Zoo(String fileName) throws FileNotFoundException + { + this(); + readAnimalData(fileName); + } + + /** + * Adds an animal to the zoo + * + * @param animal an Animal object, the animal to be added + */ + public void storeAnimal(Animal animal) + { + animalCollection.add(animal); + } + + /** + * Shows an animal by printing it's details. This includes + * it's position in the collection. + * + * @param listPosition the position of the animal + */ + public void showAnimal(int listPosition) + { + Animal animal; + if( listPosition < animalCollection.size() ) + { + animal = animalCollection.get(listPosition); + System.out.println("Position " + listPosition + ": " + animal); + } + } + + /** + * Returns how many animals are stored in the collection + * + * @return the number of animals in the collection + */ + public int numberOfAnimals() + { + return animalCollection.size(); + } + + /** + * Displays all the animals in the collection + * + */ + public void showAllAnimals() + { + System.out.println("Zoo"); + System.out.println("==="); + + int listPosition = 0; + while( listPositionlistPosition the position of the animal + */ + public void removeAnimal(int listPosition) + { + if( listPosition>=0 && listPosition it = animalCollection.iterator(); + while( it.hasNext() ) + { + Animal currentAnimal = it.next(); + String species = currentAnimal.getSpecies(); + if( species.equalsIgnoreCase("lion") ) + lionCount++; + else if( species.equalsIgnoreCase("tiger") ) + tigerCount++; + else if( species.equalsIgnoreCase("elephant") ) + elephantCount++; + else + otherCount++; + } + } + + /** + * Writes animal data to a file + * + * @param fileName a String, the name of the + * text file in which the data will be stored. + * + * @throws FileNotFoundException + */ + public void writeAnimalData(String fileName) throws FileNotFoundException + { + PrintWriter pWriter = new PrintWriter(fileName); + + for(Animal a: animalCollection) + { + String lineOfOutput = a.getName() + "," + a.getSpecies(); + pWriter.println(lineOfOutput); + } + pWriter.close(); + } + + /** + * Reads animal data from a file and adds corresponding animals to the zoo + * + * @param fileName a String, the name of the + * text file in which the data is stored. + * + * @throws FileNotFoundException + */ + public void readAnimalData(String fileName) throws FileNotFoundException + { + File dataFile = new File(fileName); + Scanner scanner = new Scanner(dataFile); + scanner.useDelimiter("[,\n]"); + + while( scanner.hasNext() ) + { + String name = scanner.next(); + name.trim(); + String species = scanner.next(); + species.trim(); + storeAnimal( new Animal(species, name) ); + } + scanner.close(); + } +} \ No newline at end of file diff --git a/Semester 2/Programming 2/Week 1/zoo-io-v3/ZooApp.class b/Semester 2/Programming 2/Week 1/zoo-io-v3/ZooApp.class new file mode 100644 index 0000000000000000000000000000000000000000..68f79e91a515d59c2a0390233c5706803433ddbb GIT binary patch literal 636 zcmZWmT~8B16g{(DI%T`^(Na;g2#8pqwlBV6Od6G^iY7HA@M5Oz0zrF_BW+ z^i+JPM%u-{LEoO~RG`p4Hl{Tk?y>$vXYBvf1#(}E5twmgGJ`jhUNp4YB&oy`#vL;r z^`iZo`eG*7a4I2+*phe}qK0jWoe;YSsYXhOI<`1%5?y>$i5{ppZPHnRG>NU5GTZNs^sf`9qm@&&M+4flfo~yH5V5A28 zsq!D$Td;r}I#Qen%%x}Mq7}!jY*+#;>}SzyWfj&j%jQ{?LE2cAodYK&3^LQNl896-(?>VFp;sRLNLm aM169~>|ADci5w4iiTXsA`Q78$!~JUp!icQ^ literal 0 HcmV?d00001 diff --git a/Semester 2/Programming 2/Week 1/zoo-io-v3/ZooApp.ctxt b/Semester 2/Programming 2/Week 1/zoo-io-v3/ZooApp.ctxt new file mode 100644 index 0000000..e34a293 --- /dev/null +++ b/Semester 2/Programming 2/Week 1/zoo-io-v3/ZooApp.ctxt @@ -0,0 +1,5 @@ +#BlueJ class context +comment0.params=file +comment0.target=ZooApp(java.lang.String) +comment0.text=\n\ Constructor\ for\ objects\ of\ class\ ZooApp\n +numComments=1 diff --git a/Semester 2/Programming 2/Week 1/zoo-io-v3/ZooApp.java b/Semester 2/Programming 2/Week 1/zoo-io-v3/ZooApp.java new file mode 100644 index 0000000..3e81d0f --- /dev/null +++ b/Semester 2/Programming 2/Week 1/zoo-io-v3/ZooApp.java @@ -0,0 +1,22 @@ + +/** + * Write a description of class ZooApp here. + * + * @author (your name) + * @version (a version number or a date) + */ +import java.io.*; +public class ZooApp +{ + /** + * Constructor for objects of class ZooApp + */ + public ZooApp( String file ) throws FileNotFoundException + { + System.out.println( file ); + Zoo zoo = new Zoo(); + zoo.showAllAnimals(); + zoo.readAnimalData( "new_animals.txt" ); + zoo.showAllAnimals(); + } +} diff --git a/Semester 2/Programming 2/Week 1/zoo-io-v3/animal_data.txt b/Semester 2/Programming 2/Week 1/zoo-io-v3/animal_data.txt new file mode 100644 index 0000000..077e4e6 --- /dev/null +++ b/Semester 2/Programming 2/Week 1/zoo-io-v3/animal_data.txt @@ -0,0 +1,10 @@ +Leo, Lion +Tommy, Tiger +Ollie, Elephant +Roland, Rat +Rudolph, Reindeer +Lenny, Lion +Nellie, Elephant +Tessa, Tiger +Hetty, Elephant +Leslie, Lion diff --git a/Semester 2/Programming 2/Week 1/zoo-io-v3/new_animals.txt b/Semester 2/Programming 2/Week 1/zoo-io-v3/new_animals.txt new file mode 100644 index 0000000..0207d8e --- /dev/null +++ b/Semester 2/Programming 2/Week 1/zoo-io-v3/new_animals.txt @@ -0,0 +1,3 @@ +Bobby, Bear +Andy, Aardvark +Polly, African Grey Parrot diff --git a/Semester 2/Programming 2/Week 1/zoo-io-v3/package.bluej b/Semester 2/Programming 2/Week 1/zoo-io-v3/package.bluej new file mode 100644 index 0000000..2e4ea95 --- /dev/null +++ b/Semester 2/Programming 2/Week 1/zoo-io-v3/package.bluej @@ -0,0 +1,51 @@ +#BlueJ package file +dependency1.from=Zoo +dependency1.to=Animal +dependency1.type=UsesDependency +dependency2.from=ZooApp +dependency2.to=Zoo +dependency2.type=UsesDependency +objectbench.height=76 +objectbench.width=686 +package.editor.height=400 +package.editor.width=560 +package.editor.x=774 +package.editor.y=277 +package.numDependencies=2 +package.numTargets=3 +package.showExtends=true +package.showUses=true +project.charset=UTF-8 +target1.editor.height=728 +target1.editor.width=900 +target1.editor.x=423 +target1.editor.y=237 +target1.height=50 +target1.name=ZooApp +target1.showInterface=false +target1.type=ClassTarget +target1.typeParameters= +target1.width=80 +target1.x=260 +target1.y=60 +target2.height=50 +target2.name=Animal +target2.showInterface=false +target2.type=ClassTarget +target2.typeParameters= +target2.width=80 +target2.x=100 +target2.y=60 +target3.editor.height=700 +target3.editor.width=900 +target3.editor.x=16 +target3.editor.y=254 +target3.height=50 +target3.name=Zoo +target3.naviview.expanded=true +target3.showInterface=false +target3.type=ClassTarget +target3.typeParameters= +target3.width=80 +target3.x=180 +target3.y=10