Advertisement
Doflamingo149

Untitled

May 28th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.83 KB | None | 0 0
  1. /*************************************************
  2. * AUTHOR: Nathan Suhardiman (18820225) *
  3. * FILE NAME: PeriodicTable *
  4. * PURPOSE: Contains elements, an array of *
  5. * objects of ElementClass, count and constructed,*
  6. * as well as the functions that adds elements to *
  7. * array and writes them to a file *
  8. * ***********************************************/
  9.  
  10. import java.io.*;
  11.  
  12. public class PeriodicTable
  13. {
  14. //CLASSFIELDS
  15. private ElementClass[][] elements;
  16. private int count;
  17. private boolean constructed;
  18.  
  19. /***********************************************
  20. * SUBMODULE: Default constructor *
  21. * IMPORT: none *
  22. * EXPORT: address of new PeriodicTable object *
  23. * ASSERTION: *
  24. * *********************************************/
  25. public PeriodicTable()
  26. {
  27. elements = new ElementClass[20][5];
  28. for(int i=0;i<elements.length;i++)
  29. {
  30. for(int j=0;j<elements[0].length;j++)
  31. {
  32. elements[i][j] = null; //Sets elements in default array to null
  33. }
  34. }
  35. count = 0; //Sets default
  36. constructed = false;
  37. }
  38.  
  39. /**********************************************
  40. * SUBMODULE: Alternate constructor *
  41. * IMPORT: none *
  42. * EXPORT: address of new PeriodicTable object *
  43. * ASSERTION: *
  44. * ********************************************/
  45. public PeriodicTable(ElementClass[][] inElements, int inCount, boolean inConstructed)
  46. {
  47. int i=0,j=0;
  48. while(i<elements.length && elements[i][j]!=null)
  49. {
  50. while(j<elements[0].length && elements[i][j]!=null)
  51. {
  52. elements[i][j]=inElements[i][j]; //Sets elements of elements as inElements
  53. j++;
  54. }
  55. j=0;
  56. i++;
  57. }
  58. count = inCount;
  59. constructed = inConstructed;
  60. }
  61.  
  62. /**********************************************
  63. * SUBMODULE: Copy constructor *
  64. * IMPORT: none *
  65. * EXPORT: address of new PeriodicTable object *
  66. * ASSERTION: *
  67. * ********************************************/
  68. public PeriodicTable(PeriodicTable inPeriodicTable)
  69. {
  70. elements=inPeriodicTable.getElements();
  71. count=inPeriodicTable.getCount();
  72. constructed=inPeriodicTable.getConstructed();
  73. }
  74.  
  75. //MUTATORS
  76. /********************************************
  77. * SUBMODULE: setElements *
  78. * IMPORT: inElements (ARRAY OF ElementClass)*
  79. * EXPORT: none *
  80. * ASSERTION: sets elements if valid *
  81. * ******************************************/
  82. public void setElements(ElementClass[][] inElements)
  83. {
  84. int i=0, j=0;
  85. while(i<elements.length && elements[i][j]!=null)
  86. {
  87. while(j<elements[0].length && elements[i][j]!=null)
  88. {
  89. elements[i][j]=inElements[i][j]; //If inElements is imported, elements of elements is set
  90. j++;
  91. }
  92. j=0;
  93. i++;
  94. }
  95. }
  96.  
  97. /********************************************
  98. * SUBMODULE: setCount *
  99. * IMPORT: inCount (integer) *
  100. * EXPORT: none *
  101. * ASSERTION:sets count *
  102. * ******************************************/
  103. public void setCount(int inCount)
  104. {
  105. count = inCount;
  106. }
  107.  
  108. /*******************************************
  109. * SUBMODULE: setConstructed *
  110. * IMPORT: inConstructed (boolean) *
  111. * EXPORT: none *
  112. * ASSERTION: sets constructed *
  113. * *****************************************/
  114. public void setConstructed(boolean inConstructed)
  115. {
  116. constructed = inConstructed;
  117. }
  118.  
  119. /*******************************************
  120. * SUBMODULE: equals *
  121. * IMPORT: inPeriodicTable *
  122. * EXPORT: same (boolean) *
  123. * ASSERTION: Two Periodic tables are *
  124. * interchangeable if contents are the same *
  125. * *****************************************/
  126. public boolean equals(Object inObject)
  127. {
  128. boolean same = false;
  129. int notEqual = 0, i=0, j=0;
  130. if(inObject instanceof PeriodicTable)
  131. {
  132. PeriodicTable inPeriodicTable = (PeriodicTable)inObject;
  133. if(count == inPeriodicTable.getCount())
  134. {
  135. if(constructed == inPeriodicTable.getConstructed())
  136. {
  137. while(i<elements.length && elements[i][j] != null)
  138. {
  139. while(j<elements[0].length && elements[i][j] != null)
  140. {
  141. if(elements[i][j] == inPeriodicTable.getElements()[i][j])
  142. {
  143. same = true;
  144. }
  145. else
  146. {
  147. notEqual++;
  148. }
  149. j++;
  150. }
  151. j=0;
  152. i++;
  153. }
  154. }
  155. }
  156. }
  157.  
  158. if(notEqual>0) //If one or more elements in either arrays aren't equal, returns false
  159. {
  160. same = false;
  161. }
  162. return same;
  163. }
  164.  
  165. public ElementClass[][] getElements()
  166. {
  167. return elements;
  168. }
  169.  
  170. public int getCount()
  171. {
  172. return count;
  173. }
  174.  
  175. public boolean getConstructed()
  176. {
  177. return constructed;
  178. }
  179.  
  180. /********************************************
  181. * SUBMODULE: readFile *
  182. * IMPORT: fileName (String) *
  183. * EXPORT: none *
  184. * ASSERTION: Reads file and if valid, adds *
  185. * contents to array of ElementClass *
  186. * ******************************************/
  187. public void readFile(String fileName)
  188. {
  189. FileInputStream fileStrm = null;
  190. InputStreamReader rdr;
  191. BufferedReader bufRdr;
  192. int columns, rows;
  193. int i=0, j=0;
  194. String line;
  195.  
  196. rows = countRows(fileName); //Calls method to count rows in file
  197.  
  198. try
  199. {
  200. fileStrm = new FileInputStream(fileName);
  201. rdr = new InputStreamReader(fileStrm);
  202. bufRdr = new BufferedReader(rdr);
  203.  
  204. if(fileStrm == null) // If file doesn't exist, throws exception
  205. {
  206. IOException e = new IOException("No file found");
  207. throw e;
  208. }
  209.  
  210. line = bufRdr.readLine(); //Reads first line
  211.  
  212. for(int a=0;a<rows;a++) //Increment by rows
  213. {
  214. String[] lineArray = line.split("<"); //Splits row into columns
  215. columns = lineArray.length -1; //Length of column is set
  216. for(int b=0;b<columns; b++) //Incrments by columns in this row
  217. {
  218. String[] classFields = lineArray[b+1].split(","); //Splits each column by classfields it contains
  219. String symbol = classFields[0]; //Symbol is first class field
  220. String name = classFields[1]; //Name is second class field
  221. int atomicNumber = Integer.parseInt(classFields[2]); //Atomic number is third class field
  222. double mass = Double.parseDouble(classFields[3]); //Mass is fourth class field
  223. //Last class field is ambiguous for now
  224. String lastClassField = classFields[4].substring(0, classFields[4].length()-2);
  225. //If last class field is a state, ie. 'G', 'L' or 'S'.
  226. if(lastClassField.equals("S") || lastClassField.equals("L") || lastClassField.equals("G") && count <=100)
  227. {
  228. char state = lastClassField.charAt(0); //Sets state
  229. //Constructs new element of NonMtalClass
  230. elements[i][j] = new NonMetalClass(symbol, name, atomicNumber, mass, state);
  231. count++; //Increments number of elements in periodic table
  232. }
  233. else if(count<=100) //Else it should be a number (This submodule can only be called if file was valid)
  234. {
  235. double conductivity = Double.parseDouble(lastClassField); //Sets conductivity
  236. //Constructs new element of MetalClass
  237. elements[i][j] = new MetalClass(symbol, name, atomicNumber, mass, conductivity);
  238. count++; //Increments number of elements in periodic table
  239. }
  240.  
  241. if(i<elements.length-1 && j == elements[0].length-1) //If at end of row of periodic table, goes to nxt row
  242. {
  243. j=0;
  244. i++;
  245. }
  246. else if(j<elements[0].length) //If there is still a vacant spot in row, it moves to that spot.
  247. {
  248. j++;
  249. }
  250. }
  251. line=bufRdr.readLine(); //Reads next row of file
  252. }
  253. constructed = true; //File has been read now
  254. }
  255. catch(IOException e)
  256. {
  257. if(fileStrm != null)
  258. {
  259. try
  260. {
  261. fileStrm.close();
  262. }
  263. catch(IOException ex1)
  264. {
  265.  
  266. }
  267. }
  268. }
  269. }
  270.  
  271. /**************************************
  272. * SUBMODULE: addElement *
  273. * IMPORT: inObject (Object) *
  274. * EXPORT: none *
  275. * ASSERTION: Adds element to periodic *
  276. * table *
  277. * ************************************/
  278. public void addElement(Object inObject)
  279. {
  280. ElementClass inElement;
  281. inElement = (ElementClass)inObject;
  282. System.out.println(inElement.getName());
  283.  
  284. boolean filled = false;
  285. for(int i=0;i<elements.length;i++) //Increments by rows
  286. {
  287. for(int j=0;j<elements[0].length;j++) //Increments by columns
  288. {
  289. if(elements[i][j]==null && filled == false)
  290. {
  291. elements[i][j] = inElement; //Adds element to next vacant spot
  292. filled = true; //Once filled, it wont attempt to add more elements until function is called again
  293. }
  294. }
  295. }
  296. count++; //Increments number of elements in array elements
  297. }
  298.  
  299. /************************************
  300. * SUBMODULE: writeFile *
  301. * IMPORT: choice (integer) *
  302. * EXPORT: none *
  303. * ASSERTION: Writes metals or non- *
  304. * metals to file *
  305. * **********************************/
  306. public void writeFile(int choice)
  307. {
  308. FileOutputStream fileStrm = null;
  309. PrintWriter pw;
  310.  
  311. try
  312. {
  313. String fileName = "output.txt"; //File name is ser to "output.txt"
  314. fileStrm = new FileOutputStream(fileName);
  315. pw = new PrintWriter(fileStrm);
  316.  
  317. for(int i=0;i<elements.length;i++)
  318. {
  319. for(int j=0;j<elements[0].length;j++)
  320. {
  321. if(choice==1 && elements[i][j] instanceof MetalClass) //If user selected option 1, metals are printed
  322. {
  323. pw.println(elements[i][j].getSymbol()+ "," +elements[i][j].getName()+ "," +elements[i][j].getAtomicNumber()+ "," +elements[i][j].getMass()+ "," +((MetalClass)elements[i][j]).getConductivity());
  324. }
  325. else if(choice == 2 && elements[i][j] instanceof NonMetalClass) //Else non-metals are printed
  326. {
  327. pw.println(elements[i][j].getSymbol()+ "," +elements[i][j].getName()+ "," +elements[i][j].getAtomicNumber()+ "," +elements[i][j].getMass()+ "," +((NonMetalClass)elements[i][j]).getState());
  328. }
  329.  
  330. }
  331. }
  332. pw.close();
  333. }
  334. catch(IOException e)
  335. {
  336. if(fileStrm != null)
  337. {
  338. try
  339. {
  340. fileStrm.close();
  341. }
  342. catch(IOException ex2)
  343. {
  344.  
  345. }
  346. }
  347. }
  348. }
  349.  
  350. /**********************************************
  351. * SUBMODULE: toString *
  352. * IMPORT: none *
  353. * EXPORT: outString (String) *
  354. * ASSERTION: String contains all elements and *
  355. * their class fields, as well as count and *
  356. * constructed *
  357. * ********************************************/
  358. public String toString()
  359. {
  360. String outString = "";
  361. for(int i=0;i<elements.length;i++)
  362. {
  363. for(int j=0;j<elements[0].length;j++)
  364. {
  365. if(elements[i][j]!= null)
  366. {
  367. if(elements[i][j] instanceof MetalClass)
  368. {
  369. outString = outString +elements[i][j].getName()+ ", " +elements[i][j].getSymbol()+ ", " +elements[i][j].getAtomicNumber()+ ", " +elements[i][j].getMass()+ ", " +((MetalClass)elements[i][j]).getConductivity();
  370.  
  371. }
  372. else if(elements[i][j] instanceof NonMetalClass)
  373. {
  374. outString = outString +elements[i][j].getName()+ ", " +elements[i][j].getSymbol()+ ", " +elements[i][j].getAtomicNumber()+ ", " +elements[i][j].getMass()+ ", " +((NonMetalClass)elements[i][j]).getState();
  375.  
  376. }
  377. }
  378.  
  379. }
  380. }
  381. outString = outString + "Count: "+count+ " Constructed: "+constructed;
  382. return outString;
  383. }
  384.  
  385. /********************************************
  386. * SUBMODULE: countRows *
  387. * IMPORT: fileName (String) *
  388. * EXPORT: rows (integer) *
  389. * ASSERTION: Counts rows in file *
  390. * ******************************************/
  391. private int countRows(String fileName)
  392. {
  393. FileInputStream fileStrm = null;
  394. InputStreamReader rdr;
  395. BufferedReader bufRdr;
  396. int rows = 0;
  397. String line;
  398.  
  399. try
  400. {
  401. fileStrm = new FileInputStream(fileName);
  402. rdr = new InputStreamReader(fileStrm);
  403. bufRdr = new BufferedReader(rdr);
  404.  
  405. if(fileStrm == null) //If file doesn't exist, exception is thrown
  406. {
  407. FileNotFoundException e = new FileNotFoundException("No file found");
  408. throw e;
  409. }
  410. else
  411. {
  412. line = bufRdr.readLine(); //Reads first line
  413. while(line != null) //Loops while not end of file
  414. {
  415. rows++; //Increments number of rows
  416. line=bufRdr.readLine(); //Reads next line
  417. }
  418. }
  419. }
  420. catch(IOException ex1)
  421. {
  422. if(fileStrm != null)
  423. {
  424. try
  425. {
  426. fileStrm.close();
  427. }
  428. catch(IOException ex2)
  429. {
  430.  
  431. }
  432. }
  433. }
  434. return rows;
  435. }
  436. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement