Homework - Classes, Strings, Inheritance

  1. Write a class Date that will contain a date. Each Date object will contain fields for the month, day, and year (all ints). Create the following methods:
    1. default constructor that initializes the date to January 1, 1996
    2. constructor that initializes the date with the three int parameters
    3. nextDay - returns a Date that is one day after the invoking Date object. (Don't forget about the end of a month. Ignore leap years.) nextDay takes no parameters.
    4. before - returns boolean to indicate whether the invoking Date is an earlier Date than the Date passed as a parameter.
    5. toString - returns a String which contains the Date.

  2. Write a DateTest class which contains a main function to test your Date class. The main should:
    1. Create a Date using the default constructor.
    2. Create a Date containing the date October 26, 1999.
    3. Use the nextDay method to create a Date containing the date October 28, 1999.
    4. Print the message "Earlier" if your second date (Oct 26) is before your birthday for this year. If not, print the message "Later".
    5. Create an array of Dates which can hold 10 Dates. Store 3 Dates in the array and then print the array.

  3. Write a class called StringOps that contains the following static methods:
    1. countChar - this method has two parms - a String and a char, and returns an int containing the number of times the given character was found in the String.
    2. removeChar - this method has two parms - a String and a char, and returns a String which is the original String with every occurrence of the given char removed.

  4. Write a class called StringTest which contains a main function to test your StringOps class. The main should create a String containing the phrase "The road goes ever on and on". Count and print the number of times the letter e occurs in this String, then print the string after removing all occurrences of the letter r.

  5. Implement a class Person. Implement a subclass of Person called Employee. A Person has a name and an address. An Employee has a social security number and a salary. Write the methods for both classes, including constructors, and a print method. Write a raise member function for the Instructor class which returns void and has one int parm. The parm is the percentage of the raise; calculate the new salary by adding the raise (salary * the parm * .01) to the salary.

  6. Which of the following are valid? If invalid, explain why.
    1. Person someone = new Person("Sam", "Bagshot Row");
    2. Employee other = new Employee("Merry","Buckland",123456789,50000);
    3. Person another = other;
    4. Employee friend = someone;

  7. Write a class called InTest which contains a main function to test your Person and Employee classes. Main should create an array of 4 Person objects, make the first 2 array elements point to Person objects, make the last 2 array elements point to Employee objects, and print all array elements.

  8. What will be output by the main written in the previous problem.

Email Me | Office Hours | My Home Page | Department Home | MCC Home Page