Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
 
For 2021 - online Python 3 training - see ((here)).

Our plans were to retire in summer 2020 and see the world, but Coronavirus has lead us into a lot of lockdown programming in Python 3 and PHP 7.
We can now offer tailored online training - small groups, real tutors - works really well for groups of 4 to 14 delegates. Anywhere in the world; course language English.

Please ask about private 'maintenance' training for Python 2, Tcl, Perl, PHP, Lua, etc.
Binomial Coefficient (Pascal Triangle) objects in Java

An exercise on our Java course from which our delegates gain a lot of appreciation is the one in which I ask them to set up an array of arrays containing Pascal's Triangle of Binomial Coefficients. It sounds mighty complex, but I explain it on the board and it comes down to just defining the arrays and filling the elements with simple additions.

Arrays and Objects are both fundamental to Java, and our course teaches both; and until delegates know about both (and about other things too!) they can't really write fully mature Java programs - and neither can I while I'm teaching. Arrays are usually covered first, so the sample answers you'll previously have found on our web site are in-line coded (perhaps with some calls to static methods) which - if I were interviewing for a job - would have me asking "Did you consider using a class of objects"?

Today, I taught objects before I taught arrays - I felt it was right to swap them around for the group I have , and it worked well. It also enabled me to use an object like I really should for my Pascal's Triangle.

Here's my main code - make a triangle of 10 rows, print it out, look up an element:

  public static void main (String [] args) {
    PascalAsObject pt = new PascalAsObject(10);
    pt.prettyPrint();
    System.out.println(pt.getRowCol(6,3));
  }


The beauty is that the control code is so straight forward, the detail of the triangle isn't exposed to the top level and (importantly) the methods in the PascalAsObject class are available for use in other programs, without the user needing to have any knowledge of how they work.

How does that work?

  WomanWithCat:dvla_4 grahamellis$ java PascalAsObject
                                   1
                                1     1
                             1     2     1
                          1     3     3     1
                       1     4     6     4     1
                    1     5    10    10     5     1
                 1     6    15    20    15     6     1
              1     7    21    35    35    21     7     1
           1     8    28    56    70    56    28     8     1
        1     9    36    84   126   126    84    36     9     1


Full source code [here].
(written 2015-02-03, updated 2015-02-04)

 
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
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)
  [4422] Objects - from physical to virtual or abstract - Java - (2015-02-10)

J705 - Java - Arrays
  [1497] Training Season Starts again! - (2008-01-07)
  [1498] Java is a dynamic language .... (and comparison) - (2008-01-08)
  [1614] When an array is not an array - (2008-04-17)
  [2648] Java arrays - are they true arrays or not? - (2010-02-23)
  [3038] Setting up individual variables, and arrays, in Java - some commented examples - (2010-11-09)
  [3039] Fresh Paint - Java Arrays - (2010-11-09)
  [3118] Arrays of arrays - or 2D arrays. How to program tables. - (2011-01-02)
  [4347] Arrays in Java - an introduction for newcomers - (2014-12-10)
  [4428] Using the lead - passing arrays and other collections in Java - (2015-02-16)


Back to
Java -making sure you have the right versions
Previous and next
or
Horse's mouth home
Forward to
Java - converting an integer to a fixed length string
Some other Articles
Crisp morning, fast run
Challenging the IT course business model
Buffet review - taking the price into account
Java - converting an integer to a fixed length string
Binomial Coefficient (Pascal Triangle) objects in Java
Java -making sure you have the right versions
Location, location location. And a chance of a giggle!
A good example of recursion - a real use in Python
Setting up and using a dict in Python - simple first example
Additional Python courses added to our schedule
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., 2021: 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/mouth/4413_Bin ... -Java.html • PAGE BUILT: Sun Oct 11 16:07:41 2020 • BUILD SYSTEM: JelliaJamb