Main Content

Teaching Object Oriented Java with Students and Ice Cream

Archive - Originally posted on "The Horse's Mouth" - 2008-02-12 23:00:53 - Graham Ellis

"I'm getting tired of students. Can we do something else". So said my delegates at Cardiff University today. So we did Ice Cream and other deserts!

Perhaps I had better explain. I'm running a Java Course there, and looking for examples of classes and objects to write about. At the start of the course, delegates know diddly squat about OO design, and how it works in Java, so the first sample programs they write are tentative steps and using only an edited selection of the facilities available. Then they extend the example and find that "I wish I had known before I started that I would be going this way" but the catch is that they couldn't have known - too much information before the first practical means a poor practical and some points missed. So after the first example has been written, stretched, overstretched and pulled apart nearly to the point of breaking we consolidate the knowledge gained into a fresh example which can be written, from the beginning with the end target in mind.

I'm not doing to show you any of the "student" examples. They look horrid / they show things that are kludged together nastily and I would be ashamed of them. But I will show you the Dairy Queen class!

public class DairyQueen {
 
  public static void main (String [] args) {
 
    Desert [] Ice = new Desert[6];
 
    Ice[0] = new Icecream("Chocolate", 1000, 100,0);
    Ice[1] = new Icecream("Vanilla", 1400, 150,-15);
    Ice[2] = new Icecream("Strawberry", 1200, 100,-15);
    Ice[3] = new Icecream("Nut", 900, 100,-15);
    Ice[4] = new Icecream("Pistaccio", 450, 50,-25);
    Ice[5] = new Colddesert(1500,90,-10,"Strawberry and courgette cheesecake");
 
    for (int k=0; k       String what = Ice[k].getwhat();
      int cals = Ice[k].getcals();
      int wei = Ice[k].getweight();
 
      System.out.print(what);
      System.out.print(" ");
      System.out.print(cals);
      System.out.print(" ");
      System.out.println(wei);
    }
  }
}


And how does that run?

Dorothy:febjava grahamellis$ java DairyQueen
Chocolate flavoured Ice Cream 1000 100
Vanilla flavoured Ice Cream 1400 150
Strawberry flavoured Ice Cream 1200 100
Nut flavoured Ice Cream 900 100
Pistachio flavoured Ice Cream 450 50
Strawberry and courgette cheesecake and custard 90 1500
Dorothy:febjava grahamellis$


Now - do you feel cheated that I've not shown you all the code? I'm glad you've noticed that I've not shown you everything but that's the very joy of Object Oriented programming. YOU write the bits that YOU know about, someone else writes all the detail that they know about, they hide it within a class of objects and you keep calling the class methods as you need too. This hiding stuff within is known as encapsulation!


Full code? Oh - if you MUST ;-)


Desert.java - basic object for a desert
Insurable.java - how to define something that's insurable
Icecream.java - an ice cream desert is a desert with other features
Colddesert.java - a cold desert is another type of desert
DairyQueen.java - the whole application (above) which pulls it together