Introduction to Computer Science II by James Tam Return to the course web page

CPSC 233: Assignment 2(Worth 2%)

 

Due Monday February 2

Writing a lucky number generator using two classes

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 Assignment 1.   In the first assignment 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 colorModifier;
                Lucky generator = new Lucky ();
                    :        :                :        :
                ageModifier = generator.determineAgeModifier ();
                colorModifier = generator.determineColorModifier();
                    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 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 is contained in the "start" method of class Lucky.  Unlike the first approach, the calls to the different methods of class Lucky won't be made in the main method of the Driver class but instead will be made in the start method of class Lucky:

  class Lucky
 { 
        public void start ()
        {
                int ageModifier;
                int colorModifier;
                ageModifier = determineAgeModifier ();
                colorModifier = determineColorModifier();
                    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 maybe the random number generator class.   (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:

  1. 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.
  2. public int determineColorModifier (): This method prompts the user to enter his or her favourite primary color.  Based on the value entered, the program will then determine the numeric value for the color modifier and return this value back to the caller of the method.
  3. public int determineGenderModifier (): This method prompts the user to enter his or her gender.  Based on the value entered, the program will then determine the numeric value for the gender modifier and return this value back to the caller of the method.
  4. 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 hWighest 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.
  5. public int generateLuckyNumber (int ageModifier, int colorModifier, int genderModifier, int educationModifier): The parameters to this method are the four numeric modifiers that were generated by the previous four methods.  Method generateLuckyNumber applies the formula outlined in the first assignment in order to calculate the largest lucky number.  A call is in made to class Random (or some other class that generates random numbers) in order to generate a lucky number from zero to this maximum value.  The lucky number is returned to the caller of generateLuckyNumber.
  6. public void display (int luckyNumber):  This method will likely be very short.  It takes the lucky number generated by the previous method (#5) and displays it to the onscreen along with whatever formatting statements that you wish to add.

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).

Sample Executable

You can try out the sample byte code program called "Lucky.class" which can be found in Unix in the directory: /home/233/assignments/assignment1+2

New Concepts to be applied for the assignment

  1. Writing programs that consist of more than one class (a very simple Object-Oriented program).

External libraries that can be used

  1. Libraries that allow for text-based (console) input and output.
  2. Libraries that will randomly generate numbers (e.g., class Random or class Math).