CS134 Unit 04 Exercise: Building programs 1

Table of Contents


Create your own source code files in your replit project. Remember to use the correct file extension:

File extension Language Run command
.py Python python3 FILENAME.py
.lua Lua lua FILENAME.lua
.cpp C++ g++ FILENAME.cpp -o program1 && ./program1.out

So for example, you might create these files:

Python
Filename Run command
program1.py python3 program1.py
program2.py python3 program2.py
program3.py python3 program3.py
program4.py python3 program4.py
Lua
Filename Run command
program1.lua lua program1.lua
program2.lua lua program2.lua
program3.lua lua program3.lua
program4.lua lua program4.lua
C++
Filename Build C++ code Run program
program1.cpp g++ program1.cpp -o program1.out ./program1.out
program2.cpp g++ program2.cpp -o program2.out ./program2.out
program3.cpp g++ program3.cpp -o program3.out ./program3.out
program4.cpp g++ program4.cpp -o program4.out ./program4.out

1. Program 1: Tell a joke

Topics: Displaying text output to the screen

Write a program that meets the following requirements:

  1. Program has a HEADER - display the program's name at the start of the program.
  2. Program has a FOOTER - display "Goodbye!" at the end of the program.
  3. The program displays a joke to the screen.
  4. Utilize vertical spacing to split up the HEADER, FOOTER, and joke section.

Example output:

R.W.'S JOKE
Stolen from an unknown location on the internet

Two fish are in a tank.

One turns to the other and says, "How do you drive this thing?!"

Goodbye!

2. Program 2: Monster bestiary

Topics: Creating variables, basic arithmetic, displaying text and variable output to the screen

Write a program that meets the following requirements:

  1. Program has a HEADER - display the program's name at the start of the program.
  2. Program has a FOOTER - display "Goodbye!" at the end of the program.
  3. Create three string variables to represent monster names. Store any monster names you'd like.
  4. Create three integer variables to represent how much gold you gain from defeating each monster. Store any integer values you'd like.
  5. Display the list of monsters and how much gold each one drops.
  6. Create an integer variable to store the sum of all 3 monsters' gold.
  7. Display the sum of all 3 monsters' gold to the screen.
  8. Make sure code and program user interface are both clean and organized.

Example output:

MONSTER BESTIARY

IMP         6  gold
COEURL      14 gold
GOOBBUE     30 gold

TOTAL GOLD... 50 gold

Goodbye!

3. Program 3: Price and calorie calculation

Topics: Variables, input, and output

  1. Cost per eggs:
    • Ask the user how much a dozen (12) eggs cost.
    • Then calculate the price per 1 egg. (price / 12)
    • Display the result
  2. Cost per oz:
    • Ask the user how much a 1-oz bottle of vanilla costs.
    • Note that 1 oz = 6 tsp.
    • Calculate the cost per tsp. (price / 6)
    • Display the result
How much do a dozen eggs cost? 4.41
12 eggs per 1 dozen
Cost per 1 egg: 0.37

How much does 1 oz of vanilla cost? 4.39
6 teaspoons per 1 oz
Price per 1 teaspoon: 0.73

4. Program 4: Recipe v3

Topics: Variables, input, and output

Write a program that meets the following requirements:

1. Copy your recipe program: First off, copy your previous recipe program, the version that includes variables for ingredients and adjusting amounts based on batches.

2. Store variables for amount, price, and calories: Add additional variables for each ingredient so that you're storing amount, price per that amount, and calories per that amount. Make sure to also update your ingredient list output to display the price and calories in addition to the amounts.

For example:

Ingredient Amount per 1 batch Price Calories
Butter butter_amount=1 stick butter_price=1.40 per 1 stick butter_calories=810 per 1 stick
Egg egg_amount=2 eggs egg_price=0.74 per 2 eggs egg_calories=78 per 2 eggs
Vanilla vanilla_amount=1 tsp vanilla_price=0.73 per 1 tsp tsp_calories=12 per 1 tsp

3. Update all based on batches: After batches is entered, use the same math to figure out the updated calorie counts and prices.

4. Give totals: Somewhere in the recipe, display the total price and total calories.

Example output:

Incomplete cookie recipe

How many batches? 1

INGREDIENTS
 * 1 stick of butter $1.40 (810 calories)
 * 2 eggs $0.74 (78 calories)
 * 1 oz vanilla $0.73 (12 calories)

Total price: $2.87
Total calories: 900

DIRECTIONS
 1. Mix things haphazardly in a bowl.
 2. Make sure to get flour everywhere, even though I didn't list flour in the ingredients.

 (etc).
Incomplete cookie recipe

How many batches? 31

INGREDIENTS
 * 3 stick of butter $4.20 (2430 calories)
 * 6 eggs $2.22 (234 calories)
 * 3 oz vanilla $2.19 (36 calories)

Total price: $8.61
Total calories: 2700

DIRECTIONS
 1. Mix things haphazardly in a bowl.
 2. Make sure to get flour everywhere, even though I didn't list flour in the ingredients.

 (etc).

Author: Rachel Wil Sha Singh

Created: 2023-09-15 Fri 14:13

Validate