Four class program with inheritance based on Carol Lewis' presentation Scenario: "Dogs" - the super class and "Working dogs" the sub class. Working dogs have an extra field ie the trainer's name. Here is the text file. Name, age, pedigree, trainer's name Butch#2#false April#1#true Shaka#2#true#Thabiso Morgan#4#false Ratty#9#false Ricky#1#false#Khutso Totum#2#true Seal#1#false#Marge NOTE: There is only ONE array. This array has members that are "Dogs" and members that are "WorkingDogs". The array is declared in the super class (parent class) ========================================================================== /* * Full program with four classes * Super class: Dogs. Subclass: Working dogs * Read in from a text file */ package inheritance; public class DogsUI { public static void main(String[] args) { DogManager dm = new DogManager(); System.out.println("Display all from text file"); String text = dm.displayAll(); System.out.println(text); System.out.println("Display sorted by name"); dm.bubbleSortName(); text = dm.displayAll(); System.out.println(text); System.out.println("Display all by age"); dm.bubbleSortAge(); text = dm.displayAll(); System.out.println(text); System.out.println("Display"); dm.display(); } } =========================================================================== /* * Dog Manager with methods to create the object arrays * The constructor method reads in the text file * Various bubble sort methods */ package inheritance; import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; import java.util.logging.Level; import java.util.logging.Logger; public class DogManager { // global variables including arrays // The array is of the parent class ie only one array type private Dogs[] dogArray = new Dogs[20]; private int size = 0; DogManager() { try { Scanner scFile = new Scanner(new File("dogs.txt")); while(scFile.hasNext()) { String line = scFile.nextLine(); Scanner scLine = new Scanner(line).useDelimiter("#"); String name = scLine.next(); String ageSt = scLine.next(); int age = Integer.parseInt(ageSt); String pedigreeSt = scLine.next(); boolean pedigree = Boolean.parseBoolean(pedigreeSt); //Separate working dogs from dogs // Each class has a different constructor method if(scLine.hasNext()) { String trainer = scLine.next(); dogArray[size] = new WorkingDog (name, age, pedigree,trainer); } else { dogArray[size] = new Dogs (name, age, pedigree); } size = size + 1; } // end while } catch (FileNotFoundException ex) { System.out.println("Error. File not found."); Logger.getLogger(DogManager.class.getName()).log(Level.SEVERE, null, ex); } } // --------------------------------------------- public String displayAll() { String s = ""; for(int i = 0; i < size; i++) { s = s + dogArray[i] + "\n"; } // end for return s; } // --------------------------------------------- // Sorts alphabetically by the dog's name public void bubbleSortName (){ Dogs temp; for(int i = 0; i < size - 1; i++){ for (int j = 0; j < size -1 - i; j++) { String name1 = dogArray[j].getName(); String name2 = dogArray[j + 1].getName(); int value = name1.compareTo(name2); if(name1.compareTo(name2)> 0) { temp = dogArray[j]; dogArray[j] = dogArray[j + 1]; dogArray[j + 1] = temp; } // end if } // end inner loop } // end outer loop } // end method // --------------------------------------- // Sorts numerically by the dog's age public void bubbleSortAge (){ Dogs temp; for(int i = 0; i < size - 1; i++){ for (int j = 0; j < size -1 - i; j++) { int age1 = dogArray[j].getAge(); int age2 = dogArray[j + 1].getAge(); if(age1 > age2) { temp = dogArray[j]; dogArray[j] = dogArray[j + 1]; dogArray[j + 1] = temp; } // end if } // end inner loop } // end outer loop } // end method // --------------------------------------- public void display() { for(int i = 0; i < size; i++){ System.out.println(dogArray[i]); } } } // end class ======================================================================= /* * Super class: Dogs. Subclass: Working dogs */ package inheritance; public class Dogs { private String name = null; private int age = 0; private boolean pedigree = false; Dogs(String n, int a, boolean p) { name = n; age = a; pedigree = p; } // ---------------------------------- public String getName() { return name; } // ---------------------------------- public void setName(String name) { this.name = name; } // ---------------------------------- public int getAge() { return age; } // ------------------------------------ public void setAge(int age) { this.age = age; } // ------------------------------------ public boolean isPedigree() { return pedigree; } // ------------------------------------ public void setPedigree(boolean pedigree) { this.pedigree = pedigree; } // ------------------------------------- public String toString() { return name + "\t" + age + "\t" + pedigree; } } =================================================================== /* * Extends the dog class */ package inheritance; public class WorkingDog extends Dogs{ // Working dog has an additional field private String trainer = null; // Sub-class constructor uses the super-class constructor just adding the // extra field WorkingDog(String n, int a, boolean p, String t) { super(n,a,p); trainer = t; } // end constructor // -------------------------------------- public String getTrainer() { return trainer; } // --------------------------------------- public void setTrainer(String trainer) { this.trainer = trainer; } // ---------------------------------------- public String toString() { // returns "Dog" toString with the extra field return super.toString() + "\t" + trainer; } } =============================================================================== OUTPUT with various bubble sort methods run: Display all from text file Butch 2 false April 1 true Shaka 2 true Thabiso Morgan 4 false Ratty 9 false Ricky 1 false Khutso Totum 2 true Seal 1 false Marge Display sorted by name April 1 true Butch 2 false Morgan 4 false Ratty 9 false Ricky 1 false Khutso Seal 1 false Marge Shaka 2 true Thabiso Totum 2 true Display all by age April 1 true Ricky 1 false Khutso Seal 1 false Marge Butch 2 false Shaka 2 true Thabiso Totum 2 true Morgan 4 false Ratty 9 false Display April 1 true Ricky 1 false Khutso Seal 1 false Marge Butch 2 false Shaka 2 true Thabiso Totum 2 true Morgan 4 false Ratty 9 false BUILD SUCCESSFUL (total time: 0 seconds)