incomplete

Driver

Apr 15th, 2018
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.25 KB | None | 0 0
  1. /****************************************************/
  2. /*                  Driver Class                    */
  3. /* The Driver class contains methods to open the    */
  4. /* file. Then adds the next line of the file to the */
  5. /* array in ObjectList. Then gets the returned,     */
  6. /* reduced Fraction object and sends it to the      */
  7. /* FractionCounter class ctor to be handled by the  */
  8. /* FractionCounter class. Then, finally return the  */
  9. /* string value of the FractionCounter object       */
  10. /****************************************************/
  11.  
  12. import java.io.FileInputStream;
  13. import java.io.FileNotFoundException;
  14. import java.util.Scanner;
  15.  
  16. public class Driver {
  17.  
  18.     /******************************************/
  19.     /* Class (instance) variable declarations */
  20.     /* Object creations                       */
  21.     /******************************************/
  22.     Scanner dataFileScanner = null;
  23.     private int totalLineCount = 0;
  24.     ObjectList myFractions = new ObjectList();
  25.     Fraction[] fractionList = new Fraction[255];
  26.  
  27.     public static void main(String[] args){
  28.         Driver driver = new Driver();
  29.         driver.openTheFile();
  30.         driver.addToList();
  31.         driver.populateAndPrint();
  32.         driver.expand();
  33.         driver.closeTheFile();
  34.     }
  35.  
  36.     public void openTheFile(){
  37.         try {
  38.             dataFileScanner = new Scanner(new FileInputStream("fractions.txt"));
  39.         } catch (FileNotFoundException e) {
  40.             System.out.println("The file was not found");
  41.             System.out.println("Check the directory and make sure fractions.txt is in the correct workspace folder");
  42.         }
  43.     }
  44.  
  45.     /****************************************************/
  46.     /* Adding each fraction as a String to the object list
  47.      * This method will check if there is another line,
  48.      * then add that line to the next position in the array
  49.      * stored in ObjectList */
  50.     /****************************************************/
  51.     public void addToList(){
  52.         while(dataFileScanner.hasNext()){
  53.             myFractions.add(dataFileScanner.nextLine());
  54.             totalLineCount++;
  55.         }
  56.     }
  57.  
  58.     /****************************************************/
  59.     /* This method contains a loop that goes over each  */
  60.     /* Fraction Object in the fractionList then passes  */
  61.     /* it to the constructor of a new FractionCounter   */
  62.     /* object. */
  63.     /****************************************************/
  64.     public void populateAndPrint(){
  65.         for(int i=0;i<myFractions.fractionList.length;i++){
  66.             FractionCounter fCounter = new FractionCounter(myFractions.fractionList[i]);   
  67.             //System.out.println(fCounter.toString());
  68.         }
  69.     }
  70.  
  71.     /****************************************************/
  72.     /* this method will create a new array, set the     */
  73.     /* size to 1 more than the length of the list of    */
  74.     /* fractionObjects. Copy the contents of the        */
  75.     /* original to the new, then set the old to the new */
  76.     /* array                                            */
  77.     /****************************************************/
  78.     public void expand() {
  79.         Fraction[] newFractionList = new Fraction[fractionList.length + 1];
  80.         System.arraycopy(fractionList, 0, newFractionList, 0, fractionList.length);
  81.         fractionList = newFractionList;
  82.     }
  83.  
  84.     public int getTotalLineCount(){
  85.         return totalLineCount;
  86.     }
  87.  
  88.     public void setTotalLineCount(int totalLineCount){
  89.         this.totalLineCount = totalLineCount;
  90.     }
  91.     //we're done, so let's close the file. :)
  92.     public void closeTheFile(){
  93.         dataFileScanner.close();
  94.     }
  95. }
Add Comment
Please, Sign In to add comment