MIT | MIT-AITI | Ethiopia 2004

Java Lab 10: Students - Part I

  1. Create a package called students. All the classes you create in this and the next lab will be created in this package. You should compile after each of the following steps.
  2. Create a class called Student, which should have two properties, a name and a year and methods to get the name and get the year of the student. Initialize these properties to arguments passed into the constructor.
  3. Create a subclass of Student called Undergrad. The Undergrad constructor should accept name and year arguments. Add a method to Undergrad called description which returns a String containing the name of the undergrad, then a space, then a capital 'U', then a space, and then the year of the undergrad. For example, the description method of an Undergrad instance with the name "Michael" and the year 2006, should return the String "Michael U 2006".
  4. Create a subclass of Student called Grad. The Grad constructor should accept only the name of the Grad as an argument, and it should always initialize the Grad's year to 5. Add a description method to Grad which returns a String containing the name of the Grad, followed by a space and then the letter 'G'. The description method of a Grad named "Jennifer" should return the String "Jennifer G".
  5. Create a subclass of Undergrad called Intern. In addition to the name and year properties, Intern should have a wage and a number of hours that are initialized in the constructor. Add a getPay method to Intern which returns the wage times the number of hours. Add a description method to Intern which returns a String containing the result of calling Undergrad's description method followed by the return value of the getPay method. The description method of an Undergrad named "Elizabeth" whose year is 2005 and worked 20 hours at $10.32/hour, should return the String "Elizabeth U 2005 206.4".
  6. Create a subclass of Grad called ResearchAssistant. ResearchAssistant has a salary that is initialized in the constructor and a getPay method that returns the salary. Add a description method to ResearchAssistant which returns a String containing the result of Grad's description method, followed by the result of getPay. The description method of a ResearchAssistant with the name "Greg" and a $2000.00 salary would return the String "Greg G 2000.0".
  7. Create a class called StudentTest that has a main method. Use the main method to test the class hierarchy you just built. Create some instances of Undergrad, Grad, Intern, and Research Assistant. Print out the result of their description methods. Compile and run.

Submission: Submit all of your code for this lab.

Hosted by www.Geocities.ws

1