Scanner
Constructors

Nicholas Duchon: Mar 21, 2018.

Outline

 Code Versions:

  1. does it all switch in read
  2. uses switch in CA factory method
  3. random instantiation
  4. toString focus

Points

There are two key concepts being presented in this example are:

  1. Creating text data files for a complex hierarchy
  2. Reading a text data file using Scanner constructors

Hierarchy

Here is the class hierarchy for this example:

The class declarations are:

The following table gives you some idea of the instance structure of the classes in this example:

CA
int id
String n
CB
int id
String n
double da
int za
CC
int id
String n
double da
int za
boolean bb
int zb
CD
int id
String n
double da
int za
boolean bd
double dd
String sd
int zd
CE
int id
String n
ArrayList <Double> aLe
CF
int id
String n
ArrayList <Double> aLe ArrayList <String> aLf
CG
int id
String n
ArrayList <Double> aLe int [] zg

Two functions

  1. Create a data file with a variety of random information
    1. main determines that m objects will be created, the code current has m = 20
    2. the name of the data file is currently hard coded as data.txt
    3. each class has its own toString method, which uses the super.toString call as the first line
    4. the constructor calls the writeStuff (int) method
      1. this method calls the CA factory method getRandomObject
        this method will create a random object of one of the class CA ... CG
      2. Each class has its own random no-parameter constructor
      3. the data file is open for writing, and the elements of the ArrayList <CA> stuff are written to the file, using the toString methods
    5. the stuff.sort method is called using a lambda expression comparator
  2. Read a data file
    1. Open a Scanner on the file, reading one line at a time
    2. the first String is used in a switch construct to determine which class to instantiate
    3. Scanner constructors are used in every class, and taking explicit advantage of Scanner constructors in the parent classes.
    4. again, the stuff ArrayList is sorted and the results printed.

Comments


Development

The sequence I used for developing this code.

  1. I started with the public class to get the right file name
  2. I added declarations for all the classes I intended to use, with their appropriate extensions (CA, etc)
    NO code withing the classes yet
    this should compile correctly
  3. I added main to the public class
  4. declared the ArrayList <CA> stuff
    1. ArrayList is very nice data structure when you don't know how many of something you are going to have
  5. added appropriate attributes to each class
  6. added toString methods to each class - declarations with minimal body, but calls to super
  7. CA.getRandomObject factory method, needed to add instances to stuff
  8. added call to print each element of stuff, one by one, using toString on each element, with an advanced for-each loop
  9. class by class, one by one
    1. added random constructor
    2. appropriate toString body
    3. tested the code after each class was implemented
  10. added code to sort the stuff data structure
  11. printed before and after sort printing to see result
  12. add write to file code in writeStuff method
  13. checked that the output file is correct
  14. added code to read file line by line, using Scanner.nextLine
  15. added scanner constructors outlines (only the declarations at first)
  16. added the switch in the readStuff method - this will compile and run, even if the appropriate are not yet created
  17. one by one, class by class:
    1. call the super Scanner constructor
    2. add the body of the Scanner constructor for that class, beyond the parent class processing
    3. tested the program so far
  18. final testing
  19. DONE!

Output:

This is the output of the writeStuff operation, the readStuff is very similar.
Here we have the unsorted and the sorted versions of the stuff ArrayList.

stuff:
g   717  15.38365  28.17976   2.80658   2.54967  18.31233  12.87867 :    66  222   31   37  137  240  179  397  117   97
e   809  13.28916  19.89148  14.07111   5.69699  28.54088  33.61740 :
g   503  29.15754  10.17436   2.62909  18.48133  26.48449   7.18132 :   193  167  228   83  141   61  122    3  252  285
b   961  746   2605.766
d   313 1577   4328.921  22   true  348.16708 Bjoiekhijn
g   701  14.39071   0.20667  29.97506  25.60834   0.54222  16.26749 :    31  334  263  396  378   25   37   55   84  332
e   297  25.73191  35.52947  11.73526  30.06353   1.73089  23.95225 :
a   535
c   556 1420   2461.106   27 false
b   474 1741   5535.299
d   468  751   2338.075 131   true  238.00327 Vthxwnanny
e   824   6.34942  29.10032   6.43827   5.24937   2.75068  27.92549 :
g   476  35.25088  25.37527  36.95420  27.90710   5.02629   1.32536 :    24  205  161   98   94  331  154   33  270  270
d    69  727   9138.710  13   true  238.10920 Ekfkhgnaot
c   288  572   5487.581   34 false
b   332 1886   4980.749
a    92
c   388 1279   8696.436  155 true
f   681  36.12881  33.97121  34.06929  11.29526   5.56375   6.27562 : Niq Qqp Zna Soy Uax Wxs *
a   564
---- SORTED stuff:
a   535
a    92
a   564
b   961  746   2605.766
b   474 1741   5535.299
b   332 1886   4980.749
c   556 1420   2461.106   27 false
c   288  572   5487.581   34 false
c   388 1279   8696.436  155 true
d   313 1577   4328.921  22   true  348.16708 Bjoiekhijn
d   468  751   2338.075 131   true  238.00327 Vthxwnanny
d    69  727   9138.710  13   true  238.10920 Ekfkhgnaot
e   809  13.28916  19.89148  14.07111   5.69699  28.54088  33.61740 :
e   297  25.73191  35.52947  11.73526  30.06353   1.73089  23.95225 :
e   824   6.34942  29.10032   6.43827   5.24937   2.75068  27.92549 :
f   681  36.12881  33.97121  34.06929  11.29526   5.56375   6.27562 : Niq Qqp Zna Soy Uax Wxs *
g   717  15.38365  28.17976   2.80658   2.54967  18.31233  12.87867 :    66  222   31   37  137  240  179  397  117   97
g   503  29.15754  10.17436   2.62909  18.48133  26.48449   7.18132 :   193  167  228   83  141   61  122    3  252  285
g   701  14.39071   0.20667  29.97506  25.60834   0.54222  16.26749 :    31  334  263  396  378   25   37   55   84  332
g   476  35.25088  25.37527  36.95420  27.90710   5.02629   1.32536 :    24  205  161   98   94  331  154   33  270  270
---- Bye ----



End.