FAQ: Debugging - Debugging Techniques

This community-built FAQ covers the “Debugging Techniques” exercise from the lesson “Debugging”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Learn Java

FAQs on the exercise Debugging Techniques

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!
You can also find further discussion and get answers to your questions over in #get-help.

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head to #get-help and #community:tips-and-resources. If you are wanting feedback or inspiration for a project, check out #project.

Looking for motivation to keep learning? Join our wider discussions in #community

Learn more about how to use this guide.

Found a bug? Report it online, or post in #community:Codecademy-Bug-Reporting

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

im looking at the Debugging Techniques section 7 of 8 where im reviewing the AreaCalculator program. I ran through all the different shapes to find the area. One place im not certain is the if , else if code block. why after calling the different methods they have (5,6) for triangle, for rectangle there is (4,5) i see they are passes as parameters, but once we run through the code will our entry from passing our set values override the ones in the code? Are they just temporary place holders?

if (shape == 1) {
area_triangle(5,6);
} else if (shape == 2) {
area_rectangle(4,5);
} else if (shape == 3) {
area_circle(4);
} else if (shape == 4) {
quit();
break;

2 Likes

after testing and commenting things out, makes sense… those are “default values” if you were to remove/comment scanner section . and of course those values can change up to you if you decide not to have user input

//Scanner keyboard = new Scanner(System.in);

	//System.out.print("Length: ");
	//length = keyboard.nextInt();

	//System.out.print("Width: ");
	//width = keyboard.nextInt();

Hmm… Was there a bug? I got correct answers for all shapes each time I tried different numbers :slight_smile:

1 Like

yeah I didn’t get any bugs either lol

1 Like

Mine also ran however it’s because the bug is a logic error. Thus, the debugger will not be able to catch it.
This occurs in the area_triangle method:

public static double area_triangle(int base, int height) {
Scanner keyboard = new Scanner(System.in);
System.out.print("Base: ");
base = keyboard.nextInt();
System.out.print("Height: ");
height = keyboard.nextInt();
System.out.println();
// What you want: (Divided by 2, not * 2)
// int A = (base * height)/2;
int A = (base * height) * 2;
System.out.println("The area is " + A + “.”);
return A;
}

If you put base and height as 2, you would get the area as 8. However, the area of a 2x2 triangle is 2. In this case, using print statements has allowed us to catch an error because our expected answer is different than what the code is giving us.

1 Like

I can’t find the bug.

I had a play and the triangle area rounds to an integer when it could be a decmial for example base 7 and height 5 gives an area of 17 instead of 17.5.

I solved it by changing the traingle area calculation to:

double A = (double)base * (double)height * 0.5;

There might be a tider way to fix it as typing double three times seems wrong but I can’t find it.

Something must have been changed recently because the lesson completes as soon as I do anything and when doing 2x2 for the Triangle as you said, it still comes out as the correct answer of 2. The same goes for all of the other shapes.

Checking the code for the Triangle after resetting the workspace:

public static double area_triangle(int base, int height) {
Scanner keyboard = new Scanner(System.in);
System.out.print("Base: ");
base = keyboard.nextInt();
System.out.print("Height: ");
height = keyboard.nextInt();
System.out.println();
int A = (base * height) / 2;
System.out.println("The area is " + A + ".");
return A
}

So it already properly divides rather than multiplying now.

There is no bug here, all the shapes output their respective area correctly.

I don’t understand how to check the program at all. When I run it, nothing appears. What do I need to enter in a bash file ?

Here’s what I tried to enter and what the result was:

$ circle

bash: circle: command not found

$ 2x3

bash: 2x3: command not found

I think we are to figure out exactly what the code should look like and the expected output.

There is no compiletime / runtime errors. only logical errors.

It is a bit disorienting for the page to show a green check mark, when in actuality there is a logical problem.

I think the one shape calculation that is incorrect is for the Triangle.
The calculation in the provided code is: int A = (base * height) / 2;

The correct formula is area = 1/2 * base * height

So, the correct calculation is either:

1. int A = (base * height) * 1/2;


                       or 

2. int A = 1/2 * (base * height);

Which one is it ?

It doesn’t matter which one is used, they both give the same result.

Both equations are equivalent to base * height * 1/2 (the order of multiplicands doesn’t matter if multiplication is the only operation being performed).

But, should it be /2 or *1/2 ?

Both / 2 and * 1/2 accomplish the same time. However, for both, make sure you are getting the correct result by putting brackets where necessary. See the article on the order of operations below for more information.

While what is true ? on line 11 ?

while(true) functions as an infinite loop.

The condition of a while loop must evaluate to either true or false. While the condition is true, the loop iterates. Once the condition evaluates to false, the loop ends. By setting the condition to true, we’ve created an infinite loop since the condition is always true.

Example
// this is an infinite loop
while(true) {
  System.out.println("hello");
}

More on infinite loops here.

We can break out of an infinite loop using the break keyword. A break statement exits the loop it’s in. More on break here.

Still don’t get what condition is being evaluated.

In this program the loop breaks once 'int shape = keyboard.nextInt(); is defined.

There is no boolean condition being evaluated. It’s an intentional infinite loop.