## ENGG1811 Lab 5: Using The OO Basic Editor

During the first hour of the lab class you will complete the MidTerm exam (details provided separately). This document covers the practical work for the second hour.

### Objectives

After completing this lab, students should be able to

• Enter and run a simple Basic program using the OpenOffice Basic Editor
• Use constants, variables and expressions to solve a simple problem
• Explain how macros are run from within an OpenOffice application

### Assessment

As with most labs, one mark is awarded to successful attempts at Online Assessment question. The exercise will require you to enter a series of assignment statements, which you will need to embed in a subprogram, similar to the main exercise.

For the other two marks your tutor will ask you to demonstrate your program. As with all labs in this course, you may be asked to explain your solution and how you devised it. Your tutor will record whether you have successfully completed the work, provided he or she is confident you fully understand the solution and it is reasonably well presented.

You should get used to formatting the program well, using meaningful names, indentation, avoiding literal numbers like 1000 or 3.14159 inside procedures.

### The Equatorial Rope Paradox

The example in last week's lectures read a value, did a small calculation and displayed the result on a window using MsgBox. This exercise uses a similar kind of subprogram to calculate the result of the following problem, which has a non-intuitive answer:

 Imagine a rope circling the Earth at the equator, tight against the surface (assume the earth is a spheroid and the equator is circular). If you were to cut the rope, insert a one metre long section, and move the rope uniformly away from the surface, how far away would the rope be from the Earth now? (Most people guess a fraction of a millimetre.) You can solve this with simple geometry, since circumference and radius of a circle are related by the obvious formula C = 2πR where R is the radius and C is the circumference. And yes, there is an obvious analytical solution to the problem, but we're just using it as an example of a sequential (step-by-step) algorithm.

• Download the workbook lab05.ods to the Desktop. Later save it to a USB drive if you have brought one, or your Z: drive, which is preserved. Of course you may have done the exercise at home. The workbook contains very little content, you could well have started with a new workbook.

• Navigate to Tools - Macros - Organize Macros - OpenOffice Basic... then press Organizer. Click on the + next to lab05.ods, and then the Standard icon (if there is one). Press New. Call the module RopeParadox or similar. Press Edit to open the editor.

• The subprogram to solve the problem will use the structure from the lecture example.

Start with a comment describing the problem, and your name as author, plus today's date.

• Then we need two important constants, one defining the value of π and one the radius of the Earth at the equator, which is 6378.137 kilometres using the GPS world model. By convention their names are written in upper case:

```Option Explicit   ' require all variables to be declared

' Calculates how far out from the equator's surface
' a rope 1m longer than the equatorial circumference would lie.

' Author:   ______________________
' Written:  __/__/__

Const PI = 3.14159265358979
Const EQUATOR_RADIUS = 6378.137  '  semi-major axis of the WGS 84 Ellipsoid, in km
' Ref: National Geospatial Intelligence Agency,
' http://earth-info.nga.mil/GandG/wgs84/gravitymod/egm96/egm96.html
``` • Next, define a constant for the conversion factor (say KILO) between km and m.

• Following the constants change the default subprogram name from Main to ShowRopeParadox.

• Within the subprogram, indented, you will need to declare variables to hold the various calculated circumferences and radii. You need one for each of: the extra length, earth's circumference, the rope circumference after the extra length is inserted, the new rope radius, and the difference in radii, which is the answer we want.

The variable names should be two or three-word phrases in TitleCaseRunTogether, but with an initial small letter so it's clearly a variable name. You can abbreviate if it's obvious. For example,

```    Dim ropeCircum As Double    ' circumference of rope after adding the extra bit
```
• The extra rope length (1m in the original puzzle) should be obtained from the user width the InputBox function, similar to the lecture example, but assigned to the first of your appropriately variables.

• Now use assignment statements to calculate values for each of the other variables in turn. Think about how you'd do the calculations on paper, one step at a time (don't try to solve the whole thing at once). In all forms of Basic, the notation is

```variable = expression
```
where variable is the name of the variable to be assigned to, and expression comprises variables, literal numbers and named constants, combined with the usual arithmetic operators (but which is the appropriate division operator?). Remember that the equatorial radius is in kilometres but the extra length is in metres (1.0 to be exact). The answer should also be in metres, so you'll have to apply an appropriate factor.
• Having worked out the difference in radii, use MsgBox to show its value on a pop-up window, along with a brief message. Glue the parts of the message together with the & operator. Again it's like the lecture example: what you're doing is adopting a solution pattern and adapting it to suit your particular needs, a common approach.

• As you type the program make use of the clipboard to save retyping variable names. Double-click or highlight, Ctrl-C, click, Ctrl-V is quick and easy. Check the program for obvious errors. The compile icon (the down arrow next to the library name) can find some errors, but only gross syntax violations.
• You can run the subprogram one of two ways: the quickest is to press F5 or the little blue triangle, but you should also try the sheet menu Tools - Macros - Run Macro.

• We won't give the answer away, but if the number your program produces is negative, or really tiny (say 1e-9 metres) or really huge (say several kilometres) it's probably wrong. Recheck the statements carefully, especially the conversion between km and m.

• To show why it's a paradox, now change the constant holding the equatorial radius to 0.55, which is the approximate radius in km of the roughly circular cycling track around Centennial Park. Note how you only had to change the constant value at one place, even though you probably used it twice.

• Show your program to the tutor, and explain the results. If you want full marks from them, you will have to investigate how to use the Format function to express the answer to 3 decimal places (Hint: the format code is "0.000"). This produces a much more satisfying result.