Skip to content

Commit

Permalink
Virtual cat using abstract classes
Browse files Browse the repository at this point in the history
  • Loading branch information
diegogarciacs authored Oct 30, 2021
1 parent 1241bab commit f0c1d81
Show file tree
Hide file tree
Showing 9 changed files with 600 additions and 0 deletions.
Binary file added Lab 8/CECS277-Lab8.pdf
Binary file not shown.
11 changes: 11 additions & 0 deletions Lab 8/Lab 8.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
72 changes: 72 additions & 0 deletions Lab 8/src/Cat.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
public abstract class Cat
{
//initialize variables
private String name;
private int hunger;
/*
Default constructor that initializes name and hunger.
@param the name of the cat.
*/
public Cat(String n)
{
name = n;
hunger = 10;
}
/*
Returns the name of the cat.
@return name of the cat.
*/
public String getName()
{
return name;
}
/*
Returns the current hunger of the cat.
@return hunger current hunger of the cat.
*/
public int getHunger()
{
return hunger;
}
/*
Increments hunger by the passed in variable amount and returns the new hunger.
@param the amount to increment/decrement hunger by.
*/
public int incrementHunger(int val)
{
hunger += val;
if(hunger < 1)
{
hunger = 1;
}
else if(hunger > 10)
{
hunger = 10;
}
return hunger;
}
/*
Abstract method that will feed the cat that it's called on.
@param Player object to be affected.
@return String of cats reaction to feeding.
*/
abstract String feed(Player p);
/*
Abstract method that will play with the cat that it's called on.
@param Player object to be affected.
@return String of cats reaction to playing.
*/
abstract String play(Player p);
/*
Abstract method that will pet the cat that it's called on.
@param Player object to be affected.
@return String of cats reaction to petting.
*/
abstract String pet(Player p);

public String toString()
{
return name + " is currently at a level of " + hunger + "/10 hunger.";
}
}
133 changes: 133 additions & 0 deletions Lab 8/src/CheckInput.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
import java.util.Scanner;

