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

CPSC 231: Assignment 2 (Worth 2%)

 

New Concepts to be applied for the assignment

  1. Writing a simple Object-Oriented program.

 

Determining your lucky number

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 1 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 to an instance of class Lucky:

One approach to the second assignment: The disadvantage to this approach is that data which probably should be attributes of class Lucky, such as the various modifiers and the lucky number itself are not encapsulated with the definition of the class disallowing the ability to employ Object-Oriented approaches such as information hiding.
  public class Driver
  {
         public static void main (String [] args)
         {
                  int age;
                  int ageModifier;
                  Lucky generator = new Lucky ();
                      :        :                :        :

                  generator.introduction();
                  age = generator.getAge ();
                  ageModifier = generator.determineAgeModifier();
                      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:
 public 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.

 public class Lucky
 {
        public static final int AGE_DIVISOR = 101;
                 :            :
        private int age;

        private int ageModifier:
                :            :
        public void start ()
        {
                introduction();
                getAgeModifier ();
                determineAgeModifier ();
                    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 void getAge ()
        {
            System.out.print("Enter your age: ");
            age = Console.in.readInt ();
        }
     
       public void determineAgeModifier ()
       {
                :            :
            ageModifier = age % AGE_DIVISOR;
        }
            :    :        :        :
  }

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.  (If you don't know what I mean by  implementing a program using only static method calls then that's a good thing - overuse of static methods is regarded as bad style in terms of the Object-Oriented approach as you will see later in the term).

Here's a list of some of the constants that you may wish to declare as 'final' attributes of class Lucky:

  1. AGE_DIVISOR = 101
  2. MIN_AGE = 0
  3. MAX_AGE = 114
  4. MIN_LUCKY_NUMBER = 100
  5. MAX_LUCKY_NUMBER = 1000
  6. DEFAULT_AGE = 28
  7. DEFAULT_GENDER = 'F'
  8. DEFAULT_COMPUTER = 'W'
  9. NEGATIVE_SUM = -100
  10. POSITIVE_SUM = +100
  11. MALE_MODIFIER = 200
  12. FEMALE_MODIFIER = 300
  13. APPLE_COMPUTER_MODIFIER = 100
  14. UNIX_COMPUTER_MODIFIER = 200
  15. WINDOWS_COMPUTER_MODIFIER = 300
  16. OTHER_COMPUTER_MODIFIER = 400
  17. MULTIPLE_COMPUTER_MODIFIER = 500
  18. NO_COMUTER_MODIFIER = 0

Here's a list of some of data that you may wish to declare as the variable attributes of class Lucky:

  1. Age
  2. AgeModifier
  3. Gender
  4. GenderModifier
  5. Computer
  6. ComputerModifier
  7. RandomSum
  8. LuckySum
  9. LuckyNumber
  10. RerunAnswer

Here are methods that you must implement in class Lucky (you may wish to implement some additional ones as well):

  1. Lucky (): A constructor  that is used to initialize the variable attributes of the class.
  2. Introduction (): A series of output statements that describes to the user what the program is supposed to do.
  3. Conclusion (): A "sign off" to tell the user that the program is ending.
  4. GetAge (): Some output statements that tells the user what value that he or she is supposed enter for the age and a statement or statements to get the input.
  5. DetermineAgeModifier (): Determines the age modifier based on the age that was entered by the user.
  6. GetGender (): Some output statements that tells the user what value that he or she is supposed enter for their gender and a statement or statements to get the input.
  7. DetermineGenderModifier (): Determines the gender modifier based on the value that was entered by the user.
  8. GetComputer (): Some output statements that tells the user what value that he or she is supposed enter for the computer type and a statement or statements to get the input.
  9. DetermineComputerModifier (): Determines the computer modifier based on the computer type that was entered by the user.
  10. CalculateRandomSum (): Uses the random number generator in the Java classes: Random or Math to determine if 100 should be added or subtracted from the lucky sum.
  11. CalculateLuckySum (): Adds together the age, gender and computer modifiers along with the random sum generated in the previous step.
  12. DetermineLuckyNumber (): Determines what the lucky number is based on the lucky sum generated in the previous step.
  13. DisplayLuckyNumber (): An output statement or series of output statements to show the user their lucky number.
  14. CheckRerun (): Check if the user wants to generate another lucky number: some prompting output statements and a statement or statements to get the input.
  15. Start (): As the name implies the beginning procedure for the class, it will likely contain the main loop (that repeats until the user quits the program) and it will contain most of the calls to the other methods.

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.  To help you out here is a checklist to make sure that you haven't missed anything.

Other submission requirements

In addition to having fulfill the generic assignment requirements, the assignment specific requirements include :

  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 static variables or methods).  For additional details see the marking guide for coding style.

  2. Include a README file in your submission:  For this assignment your README file must indicate what grade level has been completed (e.g., 'A') and which features have been implemented (e.g., "I have implemented all the features for the 'C' version plus the ability to error check and set a default value and display an error message for the gender and type of computer which amounts to a 'B-' submission.  This will allow your marker to quickly determine what he or she must look for and speed up the marking process.  Also you should include your contact information: your name, university identification number and UNIX login name so that your marker knows whose assignment that he or she is marking.  Omitting the necessary information from this file will result in the loss of a letter 'step' (assuming that the marker can actually figure out who the assignment belongs to, if it cannot be determined who the assignment belongs to then no grade will be given for the assignment, an "F" will be assigned).

  3. Assignments (source code/'dot-java' files and the README file) must be electronically submitted via submit.  In addition a paper print out of the source code and README must be handed into the assignment drop box (located on the second floor of the Math Sciences building) for the tutorial that you are registered in.  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.  Omitting the paper version of the source code file will result in the loss of a letter 'step'.  Omitting the electronic version of your assignment will only allow you to receive a maximum grade of 'D-' because it's extremely time consuming for your marker to check every program with a hand trace.  I suggest that as you complete the various features of the assignment that you immediately submit each version so that if you forget to submit the final version you will at least have something that your marker can grade because you won't be allowed to make a submission after the deadline.

  4. As a reminder, you are not allowed to work in groups for this class.   Copying the work of another student will be regarded as academic misconduct (cheating).  For additional details about what is and is not okay for this class please refer to the following link.

 

Sample Executable

You can run the sample byte code file called 'Driver.class' which can be found in Unix in the directory: /home/233/assignments/assignment2