<< Chapter < Page Chapter >> Page >
  • You may not remove a slip of paper from a compartment without replacing it by another slip of paper on which you have written a value.
  • You may not put a slip of paper in a compartment without removing the one already there.

Checkout counter example

In understanding how you might behave as a human computer program, consider yourself to have a job working at the checkout counter of a small grocery store in the 1930s.

You have two tools to work with:

  • A mechanical adding machine
  • The rack of compartments described above

Initialization

Each morning, the owner of the grocery store tells you to insert a name in the slot above each compartment and to place a little slip of paper with a number written on it inside each compartment. (In programming jargon, we would refer to this as initialization.)

Each of the names on the compartments represents a type of grocery such as

  • Beans
  • Apples
  • Pears

No two compartments can have the same name.

No compartment is allowed to have more than one slip of paper inside it.

The price of a can of beans

When you place a new slip of paper in a compartment, you must be careful to remove and destroy the one that was already there.

Each slip of paper that you insert into a compartment contains the price for the type of grocery identified by the label on the compartment.

For example, the slip of paper in the compartment labeled Beans may contain the value 15, meaning that each can of beans costs 15 cents.

The checkout procedure

As each customer comes to your checkout counter during the remainder of the day, you execute the following procedure:

  • Examine each grocery item to determine its type.
  • Read the price stored in the compartment corresponding to that type of grocery.
  • Add that price to that customer's bill using your mechanical adding machine.

In programming jargon, we would say that as you process each grocery item for the same customer, you are looping . We would also say that you are executing a procedure or an algorithm .

When you have processed all of the grocery items for a particular customer, you would

  • Press the TOTAL key on the adding machine,
  • Turn the crank, and
  • Present the customer with the bill.

A schematic representation of the procedure

We might represent the procedure in schematic form as shown in Figure 1 .

Figure 1 . A checkout counter algorithm.
For each customer, do the following: For each item, do the following:a. Identify the type of grocery item b. Get the price from the compartmentc. Add the price to accumulated total End loop on grocery itemsPresent customer with a bill End loop on a specific customer

Common programming activities

This procedure illustrates the three activities commonly believed to be the fundamental activities of any computer program:

  • sequence
  • selection
  • loop

Sequence

A sequence of operations is illustrated by the three items labeled a, b, and c in Figure 1 because they are executed in sequential order.

Selection

The process of identifying the type of grocery item is often referred to as selection . A selection operation is the process of selecting among two or more choices.

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Object-oriented programming (oop) with java. OpenStax CNX. Jun 29, 2016 Download for free at https://legacy.cnx.org/content/col11441/1.201
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Object-oriented programming (oop) with java' conversation and receive update notifications?

Ask