Java Q&A
by Joseph Kiniry, John Motil, and David Epstein


Table 1:

Command               BNF

ImportCommand       'Import' ImportLib
ClassCommand        'Class' <classname>
EndClassCommand     'EndClass' <classname>
ConstantCommand     'Constant' <constantname> '=' Value 'ofType' PrimitiveTypeCategory 'is' Visibility
ClassBoxCommand     'Box' <boxname> Type 'is' Visibility
ClassBoxesCommand   'Boxes' <boxname> (',' <boxname>)+ Type 'are' Visibility
InvariantCommand    'Invariant'
CheckCommand        'Check' ScalarBoolExpr 'bounce' StringLiteral
EndInvariantCommand 'EndInvariant'
ConstructorCommand  'Constructor' <classname> '(' SlotDeclList ') is public'
EndConstructorCommand 'EndConstructor' <classname>
RoutineCommand      'Routine' <routinename> '(' SlotDeclList ') is' Visibility
StartCommand        'Start'
EndRoutineCommand   'EndRoutine' <routinename>
FunctionCommand     'Function' <functionname> Type '(' SlotDeclList ') is' Visibility
EndFunctionCommand  'EndFunction' <functionname>
PreCheckCommand     'PreCheck' ScalarBoolExpr 'bounce' StringLiteral
PostCheckCommand    'PostCheck' ScalarBoolExpr 'bounce' StringLiteral
SlotCommand         'Slot' <slotname> Type
LocalBoxCommand     'Box' <boxname> Type
LocalBoxesCommand   'Boxes' <boxname> (',' <boxname>)+ Type
IfCommand           'If' ScalarBoolExpr 'then'
ElseIfCommand       'ElseIf' ScalarBoolExpr 'then'
ElseCommand         'Else'
EndIfCommand        'EndIf'
RepeatCommand       'Repeat'
ExitOnCommand       'ExitOn' ScalarBoolExpr
EndRepeatCommand    'EndRepeat'
SetCommand          'Set' Assignment
CallCommand         'Call' RoutineReference
NewArrayCommand     'NewArray' <arrayname> Type '[' ScalarIntExpr ']'
NewCommand          'New' LhsExpr 'ofClass' ClassTypeCategory [WithSlots]
InputCommand        'Input' <varname>
OutputCommand       'Output' Expr
OutputlnCommand     'Outputln' Expr
DebugCommand        'Debug' Expr
DebuglnCommand      'Debugln' Expr
TryCallCommand      'TryCall' FileIORoutineReference 'OnFail' RoutineReferenceOrAssignment
TrySetCommand       'TrySet' StringConversionAssignment 'OnFail' RoutineReferenceOrAssignment

Example 1:
(a)
Output "hello, world"

(b)
Routine myFirstRoutine(none)
 -- Does outputs the message "hello, world"
 Start
   Output "hello, world"
EndRoutine myFirstRoutine

(c)
Import JJIO
Class MyFirstClass
 --Name: userID goes here
 Routine myFirstRoutine(none)
  -- Does outputs the message "hello, world"
  Start
    Output "hello, world"
 EndRoutine myFirstRoutine
EndClass MyFirstClass

Example 2:
(a)
Routine credit (amount) is public
   Slot amount ofType real
-- Does deposit an amount into account
    PreCheck (amount >= 0.00) bounce "Negative deposit!"

(b)
PreCheck (amount >= 0.00) bounce "Negative deposit!"

(c)
if (amount >= 0.00) JJSystem.halt("Negative deposit!");


