First Class Java. First step and moving forward.
Archive - Originally posted on "The Horse's Mouth" - 2008-01-10 07:52:58 - Graham EllisJava is an Object Oriented language that's great for medium size through large to huge systems. Which means that it's not usually the best approach for short applications ... of the size that one typically uses as examples on a training course. So examples that I produce show mechanisms and extensibility rather than practical use in themselves. ((Put another way - why use a system of directory and file management, a.k.a. packages and classes, when you've only got one or two files!))
So here - for anyone who gets presented with a large Java project to maintain - is a very short "hello Java objects" example that shows a single class, nothing complicated, called from a single simple test program.
public class HotelBooking {
int duration;
int occupancy;
boolean onCourse;
String GuestName;
public HotelBooking(int numberNights, int numberGuests,
boolean delegate, String LeadName) {
duration = numberNights;
occupancy = numberGuests;
onCourse = delegate;
GuestName = LeadName;
}
public float getcost() {
if (onCourse) {
return 70.5f * duration;
}
if (occupancy == 1) {
return 80.0f * duration;
}
return 95.0f * duration;
}
public String getname() {
return GuestName;
}
}
And here's the test program:
public class TestHotel {
public static void main (String [] args) {
HotelBooking Steve = new HotelBooking(3,1,true,"Steve Sunshine");
HotelBooking Nick = new HotelBooking(1,1,false,"Nick Night");
float costone = Steve.getcost();
float costtwo = Nick.getcost();
String nameone = Steve.getname();
String nametwo = Nick.getname();
System.out.print(nameone + " " );
System.out.println(costone);
System.out.print(nametwo + " " );
System.out.println(costtwo);
}
}
I run that and I get
[trainee@snowdrop bkp]$ java TestHotel
Steve Sunshine 211.5
Nick Night 80.0
[trainee@snowdrop bkp]$
But of course there's a lot more to Java than that!.
I went on to enhance my examples to include several different methods to create hotel bookings (overloading), to specifiy that some variables are private, to add in arrays of objects, and to place my HotelBooking class into a package of its own so that it can leater be easily bundled with a whole series of other classes.
And then you move on to saying that "I have a booking - but it's not a normal one; it differs because it's in association with an event". Rather than duplicate the code for a regular booking, which would mean that there was thereafter double the maintainance, in Java you'll describe the event booking as extending the regular booking and you'll only supply the extra functionallity and changed coding.
You can then extend classes in a number of different ways, giving you a whole alternative series of different booking types ... which you can hold in an array or something similar, and you'll rapidly find that you have a powerful application coming together, easy to maintain as the code for each piece of logic is only there once, and easy to enhance as you can very easily add in extra subclasses.
Why a Java example today? Because I was teaching a Java Bootcamp yesterday! And I'll be running another Bootcamp, and also an extended Java Programing for the Web at the end of next month.