/**
* Static functions used to check console input for validity.
*
* Use: Place CheckInput class in the same project folder as your code.
* Call CheckInput functions from your code using "CheckInput."
*
* Example: int num = CheckInput.getInt();
*
* @author Shannon Cleary 2020
*/
public class CheckInput {

/**
* Checks if the inputted value is an integer.
* @return the valid input.
*/
public static int getInt() {
Scanner in = new Scanner( System.in );
int input = 0;
boolean valid = false;
while( !valid ) {
if( in.hasNextInt() ) {
input = in.nextInt();
valid = true;
} else {
in.next(); //clear invalid string
System.out.println( "Invalid Input." );
}
}
return input;
}

/**
* Checks if the inputted value is an integer and
* within the specified range (ex: 1-10)
* @param low lower bound of the range.
* @param high upper bound of the range.
* @return the valid input.
*/
public static int getIntRange( int low, int high ) {
Scanner in = new Scanner( System.in );
int input = 0;
boolean valid = false;
while( !valid ) {
if( in.hasNextInt() ) {
input = in.nextInt();
if( input <= high && input >= low ) {
valid = true;
} else {
System.out.println( "Invalid Range." );
}
} else {
in.next(); //clear invalid string
System.out.println( "Invalid Input." );
}
}
return input;
}
/**
* Checks if the inputted value is a non-negative integer.
* @return the valid input.
*/
public static int getPositiveInt( ) {
Scanner in = new Scanner( System.in );
int input = 0;
boolean valid = false;
while( !valid ) {
if( in.hasNextInt() ) {
input = in.nextInt();
if( input >= 0 ) {
valid = true;
} else {
System.out.println( "Invalid Range." );
}
} else {
in.next(); //clear invalid string
System.out.println( "Invalid Input." );
}
}
return input;
}

/**
* Checks if the inputted value is a double.
* @return the valid input.
*/
public static double getDouble() {
Scanner in = new Scanner( System.in );
double input = 0;
boolean valid = false;
while( !valid ) {
if( in.hasNextDouble() ) {
input = in.nextDouble();
valid = true;
} else {
in.next(); //clear invalid string
System.out.println( "Invalid Input." );
}
}
return input;
}

/**
* Takes in a string from the user.
* @return the inputted String.
*/
public static String getString() {
Scanner in = new Scanner( System.in );
String input = in.nextLine();
return input;
}

/**
* Takes in a yes/no from the user.
* @return true if yes, false if no.
*/
public static boolean getYesNo(){
boolean valid = false;
while( !valid ) {
String s = getString();
if( s.equalsIgnoreCase("yes") || s.equalsIgnoreCase("y") ) {
return true;
} else if( s.equalsIgnoreCase("no") || s.equalsIgnoreCase("n") ) {
return false;
} else {
System.out.println( "Invalid Input." );
}
}
return false;
}
}
77 changes: 77 additions & 0 deletions Lab 8/src/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
Vo, Nguyen
Garcia, Diego
LAB 8 CS277
10/14/2021
*/
class Main {
public static void main(String[] args) {
Player owner = new Player();

System.out.println("Choose a kitty");
System.out.println("1. Ocelot");
System.out.println("2. Tabby");
System.out.println("3. Tiger");
int cat = CheckInput.getIntRange(1, 3);
System.out.print("Name your kitty: ");
String n = CheckInput.getString();
switch(cat)
{
case 1: Ocelot c = new Ocelot(n);
interactCat(c, owner);
break;
case 2: Tabby a = new Tabby(n);
interactCat(a, owner);
break;
case 3: Tiger t = new Tiger(n);
interactCat(t, owner);
break;
}
}
/*
Method that initiates the interaction with chosen cat.
@param c the object of cat.
@param p the object of player.
*/
public static void interactCat(Cat c, Player p)
{
while(p.getHp() > 0)
{
System.out.println("1. Feed your cat");
System.out.println("2. Play with your cat");
System.out.println("3. Pet your cat");
int action = CheckInput.getIntRange(1,3);
switch(action)
{
case 1: System.out.print(c.feed(p));
break;
case 2: System.out.print(c.play(p));
break;
case 3: System.out.print(c.pet(p));
break;
}
System.out.print(p.toString());
if(c.getHunger() == 1)
{
System.out.println(c.getName() + " is extremely hungry.");
}
else if(c.getHunger() <= 3)
{
System.out.println(c.getName() + " is very hungry.");
}
else if(c.getHunger() <= 7)
{
System.out.println(c.getName() + " is a bit hungry.");
}
else if(c.getHunger() < 10)
{
System.out.println(c.getName() + " is normal.");
}
else if(c.getHunger() == 10)
{
System.out.println(c.getName() + " is full.");
}
}
System.out.println("You are dead. Game Over!");
}
}
87 changes: 87 additions & 0 deletions Lab 8/src/Ocelot.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
public class Ocelot extends Cat
{
/*
Constructor that names ocelot while refering to the parent object cat.
@param name of ocelot.
*/
public Ocelot(String n)
{
super(n);
}
/*
Function that increments hunger of ocelot and deals damage if ocelot is full.
@param Object of player to update health.
@return string detailing the result of feeding the ocelot.
*/
@Override
public String feed(Player p)
{
incrementHunger(2);
if(getHunger() == 10) // 1 dmg
{
p.takeDamage(2);
return getName() + " is full and slashed you for " + 2 + " HP.\n";
}

return getName() + " enjoys a salmon's tail.\n";
}
/*
Accepts object of player to play with ocelot and returns a string detailing the experience.
@param p Player object.
@return String results of playing.
*/
@Override
public String play(Player p)
{
incrementHunger(-2);
if(getHunger() <= 1) // 4 dmg
{
p.takeDamage(4);
return getName() + " is extremely hungry and bites you for " + 4 + " HP.\n";
}
else if(getHunger() <= 3) // 3 dmg
{
p.takeDamage(3);
return getName() + " is very hungry and scratches you for " + 3 + " HP.\n";
}
else if(getHunger() <= 5) // 1 dmg
{
p.takeDamage(1);
return getName() + " is a bit hungry and hisses at you for " + 1 + " HP.\n";
}

return getName() + " jumps on you and starts to cuddle.\n" ;
}
/*
Accepts object of player to pet the ocelot and returns a string detailing the experience.
@param p Player object.
@return String results of petting.
*/
@Override
public String pet(Player p)
{
incrementHunger(-2);
if(getHunger() <= 1) // 3 dmg
{
p.takeDamage(3);
return getName() + " is extremely hungry and scratches you for " + 3 + " HP.\n";
}
else if (getHunger() <= 3) // 2 dmg
{
p.takeDamage(2);
return getName() + " is very hungry and scratches you for " + 2 + " HP.\n";
}
else if(getHunger() <= 5) // 1 dmg
{
p.takeDamage(1);
return getName() + " is a bit hungry and scratches you for " + 1 + " HP.\n";
}

return getName() + " lets you pet them before hiding from the tiger.\n";
}
@Override
public String toString()
{
return getName() + " is currently at a level of " + getHunger() + "/10 hunger.";
}
}
Loading

0 comments on commit f0c1d81

Please sign in to comment.