You are on page 1of 8

INSTRUCTIONS

1. Using Eclipse, create a new Java project.


2. Set up files hierarchy and names following the next instructions:
1. Project name: <student id>_lab5 (e.g. 12345678_lab5)
2. Package name: labs.lab5 
3. Add classes:
1. Ingestible (problems 1-5)
2. Doable (problems 1-5)
3. Player (problems 1-5)
4. GameElement (problem 1-5)
5. Food (problems 1-5)
6. Drink (problems 1-5)
7. Pills (problems 1-5)
8. Activity (problems 1-5)
9. Encourager (problems 6-8)
10. Person (problems 6-8)
11. Friend (problems 6-8)
12. Parent (problems 6-8)
13. Dog (problems 6-8)
14. Nature (problems 6-8)
15. Segment (problems 9-10)
4. Make sure that your code passes all the example test
cases suggested.  Keep in mind that we will test other
values in addition to the ones provided here.
3. For submission:
1. Create a folder with the name: <Lastname>-<student id>
1. e.g. Navarro-12345678
2. Select Ingestible.java, Doable.java, Player.java, Food.java,
Drink.java, Pills.java, Activity.java, Encourager.java,
Person.java, Friend.java, Parent.java, Dog.java,
Nature.java, and Segment.java from your Eclipse project
structure and save them into the folder created in the
previous step.
3. Compress the whole folder into a zip file and upload it as
an attachment (e.g. Navarro-12345678.zip)

IMPORTANT:

If your code does not run because you did not correctly follow the instructions
provided, you will be allowed to re-submit this assignment. However, 5 points
will be discounted from your maximum grade. Resubmission will be allowed
one time only! Please make sure to clarify any questions you may have about
the setup and/or submission with the TA before you resubmit the lab. Also be
sure to check this checklist before submitting.

JUNIT TEST FILE FOR LAB5: Lab5Test.java  Download Lab5Test.java

PROBLEM PROMPTS

Part A:

Problems 1-5: Health Game

For this problem, you will develop the infrastructure for a game meant to
teach healthy habits. In particular, this game focuses on what you ingest (eat,
drink, take) and what you do (activities), and their effect on your overall health.
For this problem, you are given two interfaces:
1. The Ingestible interface, which specifies behavior for all ingestible

(edible/consumable) items (Template: Ingestible.java   Download


Ingestible.java)
2. The Doable interface, which specifies behavior for all "doable"

activities (Template: Doable.java   Download Doable.java)

You must implement:


1. The Player class, which represents a player in the game

(Template: Player.java   Download Player.java)


2. The GameElement abstract class, which defines data and behavior for

all elements/items in the game (Template: GameElement.java   


Download GameElement.java)
3. The following GameElement subclasses:
1. Food (implements the Ingestible interface)

(Template: Food.java   Download Food.java)


2. Drink (implements the Ingestible interface)

(Template: Drink.java   Download Drink.java)


3. Pills (implements the Ingestible interface)

(Template: Pills.java   Download Pills.java)


4. Activity (implements the Doable interface)

(Template: Activity.java   Download Activity.java)

This is how ingesting/doing the different game elements affect a player's


health:

 A GameElement with a health score of 5 is a neutral element,


meaning ingesting/doing it has no effect on health.
 For GameElements with a health score > 5:
o Activity: For every health score point above 5, add 0.05
to the player's health.
 For example, an activity with a health score of
7 will add 0.1 to the player's health, since:
 7 is 2 more than 5, and
 2 * 0.05 = 0.1
 As another example, an activity with a health
score of 9 will add 0.2 to the player's health,
since:
 9 is 4 more than 5, and
 4 * 0.05 = 0.2
o Drink: For every health score point above 5, add 0.05 to
the player's health
o Food: For every health score point above 5, add 0.1 to
the player's health
o Pills: For every health score point above 5, add 0.1 to the
player's health
 For GameElements with a health score < 5:
o Activity: For every health score point below 5, subtract
0.05 from the player's health.
 For example, an activity with a health score of
3 will subtract 0.1 from the player's health,
since:
 3 is 2 less than 5, and
 2 * 0.05 = 0.1
 As another example, an activity with a health
score of 1 will subtract 0.2 from the player's
health, since:
 1 is 4 less than 5, and
 4 * 0.05 = 0.2
o Drink: For every health score point below 5, subtract 0.05
from the player's health
o Food: For every health score point below 5, subtract 0.1
from the player's health
o Pills: For every health score point below 5, subtract 0.1
from the player's health

Example:

Player robert = new Player("Robert");


robert.getHealth(); // returns 0.5

Ingestible broccoli = new Food("broccoli", 10);


Ingestible cheetos = new Food("cheetos", 0);
Ingestible water = new Drink("water", 100);
Ingestible wine = new Drink("wine", 5);
Ingestible vitamins = new Pills("vitamins", 9);
Ingestible illegalDrugs = new Pills("illegal drugs", -100);

Doable work = new Activity("work", 3, 8);


Doable sleep = new Activity("sleep", 10, 7);
Doable play = new Activity("play", 8, 1);
Doable study = new Activity("study", 4, 3);

broccoli.ingest(robert); // returns "Robert is eating broccoli"


