Selection control statements allow for some pretty sophisticated decision making and they form the basis of artificial intelligence expert systems. In part A of this lab you will expand a very small expert system that gives advice.
Repetition statements and recursion allow tasks to be repeated again and again. Part B provides experience with designing and writing simple repetitions with a variety of constructs.
When control structures are included in methods, program flow is usually determined by the values of inputs, consequently, robust testing to ensure the reliability of the code is a problem. This lab also illustrates what it means to thoroughly and systematically test a program that contains selection structures.
This lab reinforces the repetitions constructs. It also gives you an opportunity to write a fairly complicated solution with a partner.
Selection statements can be used whenever you want to make a choice. In this lab we will model a small "expert system" in which advice is given based on some input.
To find good solutions for these problems you must select a structure that meets your needs. If statements can take on the following kinds of structure.
So far the programs you have written have been rather easy to test. You gave a few input examples via a user interface and then verified that the results were correct. In the graphics programs you could see whether your output looked as you intended. In more complex programs, especially those involving selection statements it is absolutely necessary to confirm that all paths lead to the expected output. This is often extremely time consuming.
There are two places where error can be introduced. During design and analysis you can make a mistake in your logic. During implementation you can make a mistake in translating your logic to code. Consequently it is important to be systematic. Take care in developing a solution long before you begin to implement code. Then, when the code compiles, thoroughly test it. Download the class ActivityExpert from ActivityExpert.java that shows the result of the design phase within a comment. It also contains two methods, testRules() and userInput() that illustrate the difference between thorough testing and getting user input.
Repetition Structures
Doing repetitive tasks in a program involves identifying the following:
// start state defined before the while statement
while (repetition condition)
{body; update statement;}
for (initialization statement;repetition condition; update statement )
{body}
// start state defined before do statement
do {body; update statement}
while (repetition condition)
Step 2: Attempt to write a switch statement to model the expert system in ActivityExpert. Can you do it? How much easier/harder is the switch than the various if/else statements? Characterize the type of problem that the expert system is. Does this kind of problem lend itself to a switch statement? Why or why not?
Step 3: Add another predicate to the ActivityExpert. For example, include an option to determine whether you are caught up at work. Add corresponding consequences. Choose one of the four implementations above (flat list, cascading list, decision tree, switch) and augment the code to include your new predicate. Modify the ActivityExpert testing methods (testRules() and userInput() ) to test your new code.
| Problem | Partner A | Partner B |
| 1 | "while" and "for" | "do" and "while" |
| 2 | "for" and "do" | "for" and "while" |
| 3 | "do" and "while" | "do" and "for" |
| 4 | "for" and "while" | "while" and "do" |
Problem 1: Write a function that sums the integers from n to m .. This exercise should suggest a "closed form", a simple formula that solves this problem. Do NOT write the solution using the closed form, instead do the sequence of calculations in your solution. Use your solution to explain why the closed form is correct (e.g. an "informal" inductive proof).
Problem 2: Write a function that sums n ODD numbers beginning at m. Warning this problem has a different input specification than 1.1. Again this exercise should suggest a closed form solution. Find it, and prove it is correct.
Problem 3: Write a function that sums n EVEN numbers beginning at m. Is there a closed form? If so, prove it is correct.
Problem 4: Write a function that multiples n integers beginning at m.
You should write all of your methods within one class, and then use another class to test demonstrate a test run that does a reasonable test on all functions.
Version 1: Evaluation of a condition costs 100 times what it costs to do arithmetic (plus, times etc.)To get a handle on this problem, start by calculating the cost of doing each problem above where 10 numbers will be summed. Extrapolate to 100 numbers, then to 1000 numbers, and then generalize to n numbers. For each version, which do you think is best: doing the closed form or doing the series? Give a GOOD explanation (bordering on formal proof).Version 2: Evaluation of arithmetic costs 100 times what it costs to evaluate a condition.
You and your partner should design this solution with great care. Although not required, you may submit a design before beginning to implement. Obviously this will probably help you a great deal. DO NOT simply start to program this one. You will waste a lot of time. Do not initially get bogged down in fancy display. Simply use a String object to get the entire calendar into and the JOptionPane method "showMessageDialog" to print the calendar in the history window.
OPTIONAL: have some fun with this one. Use the graphics primitives you have
learned to display a more interesting calendar. For this part of the lab, let
your imagination roam a bit. Use on line
resources to find JAVA graphics primitives to control font type, style
and size. Make the calendar a "work" calendar, with the weekend diminished.
Better yet, make it a weekend calendar, with the week diminished. Or try
printing it in a non-traditional form.
In this lab you are required to produce a lab report and to arrange for a demo of your solutions.
The code you write should include:
ASK!!!!!!! questions, in class, in lab, during office hours and especially via the class newsgroup. Please do not phone.
You may get help from classmates, tutors and other students outside your team on system technicals, syntax bugs, and small issues related to the Java language.
You may NOT get help from the above groups to develop the solution or to initially implement your code. If you cannot get to the stage of implementing Java code, please see me during lab, send email or visit during my office hours.
A "down" system on the morning the lab is due will not cause an extension of the due date. Other universal calamities may be negotiated with the entire class. The point: hand SOMETHING of worth (e.g. the first few steps) in by the due date.
What if your lab partner has vanished? Find another. At this stage of the game
you should have your lab partner's email address and phone. Whenever you work
together, each of you should keep a copy of the results. If you are not sure of
the mechanics of copying files etc. ASK!!