Practice Questions (Code Writing)
Inheritance
Table of contents
CodeCheck Questions
-
A labeled point has an x- and y-position and a label.
Implement the toString method of the LabeledPoint class.
-
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 anAuto
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). -
Complete the
Car
class below so that it inherits from the givenVehicle
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.
Medium CodeCheck Questions
-
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 #).
-
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 | | | | | +-------------+