Platinum Partner
netbeans

NetBeans in the Classroom: How to Teach JavaBeans

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.

{{ tag }}, {{tag}},

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}
{{ parent.authors[0].realName || parent.author}}

{{ parent.authors[0].tagline || parent.tagline }}

{{ parent.views }} ViewsClicks
Tweet

{{parent.nComments}}