# HW- variables, operators, input and output Solution

CSC 200 Homework #3 Variables Operators, and Input/Output

This homework covers variables, operators, and input and output. You must use these statements in the solutions to these problems. You must also follow the good programming practices discussed in class.

As a general practice for our programming assignments we will assume that the user will enter appropriate data values from the keyboard. If you are expecting the data value to be a whole number then the user will enter a whole number. If you are expecting the user to enter a double value, then the user will enter a double value.

For this homework, you may only use statements that were discussed in class in Chapter 1 and Chapter 2 appropriately. All assignments must be typed. No handwritten work will be accepted.

Homework is not a group exercise. Homework is an individual exercise to make sure that you understand the material that has been covered. You may discuss the homework problems with your classmates in general terms. You may not share the answer or the solution files with your classmates. Your papers must all be your own work. Your instructor should not be able to look at two programs and see that they are too similar. If that happens, then all parties involved will receive a 0 for the assignment.

Part I. Exercises

1. Order of Precedence
• Evaluate each expression listed below step by step.
• For each problem you must show your work as we discussed in class.
• Each step can only advance the calculation by one operation.
• Show each step of the evaluation of the complete statement according to the order of precedence that would be completed by the computer.
• Continue to show step by step until the problem has been solved.
• At the end of the evaluation show the current value of a and also the current value of d.
• Show both values even if they are not both used in the problem statement.
• For each statement assume that a =1 and d = 1.0 before the evaluation.
• You must determine the answer by hand. You may use the computer to check your answers.

Here is an example:
a= 23 / 2 -7 *5

Evaluation:
a = 11 – 7 * 5
a = 11 – 35
a = -24 d = 1.0 // show both values in your final answer

Expressions to evaluate:
• a = 46 % 9 + 4 * 4 – 2
• a = 45 + 43 % 5 * (23 * 3 %2)
• a%= 3 / a + 3
• d += 1.5 * 3 + (++a)
• d -= 1.5 * 3 + a++

2. Lunch Money
• Write Java statements that will declare and initialize a variable to represent the money that you have for lunch today. You have 5 dollars and 72 cents in your wallet.
• Write a single Java statement that will print out the following (except use your own name):
My name is Your Name and I have \$5.72 for lunch today

• Write a Java statement to declare a constant to represent the highest letter grade that you can receive in this course.

4. Evaluate Expression
• Evaluate the following expression. List the result and the data type of the result.
• You should answer this problem by hand, but you may check your results in Eclipse.

5+3 < 2 * 7

Part II. Programming Problems

• You must follow the good programming practices discussed in class. (Remember that in some places the book shows examples that include poor programming practices that are not allowed in this class!)
• For each programming problem, you must create an output text file as discussed in the labs that will show the complete information on your console.
• Be sure that you understand your program completely in English before you try to solve the problem in Java. It helps if you rewrite the problem statement in your own words before you begin coding. Be sure that you think about the explicitly defined information and the implied information from the problem statement.
• All input and output must be console input and output
• Each problem will have one output text file to turn in with the assignment.

1. Milk Problem

Problem Statement: A milk carton can hold 3.78 liters of milk. Each morning a dairy farm ships cartons of milk to a local grocery store. The cost of producing one liter of milk is \$0.38, and the profit of each cart of milk is \$0.27. Write a program that does the following:

a. Prompts the user to enter the total amount of milk produced in the morning.
b. Outputs the number of milk cartons needed to hold milk.
c. Outputs the cost of producing milk.
d. Outputs the profit for producing milk

One run of this program will produce an answer for one total number of liters of milk. Your program must work for any valid number of liters of milk. As you run your program you save the output of your program to a text file as described in lab instructions.

Note – the first line of code after your variable declarations and initializations must be a statement that prints out the following (except use your actual name).

When you have completed this project run your program for each value of liters of milk listed below. Save the output after each run to turn in with this assignment.
• 15.85
• 2.75
• 100
• 3.78

Print out a copy of your source code and your output file to turn in with this assignment.

2. House Renovation Problem

Problem Statement: Write a program that computes the cost of painting and installing carpet in a room. Assume that the room has one door, two windows, and one bookshelf. The user will enter, in feet, the length, width, and height of a room. The user will enter the widths and heights, in feet, of the door, each window, and the bookshelf. The user will enter the cost, per square foot, of painting the walls. The user will enter the cost, per square foot, of installing the carpet. The program must print out the cost of painting the walls and installing the carpet.

You must use console input and output for this problem. One run of your program will produce costs for one renovation of painting and carpeting for one room. As you run your program you must paste a copy of the console output into a text file as we completed in the labs.

Note – the first line of code after your variable declarations and initializations must be a statement that prints out the following (except use your actual name).

When you have completed this project run your program for each of the house renovation projects listed below. Save the output after each run to turn in with this assignment.

• First renovation project
o Room is 12 feet long, 12 feet wide, and 12 feet high
o Door is 2 feet wide by 10 feet high
o Window 1 is 3 feet wide by 5 feet high
o Window 2 is 3 feet wide by 5 feet high
o Bookshelf is 8 feet wide by 10 feet high
o Cost per square foot for painting is \$1.00
o Cost per square foot for carpeting is \$2.00
• Second renovation project
o Room is 24 feet long, 12 feet wide, and 8 feet high
o Door is 2 feet wide by 6 feet high
o Window 1 is 3 feet wide by 5 feet high
o Window 2 is 8 feet wide by 5 feet high
o Bookshelf is 8 feet wide by 6 feet high
o Cost per square foot for painting is \$1.57
o Cost per square foot for carpeting is \$2.89
• Third renovation project
o Room is 12.75 feet long, 11.25 feet wide, and 12 feet high
o Door is 1.95 feet wide by 10 feet high
o Window 1 is 2.5 feet wide by 5 feet high
o Window 2 is 6.8 feet wide by 7.2 feet high
o Bookshelf is 8 feet wide by 6 feet high
o Cost per square foot for painting is \$0.95
o Cost per square foot for carpeting is \$1.25
Print out a copy of your source code and your output file to turn in with this assignment.