robert.getHealth(); // returns 1.0
broccoli.ingest(robert); // returns "Robert is eating broccoli"
robert.getHealth(); // returns 1.0
cheetos.ingest(robert); // returns "Robert is eating cheetos"
robert.getHealth(); // returns 0.5
water.ingest(robert); // returns "Robert is drinking water"
robert.getHealth(); // returns 0.75
wine.ingest(robert); // returns "Robert is drinking wine"
robert.getHealth(); // returns 0.75
illegalDrugs.ingest(robert); // returns "Robert is taking illegal drugs"
robert.getHealth(); // returns 0.25
vitamins.ingest(robert); // returns "Robert is taking vitamins"
robert.getHealth(); // returns 0.65

work.doIt(robert); // returns "Robert is doing work for 8 hours"


robert.getHealth(); // returns 0.55
sleep.doIt(robert); // returns "Robert is doing sleep for 7 hours"
robert.getHealth(); // returns 0.8
play.doIt(robert); // returns "Robert is doing play for 1 hours"
robert.getHealth(); // returns 0.95
study.doIt(robert); // returns "Robert is doing study for 3 hours"
robert.getHealth(); // returns 0.9
// Don't do it, Robert!
illegalDrugs.ingest(robert); // returns "Robert is taking illegal drugs"
robert.getHealth(); // returns 0.4
illegalDrugs.ingest(robert); // prints "Robert died!" and returns "Robert is taking
illegal drugs"
robert.getHealth(); // returns 0.0

Part B:

Problems 6-8: "Cheer Up" Game

Everyone needs encouragement from time-to-time, whether it's from people,


pets, or something else. For this problem, you will develop infrastructure for a
game meant to simulate the different types of encouragement we can get from
various sources.

For this problem, you are given one interface:


1. The Encourager interface, which specifies behavior for all objects

that provide encouragement (Template: Encourager.java 


Download Encourager.java

You must implement:


1. The Person abstract class (implements Comparable), which defines
data and behavior for all persons in the game

(Template: Person.java   Download Person.java)


2. The following Person subclasses, which both implement
the Encourager interface:

1. Friend (Template: Friend.java   Download Friend.java)

2. Parent (Template: Parent.java   Download Parent.java)

3. Dog (implements the Encourager interface) (Template: Dog.java   


Download Dog.java)
4. Nature (implements the Encourager interface)

(Template: Nature.java   Download Nature.java)

Example:
Encourager[] encouragers = {
  new Dog(),
  new Friend("Jing", 20),
  new Friend("Aly", 21),
  new Nature(),
  new Parent("Mom", 50),
  new Parent("Dad", 51)
};

for (Encourager e : encouragers) {


  System.out.println(e.encourage());
}
/*
 * The above prints out:
 *
 * Give wet sloppy kisses | Lay on your feet
 * Come over to hang out | Bring snacks
 * Come over to hang out | Bring snacks
 * Shine sun | Blow gentle breeze
 * Call on the phone | Say you're their favorite child
 * Call on the phone | Say you're their favorite child
*/

Person[] people = {
  new Friend("Jing", 20),
  new Friend("Aly", 21),
  new Friend("Aly", 18),
  new Friend("Aly", 35),
  new Parent("Mom", 50),
  new Parent("Dad", 51)
};

Arrays.sort(people);
for (Person p : people) {
  System.out.println(p);
}
/*
 * The above prints out:
 *
 * Name: Aly, Age: 18
 * Name: Aly, Age: 21
 * Name: Aly, Age: 35
 * Name: Dad, Age: 51
 * Name: Jing, Age: 20
 * Name: Mom, Age: 50
*/

Problems 9-10: Segment

Suppose we have a string holding the text of an entire book. If we want to


analyze segments of the text, it is inefficient to make substrings. Instead, we
should just store a reference to the original text and the starting and ending
positions. Implement a class Segment that does this, and have it implement
the CharSequence (Links to an external site.) interface of the standard Java

library. Use this template: Segment.java  Download Segment.java

Example:

String book = " 0 1 2 3 4 5 6 7 8 910111213141516171819";

Segment firstHalf = new Segment(book, 0, 20);


System.out.println(firstHalf); // prints " 0 1 2 3 4 5 6 7 8 9"

Segment secondHalf = new Segment(book, 20, 40);


System.out.println(secondHalf); // prints "10111213141516171819"

// Checking subSequence:
CharSequence endOfFirstHalf = firstHalf.subSequence(15, 20);
System.out.println(endOfFirstHalf); // prints "7 8 9"

// Checking charAt:
firstHalf.charAt(7); // returns '3'
firstHalf.charAt(19); // returns '9'

// Checking length:
firstHalf.length(); // returns 20
firstHalf.subSequence(5, 15).length(); // returns 10

// Demonstrate that String.join works with Segments:


String.join("XXXXX", firstHalf, secondHalf); // returns " 0 1 2 3 4 5 6 7 8
9XXXXX10111213141516171819"

// Demonstrate PrintSteam.append works with Segments


System.out.print(firstHalf);
System.out.print("-----");
System.out.print(secondHalf);
// the above prints: " 0 1 2 3 4 5 6 7 8 9-----10111213141516171819"
PreviousNext

You might also like