Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- CLASS: PeriodicTable
- CLASSFIELDS: elements (ARRAY OF ElementClass)
- count (integer)
- constructed (boolean)
- CONSTRUCTORS:
- SUMODULE: Default constructor
- IMPORT: none
- EXPORT: address of new PeriodicTable object
- ASSERTION: Sets default elements, count and constructed
- ALGORITHM: FOR i:=0 TO LENGTH OF elements CHANGEBY 1 DO
- FOR j:=0 TO WIDTH OF elements CHANGEBY 1 DO
- elements := null
- ENDFOR
- ENDFOR
- count := 0
- constructed := false
- END SUBMODULE
- SUBMODULE: Alternate constructor
- IMPORT: inElements (ARRAY OF ElementClass), inCount (integer), inConstructed (boolean)
- EXPORT: none
- ASSERTION: Creates object if imports are valid and FAILS otherwise
- ALGORITHM:
- i:=0
- j:=0
- WHILE(i<LENGTH OF elements AND elements[i][j] != NULL)DO
- WHILE(j<WIDTH OF elements AND elements[i][j] != NULL)DO
- elements[i][j] = inElements[i][j]
- j:=j+1
- ENDWHILE
- j:=0
- i:=i+1
- ENDWHILE
- count := inCount
- constructed := inConstructed
- END SUBMODULE
- SUBMODULE: Copy constructor
- IMPORT: inPeriodicTable (PeriodicTable)
- EXPORT: none
- ASSERTION:
- ALGORITHM:
- elements:=inPeriodicTable.getElements<- none
- count := inPeriodicTable.getCount <- none
- constructed := inPeriodicTable.getConstructed <- none
- END SUBMODULE
- MUTATORS:
- SUBMODULE: setElements
- IMPORT: inElements (ElementClass)
- EXPORT: none
- ASSERTION: Sets elements if valid
- ALGORITHM:
- i:=0
- j:=0
- WHILE(i<LENGTH OF elements AND elements[i][j] != NULL)DO
- WHILE(j<WIDTH OF elements AND elements[i][j] != NULL)DO
- elements[i][j] = inElements[i][j]
- j:=j+1
- ENDWHILE
- j:=0
- i:=i+1
- ENDWHILE
- END SUBMODULE
- SUBMODULE: setCount
- IMPORT: inCount
- EXPORT: none
- ASSERTION: Sets count if valid
- ALGORITHM:
- count := inCount
- END SUBMODULE
- SUBMODULE: setConstructed
- IMPORT: inConstructed
- EXPORT: none
- ASSERTION:
- ALGORITHM: Sets constructed
- constructed := inConstructed
- END SUBMODULE
- SUBMODULE: equals
- IMPORT: inPeriodicTable PeriodicTable
- EXPORT: same (boolean)
- ASSERTION: Two periodic tables are interchangeable if they have the same elements, count and constructed
- ALGORITHM:
- same := false
- notEqual:=0
- i:=0
- j:=0
- IF (inObject ISA PeriodicTable) THEN
- make inObject a PeriodicTable named inPeriodicTable
- IF(count = inPeriodicTable.getCount <- none) THEN
- IF(constructed = inPeriodicTable.getConstructed<- none)THEN
- WHILE(i< LENGTH OF elements AND element[i][j]!= NULL)
- WHILE(j<WIDTH OF elements AND element[i][j]!= NULL)
- IF(elements[i][j].equals <-inPeriodicTable.getElements <- none)
- same := true
- ELSE
- notEqual:=notEqual+1
- ENDIF
- j:=j+1
- ENDWHILE
- j:=0
- i:=i+1
- ENDWHILE
- ENDIF
- ENDIF
- ENDIF
- IF(notEqual>0)THEN
- same := false
- ENDIF
- END SUBMODULE
- ACCESSORS:
- SUBMODULE: getElements
- IMPORT: none
- EXPORT: elements
- END SUBMODULE
- SUBMODULE: getCount
- IMPORT: none
- EXPORT: count
- END SUBMODULE
- SUBMODULE: getConstructed
- IMPORT: none
- EXPORT: constructed
- END SUBMODULE
- SUBMODULE: readFile
- IMPORT: fileName (String)
- EXPORT: none
- ASSERTION: Reads file and constructs elements
- ALGORITHM:
- rows = countRows <- fileName
- count := 0
- i:=0
- j:=0
- theFile = OPENFILE fileName
- IF(theFile = NULL)
- FAIL(THROW EXCEPTION)
- ENDIF
- INPUT line from theFile
- WHILE(line NOT EOF)
- FOR a:=0 TO rows CHANGEBY 1 DO
- lineArray = SPLIT line ON "<"
- FOR b:= 0 TO columns CHANGEBY 1 DO
- classFields = SPLIT lineArray[b+1] ON ","
- symbol := classFields[0]
- name := classFields[1]
- atomicNumber := (CONVERT TO integer) classFields[2]
- mass := (CONVERT TO real) classFields[3]
- lastClassField := classFields[4] TAKE AWAY last two characters
- IF(lastClassField EQUALS "S", "L" OR "G" AND count<=100)THEN
- state := (CONVERT TO character)lastClassField
- elements[i][j] = CONSTRUCT new NonMetalClass USING symbol, name, atomicNumber, mass, state
- count := count + 1
- ELSE IF(FIRST LETTER OF lastClassField = '0','1','2','3','4','5','6','7','8' OR '9' AND count <=100)THEN
- conductivity := (CONVERT TO real)lastClassField
- elements[i][j] = CONSTRUCT new MetalClass USING symbol, name, atomicNumber, mass, conductivity
- count := count + 1
- ENDIF
- IF(i<LENGTH OF elements-1 AND j = WIDTH OF elements-1)THEN
- j:=0
- i:=i+1
- ELSE IF(j<LENGTH OF elements)THEN
- j:=j+1
- ENDIF
- ENDFOR
- INPUT line from theFile
- ENDFOR
- constructed := true
- ENDIF
- CLOSEFILE theFile
- END SUBMODULE
- SUBMODULE: addElement
- IMPORT: inObject (ElementClass) choice (integer)
- EXPORT:
- ASSERTION:
- ALGORITHM:
- i:=0
- j:=0
- added := false
- IF(constructed = true AND count < 100)THEN
- WHILE(i<LENGTH OF elements AND added = false)DO
- WHILE(j<WIDTH OF elements AND added = false)DO
- IF(elements[i][j]=NULL AND inObject IS AN INSTANCE OF MetalClass)THEN
- elements[i][j]=inObject
- added := true
- ELSE IF(elements[i][j]=NULL AND inObject IS AND INSTANCE OF NonMetalClass)THEN
- elements[i][j]=inObject
- added := true
- ENDIF
- j:=j+1
- ENDWHILE
- i:=i+1
- ENDWHILE
- ELSE
- END SUBMODULE
- SUBMODULE: writeFile
- IMPORT: choice (integer)
- EXPORT: none
- ASSERTION: Writes periodicTable to file
- ALGORITHM:
- theFile = OPENFILE "output.txt"
- FOR i:=0 TO LENGTH OF elements CHANGEBY 1 DO
- FOR j:=0 TO WIDTH OF elements CHANGEBY 1 DO
- IF(choice = 1 AND elements[i][j] IS AN INSTANCE OF MetalClass) THEN
- WRITE elements[i][j].getSymbol <- none+ "," +elements[i][j].getName <- none+ "," +elements[i][j].getAtomicNumber <- none+ "," +elements[i][j].getMass <- none+ "," +(CONVERT TO MetalClass)elements[i][j].getConductivity <- none
- ELSE IF(choice = 2 AND elements[i][j] IS AN INSTANCE OF NonMetalClass)THEN
- WRITE elements[i][j].getSymbol <- none+ "," +elements[i][j].getName <- none+ "," +elements[i][j].getAtomicNumber <- none+ "," +elements[i][j].getMass <- none+ "," +(CONVERT TO MetalClass)elements[i][j].getState <- none
- ENDIF
- ENDFOR
- ENDFOR
- END SUBMODULE
- SUBMODULE: clone
- IMPORT:
- EXPORT: clonePeriodicTable (Object)
- ASSERTION:
- ALGORITHM:
- CONSTRUCT clonePeriodicTable USING ALTERNATE CONSTRUCTROR WITH elements, count, constructed
- END SUBMODULE
- SUBMODULE: toString
- IMPORT:
- EXPORT:
- ASSERTION:
- ALGORITHM:
- outString = ""
- FOR(i:=0 TO LENGTH OF elements CHANGEBY 1)
- FOR(j:=0 TO WIDTH Of elements CHANGEBY 1)
- IF(elements[i][j] != NULL)
- IF(elements[i][j] IS AN INSTANCE of MetalClass) THEN
- outString = outString+ elements.getName <- none + ", " +elements.getSymbol <- none + ", " +elements.getAtomicNumber <- none + ", " +elements.getMass <- none+ ", " +(CONVERT TO MetalClass)elements.getConductivity <- none
- ELSE IF(elements[i][j] IS AN INSTANCE OF NonMetalClass) THEN
- outString = outString+ elements.getName <- none + ", " +elements.getSymbol <- none + ", " +elements.getAtomicNumber <- none + ", " +elements.getMass <- none+ ", " +(CONVERT TO NonMetalClass)elements.getState <- none
- ENDIF
- ENDFOR
- ENDFOR
- outString = outString + " count: " +count+ " constructed: " +constructed
- END SUBMODULE
- PRIVATE SUBMODULES:
- SUBMODULE: countRows
- IMPORT: fileName (String)
- EXPORT: rows (integer)
- ASSERTION: Counts number of rows in file
- ALGORITHM:
- theFile = OPENFILE fileName
- IF theFile CAN'T BE OPENED THEN
- FAIL (THROW EXCEPTION)
- ENDIF
- rows := 0
- INPUT line from theFile
- WHILE (line NOT EOF)
- numLines = numLines + 1
- INPUT line from theFile
- ENDWHILE
- CLOSEFILE theFile
- END SUBMODULE
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement