A train, a person, a dog, a signpost, a meal, a station ... they're all similar objects in programming terms. We can set them up in our program, save and restore attributes - and they make an excellent teaching parallel comparing real life things ("objects") of a certain type ("class").
Objects and classes can also be used for more abstract items and structures, such as file handlers, and on our courses we move on and look at the use of classes and objects in this way too. In my most recent day's training, I have used both
Station objects of the original (represents something physical) and more abstarct (stream handler) types, with my main code doing no more than providing the wires that link the two.
public static void main (String [] args) {
MyStream RailStations = new MyStream(args[0]);
Stations [] NationalRail = new Stations[3000];
int stationCount = 0;
String lyne;
while ((lyne = RailStations.readLine()) != null) {
NationalRail[stationCount++] = Stations.factory(lyne);
}
for (int k=0; k
Stations current = NationalRail[k];
if (current.isitSmall()) {
System.out.println(current.name + " " +
current.getPassengers());
}
}
}
}
For true (production) code, I would have used a
Vector or
ArrayList object rather than hardcoding a 3000 record limit, and I would have introduced error checking and exceptions into my code, allowing for my factory to be passed poor information that didn't result in an object being returned - by these are "to follow" things at this point in the middle of the course.
Source code (both the main program and the classes) at
[here].
Sample output:
WomanWithCat:dvla5 grahamellis$ java StationStatistics railstats.xyz
Pilning 146
Barry Links 86
Tees-Side Airport 14
Hensall 184
Breich 90
Chapelton 190
Achanalt 164
Sugar Loaf 120
Denton 30
Acklington 192
Elton & Orston 72
Buckenham 100
Havenhouse 132
Coombe 60
Dorking West 16
Reddish South 56
WomanWithCat:dvla5 grahamellis$
(written 2015-02-10, updated 2015-02-12)
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
J710 - Java - Extending Classes and More [656] Think about your design even if you don't use full UML - (2006-03-24)
[831] Comparison of Object Oriented Philosophy - Python, Java, C++, Perl - (2006-08-13)
[1066] Final, Finally and Finalize - three special words in Java - (2007-02-05)
[1217] What are factory and singleton classes? - (2007-06-04)
[1294] An example of Java Inheritance from scratch - (2007-08-00)
[1501] Java - using super to call a method in the parent class - (2008-01-10)
[1538] Teaching Object Oriented Java with Students and Ice Cream - (2008-02-12)
[1556] Java - a demonstration of inheritance on just one page - (2008-02-26)
[1819] Calling base class constructors - (2008-10-03)
[2185] Abstract Classes - Java - (2009-05-16)
[2434] Abstract classes, Interfaces, PHP and Java - (2009-10-03)
[2604] Tips for writing a test program (Ruby / Python / Java) - (2010-01-29)
[2860] What methods are available on this Java object? - (2010-07-08)
[3047] What is a universal superclass? Java / Perl / Python / Other OO languages - (2010-11-13)
[4334] Splitting out code into name blocks for clarity and reusability - (2014-11-30)
[4366] Changing what operators do on objects - a comparison across different programming languages - (2014-12-26)
[4394] Philosophy behind object design - and how I applied in to a Java example - (2015-01-14)
[4419] Java Inheritance example - group of classes - step by step - (2015-02-08)
J706 - Java - Objects and Classes [96] Variable Scope - (2004-10-22)
[477] Class, static and unbound variables - (2005-10-25)
[1163] A better alternative to cutting and pasting code - (2007-04-26)
[1296] An example of Java Inheritance from scratch - (2007-08-08)
[1500] First Class Java. First step and moving forward. - (2008-01-10)
[1906] Long, Longer, Longest in Java - (2008-11-25)
[1925] Introduction to Object Oriented Programming - (2008-12-06)
[2169] When should I use OO techniques? - (2009-05-11)
[2422] Looking inside Java classes - javap and javadoc - (2009-09-25)
[2616] Defining a static method - Java, Python and Ruby - (2010-02-01)
[2651] Calculation within objects - early, last minute, or cached? - (2010-02-26)
[4413] Binomial Coefficient (Pascal Triangle) objects in Java - (2015-02-03)
Some other Articles
FileMaker Day to Unix Time conversionA Welsh valley - what the transport looks like in 2015Looking Forward - TransWilts Community Rail Partnership and TransWilts CICCampus - about the logos / way marks you have been choosing fromObjects - from physical to virtual or abstract - JavaHow healthy are the stars of stage and screen?Flexibility in input - read from file, web resource or keyboardMelksham Community Campus - which of these potential signs do you like?Crisp morning, fast run