Module 4 - Game Skills 1 |
Stay Tuned for Some Important Advice! Hey, look at you! You are over half way finished with the course now.
If there is one thing you have learned about computer programs, hopefully it is that you must "tell" the computer exactly what to do by writing code correctly...and that means paying attention to detail. The computer can't guess what you want to do--you have to tell it. From now on, the programs you write will be a little different. They will be longer and more detailed. (No groaning, please.) You have reached a stage in learning to design and write computer programs where it is very important for you to see and study examples of code. If you were in a regular classroom situation, your instructor would be able to go over examples on the board and you could ask questions; however, in the online setting, much more responsibility falls on you. In order to truly understand the concepts, you must read the material, study the examples, and ask yourself questions. As you know, you have to define every single step in a program. Consequently, as we define more complicated problems, programs will get longer and more detailed. When this happens, some students get frustrated. You can avoid this with a simple strategy that you may have learned to improve study habits in other classes.
You can more easily learn to write computer programs if you read the material, study the examples, and ask yourself questions. Actually, that's what it takes to be successful in any course, right? So, let's get started with the last half of the course! |
Objectives: After studying the Module 4 Game Skills 1, you will be able to:
|
YOUR LESSON Introduction Earlier in Module 4, you learned that the READ-DATA statement is an excellent way to get large amounts of data into a computer program. There are several important things to remember about READ-DATA statements.
READ-DATA statements can be used for almost any situation where you need to get data into a program, but one common use is applications where you need to enter repetitive sets of information. It is also possible to combine numeric and alphanumeric formats in a single PRINT USING statement so you can print out numeric and string data at the same time. This is especially useful when printing tables of data...as you are about to see. |
Practice Time Let's imagine that your science teacher wants you to calculate the circumference (the distance around the equator) of each planet in the solar system. How would you go about writing a program to do that? If you apply the 5 step program development process, it should be pretty simple. So, let's give it a try. |
Define the Problem Here are the details we need to know.
Define the Algorithm Next, we need a flowchart. The flowchart should show reading in the data, any calculations, and what the output will be. Other details may be added as needed. Below is a flowchart depicting the algorithm for the program. |
The
"set" of data being read in this algorithm consists of:
|
Notice the amount of detail shown in the flowchart in terms of the variable names, the calculations, and the print statements. Your flowcharts should also show this level of detail in future assignments. The flowchart first shows that data will be read in as sets (the name and the diameter) for each planet. Next, the radius of each planet is calculated. After that, the value of pi is assigned as a constant. Then, the circumference calculations are performed for each planet. Next, the table heading and column headings are printed. Notice that the TAB values have been left blank. That's a detail that will have to be figured out once we start testing the program. Finally, PRINT USING statements are used to print out the name of the planet, its radius, and its circumference. Notice that the spacing is pretty tight. It is hard to determine the spacing at this stage, so it is best to just indicate in general what will be output. The backslashes indicate a place for the planet name. The first set of number symbols (#) indicate the format of the radius (it will contain a comma). The last set of number symbols (#) indicate the format of the circumference (it will contain a comma and display two decimal places). This is a concise, detailed flowchart that gives us a good "picture" of the algorithm for the program. |
REM
The Planets REM Penny Programmer REM 02/20/2001 ' REM Input Data READ planet1$, diameter1 READ planet2$, diameter2 READ planet3$, diameter3 READ planet4$, diameter4 READ planet5$, diameter5 READ planet6$, diameter6 READ planet7$, diameter7 READ planet8$, diameter8 ' REM Calculate Radius radius1 = diameter1 / 2 radius2 = diameter2 / 2 radius3 = diameter3 / 2 radius4 = diameter4 / 2 radius5 = diameter5 / 2 radius6 = diameter6 / 2 radius7 = diameter7 / 2 radius8 = diameter8 / 2 ' REM Calculate Circumference CONST pi = 3.14 circ1 = 2 * pi * radius1 circ2 = 2 * pi * radius2 circ3 = 2 * pi * radius3 circ4 = 2 * pi * radius4 circ5 = 2 * pi * radius5 circ6 = 2 * pi * radius6 circ7 = 2 * pi * radius7 circ8 = 2 * pi * radius8 ' REM Output COLOR 2, 4 CLS PRINT TAB(15); "The Planets" PRINT "Planet"; TAB(15); "Radius (miles)"; TAB(35); "Circumference (miles)" PRINT USING "\ \ ##,### ###,###.##"; planet1$; radius1; circ1 PRINT USING "\ \ ##,### ###,###.##"; planet2$; radius2; circ2 PRINT USING "\ \ ##,### ###,###.##"; planet3$; radius3; circ3 PRINT USING "\ \ ##,### ###,###.##"; planet4$; radius4; circ4 PRINT USING "\ \ ##,### ###,###.##"; planet5$; radius5; circ5 PRINT USING "\ \ ##,### ###,###.##"; planet6$; radius6; circ6 PRINT USING "\ \ ##,### ###,###.##"; planet7$; radius7; circ7 PRINT USING "\ \ ##,### ###,###.##"; planet8$; radius8; circ8 ' REM Data Statements DATA Mercury,3032 DATA Venus,7521 DATA Earth,7926 DATA Mars,4213 DATA Jupiter,88732 DATA Saturn,74975 DATA Uranus,31763 DATA Neptune,30603 ' END |
Notice that "good programming practice" has been followed in this example. Before leaving this example, be sure you recognize two important features.
Another important point is that not all of the information that was read in got printed out. Diameter was needed in order to do the calculations, but it did not appear in the output. It could have been included, but the programmer decided not to for some reason. |
Test and Debug the Program The program used in this example is available for you to download and study.
However, there is one catch...it doesn't work properly. There are a few mistakes in the program that you will need to find. Several bugs are syntax errors and several are logic errors. In order for you to gain some experience testing and debugging, download the file, run it, test it, compare it to the code provided above, and fix the mistakes. Re-save the program as planets.bas because you may need it again in the future!
Document the Program As with previous examples, the main "documentation" is to include REM statements in the program to divide it up into easily identifiable sections. In this course, you probably will not have the need to write a documentation manual for any programs. |
Check
Your Understanding
|
DATA
statements should never appear in flowcharts, but they always appear
in programs anywhere above the END statement.
|
YOUR ASSIGNMENT The program in this assignment is designed to give you experience in the following areas:
Details of this assignment are provided in the following pdf file which you should print out and carefully study. Download and print this file now: 4_6_1.pdf Submit your completed assignment "Assignment 04.06" to your instructor for a grade. |
A QBasic Review Before you move on to the next assignment, please make sure that you can do the following.
READ - DATA statements give you some powerful techniques for getting sets of data efficiently into a program and PRINT USING allows you to format variables with great precision. |
When you have completed this assignment in the Dropbox area, return to the Game Board. You are now ready for Assignment 4.07 Game Skills 2.S |