Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*************************************************
- * AUTHOR: Nathan Suhardiman (18820225) *
- * FILE NAME: PeriodicTable *
- * PURPOSE: Contains elements, an array of *
- * objects of ElementClass, count and constructed,*
- * as well as the functions that adds elements to *
- * array and writes them to a file *
- * ***********************************************/
- import java.io.*;
- public class PeriodicTable
- {
- //CLASSFIELDS
- private ElementClass[][] elements;
- private int count;
- private boolean constructed;
- /***********************************************
- * SUBMODULE: Default constructor *
- * IMPORT: none *
- * EXPORT: address of new PeriodicTable object *
- * ASSERTION: *
- * *********************************************/
- public PeriodicTable()
- {
- elements = new ElementClass[20][5];
- for(int i=0;i<elements.length;i++)
- {
- for(int j=0;j<elements[0].length;j++)
- {
- elements[i][j] = null; //Sets elements in default array to null
- }
- }
- count = 0; //Sets default
- constructed = false;
- }
- /**********************************************
- * SUBMODULE: Alternate constructor *
- * IMPORT: none *
- * EXPORT: address of new PeriodicTable object *
- * ASSERTION: *
- * ********************************************/
- public PeriodicTable(ElementClass[][] inElements, int inCount, boolean inConstructed)
- {
- int i=0,j=0;
- while(i<elements.length && elements[i][j]!=null)
- {
- while(j<elements[0].length && elements[i][j]!=null)
- {
- elements[i][j]=inElements[i][j]; //Sets elements of elements as inElements
- j++;
- }
- j=0;
- i++;
- }
- count = inCount;
- constructed = inConstructed;
- }
- /**********************************************
- * SUBMODULE: Copy constructor *
- * IMPORT: none *
- * EXPORT: address of new PeriodicTable object *
- * ASSERTION: *
- * ********************************************/
- public PeriodicTable(PeriodicTable inPeriodicTable)
- {
- elements=inPeriodicTable.getElements();
- count=inPeriodicTable.getCount();
- constructed=inPeriodicTable.getConstructed();
- }
- //MUTATORS
- /********************************************
- * SUBMODULE: setElements *
- * IMPORT: inElements (ARRAY OF ElementClass)*
- * EXPORT: none *
- * ASSERTION: sets elements if valid *
- * ******************************************/
- public void setElements(ElementClass[][] inElements)
- {
- int i=0, j=0;
- while(i<elements.length && elements[i][j]!=null)
- {
- while(j<elements[0].length && elements[i][j]!=null)
- {
- elements[i][j]=inElements[i][j]; //If inElements is imported, elements of elements is set
- j++;
- }
- j=0;
- i++;
- }
- }
- /********************************************
- * SUBMODULE: setCount *
- * IMPORT: inCount (integer) *
- * EXPORT: none *
- * ASSERTION:sets count *
- * ******************************************/
- public void setCount(int inCount)
- {
- count = inCount;
- }
- /*******************************************
- * SUBMODULE: setConstructed *
- * IMPORT: inConstructed (boolean) *
- * EXPORT: none *
- * ASSERTION: sets constructed *
- * *****************************************/
- public void setConstructed(boolean inConstructed)
- {
- constructed = inConstructed;
- }
- /*******************************************
- * SUBMODULE: equals *
- * IMPORT: inPeriodicTable *
- * EXPORT: same (boolean) *
- * ASSERTION: Two Periodic tables are *
- * interchangeable if contents are the same *
- * *****************************************/
- public boolean equals(Object inObject)
- {
- boolean same = false;
- int notEqual = 0, i=0, j=0;
- if(inObject instanceof PeriodicTable)
- {
- PeriodicTable inPeriodicTable = (PeriodicTable)inObject;
- if(count == inPeriodicTable.getCount())
- {
- if(constructed == inPeriodicTable.getConstructed())
- {
- while(i<elements.length && elements[i][j] != null)
- {
- while(j<elements[0].length && elements[i][j] != null)
- {
- if(elements[i][j] == inPeriodicTable.getElements()[i][j])
- {
- same = true;
- }
- else
- {
- notEqual++;
- }
- j++;
- }
- j=0;
- i++;
- }
- }
- }
- }
- if(notEqual>0) //If one or more elements in either arrays aren't equal, returns false
- {
- same = false;
- }
- return same;
- }
- public ElementClass[][] getElements()
- {
- return elements;
- }
- public int getCount()
- {
- return count;
- }
- public boolean getConstructed()
- {
- return constructed;
- }
- /********************************************
- * SUBMODULE: readFile *
- * IMPORT: fileName (String) *
- * EXPORT: none *
- * ASSERTION: Reads file and if valid, adds *
- * contents to array of ElementClass *
- * ******************************************/
- public void readFile(String fileName)
- {
- FileInputStream fileStrm = null;
- InputStreamReader rdr;
- BufferedReader bufRdr;
- int columns, rows;
- int i=0, j=0;
- String line;
- rows = countRows(fileName); //Calls method to count rows in file
- try
- {
- fileStrm = new FileInputStream(fileName);
- rdr = new InputStreamReader(fileStrm);
- bufRdr = new BufferedReader(rdr);
- if(fileStrm == null) // If file doesn't exist, throws exception
- {
- IOException e = new IOException("No file found");
- throw e;
- }
- line = bufRdr.readLine(); //Reads first line
- for(int a=0;a<rows;a++) //Increment by rows
- {
- String[] lineArray = line.split("<"); //Splits row into columns
- columns = lineArray.length -1; //Length of column is set
- for(int b=0;b<columns; b++) //Incrments by columns in this row
- {
- String[] classFields = lineArray[b+1].split(","); //Splits each column by classfields it contains
- String symbol = classFields[0]; //Symbol is first class field
- String name = classFields[1]; //Name is second class field
- int atomicNumber = Integer.parseInt(classFields[2]); //Atomic number is third class field
- double mass = Double.parseDouble(classFields[3]); //Mass is fourth class field
- //Last class field is ambiguous for now
- String lastClassField = classFields[4].substring(0, classFields[4].length()-2);
- //If last class field is a state, ie. 'G', 'L' or 'S'.
- if(lastClassField.equals("S") || lastClassField.equals("L") || lastClassField.equals("G") && count <=100)
- {
- char state = lastClassField.charAt(0); //Sets state
- //Constructs new element of NonMtalClass
- elements[i][j] = new NonMetalClass(symbol, name, atomicNumber, mass, state);
- count++; //Increments number of elements in periodic table
- }
- else if(count<=100) //Else it should be a number (This submodule can only be called if file was valid)
- {
- double conductivity = Double.parseDouble(lastClassField); //Sets conductivity
- //Constructs new element of MetalClass
- elements[i][j] = new MetalClass(symbol, name, atomicNumber, mass, conductivity);
- count++; //Increments number of elements in periodic table
- }
- if(i<elements.length-1 && j == elements[0].length-1) //If at end of row of periodic table, goes to nxt row
- {
- j=0;
- i++;
- }
- else if(j<elements[0].length) //If there is still a vacant spot in row, it moves to that spot.
- {
- j++;
- }
- }
- line=bufRdr.readLine(); //Reads next row of file
- }
- constructed = true; //File has been read now
- }
- catch(IOException e)
- {
- if(fileStrm != null)
- {
- try
- {
- fileStrm.close();
- }
- catch(IOException ex1)
- {
- }
- }
- }
- }
- /**************************************
- * SUBMODULE: addElement *
- * IMPORT: inObject (Object) *
- * EXPORT: none *
- * ASSERTION: Adds element to periodic *
- * table *
- * ************************************/
- public void addElement(Object inObject)
- {
- ElementClass inElement;
- inElement = (ElementClass)inObject;
- System.out.println(inElement.getName());
- boolean filled = false;
- for(int i=0;i<elements.length;i++) //Increments by rows
- {
- for(int j=0;j<elements[0].length;j++) //Increments by columns
- {
- if(elements[i][j]==null && filled == false)
- {
- elements[i][j] = inElement; //Adds element to next vacant spot
- filled = true; //Once filled, it wont attempt to add more elements until function is called again
- }
- }
- }
- count++; //Increments number of elements in array elements
- }
- /************************************
- * SUBMODULE: writeFile *
- * IMPORT: choice (integer) *
- * EXPORT: none *
- * ASSERTION: Writes metals or non- *
- * metals to file *
- * **********************************/
- public void writeFile(int choice)
- {
- FileOutputStream fileStrm = null;
- PrintWriter pw;
- try
- {
- String fileName = "output.txt"; //File name is ser to "output.txt"
- fileStrm = new FileOutputStream(fileName);
- pw = new PrintWriter(fileStrm);
- for(int i=0;i<elements.length;i++)
- {
- for(int j=0;j<elements[0].length;j++)
- {
- if(choice==1 && elements[i][j] instanceof MetalClass) //If user selected option 1, metals are printed
- {
- pw.println(elements[i][j].getSymbol()+ "," +elements[i][j].getName()+ "," +elements[i][j].getAtomicNumber()+ "," +elements[i][j].getMass()+ "," +((MetalClass)elements[i][j]).getConductivity());
- }
- else if(choice == 2 && elements[i][j] instanceof NonMetalClass) //Else non-metals are printed
- {
- pw.println(elements[i][j].getSymbol()+ "," +elements[i][j].getName()+ "," +elements[i][j].getAtomicNumber()+ "," +elements[i][j].getMass()+ "," +((NonMetalClass)elements[i][j]).getState());
- }
- }
- }
- pw.close();
- }
- catch(IOException e)
- {
- if(fileStrm != null)
- {
- try
- {
- fileStrm.close();
- }
- catch(IOException ex2)
- {
- }
- }
- }
- }
- /**********************************************
- * SUBMODULE: toString *
- * IMPORT: none *
- * EXPORT: outString (String) *
- * ASSERTION: String contains all elements and *
- * their class fields, as well as count and *
- * constructed *
- * ********************************************/
- public String toString()
- {
- String outString = "";
- for(int i=0;i<elements.length;i++)
- {
- for(int j=0;j<elements[0].length;j++)
- {
- if(elements[i][j]!= null)
- {
- if(elements[i][j] instanceof MetalClass)
- {
- outString = outString +elements[i][j].getName()+ ", " +elements[i][j].getSymbol()+ ", " +elements[i][j].getAtomicNumber()+ ", " +elements[i][j].getMass()+ ", " +((MetalClass)elements[i][j]).getConductivity();
- }
- else if(elements[i][j] instanceof NonMetalClass)
- {
- outString = outString +elements[i][j].getName()+ ", " +elements[i][j].getSymbol()+ ", " +elements[i][j].getAtomicNumber()+ ", " +elements[i][j].getMass()+ ", " +((NonMetalClass)elements[i][j]).getState();
- }
- }
- }
- }
- outString = outString + "Count: "+count+ " Constructed: "+constructed;
- return outString;
- }
- /********************************************
- * SUBMODULE: countRows *
- * IMPORT: fileName (String) *
- * EXPORT: rows (integer) *
- * ASSERTION: Counts rows in file *
- * ******************************************/
- private int countRows(String fileName)
- {
- FileInputStream fileStrm = null;
- InputStreamReader rdr;
- BufferedReader bufRdr;
- int rows = 0;
- String line;
- try
- {
- fileStrm = new FileInputStream(fileName);
- rdr = new InputStreamReader(fileStrm);
- bufRdr = new BufferedReader(rdr);
- if(fileStrm == null) //If file doesn't exist, exception is thrown
- {
- FileNotFoundException e = new FileNotFoundException("No file found");
- throw e;
- }
- else
- {
- line = bufRdr.readLine(); //Reads first line
- while(line != null) //Loops while not end of file
- {
- rows++; //Increments number of rows
- line=bufRdr.readLine(); //Reads next line
- }
- }
- }
- catch(IOException ex1)
- {
- if(fileStrm != null)
- {
- try
- {
- fileStrm.close();
- }
- catch(IOException ex2)
- {
- }
- }
- }
- return rows;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement