Chairperson and Program Coordinator for the Computer Science Technology Program at Dawson College Instructor and Program Consultant for the School of Extended Learning Computer Institute at Concordia University I have been passionate about programming since buying an Apple][+ in 1980. I paid the extra $450 to bring the RAM up to 48K! Ken has posted 17 posts at DZone. You can read more from them at their website. View Full User Profile

NetBeans in the Classroom: How to Teach JavaBeans

06.23.2014
| 2313 views |
  • submit to reddit
Ken Fogel is the Program Coordinator and Chairperson of the Computer Science Technology program at Dawson College in Montreal, Canada. He is also a Program Consultant to and part-time instructor in the Computer Institute of Concordia University's School of Extended Learning. He blogs at omniprogrammer.com and tweets @omniprof. His regular columns about NetBeans in education are listed here.

Beans, beans, good for your heart, the more you eat the more you...

I’m crazy for beans. Not the ones I eat but the beans I code. The real world is a very complicated place. I explain to my students that the software we write extracts the minimum amount of information from the real work to solve a problem. This is called an abstraction. The information that is used to solve problems rarely fits a single primitive. Instead we find ourselves needing to combine different types of data such as integers, doubles, booleans, and strings. We may need to use a data object as member of another data object.

An explanation that I once read of the difference between structured and object oriented programmers went something like this: "Structured programmers focus on the actions their program will carry out and design the data structures to fit the actions. Object oriented programmers work the other way around by first modeling the data and then deciding what actions to perform on it." This is obviously a gross simplification but it does allow me to make a point about what I believe is important in object oriented programming. Data can exist without programs but programs cannot exist without data. Therefore a programmer must model the data before they write the program regardless of the methodology.

Once I have lectured on the various primitive types, I immediately give my students an exercise in designing objects. I look for some common information that they have in their environment. For example, here in Canada the government requires many manufactured goods to have labels identifying the energy the product consumes. For a stove it is electricity and for a car is gasoline, diesel or electric consumption.

For this exercise I give the students the following information.

Then I include the Energuide.

From this information I expect the following class from my students.

package com.kenfogel.carbean;

/**
 * Data model for the energy consumption of an automobile
 *
 * @author Ken
 */
public class MyCarBean {

    // Model data
    private String manufacturer;
    private String model;
    private int engineDisplacement;
    private int cylinders;
    private String transmission;
    private String driveTrain;
    private int weight;

    // Fuel consumption data
    private double cityConsumptionInLitres;
    private double cityConsumptionInMilesPerGallon;
    private double highwayConsumptionInLitres;
    private double highwayConsumptionInMilesPerGallon;
    private double estimatedAnnualFuelCost;

}

Notice that I use long expressive names. I generally forbid abbreviations and acronyms. I prefer salesOfBoats over sob.

All the class variables are private and so off limits to any code not inside this class. Without an IDE like NetBeans I would need a fair amount of time to explain constructors, setters and getter. But with NetBeans I just need to tell then to place the cursor on the blank line after the last variable, go to the Source menu and select Insert code.

A small dialog will open and here they will select Getter and Setter.

The next dialog shows all the class variables. Since we want setters and getters for all of them we just check off the box with the class name MyCarBean and click on Generate.

Now the student has their first, almost complete, data bean.

package com.kenfogel.carbean;

/**
 * Data model for the energy consumption of an automobile
 *
 * @author Ken
 */
public class MyCarBean {

    // Model data
    private String manufacturer;
    private String model;
    private int engineDisplacement;
    private int cylinders;
    private String transmission;
    private String driveTrain;
    private int weight;

    // Fuel consumption data
    private double cityConsumptionInLitres;
    private double cityConsumptionInMilesPerGallon;
    private double highwayConsumptionInLitres;
    private double highwayConsumptionInMilesPerGallon;
    private double estimatedAnnualFuelCost;

    public String getManufacturer() {
        return manufacturer;
    }

    public void setManufacturer(String manufacturer) {
        this.manufacturer = manufacturer;
    }

    public String getModel() {
        return model;
    }

    public void setModel(String model) {
        this.model = model;
    }

    public int getEngineDisplacement() {
        return engineDisplacement;
    }

    public void setEngineDisplacement(int engineDisplacement) {
        this.engineDisplacement = engineDisplacement;
    }

    public int getCylinders() {
        return cylinders;
    }

    public void setCylinders(int cylinders) {
        this.cylinders = cylinders;
    }

    public String getTransmission() {
        return transmission;
    }

    public void setTransmission(String transmission) {
        this.transmission = transmission;
    }

    public String getDriveTrain() {
        return driveTrain;
    }

    public void setDriveTrain(String driveTrain) {
        this.driveTrain = driveTrain;
    }

    public int getWeight() {
        return weight;
    }

    public void setWeight(int weight) {
        this.weight = weight;
    }

    public double getCityConsumptionInLitres() {
        return cityConsumptionInLitres;
    }

    public void setCityConsumptionInLitres(double cityConsumptionInLitres) {
        this.cityConsumptionInLitres = cityConsumptionInLitres;
    }

    public double getCityConsumptionInMilesPerGallon() {
        return cityConsumptionInMilesPerGallon;
    }

    public void setCityConsumptionInMilesPerGallon(double cityConsumptionInMilesPerGallon) {
        this.cityConsumptionInMilesPerGallon = cityConsumptionInMilesPerGallon;
    }

    public double getHighwayConsumptionInLitres() {
        return highwayConsumptionInLitres;
    }

    public void setHighwayConsumptionInLitres(double highwayConsumptionInLitres) {
        this.highwayConsumptionInLitres = highwayConsumptionInLitres;
    }

    public double getHighwayConsumptionInMilesPerGallon() {
        return highwayConsumptionInMilesPerGallon;
    }

    public void setHighwayConsumptionInMilesPerGallon(double highwayConsumptionInMilesPerGallon) {
        this.highwayConsumptionInMilesPerGallon = highwayConsumptionInMilesPerGallon;
    }

    public double getEstimatedAnnualFuelCost() {
        return estimatedAnnualFuelCost;
    }

    public void setEstimatedAnnualFuelCost(double estimatedAnnualFuelCost) {
        this.estimatedAnnualFuelCost = estimatedAnnualFuelCost;
    }

}

What is missing is the constructor and some other methods and that will be the topic for my next article.

Published at DZone with permission of its author, Ken Fogel.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

Geertjan Wielenga replied on Mon, 2014/06/23 - 6:14am

I think you should also include some discussion with your class about Lombok, if you're talking about JavaBeans: https://netbeans.org/kb/73/java/annotations-lombok.html

Ken Fogel replied on Mon, 2014/06/23 - 9:11am

Lombok reminds me of the @synthesize in Objective-C. The question becomes at what point do you show students automation like Lombok. When I switched to JSF from Servlets/JSP some members of my department were concerned that our students were not being exposed to low-level concepts.

Ewald Horn replied on Mon, 2014/06/30 - 11:35am

I agree, while Lombok is a great tool, I am also concerned that the introduction of it from the start will be confusing. Students have, in general, enough of a hard time understanding getters, setters and scope, let alone auto-generated code!

That being said, once the introductory material is out of the way, and students start to find creating getters and setters tedious, Lombok is like a breath of fresh air!

Irrespective of your approach, you have to admit, using NetBeans as a teaching tool is a lot simpler than other popular Java IDE's, simply because it's a single download, batteries included. Just imagine having to spend days to get environments configures, the right plugins downloaded etc...

Vidhyadharan De... replied on Tue, 2014/07/01 - 1:02am in response to: Geertjan Wielenga

I like the tutorial lombok.. Thanks Geertjan...


Thomas Maynard replied on Thu, 2014/07/10 - 7:40pm

I used a slide rule in my college days.  But does any student at all, ever learn to use a slide rule today?

I also used to be reasonably reactionary: If they don't learn the fiddly bits they won't really understand the subject.

But then: In 20 years, there won't be any fiddly bits any more.  Students need to learn how to get the job done, how to be productive from the start, and not to worry about internals (at least, not too much).

You don't need to know much about internal combustion to get a driver's license.  Teach what's important, and what makes the student better fit for the future, and you'll be doing your job properly.

Edit The sooner Lombok is integrated into the core Java language, the better off the world will be.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.