Homework - Classes, Strings, Inheritance
-
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:
- default constructor that initializes the date to January 1, 1996
- constructor that initializes the date with the three int parameters
- 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.
- before - returns boolean to indicate whether the invoking Date is an
earlier Date than the Date passed as a parameter.
- toString - returns a String which contains the Date.
-
Write a DateTest class which contains a main function to test your Date
class. The main should:
- Create a Date using the default constructor.
- Create a Date containing the date October 26, 1999.
- Use the nextDay method to create a Date containing the date October
28, 1999.
- Print the message "Earlier" if your second date (Oct 26) is before your
birthday for this year. If not, print the message "Later".
- Create an array of Dates which can hold 10 Dates. Store 3 Dates in the
array and then print the array.
-
Write a class called StringOps that contains the following static methods:
- 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.
- 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.
-
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.
-
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.
-
Which of the following are valid? If invalid, explain why.
- Person someone = new Person("Sam", "Bagshot Row");
- Employee other = new Employee("Merry","Buckland",123456789,50000);
- Person another = other;
- Employee friend = someone;
-
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.
-
What will be output by the main written in the previous problem.
Email Me |
Office Hours |
My Home Page |
Department Home |
MCC Home Page