AP CSA 1.2 Exercise 1: Snack Bar Order

Unit 1: Using Objects and Methods · Lesson 1.2 · Exercise 1

Snack Bar Order

Variables and Data Types. Write real Java, run it, and submit it to be graded against hidden test cases.

Why this one is worth doing

Three types, three jobs. Picking int where you needed double is the single most common way a correct-looking program prints the wrong number.

What to write

  1. Read a snack name (one word), then a whole number of them, then the price of one as a decimal.
  2. Store each in a variable of the right type: String, int, double.
  3. Print the name, then the count, then the unit price, each on its own line.
  4. Print the total cost on the fourth line.

Your program reads

A word, then an integer, then a decimal number, each on its own line.

Your program prints

Four lines: the snack name, the count, the unit price, and the total cost.

Worked examples

These are the cases you can see. There are more you cannot, and they use different values, so an answer that prints these numbers as constants will fail.

Example 1 input
pretzel
3
2.5
Example 1 output
pretzel
3
2.5
7.5
Example 2 input
water
1
1.25
Example 2 output
water
1
1.25
1.25

Your answer

Main.java

Input for the Run button

Run sends whatever is in the input box below. Submitting runs your program against every test case, including hidden ones with different input.


  

  

Stuck?

Hint 1

A count of items is a whole number, so it is an int. A price is not, so it is a double.

Hint 2

An int times a double is a double in Java. Store the total in a double or you will lose the cents.

Hint 3

Printing a double always shows a decimal point: 6 prints as 6, but 6.0 prints as 6.0.

Where to go next

Get in Touch

Whether you're a student, parent, or teacher — I'd love to hear from you.

Just want free AP CS resources?

Enter your email below and check the subscribe box — no message needed. Students get daily practice questions and study tips. Teachers get curriculum resources and teaching strategies.

Typically responds within 24 hours

Message Sent!

Thanks for reaching out. I'll get back to you within 24 hours.

🏫 Welcome, fellow educator!

I offer curriculum resources, practice materials, and study guides designed for AP CS teachers. Let me know what you're looking for — whether it's classroom materials, a guest speaker, or Teachers Pay Teachers resources.

Email

[email protected]

📚

Courses

AP CSA, CSP, & Cybersecurity

Response Time

Within 24 hours

Prefer email? Reach me directly at [email protected]