9.10. Mixed Up Code Practice¶
Try to solve each of the following. Click the Check button to check each solution. You will be told if your solution is too short, has a block in the wrong order, or you are using the wrong block. Some of the problems have an extra block or two that aren’t needed in the correct solution. Try to solve these on your phone or other mobile device!
The following program segment should create an empty Dog class that is a child of the Animal class. But, the blocks have been mixed up and may include extra blocks that are not needed in a correct solution. Drag the needed blocks from the left and put them in the correct order on the right. Click the Check button to check your solution.
The following program should overload a void method talk with no parameters. But, the blocks have been mixed up and may include extra blocks that are not needed in a correct solution. Drag the needed blocks from the left and put them in the correct order on the right. Click the Check button to check your solution.
There is a Car
class whose implementation is not shown with a private int instance variable numWheels
set to 4
and a no-argument constructor. There should also be a Sedan
class that inherits from Car
while adding an integer numSeats
instance variable set to 5
. The Minivan
class should also inherit from Car
while having its own numSeats
instance variable that is set to 7. But, the blocks have been mixed up and may include extra blocks that are not needed in a correct solution. Drag the needed blocks from the left and put them in the correct order on the right. Click the Check button to check your solution.
The following program should create a class Person
that has firstName
and lastName
as String instance variables & also has a constructor that takes those in that order. Next, the program should create a Customer
class that inherits from Person
(initializing the instance variables too) and also has a String instance variable called id
. All three of these should be addressed in the Customer
constructor. But, the blocks have been mixed up and may include extra blocks that are not needed in a correct solution. Drag the needed blocks from the left and put them in the correct order on the right. Click the Check button to check your solution.
There should be a House
class with private int instance variables numWindows
and numDoors
and a constructor that takes those in that order. There should also be a MobileHouse
class that inherits from House
while adding a numWheels
instance variable (and thus has a constructor that takes numWindows
, numDoors
, and numWheels
in that order). But, the blocks have been mixed up and may include extra blocks that are not needed in a correct solution. Drag the needed blocks from the left and put them in the correct order on the right. Click the Check button to check your solution.
The following program should create a class Entity
that has healthPoints
as an integer instance variable, a constructor that takes that as an argument, and a fight()
method that prints "Attacked the hero!"
and returns nothing. Next, the program should create a Hero
class that inherits from Entity
and has a zero-argument constructor that initializes healthPoints
to 100. Finally, the Hero
class should override the fight()
method, instead printing "Attacked the enemy!"
. But, the blocks have been mixed up and may include extra blocks that are not needed in a correct solution. Drag the needed blocks from the left and put them in the correct order on the right. Click the Check button to check your solution.
The following program should create a Furniture
class. The class should have a String instance variable material
and a integer instance variable cost
. The Furniture
class should also include an equals()
method that returns true
if two Furniture
objects have the same material
and cost
. But, the blocks have been mixed up and may include extra blocks that are not needed in a correct solution. Drag the needed blocks from the left and put them in the correct order on the right. Click the Check button to check your solution.
There is a Car
class whose implementation is not shown. The Car
class has a method called drive()
that prints "vroom"
. The following program should create a subclass of Car
called Racecar
. The Racecar
class should override drive()
with a new drive()
method that prints "vroom"
twice by calling Car
’s drive()
method twice. But, the blocks have been mixed up and may include extra blocks that are not needed in a correct solution. Drag the needed blocks from the left and put them in the correct order on the right. Click the Check button to check your solution.
There is a Computer
class with private String instance variables name
and company
. You should override the Object equals
method to evaluate whether both Computers
have the same name
and company
, in which case they are “equal”. There is also a Laptop
class that inherits from Computer
while adding a String keyboardType
instance variable. The Laptop
class should override the equals
method from Computer
, instead evaluating whether the name
, company
, and keyboardType
are the same (remember that name
and company
are not readable to Laptop
so some polymorphism might be needed). But, the blocks have been mixed up and may include extra blocks that are not needed in a correct solution. Drag the needed blocks from the left and put them in the correct order on the right. Click the Check button to check your solution.
There is a Food
class (whose implementation is not shown) with a private numCalories
integer instance variable. Food
has a chomp()
method that returns nothing and prints "{numCalories} calories consumed"
(e.g., "5 calories consumed"
). There should also be a Fruit
subclass that inherits from Food
and adds the private color
String instance variable. The Fruit
class should override the Food
chomp()
method to return nothing, print "{numCalories} calories consumed"
, and print "fruit is {color}"
(on a new line). Finally, there should be an Apple
subclass that inherits from Fruit
, sets color
to "red"
, and adds a variety
String private instance variable. The Apple
class should have an overriden chomp()
method that returns nothing, prints "{numCalories} calories consumed"
, prints "fruit is {color}"
, and prints "ate {variety} apple"
(all separated by new lines). Remember that the numCalories
and color
variables are private, so the Apple
class does NOT have access to their values. But, the blocks have been mixed up and may include extra blocks that are not needed in a correct solution. Drag the needed blocks from the left and put them in the correct order on the right. Click the Check button to check your solution.