CPSC 233: Assignment 2 (Worth 2%)
Due Friday October 1
New concepts to be applied for the assignment
- Writing programs that consist of more than one class (a very simple
object-oriented program).
Writing a lucky number generator
The requirements for this assignment are
identical to the ones for Assignment 1, except that you must now employ two
classes: 1) A "Driver" class (that contains the main method) 2)
Another class, "Lucky", that implements the operations described in the previous
assignment. In assignment one it was likely that your main method
contained all of your program code. For the second assignment it is likely that
your main method will now just instantiate an instance of class Lucky and
perhaps include a few method calls:
One approach to the second assignment:
class Driver
{
public static void main (String [] args)
{
int ageModifier;
int petModifier;
Lucky generator = new Lucky ();
: : : :
generator.introduction();
ageModifier = generator.determineAgeModifier ();
petModifier = generator.determinePetModifier();
etc.
// The loop that asks the user if they want to re-run the
program will be in the main method and it will encompass
// all the calls to the methods of class Lucky.
}
}
Another (better) approach to the second assignment:
class Driver
{
public static void main (String [] args)
{
Lucky generator = new Lucky ();
generator.start();
}
}
With this second approach the main loop for the program and the calls to
the different methods of class Lucky are contained in the "start" method of
class Lucky.
class Lucky
{
public void start ()
{
int ageModifier;
int petModifier;
introduction();
ageModifier = determineAgeModifier ();
petModifier = determinePetModifier();
etc.
// The loop that asks the user if they want to re-run the
program will be in method start and it will encompass all
// the calls to the methods of class Lucky.
}
public int determineAgeModifier ()
{
: :
return ageModifier;
}
: : : :
}
You cannot implement a solution to Assignment 2 that employs only a single
class nor can you implement a solution that uses static methods - aside from the
main method and the methods contained in the tio library and the ones in class
"Character". (If you don't know what I mean by implementing a program
using only static method calls then that's a good thing - it's bad style in
terms of the Object-Oriented approach).
To help you get started here is an outline of the methods that you may wish
to implement in class Lucky:
- public void introduction (): Most programs provide some sort of
description of the program (or more likely a splash screen) as the program
loads. The description may briefly indicate to the user what the program
does or the splash screen may at least indicate to the user that the program
is being loaded.
- public int determineAgeModifier (): This method prompts the user to
enter his or her age. This value is stored in an integer and returned back to
the caller of this method.
- public int determinePetModifier (): This method prompts the user to
enter in the type of pet that they own. Based on the value entered, the
program will then determine the numeric value for the pet modifier and return
this value back to the caller of the method.
- public int determineInitialModifier (): This method prompts the
user to enter his or her initial for their last name. Based on the value
entered, the program will then determine the numeric value for the initial
modifier and return this value back to the caller of the method.
- public int determineEducationModifier (): This method prompts the
user to enter his or her current level of education (if the person is
currently a student) or the highest level of education that he or she
completed (if the person is no longer a student). Based on the value entered,
the program will then determine the numeric value for the education modifier
and return this value back to the caller of the method.
Note: Your implementation may of course
differ but just make sure that each method performs a well-defined task and that
the amount of code in each method is fairly balanced (e.g., don't just write one
method that does all of the above operations).
Grading
Like Assignment 1, there will be three
possible grade levels with this assignment. However, all submissions must
implemented using two classes: a driver class which is the starting point for
the program and class Lucky which implements all the requirements specified for
Assignment 1.
Other submission requirements
All the requirements listed in Assignment 1
also apply for this assignment except that you must now submit and print out two
source code files (one for the driver class and one for class Lucky).
1. Good coding style and documentation:
They will play a role in determining your final
grade for this assignment. Your grade can be reduced by a letter step
or more (e.g., 'A' to 'A-' for poor programming style such as employing poor naming
conventions for identifiers, insufficient documentation or the use of global
variables).
2. Include a README file in your submission: For this assignment
your README file must indicate what grade level has been completed (A, B or C).
This will allow your marker to quickly determine what he or she must look for
and speed up the marking process.
3. Assignments (source code and the README file) must be electronically
submitted via submit.
In addition a paper print out of the source code must be handed into the
assignment drop box for the lab that you are registered in (located on the
second floor of the Math Sciences building). Electronically submitting the
assignment allows your marker to run the code in order to quickly determine what
features were implemented. Providing a paper printout makes it easier for
your marker to read and flip through your source code.
Sample Executable
You can run a sample executable in
"Driver.class" which can be found in Unix in the directory:
/home/233/assignments/assignment2