Determine data type and name of variable and data type of expression

Determine arithmetic equation with operators

Determine expression components

Operators and operands must be compatible

Subsection3.2.1

Select the correct Java assignment statement for each of the following problems.

ExercisesExercises

1.

Determine the number of cans of paint required to paint a room that is 12 feet wide and 15 feet long and 10 feet tall and each can of paint can cover 250 square feet of wall. The result should be stored in the variable numCansPaint.

Incorrect. There are two walls that are 12 feet wide and 10 feet tall and 2 walls that are 15 feet wide and 10 feet tall, so multiply the sizes of the walls and add them together. Then divide by the amount of wall one can of paint will cover. Because all values are integers, it will be integer division. There will likely be some leftover so we need to add one more can of paint. numCansPaint is an integer because you can’t buy partial cans of paint.

Correct. There are two walls that are 12 feet wide and 10 feet tall and 2 walls that are 15 feet wide and 10 feet tall, so multiply the sizes of the walls and add them together. Then divide by the amount of wall one can of paint will cover. Because all values are integers, it will be integer division. There will likely be some leftover so we need to add one more can of paint. numCansPaint is an integer because you can’t buy partial cans of paint.

int numCansPaint = (4 * 12 * 10 * 15) / 250 + 1;

Incorrect. There are two walls that are 12 feet wide and 10 feet tall and 2 walls that are 15 feet wide and 10 feet tall, so multiply the sizes of the walls and add them together. Then divide by the amount of wall one can of paint will cover. Because all values are integers, it will be integer division. There will likely be some leftover so we need to add one more can of paint. numCansPaint is an integer because you can’t buy partial cans of paint.

int numCansPaint = 4 * 12 * 10 * 15 / 250;

Incorrect. There are two walls that are 12 feet wide and 10 feet tall and 2 walls that are 15 feet wide and 10 feet tall, so multiply the sizes of the walls and add them together. Then divide by the amount of wall one can of paint will cover. Because all values are integers, it will be integer division. There will likely be some leftover so we need to add one more can of paint. numCansPaint is an integer because you can’t buy partial cans of paint.

int numCansPaint = 2 * 12 * 15 * 10 / 250 + 1;

Incorrect. There are two walls that are 12 feet wide and 10 feet tall and 2 walls that are 15 feet wide and 10 feet tall, so multiply the sizes of the walls and add them together. Then divide by the amount of wall one can of paint will cover. Because all values are integers, it will be integer division. There will likely be some leftover so we need to add one more can of paint. numCansPaint is an integer because you can’t buy partial cans of paint.

2.

Jamal is on a strict diet and needs to know whether or not he can eat a salad from the local fast food company. If Jamal’s total caloric intake for the day should be no more than 3600 calories and he wants to split up the calories evenly across 3 meals, indicate if the salad is a good choice by storing the result in a variable named allowableMeal. The plain salad that he wants to eat is 800 calories. He wants to add cheese (200 calories) and bacon bits (300 calories) and really wants 2 ranch dressing packs (each pack is 150 calories).

Incorrect. We want a yes/no answer as to whether Jamal should eat the meal, so the result variable is a boolean. The total calories in the salad is all the pieces added up (800 plus 200 plus 300 plus 2 packets of ranch). We need to compare this to the total number of calories per day divided by 3 and see if it’s less than or equal to that amount. Here we divide by 3.0 to ensure that we’ll do double arithmetic, though in this case it is unnecessary.

Incorrect. We want a yes/no answer as to whether Jamal should eat the meal, so the result variable is a boolean. The total calories in the salad is all the pieces added up (800 plus 200 plus 300 plus 2 packets of ranch). We need to compare this to the total number of calories per day divided by 3 and see if it’s less than or equal to that amount. Here we divide by 3.0 to ensure that we’ll do double arithmetic, though in this case it is unnecessary.

Incorrect. We want a yes/no answer as to whether Jamal should eat the meal, so the result variable is a boolean. The total calories in the salad is all the pieces added up (800 plus 200 plus 300 plus 2 packets of ranch). We need to compare this to the total number of calories per day divided by 3 and see if it’s less than or equal to that amount. Here we divide by 3.0 to ensure that we’ll do double arithmetic, though in this case it is unnecessary.

Correct. We want a yes/no answer as to whether Jamal should eat the meal, so the result variable is a boolean. The total calories in the salad is all the pieces added up (800 plus 200 plus 300 plus 2 packets of ranch). We need to compare this to the total number of calories per day divided by 3 and see if it’s less than or equal to that amount. Here we divide by 3.0 to ensure that we’ll do double arithmetic, though in this case it is unnecessary.

Incorrect. We want a yes/no answer as to whether Jamal should eat the meal, so the result variable is a boolean. The total calories in the salad is all the pieces added up (800 plus 200 plus 300 plus 2 packets of ranch). We need to compare this to the total number of calories per day divided by 3 and see if it’s less than or equal to that amount. Here we divide by 3.0 to ensure that we’ll do double arithmetic, though in this case it is unnecessary.

Write a correct Java assignment statement for each of the following problems. Be sure to declare the data type of the variable you are assigning the result of the expression to.

3.

Calculate and store the cost to ship a package at ShippersRUs that needs to go all the way across the country and must arrive within 2 days. There is a flat fee for all shipping that is $20.00. If the package is going to a different state, there is an additional $10 charge. If the package must arrive within a specified number of days, there is an additional charge based on the number of days. If it is an overnight shipment, add $49.95. For each additional day that it takes for delivery you can subtract $4.25. Store the total cost in a variable named cost.

4.

Calculate the amount of fence needed for a back yard that is 150 feet across and 275 long. The back of the house is 100 feet across and the fence should come out on either side of the house in equal amounts. (In other words, you need fence to go down one side, across the back and back up the other side, and then just enough fence to attach to the house on either side.) Store the result in a variable called fenceFeetNeeded.