Listing One
Import JJIO
Class Account
-- Name Ann Onymous
-- Does provide a simple savings bank account
-- Shows much of the programming language JJ
-- Shows PbC, Programming by Contract
-- Data attributes, fields
   Box balance ofType real is private 
   Box identity ofClass Str is public 

   Invariant
      Check (balance >= 0.00) 
         bounce "Negative balance!"
      Check (identity != null) bounce "Null id!"
   EndInvariant

   Constructor Account (b, i) is public
      Slot b ofType real -- account balance
      Slot i ofClass Str -- identification
   -- Does initialize or open the account
      PreCheck (b >= 0.00) 
         bounce "Negative initial balance!"
      PreCheck (i != null) 
         bounce "Null initial identity!"
      Set balance = b 
      Set identity = i
   EndConstructor Account

   Routine setBal (amount) is public
      Slot amount ofType real
   -- Does set or reset the amount of balance
      PreCheck (amount >= 0.00) 
         bounce "Setting negative balance!"
      Set balance = amount
   EndRoutine setBal

   Function getBal (none) ofType real is public
      Box result ofType real
   -- Does return balance in account
      Set result = balance
   EndFunction getBal

   Routine credit (amount) is public
      Slot amount ofType real
   -- Does deposit an amount into account
      PreCheck (amount >= 0.00) 
         bounce "Negative deposit!"
      Set balance = balance + amount
   EndRoutine credit

   Routine debit (amount) is public
      Slot amount ofType real
   -- Does withdraw an amount from account
      PreCheck (amount >= 0.00) 
         bounce "Negative debit!"
      PreCheck (amount <= balance) 
         bounce "Insufficient balance!"
      Set balance = balance - amount
   EndRoutine debit

   Function canCover (amount) ofType bool is public
      Slot amount ofType real
      Box result ofType bool
   -- Does check if balance covers amount
      Set result = (balance >= amount)
   EndFunction canCover
   
   Routine compound (percent, duration) is public
      Slot percent ofType real -- percent rate
      Slot duration ofType int -- time duration 
      Box rate ofType real -- interest rate
   -- Does compound balance at a rate for a time
      PreCheck (duration >= 1) 
         bounce "Negative duration!"
      Set rate = percent / 100.0
      Repeat
      ExitOn (duration == 0)
         Set balance = balance + balance * rate 
         Dec duration by 1
      EndRepeat
   EndRoutine compound

   Routine getFrom (account, amount) is public
      Slot account ofClass Account
      Slot amount ofType real
   -- Does transfer from one account to this
      PreCheck (account.balance >= amount) 
         bounce "Insufficient balance!"
      Call account.debit with (amount)
      Call credit with (amount)
   EndRoutine getFrom

   Routine show (none) is public
   -- Does display the account
      Output "Account " + identity + " has balance "
      Outputln balance
   EndRoutine show

   Routine test (none) is private
      Box his ofClass Account
      Box her ofClass Account
      Box amount ofType real
   -- Does test Account class
   Start
      New his ofClass Account with (100.0, "Dad")
      New her ofClass Account with ( 0.0, "Mom")
      Call his.credit with (200.00)
      Call his.compound with (10.0, 8)
      Call his.show
      Output "Enter her deposit "
      Input amount
      Outputln amount -- echo
      Call her.setBal with (amount)
      If his.canCover (600.00) then
         Call her.getFrom with (his, 600.00)
      Else
         Outputln "No transfer possible"
      EndIf
      Call her.debit with (300.00)
      Output "Her balance is " 
      Outputln her.getBal()
   EndRoutine test

EndClass Account

Listing Two
Import JJGui
Class TicTacToe
--Name: ?? (replace '??' to match your login name)
 Box   buttons   ofClass JJButton[] is private
 Box   ta        ofClass JJTextArea is private
 Box   gp        ofClass JJGridPanel is private
 Box   bp        ofClass JJBorderPanel is private
 Box   whoseTurn ofClass Str is private

 Constructor TicTacToe(none) is public
  Box i ofType int
   --allocate an array and loop thru New buttons
   NewArray buttons ofClass JJButton[9]
   Set i = 0
   Repeat
      New buttons[i] ofClass JJButton with ("")
      Inc i by 1
   ExitOn (i == 9)
   EndRepeat
   New ta ofClass JJTextArea with
    ("X's turn: To Start, click a button")
   New gp ofClass JJGridPanel with (3,3)
   New bp ofClass JJBorderPanel
   Set whoseTurn = "X"
 EndConstructor TicTacToe

 Routine ActsAsMain(none) is public
  Box i ofType int
   Start -- tells JJ that this is the "main"
   --loop thru adding buttons to panel
   Set i = 0
   Repeat
      Call gp.add with (buttons[i])
      Inc i by 1
   ExitOn (i == 9)
   EndRepeat
   Call bp.add with ("Center", gp)
   Call bp.add with ("South", ta)
   -- The next call is needed exactly once.
   Call bp.jjShowAsMainPanel
 EndRoutine ActsAsMain

 -- when a button is pressed, jjHandleButtonPress is
 -- automatically called with the button that was pressed.
 Routine jjHandleButtonPress(b) is public
  Slot b ofClass JJButton
  Box ok ofType bool
  Box i ofType int
  Box buttonLabel ofClass Str
  Box bCurr ofClass JJButton
   Set ok = false
   --loop thru buttons until finding the pressed one
   Set i = 0
   Repeat
      Set bCurr = buttons[i]
      If (b == bCurr) then
         Set buttonLabel = bCurr.getLabel()
         If (buttonLabel.length() == 0) then
            Set ok = true
            Call bCurr.setLabel with (whoseTurn)
         Else
            Call ta.setText with
             ("*** ERROR *** Button already pressed")
         EndIf
      EndIf
      Inc i by 1
   ExitOn (i == 9)
   EndRepeat
   If (ok) then
      If whoseTurn.equals("X") then
         Set whoseTurn = "O"
         Call ta.setText with
          ("O's next: Please click a button")
      Else
         Set whoseTurn = "X"
         Call ta.setText with
          ("X's next: Please click a button")
      EndIf
   EndIf
 EndRoutine jjHandleButtonPress
EndClass TicTacToe





1


