lab 09 part 2 & 3

This commit is contained in:
SowinskiBraeden committed 2025-03-17 12:45:55 -07:00
1 parent fbfbac1eb5
commit 821b4d2e51
5 files changed
+301

No files matched your search

+57
View File
@@ -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;
}
}
+90
View File
@@ -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<Cat> cats;
/**
* CatHotel Constructor.
* @param name of hotel.
*/
public CatHotel(String name) {
this.name = name;
cats = new ArrayList<Cat>();
}
/**
* 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<Cat> iterator = cats.iterator();
List<Cat> newList = new ArrayList<Cat>();
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());
}
}
}
@@ -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");
}
}
+76
View File
@@ -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();
}
}
@@ -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);
}
}