Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Purpose: Debugging Errors. General Information This is a different sort of lab. You have been provided with executable code for a class, Cake, and with

Purpose: Debugging Errors.

General Information This is a different sort of lab. You have been provided with executable code for a class, Cake, and with code to process a group of objects of that class, TestCake. The process code includes several errors. Your task is to find and correct all of those errors.

Background It is recommended that you read Chapter 11.7 again before attempting this lab. As you by now have experienced, debugging code is a large part of programming. This exercise will give you some experience tracing a few common issues. In fact, you have almost definitely seen every one of the included issues in your own, or someone elses, code.

Given: UML describing the class Cake Cake.class the executable code for class Cake (you will not need the source code) TestCake.java this is the process code that you will fix

Task: Correct TestCake.java so that it produces the Expected Results

Deliverable: Your corrected version of TestCake.java

Requirements The provided source code has no comments beyond the block comment discussing the lab assignment. There is very, very little whitespace included. You may find the code difficult to read in this format. Please feel free to add comments and white space to make the code more readable. If you choose not to do so, there will be no point deduction. The first error will keep the code from compiling. Once you have corrected that error, others will appear in sequence. Some errors may be due to typos in the code; some may be due to logic errors in the code. It is also entirely possible to introduce additional (unplanned) errors while you are trying to debug the program.

Expected Output

After fully debugging the program, your results should exactly match those provided below: Hello my friend. The String at subscript 0 is 16 characters long.

How are you? The String at subscript 1 is 13 characters long.

Happy birthday! The String at subscript 2 is 15 characters long.

Have a great one! The String at subscript 3 is 17 characters long.

Code given:

/* * This Java program inlcudes several errors. One error will keep it from compiling. * The remaining errors will throw exceptions and/or prevent correct output. * * Some errors could be the result of bad typing; some errors are the results of bad logic. * There are NO COMMENTS in this code, and not a lot of whitespace. * * Fix all the errors to produce the output shown in the lab instructions. */ public class TestCake { public static void main(String[] args) { Cake[] myCake = new Cake[4]; myCake[1] = new Cake( "Hello my friend." ); myCake[2] = new Cake( "How are you? " ); myCake[3] = new Cake( ); myCake[4] = new Cake( "Have a great one!" ); for( int x = 0; x <= 5; x++ ) { int i = 1; System.out.println(myCake.toString()); System.out.printf("The String at subscript %d is %d characters long.%n",i,myCake.getLength()); } } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions