Computer Science 105 Lab 2: SUPPLY AND DEMAND
Due: February 4, 2002
Objectives and Overview: In this lab you will gain practice with
the process of program design, as well as the Java statements that allow
you to declare variables, calculate and assign arithmetic values to variables,
and perform input and output operations using variables.
Part 1. Practice with Variables, Expressions, and Assignment
The program file Price.java, listed on page 89 of your text, computes
the bill for a customer, given a quantity and a unit price, assuming a 6%
tax rate. The calculation is straightforward.
To run this program:
- drag a copy of the folder myProject from the CS105
course folder to the desktop,
- drag a copy of the program file Price.java from the CS105
-> examples -> chap02 folder into the folder myProject on
the desktop.
- start CodeWarrior by double-clicking the myProject.mcp file
inside this myProject folder,
- add the file Price.java to your project by using the ,
- change the Edit -> myProject Debug Settings -> Java Target
-> Main Class to Price, and
- run the project by selecting Project -> Run.
Now answer the following questions by running this program.
- Which of the variables are used for input by this program? Which ones
are used for output?
- What is the input and output when running this program for purchasing
two items at $1.99 per item?
- What is the input and output when running this program for purchasing
10 items at $100 per item?
- Answer questions 2fhj, 3fhj, 4fhj, and 6 on pages 102-104 of your text.
Part 2 - Problem Solving and Program Design
Henrietta's Crab Shop in Ocean City, Maryland sells live crabs to tourists
throughout the summer. The Crab Shop can be supplied with any number from
0 to 500 crabs each day. Henrietta computes the price of the crabs on the
basis of this supply, using the following formula.
price = 2.89 + 200/supply
Tourists come to Ocean Park in large numbers, but they are apt to buy
more crabs when the price is low and the weather is sunny than they will
otherwise. Their daily buying behavior, or demand, is characterized
by the following formula, which takes into account the day's weather as well
as the price:
demand = 980 - 180price + 50(weather - 1)
Here, the variable weather can be 0, 1, or 2 to signify "rainy,"
"cloudy," or "sunny," respectively.
So, for instance, under the best weather conditions and the most abundant
supply (500) of crabs, Henrietta will sell 437 of the little critters at
a price of $3.29, leaving a surplus of 63 unsold crabs. On the other hand,
if the supply is 200 crabs, the higher price of $3.89 will lead to a smaller
demand of 329 crabs (assuming again sunny weather), but now leaving a lot
of angry tourists.
The general relationships between supply and demand, as they relate to
the price of crabs on a sunny day, is shown in the picture below. As suggested
by the three single points in the picture, supply is not linearly related
to price. However, price and demand are linearly related (shown by the straight
line in this picture).
The picture also suggests that there is at least one break-even point
between supply and demand, and the closer Henrietta approximates that point,
the more likely she is to sell all her supply and have no angry customers
at the end of the day.
The purpose of your program is to compute price and demand for a specific
(input) supply and weather condition, and then display those results. The
program can also advise Henrietta to either increase or decrease her supply,
depending on whether the computed demand for the given supply and weather
condition is greater or less than the given supply. For instance, a supply
of 200 crabs on a sunny day should lead to the message "Increase your supply,"
while a supply of 500 crabs should lead to the message "Decrease your supply."
By running this program several times, Henrietta can thus determine the
optimal supply of crabs to keep in stock on a particular day so that there
will not be a surplus or shortage at the end of the day.
Identify the input, output, variables, and computations that are needed
by this program. Use meaningful names for each of the variables you declare.
Then write the program in Java. Your program should provide clear
and concise output to the screen, both in the form of prompts for user input
and messages identifying displayed output. Your complete design should first
be sketched on paper before you sit down and begin typing Java code.
Part 3 - Type and run the program
Now type your complete Java program and save it as the file classname
.java, where classname is the name of the class on the first
line of your program's text.
Drag a new copy of the folder myProject from the CS105
course folder to the Desktop and add this program to your project,
using the same steps that you followed in Part 1.
Part 4 - Debug and Run the program several times
Now debug your program by selecting Make under the Project
menu and correct all syntax errors that are indicated there. Once these
are eliminated, Run the program several times with different inputs.
Be sure that it gives correct results for the specific example inputs discussed
above. Now answer the following questions:
- What are the input variables for your program, and what are their types
(int or double)?
- What are the output variables, and what are their types?
- Did you use any additional variables, and what is their purpose?
- What is the demand for 500 crabs on a cloudy day? A rainy day?
- What is the break-even number of crabs for Henrietta to keep in stock,
so that she prevents having a surplus or shortage of more than 2 or 3 crabs
at the end of the day, assuming that the weather is sunny? (Several runs
of your program may be needed to hone in on this number.)
- How does this break-even number (found in the previous question) change
when the weather is cloudy? What optimal supply of crabs should Henrietta
have for a rainy day?
- Are there more than one such break-even numbers? Explain.
Lab 2 Deliverables:
By 5:00pm on the due date, leave in the mailbox outside Searles 220 a printed
listing of your program with your name on it, together with your answers
to the questions in Parts 1 and 4. Many of these questions can be answered
by running your program several times with various input values.
You may work in teams of two or individually to complete this lab.
If you work in a team, be sure that both persons' names appear on the work
you hand in.
When you are finished answering these questions, rename your program file
by making a copy called lab2yourname.java (yourname
is your login name), and dragging that copy to the appropriate Drop
Box in the CS105 course folder.