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

CPSC 233: Assignment 2

Part I: Analysis of program output

char data = 'A';
System.out.println(data + " " + (byte) data);
data++;
System.out.println(data + " " + (byte) data);
data = (char) ~data;
System.out.println(data + " " + (byte) data);

byte num = 127;
num++;
System.out.println(num);

For the above statements explain why the program produces the output that it does. (Hint: some source material could come from the sections on numeric representations, the introduction to Java programming as well the UNIX manual pages on ASCII representations).

 

Part II: Introduction to Object-Oriented programming

New Concepts to be applied for the assignment

  1. Writing an Object-Oriented program in Java (similar to the previous assignment you are not to define any static methods aside from the 'main' method).

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, "Fun", that implements the features 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 Fun and perhaps include a method call or two to an instance of class Fun:

 public class Driver
 { 
       public static void main (String [] args)
       {
            Fun generator = new Fun ();
            generator.start();
        }
 }

With this assignment the main loop for the program and the calls to the different methods of class Fun are contained in the "start" method of class Fun.

public class Fun

{

     public final int AGE_QUOTIENT = 100;

     public final int ADDRESS_QUOTIENT = 100;

            :                        :

     private Scanner aScanner = new Scanner (System.in);

public int calculateAddressModifier (int addressNumber)

{

     int addressModifier;

               :      :

     return addressModifier;

}

public int determineFundex (int ageModifier,

                                         int addressModifier,

                                         int petModifier)

{

     int fundex;

           :     :

     return fundex;

}

public int getAddress ()

{

     int address;

           :     :

     return address;

}

     :       :      :

 

public char getPet ()

{

     String pet;

     char petType;

     // Read the user input as a String of length 1, the first character of the String to determines the pet type of the user.

            :          :         :

     petType = pet.charAt(0);

     return petType;

}

public void intro ()

{

          :     :

}

public void start ()

{

      introduction ();

      // The loop that asks the user if they want to re-run the program will be in method 'start' and it will encompass all/most of
      // the calls to the methods of class Fun.

}

Constants that you should probably include in the definition of class Fun.

  1. AGE_QUOTIENT = 100
  2. ADDRESS_QUOTIENT = 100
  3. MIN_AGE = 1
  4. MAX_AGE = 113
  5. DEFAULT_AGE = 27
  6. MIN_ADDRESS = 1
  7. MAX_ADDRESS = 9999
  8. DEFAULT_ADDRESS = 4944
  9. MIN_FUNDEX = 0
  10. MAX_BORE = 499
  11. MAX_GENUINELY_FUN = 999
  12. MAX_WIT = 1499
  13. MAX_LIFE_PARTY = 1980

Some of the information tracked by class Fun, probably should be stored in variables that are local to the 'start' method

  1. age
  2. ageModifier
  3. address
  4. addressModifier
  5. pet
  6. petModifier
  7. fundex

 

Methods that MUST be implemented as part of class Fun

Method name

Inputs

Return Type

Description

Introduction

None

Void

Provides a brief overview of what the program does.

calculateAddressModifier

Int

Int

Given the address number, it will determine and return the address modifier. It should also deal with an invalid address.

calculateAgeModifier

Int

Int

Given the age, it will determine and return the age modifier. It should also deal with an invalid age.

determineFundex

Int, Int, Int

Int

Given the information for the 3 modifiers, this method will determine and return the numeric fundex.

determinePetModifier

Char

Int

Given the pet information, it will determine and return the pet modifier. It should check that the pet information is one of the allowable types.

displayFundex

Int

Void

Given the numeric fundex, this method will determine and display the fundex category. It should check for fundex values outside of the allowable ranges (too high or low).

getAddress

None

Int

Prompt for and get as input the address number.

getAge None Int Prompt for and get as input the age.
getPet None Char Prompt for and get as input the pet information.
start None None Makes the variable declarations for the necessary variables, calls the other methods, determines if the program re-runs itself.

Submitting your work:

  1. Assignments (source code/'dot-java' files) must be electronically submitted according to [the assignment submission requirements].  Type up your response to Part I and submit it electronically with your source code. Your answers to Part I should be called "part1_answers" and acceptable formats include: text, MS-Word, Acrobat PDF, html. The TA is not required to mark this part of your assignment if it is not submitted in one of these formats. Part II will contain your Java source code and must be submitted in plain text format (make sure that you submit the dot-java files and not the dot-class files). Again if the TA cannot compile and run your code (e.g., you submitted the output of an IDE) then he or she is not required to mark that portion of the assignment.
  2. 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].
  3. Before you submit your assignment to help make sure that you haven't missed anything here is a [checklist] of items to be used in marking.

 

External libraries that can be used

  1. Libraries that allow for text-based (console) input and output.

 

Sample executable