This book is now obsolete Please use CSAwesome instead.

3.14. Code Practice with Variables

The following code should calculate the cost of a trip that is 300 miles if gas is $2.50 a gallon and your car gets 36 miles per gallon. However, the code has syntax errors, like missing semicolons, wrong case on names, or unmatched " or (. Fix the code so that it compiles and runs correctly.

Line 5 is missing a semicolon. Line 6 has Double instead of double. Remember that the primitive types all start with a lowercase letter. Line 8 has tripmiles instead of tripMiles. Remember that you should uppercase the first letter of each new word to make the variable name easier to read (use camel case).

Show Comments

The following code should calculate the body mass index (BMI) for someone who is 5 feet tall and weighs 110 pounds. However, the code has syntax errors, like missing semicolons, wrong case on names, or unmatched " or (. Fix the code so that it compiles and runs correctly.

Line 5 has Height instead of height. Remember that variable names should start with a lowercase letter. Line 6 is missing an equal sign. Line 7 is missing a * to square the height. Line 8 is missing a semicolon at the end of the statement.

Show Comments

The following code should calculate the number of miles that you can drive when you have $8.00 and the price of gas is 2.35 and the car gets 40 miles per gallon. However, the code has errors. Fix the code so that it compiles and runs correctly.

Line 5 is missing the type double. Line 6 is backwards. It should be double milesPerGallon = 40;. Line 8 is missing a /. Line 10 is missing a ).

Show Comments

The following code should calculate the cost of an item that is on clearance (70% off) when you also have a coupon for an additional 20% off the clearance price. However, the code has errors. Fix the code so that it compiles and runs correctly.

Lines 5, 6, and 7 should all be int versus double so that the decimal portion of the calculation isn’t thrown away.

Show Comments

The following code should calculate the number of hours in 320893 seconds. However, the code has errors. Fix the code so that it compiles and runs correctly.

Lines 6 and 7 are both missing a /. Line 8 is missing a (. Line 9 is missing a } to close the main method.

Show Comments

Write the code below to calculate and print how many months it will take to save $200 if you earn $20 a week.

Calculate how many weeks it would take to make $200. Next divide the number of weeks by 4 (roughly the number of weeks in a month).

Show Comments

Write the code to calculate the number of miles you can drive if you have a 10 gallon gas tank and are down to a quarter of a tank of gas and your car gets 32 miles per gallon.

First calculate the number of gallons you have left and then multiply that by the miles per gallon to get the number of miles you can still drive.

Show Comments

Write the code to calculate the number of seconds in 3 days. Remember that there are 60 seconds in a minute and 60 minutes in an hour and 24 hours in a day.

First compute the number of seconds in 1 day and then multiple that by 3 days.

Show Comments

Write the code to print a random number from 1 to 100. You can use Math.random() to get a value between 0 and not quite 1.

First multiply the output from Math.random() times 100 and then cast it to an integer. This will result in a random number from 0 to 99. Add one to make it from 1 to 100.

Show Comments

Write the code to print the number of chicken wings you can buy if you have $4.50 and they cost $0.75 each. Remember that you can’t buy part of a wing.

Divide the amount of money you have by the cost of each wing and set the result to an integer since you can’t buy a part of a wing.

Show Comments
You have attempted of activities on this page