Week 10 (Reading | Slides | Exercises)

Reading

Slides

Download Slides

Exercises

Exercise 10.1: Explain, in your own words, the differences and similarities of interfaces, abstract classes, and classes.

Exercise 10.2: Explain, in your own words, the concept of inheritance.

Exercise 10.3: Write your own MutString class. The class should have a single field which is an array of chars. Add a constructor which takes a single String argument.

Add the following methods:

  • print() - Prints the array of chars to standard out.
  • changeChar (int i, char c) - Converts the char at index i to c
  • toLowerCase() - Converts all chars to lowercase.
  • toUpperCase() - Converts all chars to uppercase.
  • trim() - Removes leading and trailing whitespace.

Hint: You may assume that the characters are limited to ASCII (i.e. 0-9, a-z, A-Z).

Exercise 10.4: Write an interface to represent a fruit. A fruit has a color, a taste, and a calorie count. Write classes, which implement the interface, for the fruits: apple, banana, pear, and orange.

  • Define a Color enum to represent different fruit colors (e.g., RED, YELLOW, GREEN, ORANGE).
  • Define a Taste enum to represent different fruit tastes (e.g., SWEET, SOUR, TART).
  • Define appropriate getter methods for the interface fields

Exercise 10.5 (Mandatory): Write an interface to represent a spellchecker with a method boolean isWord(String word) that returns true if the given word is spelled correctly. Implement three classes for three different languages.

  • Each language implementation should recognize at least three words.
  • The spellchecker must correctly handle both uppercase and lowercase letters.
  • Add a main method that takes two command-line arguments: a language name and a string. The program should split the string into words and print any misspelled words.

Exercise 10.6 (Mandatory): Write an interface to represent a vehicle with a method int getRemainingRange() that returns the number of kilometers the vehicle can drive with its current fuel. Implement two classes: a gasoline car and a hybrid car.

  • The gasoline car should store the amount of fuel left and its mileage (km per liter).
  • The hybrid car should store both the amount of gasoline and electric energy left, along with the mileage for running on gasoline and electricity.
  • The hybrid car's getRemainingRange() method should compute the total range by considering both gasoline and energy.
  • Add an int drive(int kms) method that simulates driving the specified distance, depletes the fuel accordingly, and returns the actual number of kilometers driven. For the hybrid car, electricity is used before gasoline.
  • Write a main method to test both vehicle implementations.

Exercise 10.7 (Mandatory): Write a class Person to represent a person with a name and an age. Create two subclasses: Employee (which extends Person) and Manager (which extends Employee).

  • The Person class should have a constructor that takes a name and an age.
  • The Employee class should add a job title and a salary, with an appropriate constructor.
  • The Manager class should add a monthly bonus field, with an appropriate constructor.
  • Add appropriate getter methods for all fields in each class.
  • Add a getSalary() method. Make sure Manager class takes the manager's monthly bonus into account.
  • Write a main method to test the inheritance hierarchy by creating instances of each class.

Exercise 10.8: Write a class Student to represent a student with a student ID, a first name, a last name, and a major.

  • Add a constructor that takes all four fields as parameters.
  • Add appropriate getter and setter methods for all fields.
  • Override equals() and hashCode() so that two students are equal if they have the same student ID.