Practice Questions (Code Writing)

Inheritance

Table of contents
  1. Easy CodeCheck Questions
  2. Medium CodeCheck Questions

CodeCheck Questions

  1. A labeled point has an x- and y-position and a label.

    Implement the toString method of the LabeledPoint class.

    Solve it here

  2. All vehicles used for transportation in the U.S. must have identification, which varies according to the type of vehicle. For example, all automobiles have a unique Vehicle Identification Number (VIN) assigned by the manufacturer, plus a license plate number assigend by the state in which the auto is registerd.

    Modify the Auto class to include an instance variable for the license plate number. Implement the constructor so that an Auto can be constructed with a VIN and a license plate number.

    Override the getID() method to return the id of the auto as shown in this format: VIN=1234567890,plate=ABC123 (without any spaces).

    Solve it here

  3. Complete the Car class below so that it inherits from the given Vehicle class. Cars lose value over time. In this example, you should assume that the value is reduced by 25 cents per mile driven, until it reaches zero.

    Do not add any instance variables to the Car class.

    Solve it here

Back to Top

Medium CodeCheck Questions

  1. You are given a class Rectangle that produces “ASCII art” rectangles such as the following:

    +-------------+  
    |             |  
    |             |  
    |             |  
    |             |  
    |             |  
    +-------------+  
    

    The code is given below.

    Your task is to produce a subclass FilledRectangle that produces figures such as

    +-------------+  
    |#############|  
    |#############|  
    |#############|  
    |#############|  
    |#############|  
    +-------------+  
    

    (when the fill character is #).

    Solve it here

  2. You are given a class Rectangle that produces “ASCII art” rectangles such as the following:

    +-------------+  
    |             |  
    |             |  
    |             |  
    |             |  
    |             |  
    +-------------+  
    

    The code is given below.

    Your task is to produce a subclass TitledRectangle that produces figures such as

    +-------------+  
    |             |  
    |             |  
    |    Hello    |  
    |             |  
    |             |  
    +-------------+  
    

    Solve it here

Back to Top