Skip to main content

Right Triangle Routine

This is the main section.


RightTriangle  /* compute area and hypotenuse of a right triangle
                  this routine contains examples of 
                  new ObjectScript features */
    Write !, "Compute the area and hypotenuse of a right triangle",
          !, "given the lengths of its two sides."
    Read !!, "First, choose a unit of measurement. ",
          !, "(i)nches, (f)eet, (m)iles, ",
             "(c)entimeters, m(e)ters, (k)ilometers: ", units
    // translate units to a full word
    Set units = $case( $extract( units, 1), "i":"inches", 
                                            "f":"feet",
                                            "m":"miles",  
                                            "c":"centimeters",
                                            "e":"meters", 
                                            "k":"kilometers",
                                               :"units" )
    Do { 
        Read !!, "Length of side 1: ", side1
        Quit:(side1 = "")  // exit the do loop
    }
    While $$IsNegative( side1 )
    Quit:(side1 = "")  // exit the routine
    Do { 
        Read !, "Length of side 2: ", side2
        Quit:(side2 = "")   // exit the do loop
       }
    While $$IsNegative( side2 )
    Quit:(side2 = "")  // exit the routine
    Do Compute( units, side1, side2)
    Write !!, "Current date: "
    Do ^%D
    Write !, "Current time:"
    Do ^%T
    Quit

This section contains a user-defined function and a procedure.


IsNegative(num) PUBLIC // is num negative?
{ 
    If (num '> 0) {
        Write "  Enter a positive number."
        Quit 1 // return "true"
       }
     Else {
        Write "  Accepted."
        Quit 0 // return "false"
     }
}

Compute(units,A,B) // compute and display area and hypotenuse
{ 
    Set area  = ( A * B ) / 2, 
        area = $justify( area, 0, 2),
        squaredSides = ( A ** 2 ) + ( B ** 2 )  
    
    // $zsqr function computes square root
    Set hypot = $zsqr(squaredSides)                
    
    // round hypot to 2 places
    Set hypot = $justify( hypot, 0, 2)
    
    Write !!, "The area of this triangle is ", area, " square ", units, ".",
          !!, "The hypotenuse is ", hypot, " ", units, "."
}
FeedbackOpens in a new tab