-- Some examples in JJ to cut-and-paste and run and change -- Shows many aspects of the JJ language -- Name Anonymous -- Does simple interaction with a user -- Shows the input and output of Strings Box reply ofClass Str Output "Who is buried in Grant's tomb? " Input reply Outputln reply Output "Grant is buried in Grant's tomb!" ---------------------- Cut here ----------------------------- -- Name An Onymous -- Does more interaction with a user -- Shows input, output and comparison of integers Box reply ofType int Output "What year did humans first land on the moon? " Input reply Outputln reply If (reply == 1969) then Output "Correct. " Else Output "Humans landed in 1969." EndIf ------------------------------------------------------------- -- Name A Nonymous -- Does interact even more with a user -- Shows input, output and comparison of strings Boxes reply, name ofClass Str Output "Enter your name: " Input name Outputln name Output "Who is buried in Grant's tomb? " Input reply Outputln reply -- echo If (reply.equals ("Grant")) then Output name + " that is correct." Else Output "Grant is buried there." EndIf ------------------------------------------------------------- -- Name Anon Ymous -- Does charge, depending on age -- Shows 'nice' nest of the choice form Boxes age, charge ofType int Output "Enter the age " Input age Outputln age If (age <= 12) then Set charge = 2 ElseIf (age <= 21) then Set charge = 3 ElseIf (age <= 65) then Set charge = 5 Else -- age > 65 Set charge = 4 EndIf Output "The charge is " Outputln charge ------------------------------------------------------------- -- Name Anon Amous -- Does find max of many reals ending with any negative value -- Shows a loop with nested choice Boxes max, value ofType real Outputln "Enter values; end with any negative " Set max = 0.0 Repeat Output "Enter " -- prompt Input value -- enter Outputln value -- echo ExitOn (value < 0.0) If (max < value) then Set max = value EndIf EndRepeat Output "The maximum value is " Outputln max -------------------------------------------------------------- -- Name An O'minous -- Does factorial -- Shows the loop form to repeat -- Try an input to exceed the largest int; carefull! Boxes fact, index, num ofType int Output "Enter a number " Input num Outputln num Set fact = 1 Set index = 1 Repeat ExitOn (index > num) Set fact = fact * index Inc index by 1 EndRepeat Outputln "The factorial is " Outputln fact Outputln " " -- gap between tries ------------------------------------------------------------- -- Name Anomin Os -- Does logical processing -- Shows boolean types and operations -- Try testing all 8 of the combinations Boxes early, tied, rain, play ofType bool Boxes inning, amount ofType int Output "Enter the inning " Input inning Outputln inning Set early = (inning <= 9) Outputln "Is the score tied? " Outputln "Enter true or false " Input tied Outputln tied Output "Enter rain in millimeters " Input amount Outputln amount Set rain = (amount > 0 ) Set play = (tied or early) and not rain If play then Outputln "Play ball" Else Outputln "Don't play" EndIf Outputln " " -- gap between tests ------------------------------------------------------------- -- Name Ano Nymous -- Does factorial recursively -- Shows the definition and use of a function Function factorial (num) ofType int Slot num ofType int -- Does factorial recursively Box result ofType int If (num == 0) then Set result = 1 Else Set result = num * factorial (num - 1) EndIf EndFunction factorial Routine mainTest (none) Box num ofType int -- Does test factorial function Start Output "Enter a number " Input num Outputln num Output "The factorial is " Outputln factorial (num) EndRoutine mainTest -------------------------------------------------------------- -- Name Anony Mous -- Does output a string many times -- Shows the definition and use of a routine Routine mainRoutine (none) Box age ofType int Box greet ofClass Str -- Does repeat "Ho" or other greeting (happy birthday\n) Start Outputln "How old are you ? " Input age Outputln age Call outRow with ("Ho ", age) Outputln " " EndRoutine Routine outRow (that, many) Slot that ofClass Str Slot many ofType int -- Does output a row of that string many times Repeat ExitOn (many == 0) Output that Dec many by 1 EndRepeat EndRoutine outRow ------------------------------------------------------------- -- Name Anon A Mous -- Does Inventory of many items -- Shows array Boxes item, size, i, last ofType int Boxes total, worth ofType real Box price ofType real[] -- array of prices Box quant ofType int [] -- array of quantities -- Setup arrays of given size Set size = 4 -- whatever Set last = size - 1 NewArray price ofType real[size] NewArray quant ofType int [size] -- Set the prices of all items Set price[0] = 0.10 Set price[1] = 0.30 Set price[2] = 0.50 Set price[3] = 0.70 -- Get the quantities of items Outputln "Enter quantities of each item " Set item = 0 Repeat ExitOn (item > last) Output item Output " " -- gap Input quant [item] Outputln quant[item] Inc item by 1 EndRepeat -- Compute the total worth Set worth = 0.00 Set i = 0 Repeat ExitOn (i > last) Set total = price[i] * IntToReal (quant[i]) Set worth = worth + total Inc i by 1 EndRepeat -- Output the results Output "The total worth is " Outputln worth ------------------------------------------------------------- Import JJIO Class Time -- Does provide military time instants -- Name Ano Nymous Box hour ofType int is public -- 0 to 23 hours Box min ofType int is public -- 0 to 59 minutes Constructor Time (h, m) is public Slot h ofType int Slot m ofType int -- Does set New hour and min Set hour = h Set min = m EndConstructor Time Function mpm (none) ofType int is public Box result ofType int -- does return minutes past midnight Set result = 60 * hour + min EndFunction mpm Function isValid (none) ofType bool is public Box result ofType bool -- Does indicate if time is possible If (hour > 0) and (hour <= 24) and (min >= 0) and (min < 60) then Set result = true Else Set result = false EndIf EndFunction isValid Routine showTime (none) is public -- Does output military time Output hour Output ":" -- separates hour & min If (min < 10) then Output "0" -- single digit minute EndIf Output min EndRoutine showTime Routine test (none) is public Boxes first, second ofClass Time -- Does use the Time class Start New first ofClass Time with (12,45) New second ofClass Time with (14,30) If first.isValid() and second.isValid() then Output "The time difference is " Output second.mpm() - first.mpm() Output " minutes." Else Outputln "Error in time " EndIf EndRoutine test EndClass Time -------------------------------------------------------------