CIS 160 - Object oriented programming

Objectives

  • Create a class to be used as a base/superclass class
  • Create a class that uses inheritance
  • Create a class that uses composition
  • Create class members which need different access specifiers
  • Implement a specified API
  • Create both default and non-default constructors
  • Create accessor and mutator methods
  • Create instance fields/variables
  • Create overloaded constructors
  • Override an inherited method

Requirements

  1. Write a class called Name. It should contain the following:
    1. three String instance variables (private) for first, middle, and last names
    2. three constructors for Name
      1. a default constructor
      2. a constructor for when first and last names are supplied
      3. a constructor for when all three names are supplied
    3. three methods that return the instance variables
      1. one should return the first name (call it getFirst)
      2. one should return the middle name (call it getMiddle)
      3. one should return the last name (call it getLast)
    4. three methods that set the instance variables
      1. one should set the first name (call it setFirst)
      2. one should set the middle name (call it setMiddle)
      3. one should set the last name (call it setLast)
    5. a method called toString that returns the whole name as a String; there should only be one space between the first and last names if the middle name is missing
  2. Write a class called Person. It will use the Name class (but is not a subclass). It should contain the following:
    1. three protected instance variables
      1. one called name that is of type Name (the class you created in problem 1)
      2. one called gender which will be a single character (M or F)
      3. one called id which will be a String (usually the social security number)
    2. two constructors
      1. a constructor for when only Name and gender are supplied
      2. a constructor for when Name, gender, and id are all supplied
    3. three accessor methods that return name, gender, and id (named getName, getGender, and getId)
    4. one mutator method named setId that sets the id
    5. a method called toString that returns all the instance variables as a String; see the sample output for a sample of the format; note that the semicolon and id do not appear if the object has not had the id set
  3. Write a derived class called Student from the base class Person.
    1. remember that this class extends Person
    2. there should be two protected instance variables
      1. a double called credits which indicates credit hours earned
      2. a double called gpa
    3. there should be one constructor which expects Name, gender, credits, and gpa
    4. there should be two accessor methods which return credits and gpa respectively
    5. include a method called toString that returns all the information as a String; see the sample output for a sample of the format

Sample run

Try out your classes with the test program.

Test of Name class... The first full name is Smokey The Bear The first name is Smokey The middle name is The The last name is Bear The middle name is now T. The first full name is now Smokey T. Bear The next full name is Bozo Clown The modified name is Bozo T. Clown Test of Person class... sam: Sam Ash (sex: M) sam: Sam L. Ash (sex: M) pop: Popeye T. Sailor (sex: M) pop: Popeye T. Sailor (sex: M; id: 123-45-6789) Test of Student class... mary: Mary Doe (sex: F) Credits: 17.0 GPA: 3.25 End of test

Rubric

  • 5 points for following style rules discussed in class and demonstrated in sample programs, including proper indentation, spaces for indentation, variable names start with lowercase letter, class names start with uppercase letter, and having proper documentation comments
  • 11 points for having a properly working Name class that meets the specifications
  • 12 points for having a properly working Person class that meets the specifications
  • 12 points for having a properly working Student class that meets the specifications