diff --git a/src/ca/bcit/comp1510/lab09/Cat.java b/src/ca/bcit/comp1510/lab09/Cat.java new file mode 100644 index 0000000..c6db9ef --- /dev/null +++ b/src/ca/bcit/comp1510/lab09/Cat.java @@ -0,0 +1,57 @@ +package ca.bcit.comp1510.lab09; + +/** + * Cat. + * @author Braeden Sowinski + * @version 1.0.0 + */ +public class Cat { + + /** name of cat. */ + private final String name; + + /** age of cat in years. */ + private int age; + + /** + * Cat constructor. + * @param name string. + * @param age int. + */ + public Cat(String name, int age) { + this.name = !name.equals("") ? name : "Cleo"; + this.age = age >= 0 ? age : 0; + } + + /** + * getName. + * @return name string of cat. + */ + public String getName() { + return name; + } + + /** + * getAge. + * @return age int of cat. + */ + public int getAge() { + return age; + } + + /** + * setAge. + * @param age new age int. + */ + public void setAge(int age) { + this.age = age; + } + + /** + * toString. + * @return cat data in string. + */ + public String toString() { + return name + " age " + age; + } +} diff --git a/src/ca/bcit/comp1510/lab09/CatHotel.java b/src/ca/bcit/comp1510/lab09/CatHotel.java new file mode 100644 index 0000000..be4c916 --- /dev/null +++ b/src/ca/bcit/comp1510/lab09/CatHotel.java @@ -0,0 +1,90 @@ +package ca.bcit.comp1510.lab09; + +import java.util.List; +import java.util.ArrayList; + +import java.util.Iterator; + +/** + * CatHotel? + * @author Braeden Sowinski + * @version 1.0.0 + */ +public class CatHotel { + + /** name of cat hotel. */ + private String name; + + /** cats in hotel. */ + private List cats; + + + /** + * CatHotel Constructor. + * @param name of hotel. + */ + public CatHotel(String name) { + this.name = name; + cats = new ArrayList(); + } + + /** + * addCat to hotel list. + * @param cat to add. + */ + public void addCat(Cat cat) { + cats.add(cat); + } + + /** + * removeAllGuests. + */ + public void removeAllGuests() { + cats.clear(); + } + + /** + * guesCount of hotel. + * @return numberOfGuests as int. + */ + public int guestCount() { + return cats.size(); + } + + /** + * removeOldGuests kicks out cats over + * max age. + * @param maxAge of cat. + * @return evicted cats int. + */ + public int removeOldGuests(int maxAge) { + int evicted = 0; + + Iterator iterator = cats.iterator(); + List newList = new ArrayList(); + + while (iterator.hasNext()) { + Cat current = iterator.next(); + if (current.getAge() <= maxAge) { + newList.add(current); + continue; + } + evicted++; + } + + cats = newList; + + return evicted; + } + + /** + * printGuestList from hotel. + */ + public void printGuestList() { + System.out.println("Hotel " + name + " guests:"); + + for (int i = 0; i < cats.size() - 1; i++) { + System.out.println(cats.get(i).toString()); + } + } +} diff --git a/src/ca/bcit/comp1510/lab09/CatHotelDriver.java b/src/ca/bcit/comp1510/lab09/CatHotelDriver.java new file mode 100644 index 0000000..17c9aa4 --- /dev/null +++ b/src/ca/bcit/comp1510/lab09/CatHotelDriver.java @@ -0,0 +1,43 @@ +package ca.bcit.comp1510.lab09; + +import java.util.random.RandomGenerator; + +/** + * CatHotelDriver. + * @author Braeden Sowinski + * @version 1.0.0 + */ +public class CatHotelDriver { + + /** + * main program entry. + * @param args unused. + */ + public static void main(String[] args) { + RandomGenerator rand = RandomGenerator.getDefault(); + + final int maxAge = 11; + + CatHotel hotel = new CatHotel("California"); + + hotel.addCat(new Cat("Peter", rand.nextInt(maxAge))); + hotel.addCat(new Cat("Craig", rand.nextInt(maxAge))); + hotel.addCat(new Cat("John", rand.nextInt(maxAge))); + hotel.addCat(new Cat("Mark", rand.nextInt(maxAge))); + hotel.addCat(new Cat("Richard", rand.nextInt(maxAge))); + hotel.addCat(new Cat("Bobby", rand.nextInt(maxAge))); + + hotel.printGuestList(); + System.out.println(hotel.guestCount() + " guests\n"); + + final int tooOld = 5; + hotel.removeOldGuests(tooOld); + System.out.println("Evicting cats older than 5"); + + System.out.println(hotel.guestCount() + " guests\n"); + + hotel.removeAllGuests(); + System.out.println("Evicting all guests"); + System.out.println(hotel.guestCount() + " guests\n"); + } +} diff --git a/src/ca/bcit/comp1510/lab09/Coin.java b/src/ca/bcit/comp1510/lab09/Coin.java new file mode 100644 index 0000000..12d1cb8 --- /dev/null +++ b/src/ca/bcit/comp1510/lab09/Coin.java @@ -0,0 +1,76 @@ +package ca.bcit.comp1510.lab09; + +/** + * Represents a two-sided coin that can be flipped. Modified over class + * example to use enum. + * + * @author Lewis & Loftus 9e + * @author Bruce Link + * @author BCIT + * @version 2023 + */ +public class Coin { + /** Enumeration type representing sides of a coin. */ + public enum CoinFace { + /** the side of a coin considered tails. */ + Tails, + /** the side of a coin considered heads. */ + Heads + } + + /** Coin's current face showing. */ + private CoinFace face; + + /** + * Constructs a Coin object and flips it to give it a starting value. + */ + public Coin() { + flip(); + } + + /** + * Flips this Coin by randomly choosing a face value. + */ + public void flip() { + final double half = 0.5; + if (Math.random() < half) { + face = CoinFace.Heads; + } else { + face = CoinFace.Tails; + } + } + + /** + * returns side of coin as CoinFace type. + * @return current showing face of this coin. + */ + public CoinFace getFace() { + return face; + } + + /** + * set the coin face explicitly. No required check on values as enum + * CoinFace allows no illegal values to be passed. + * @param newFace the new face to show. + */ + public void setFace(CoinFace newFace) { + face = newFace; + } + + /** + * Returns true if the current face of the Coin is heads. + * @return true if current face is heads, else false. + */ + public boolean isHeads() { + return face == CoinFace.Heads; + } + + /** + * Returns the current face value of this Coin as a String. + * @return toString description + */ + public String toString() { + return face.name(); + } +} + diff --git a/src/ca/bcit/comp1510/lab09/CoinRunner.java b/src/ca/bcit/comp1510/lab09/CoinRunner.java new file mode 100644 index 0000000..5f4dc80 --- /dev/null +++ b/src/ca/bcit/comp1510/lab09/CoinRunner.java @@ -0,0 +1,35 @@ +package ca.bcit.comp1510.lab09; + +/** + * CoinRunner. + * @author Braeden Sowinski + * @version 1.0.0 + */ +public class CoinRunner { + + /** + * main program entry. + * @param args unused. + */ + public static void main(String[] args) { + Coin c = new Coin(); + + int headsCurrent = 0; + int headsMax = 0; + + final int flips = 100; + for (int i = 0; i < flips; i++) { + c.flip(); + Coin.CoinFace face = c.getFace(); + if (face == Coin.CoinFace.Heads) { + headsCurrent++; + headsMax = headsCurrent > headsMax ? headsCurrent : headsMax; + } else { + headsCurrent = 0; + } + System.out.println(face); + } + + System.out.println("The longest run of heads is " + headsMax); + } +}