In this exercise Clara will impersonate the Red Riding Hood and has a task to bake a cake for her grandmother. Naturally, all cakes in Clara’s world are made out of leaves. You will be presented with a scenario shown in Figure 1. Your task is to first request the user to enter two integer numbers (cake width and cake height). Notice two new methods in Clara’s vocabulary and have a look at the example for computing an average of two numbers that was discussed in Lecture 6, to see how these methods are used for entering a number from keyboard. These numbers must be validated so that the cake fits inside Clara’s world. If any of the numbers does not satisfy this requirement – user must re-enter the corresponding number. When the numbers are correctly entered – Clara must draw a cake of the width and height as entered by the user. Figure 2 shows an example of the resulting cake that is 18 cells wide and 5 cells high. There is only one Clara world you have to deal with for this exercise, as shown in Figure 1. In this world Clara always starts on the second street and in the same initial position as shown in Figure 1.

University/Course: WSU

Uploaded: October 27, 2024

Files: 1

✓ Solution:

class MyClara extends Clara { 
/**
     * In the ‘run()’ method, you can write your program for Clara 
     */
void run() {
// Get the dimensions of the cake from the user
int cakeWidth = readInt(“Please enter the width of the cake:”);
int cakeHeight = readInt(“Please enter the height of the cake:”);
// Start building the cake layers
while (cakeHeight > 0) {
// Build one row of the cake
for (int i = 0; i < cakeWidth; i++) {
                placeCakeBlock();
                move();
            }
// Move Clara to the next row on the left side
            moveLeftToNextRow();
            cakeHeight–;
// Build another row of the cake
for (int i = 0; i < cakeWidth; i++) {
                placeCakeBlock();
                move();
            }
// Move Clara to the next row on the right side
            moveRightToNextRow();
            cakeHeight–;
        }
    }
// Place a leaf to represent a block of the cake
void placeCakeBlock() {
if (!onLeaf()) {
            putLeaf();

🔒 Whatsapp us for the full file.