Code Version C -
Focus:
Instantiating random
CA objects

By: Nicholas Duchon

Scanner - back

 Code Versions:

Musings

Home


This version highlights the creation of the random instances


// File: MyClassIO.java
// Date: Mar 21, 2017
// Author: Nicholas Duchon
// Purpose:
//    show Scanner constructors in the context of a class hierarchy
//    also show use of super calls in constructor hierarchy

//    Class Hierarchy:
// CA
// +---CB
//    +---CC
//    +---CD
// +---CE
//    +---CF
//    +---CG

import java.util.ArrayList;
import java.util.Random;
import java.util.Comparator;
import java.util.Scanner;
import java.io.PrintStream;
import java.io.File;

public class MyClassIO {
  String outFileName = "data.txt";
  ArrayList <CA> stuff = new ArrayList <> ();
 
  MyClassIO (int m) {
    writeStuff (m);
 
} // end int constructor
   
  void writeStuff (int m) {
    for (int i = 0; i < m; i++) {
      stuff.add (CA.getRandomObject ());
    } // end creating m random objects children of CA

    System.out.println ("stuff:");
    for (CA ca: stuff)
      System.out.println ("" + ca);
 
} // end method writeStuff
 
  public static void main (String args []) {
    MyClassIO cio = new MyClassIO (20);
    System.out.println ("---- Bye ----");
 
} // end main
} // end class MyClassIO

class CA {
  static Random rn = new Random ();
  int id;
  String n;
 
  static CA getRandomObject () {
    CA x = null;
    switch (rn.nextInt (7)) {
      case 0: x = new CA (); break;
      case 1: x = new CB (); break;
      case 2: x = new CC (); break;
      case 3: x = new CD (); break;
      case 4: x = new CE (); break;
      case 5: x = new CF (); break;
      case 6: x = new CG (); break;
    } // end switch
    return x;
 
} // end no-parameter constructor
 
  static String randomString (int m) {
    String st = "";
    st += (char)(rn.nextInt (26) + 'A');
    for (int i = 1; i < m; i++)
      st += (char) ('a' + rn.nextInt (26));
    return st;
 
} // end method randomString
 
  public CA () {
    id = rn.nextInt (1000);
    n = "a";
 
} // end constructor
 
} // end class CA

class CB extends CA {
  double da;
  int    za;
 
  public CB () {
    // super () is default
    n = "b";
    da = rn.nextDouble () * 10000;
    za = rn.nextInt (2000);
 
} // end constructor
 
} // end class CB

class CC extends CB {
  int zb;
  boolean bb;
 
  public CC () {
    // super () is default
    n = "c";
    zb = rn.nextInt (300);
    bb = (rn.nextInt (100) > 50)?true:false;
 
} // end constructor
 
} // end class CC

class CD extends CB {
  int zd;
  boolean bd;
  double dd;
  String sd;
 
  public CD () {
    // super () is default
    n = "d";
    sd = randomString (10);
    zd = rn.nextInt (150);
    bd = (rn.nextInt (100) > 25)?true:false;
    dd = rn.nextDouble () * 500;
 
} // end constructor
 
} // end class CD

class CE extends CA {
  static final int MAX = 6;
  ArrayList <Double> aLe = new ArrayList <> ();
 
  public CE () {
    // super () is default
    n = "e";
    for (int i = 0; i < MAX; i++)
      aLe.add (rn.nextDouble () * 37);
 
} // end constructor
 
} // end class CE

class CF extends CE {
  ArrayList <String> aLf = new ArrayList <> ();
 
  public CF () {
    // super () is default
    n = "f";
    for (int i = 0; i < MAX; i++)
      aLf.add (randomString (3));
 
} // end constructor
 
} // end class CF

class CG extends CE {
  int [] zg = new int [10];
 
  public CG () {
    // super () is default
    n = "g";
    for (int i = 0; i < zg.length; i++)
      zg [i] = rn.nextInt (400);
 
} // end constructor
 
} // end class CG


ND.