$30
COMPUTER SCIENCE 11A
PROGRAMMING IN JAVA
PROGRAMMING ASSIGNMENT 2
Overview:
This assignment will test your understanding of primitive data types, variables, expressions, and
the use of the Scanner objects.
Write six programs to solve the following problems. Since we have not yet covered parameters and
their use, you are not going to use static methods for these problems (i.e., all the statements
necessary to solve the problem are in the main method).
Write each program in a different file with extension .java. Call you files Problem1.java,
Problem2.java, Problem3.java, Problem4.java, Problem5.java, Problem6.java respectively.
Problem 1: Write a program that reads a four-digit positive integer, such as 1998, and then displays
it, one digit per line, like so:
1
9
9
8
Your prompt should tell the user to enter a four-digit positive integer. You can then assume that the
user follows directions.
Problem 2: Write a program that converts degrees from Fahrenheit to Celsius, using the formula:
DegreesC = 5*(DegreesF – 32)/9
Prompt the user to enter a temperature in degrees Fahrenheit as a whole number without a fractional
part. Then have the program display the equivalent Celsius temperature, including the fractional
part to at least one decimal point. A possible dialogue with the user might be:
Enter a temperature in degrees Fahrenheit: 72
72 degrees Fahrenheit is 22.2 degrees Celsius.
You need to be careful to display the correct number of digits after the decimal point. To do so,
don’t use printf, use only ARITHMETIC OPERATIONS.
Problem 3: Write a program that determines the amount of change to be dispensed from a vending
machine, using the minimum number of coins. An item in the machine can cost between 25 cents
and a dollar, in 5-cent increments (25, 30, 35, …, 90, 95, or 100), and the machine accepts only a
single dollar bill to pay for the item. For example, a possible dialogue with the user might be:
Enter price of item
(from 25 cents to a dollar, in 5-cents increments): 45
You bought an item for 45 cents and gave me a dollar, so your
change is:
2 quarters,
0 dimes,
1 nickel
Problem 4: Antonella’s Pizza Place needs a program to calculate the number of slices a pizza of
any size can be divided into. The program should perform the following steps:
• Ask the user for the diameter of the pizza in inches.
• Divide the diameter by 2 to get the radius.
• Calculate the number of slices that may be taken from a pizza of that size if each slice has
an area of 14.125 square inches.
• Display a message telling the number of slices.
The number of square inches in the total pizza can be calculated with this formula: ! = #$2
where variable r is the radius of the pizza and # is a constant with value 3.14. Display the number
of slices as a whole number (i.e., with no decimal).
Problem 5: Modify the program you wrote for Problem 4 so that it reports the number of pizzas
you need to buy for a party if each person attending is expected to eat an average of four slices.
The program should ask the user for the number of people who will be at the party and for the
diameter of the pizzas to be ordered. It should then calculate and display the number of pizzas to
purchase.
Problem 6: A cookie recipe calls for the following ingredients:
• 1.5 cups of sugar
• 1 cup of butter
• 2.75 cups of flour
The recipe produces 48 cookies with these amounts of the ingredients. Write a program that asks
the user how many cookies he or she wants to make and then displays the number of cups of each
ingredient needed for the specified number of cookies.
Guidelines:
For this assignment you should limit yourself to the Java features covered in class so far (lecture
8). Although we will cover other topics while you are working on this assignment, do not use any
of those features.
Grading:
You will be graded on
o External Correctness: The output of your program should match exactly what is expected.
Programs that do not compile will not receive points for external correctness.
o Internal Correctness: Your source code should follow the stylistic guidelines shown in
class. Also, remember to include the comment header at the beginning of your program.
Submission:
Create a folder containing your Java source code (programs). Compress (zip) the folder and
upload it to Latte by the day it is due, Wednesday, September 21 at 11:00pm.
For late policy check the syllabus.