Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
 
Retiring, March 2020 - sorry, you have missed our final public course.
The Coronavirus situation has lead us to suspend public training - which was on the cards anyway, with no plans to resume

Please ask about private 'maintenance' training for Python, Tcl, Perl, PHP, Lua, etc
Happily continuing private consultancy / programming work
 
First Class Java. First step and moving forward.

Java 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.
(written 2008-01-10)

 
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
J706 - Java - Objects and Classes
  [4422] Objects - from physical to virtual or abstract - Java - (2015-02-10)
  [4413] Binomial Coefficient (Pascal Triangle) objects in Java - (2015-02-03)
  [2651] Calculation within objects - early, last minute, or cached? - (2010-02-26)
  [2616] Defining a static method - Java, Python and Ruby - (2010-02-01)
  [2422] Looking inside Java classes - javap and javadoc - (2009-09-25)
  [2169] When should I use OO techniques? - (2009-05-11)
  [1925] Introduction to Object Oriented Programming - (2008-12-06)
  [1906] Long, Longer, Longest in Java - (2008-11-25)
  [1296] An example of Java Inheritance from scratch - (2007-08-08)
  [1163] A better alternative to cutting and pasting code - (2007-04-26)
  [477] Class, static and unbound variables - (2005-10-25)
  [96] Variable Scope - (2004-10-22)


Back to
Climate change, renewable resources and paper v plastic
Previous and next
or
Horse's mouth home
Forward to
Java - using super to call a method in the parent class
Some other Articles
Flooding by Asda-s proposed new supermarket
Web page (http) error status 405
Java, sorting, ArrayList example, generics
Java - using super to call a method in the parent class
First Class Java. First step and moving forward.
Climate change, renewable resources and paper v plastic
Java is a dynamic language .... (and comparison)
Training Season Starts again!
PHP / Web 2 logging
Single login and single threaded models - Java and PHP
4759 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96 at 50 posts per page


This is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price.

Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2020: 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/mouth/1500_Fir ... ward-.html • PAGE BUILT: Sat May 27 16:49:10 2017 • BUILD SYSTEM: WomanWithCat