Code Version D -
Focus:
toString methods

By: Nicholas Duchon

Scanner - back

 Code Versions:

Musings

Home


This version will highlight the toString methods

// 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);
    readStuff ();
 
} // end int constructor
 
  void readStuff () {
    System.out.println ("--- Read file ----");
    for (CA ca: stuff) System.out.println ("" + ca);
 
} // end method readStuff
 
  void writeStuff (int m) {
    System.out.println ("stuff:");
    for (CA ca: stuff)
      System.out.println ("" + ca);
   
    stuff.sort ((a,b) -> (a.n).compareTo(b.n));
   
    System.out.println ("---- SORTED 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 {
  int id;
  String n;
 
 
public String toString () {
    String st = "";
    st += String.format ("%s %5d", n, id);
    return st;
 
} // end toString
} // end class CA

class CB extends CA {
  double da;
  int    za;
 
 
public String toString () {
    String st = super.toString (); // in CA
    st += String.format ("%5d %10.3f", za, da);
    return st;
 
} // end toString
} // end class CB

class CC extends CB {
  int zb;
  boolean bb;
 
 
public String toString () {
    String st =
super.toString (); // in CB then CA
    st += String.format ("%5d %b", zb, bb);
    return st;
 
} // end toString
} // end class CC

class CD extends CB {
  int zd;
  boolean bd;
  double dd;
  String sd;
 
 
public String toString () {
    String st =
super.toString (); // in CB then CA
    st += String.format ("%4d %6b %10.5f %s", zd, bd, dd, sd);
    return st;
 
} // end toString
} // end class CD

class CE extends CA {
  static final int MAX = 6;
  ArrayList <Double> aLe = new ArrayList <> ();
 
 
public String toString () {
    String st =
super.toString (); // in CA
    for (Double d: aLe)
      st += String.format ("%10.5f", d);
    st += " : ";
    return st;
 
} // end toString
} // end class CE

class CF extends CE {
  ArrayList <String> aLf = new ArrayList <> ();
 
 
public String toString () {
    String st =
super.toString (); // in CE then CA
    for (String s: aLf)
      st += s + " ";
    st += "* ";
    return st;
 
} // end toString
} // end class CF

class CG extends CE {
  int [] zg = new int [10];
 
  public String toString () {
    String st =
super.toString (); // in CE then CA
    for (int k: zg)
      st += String.format ("%5d", k);
    return st;
 
} // end toString
} // end class CG


ND.