Learning to program (iii) – an intermediary Julia program (Julia 1.0)

Last time the Julia program we wrote worked on Silver Maples only. Now we will try and extend it to work with a series of different tree species. Here is the program as it stands.

println("Enter the circumference of the tree (inches): ") 
c = parse(Int, chomp(readline())) 
d = c / pi 
g = 3.0 
age = d * g 
println("The tree is ", round(age,2), " years old")

Now to add more tree species requires the use of a sort of menu. Here is a piece of code to print a menu for the user to select an option from:

println("Tree Species: ")
println("(1) Silver Maple")
println("(2) White Birch")
println("(3) Black Walnut")
println("(4) Red Oak")
println("(5) White Oak")
println("(6) American Elm")
print("Choose a tree (?): ")
t = parse(Int, chomp(readline()))

It basically prints out a table of six tree species, then prompts the user to enter the number which represents the menu item of the tree they want. The only thing missing is the growth factor associated with each tree? Now we need to use the number entered to assign a value to the variable “g“. This can be achieved using an if statement. The if statement is used in just about every language to make decisions. Basically we want to ask:

if the user inputs “1”, then the value of g is 3.0, because this is the growth factor for a Silver Maple.
if the user inputs “2”, then the value of g is 5.0, because this is the growth factor for a White Birch.
etc.

Here is the piece of code that does that:

if (t == 1)
   g = 3.0
elseif (t == 2)
   g = 5.0
elseif (t == 3)
   g = 4.5
elseif (t == 4)
   g = 4.0
elseif (t == 5)
   g = 5.0
elseif (t == 6)
   g = 4.0
end

Only one value of g will be assigned, depending on the value of t, which was the menu item selected by the user. Now let’s look at the whole program:

println("Enter the circumference of the tree (inches): ")
c = parse(Int, chomp(readline()))

println("Tree Species: ")
println("(1) Silver Maple")
println("(2) White Birch")
println("(3) Black Walnut")
println("(4) Red Oak")
println("(5) White Oak")
println("(6) American Elm")
print("Choose a tree: ")
t = parse(Int, chomp(readline()))

if (t == 1)
   g = 3.0
elseif (t == 2)
   g = 5.0
elseif (t == 3)
   g = 4.5
elseif (t == 4)
   g = 4.0
elseif (t == 5)
   g = 5.0
elseif (t == 6)
   g = 4.0
end

d = c / pi
age = d * g
println("The tree is ", round(age,digits=2), " years old")

Now the program is more functional, and technically, any number of tree species can be added to the if statement to expand its usability.

 

 

 

Why Cobol is feared.

In the course I teach on legacy software, Cobol is the most feared of all languages. Why? Probably because it does not look like any language students have seen before. After a couple of weeks of coding Cobol, they often run away screaming, or sit very silently in a corner. It is feared largely because it doesn’t things in different ways – and this can lead even the most confident programmer to have doubts about what they are coding. The challenge is that a Cobol program may run, even though there are inadequacies in the syntax. Not grievous issues, but small silent things. A good example is a cascading if statement. A C programmer is normally happy writing code which looks like this:

if (aNumber < 0)
    printf("the number is negative");
else if (aNumber > 0)
    printf("the number is positive");
else
    printf("the number is zero");

A novice Cobol programmer will try something of the form:

if aNumber is < 0 then
    display "the number is negative"
else if aNumber is > 0 then
    display "the number is positive"
else
    display "the number is zero"
end-if

This will work, however it will raise a warning of the form “IF statement not terminated by END-IF“. In more complex code, it could lead to problems with the program logic. The code should be written as:

if aNumber is < 0 then
    display "the number is negative"
else if aNumber is > 0 then
         display "the number is positive"
     else
         display "the number is zero"
     end-if
end-if

Yet Cobol provides another, simpler way to write this piece of code:

evaluate true
    when aNumber < 0 display "negative"
    when aNumber > 0 display "positive"
    when aNumber = 0 display "zero"
end-evaluate.

So Cobol need not be feared. It is just different.