Project 2 Instructions
For your second project, you will write a program that uses object-oriented principles to simulate
resource gathering and production. Unlike the first project, in which you had freedom to organize your
code as you saw fit, this project requires you to write two of your classes in a specific manner.
The first class you need to write is called ResourceGenerator. ResourceGenerator must include the
following public constructor and methods:
//constructs a random ResourceGenerator
//the resources are "A" "B" and "C"
//each resource is associated with a distinct random number
excluding 7 that this constructor chooses by rolling dice
public ResourceGenerator()
//if there is a resource associated with num and num is not
blocked, returns that resource
//otherwise returns null
public String generateResource(int num)
//prevents any resources from being generated from num
//only one number can be blocked at a time
//if a new number becomes blocked, unblocks the previously-
blocked number
public static void blockNumber(int num)
//rolls and sums two six-sided dice
public static int rollDice()
You may not add any additional public variables, constructors, or methods (any number of private
variables, constructors, and methods are fine).
The second class you need to write is called Faction. Faction must include the following public
constructor and methods:
//constructs a Faction
//a faction begins with two resource generators and no resources
public Faction()
//for each resource generator, adds the resource it generates on
this number (if any) to this faction's stockpile
public void generateResources(int num)
//if this faction has at least 8 resources, randomly reduces the
resources held by this faction by half
//if odd, round the number of resources lost down
public void loseOverflow()
//if otherFaction has at least one resource, randomly moves one
resource from that faction to this faction
//otherwise, there is no effect
public void stealResourceFrom(Faction otherFaction)
//as many times as possible, removes one of each resource type
from this faction and adds a resource generator to this faction
public void constructNewGenerators()
//returns a string representation of this object as shown in the
output sample
//this includes counts of all resource types as well as a count
of generators
public String toString()
//returns the number of resource generators possessed by this
faction
public int getNumberOfResourceGenerators()
Like the ResourceGenerator class, you may not add any additional public variables, constructors, or
methods (any number of private variables, constructors, and methods are fine).
Create a third class with a main method and any number of static helper methods. Begin by asking the
user for the number of factions in the simulation and the number of resource generators a faction must
acquire for victory. Construct the requisite number of factions. Then, the factions take turns in
numerical order, cycling back to faction zero after the last faction takes its turn, with the following steps
happening in each turn:
1. Roll the dice and print a corresponding message.
2. Take actions based on the result of the dice roll:
o If the number is not seven,
▪ Every faction generates resources based on that number.
▪ Print “Generating Resources…”.
o If the number is seven,
▪ Every faction loses overflow.
▪ Block a number chosen by random dice roll.
▪ The current faction steals a resource from another faction chosen at random.
▪ Print “Eliminating Overflow…”, “Blocking Number…”, and “Stealing Resource…” .
3. Print the current faction’s number of each resource and number of resource generators, have
the current faction construct new generators (printing “Constructing Generators…”), and print
the current faction’s number of each resource and number of resource generators again.
4. If the current faction has the requisite number of generators for victory, end the program,
printing a corresponding message.
You do not have to validate user input. That means you may assume the user gives proper input to all
prompts (for example, you may assume the numbers at the beginning of the program are both integers
greater than two).
You will not be graded on efficiency. However, you will be graded on coding style, including good
variable and method names, proper indentation, sufficient comments, and following the DRY (don’t
repeat yourself) principle. Note that any code in one class that duplicates the functionality of code in
another class incurs a penalty.
This is an individual project. Working in a group will be considered academic dishonesty.
Submit your code (the .java files) on Gradescope. Your code for this project will consist of exactly three
classes. Late submissions will lose one point per day late.