askIT

Java Objects

Lagre Download example files

If you look around you'll see many real objects: your dog, desktop, mobile or your bike.

Objects have two characteristics. They have a state and a behavior. Dogs have state (name, color, breed, hungry) and behavior (barking, fetching, wagging tail). Bicycles also have state (gear selection, current speed) and behavior (changing gear, apply the brakes).

If you're able to think state and behavior in programming you are ready to think object oriented.

Spend some time to observe real objects around you. For each object you can ask yourself:

Note some answers on a sheet. The objects you discover has diffrent steps of complexity. A lamp has two states (on or off) and two possible behaviors (turn on, turn off). A radio has multiple states (on, off, volume, station) and behaviors (turn on, turn off, increase volume, decrease volume, seek, scan).

lampe_pa_av.jpg

Example with a radio

Create a new Java project that you call MyRadio.

02_new_java_prosjekt.jpg

03_velg_java_application.jpg

04_prosjektnavn.jpg

You should now have an empty code that looks like this:

package myradio;
public class MyRadio {
        public static void main(String[] args) {
        }
       
}

Create statuses
The first thing we can do is to create code for statuses of the radio.

package myradio;
public class MyRadio {

        // States
        int onOff = 0;
        int currentVolume = 10;
        String currentStation = "The Beat";

        public static void main(String[] args) {
        }
       
}

Create constructor
Next we must create a constructor. A constructor makes it possible to create an object of type MyRadio.

package myradio;
public class MyRadio {

        // States
        int onOff = 0;
        int currentVolume = 10;
        String currentStation = "The Beat";

        // Constructor
        public MyRadio() {
                onOff = onOff;
                currentVolume = currentVolume;
                currentStation = currentStation;
        }

        public static void main(String[] args) {
        }
       
}

Create your own radio
Now we have come so far that we can make a radio of MyRadio. This is done by typing
MyRadio radioname = new MyRadio();.

package myradio;
public class MyRadio {
       
        // States
        int onOff = 0;
        int currentVolume = 10;
        String currentStation = "The Beat";
       
        // Constructor
        public MyRadio() {
                onOff = onOff;
                currentVolume = currentVolume;
                currentStation = currentStation;
        }
       
        public static void main(String[] args) {
                // Create a radio
                MyRadio newRadio = new MyRadio();
               
        }
}

Team behavior to turn the radio on and off
We now have to create different behaviors.
We want to turn the radio on or off, set the volume and change the radio station.
We use a technique named setters and the getter. First we will code set and get to turn on or off the radio.
We want to be able to change the value of the variable onOff.

We do this by code the method setOnOff to turn the radio on and off. In addition, we code the method getOnOff to display the status of the radio.

package myradio;
public class MyRadio {
       
        // States
        int onOff = 0;
        int currentVolume = 10;
        String currentStation = "The Beat";
       
        // Constructor
        public MyRadio() {
                onOff = onOff;
                currentVolume = currentVolume;
                currentStation = currentStation;
        }
       
        public static void main(String[] args) {
                // Create a radio
                MyRadio newRadio = new MyRadio();
       
        }
       
       
        // Set on off status of radio
        public int setOnOff(int statusValue){
                return onOff = statusValue;
        }
        // Get on off status of radio
        public int getOnOff(){
                return onOff;
        }
}

Usage of setOnOff and getOnOff
We need to code the methods into main. We can find out if the radio is turned on by writing code:
System.out.println("Radio is: " + newRadio.getOnOff());

To turn the radio on or off, we use the code:
newRadio.setOnOff(1); // Turns the radio on
newRadio.setOnOff(0); // Turns the radio off

This program code is now:

package myradio;
public class MyRadio {
       
        // States
        int onOff = 0;
        int currentVolume = 10;
        String currentStation = "The Beat";
       
        // Constructor
        public MyRadio() {
                onOff = onOff;
                currentVolume = currentVolume;
                currentStation = currentStation;
        }
       
        public static void main(String[] args) {
                // Create a radio
                MyRadio newRadio = new MyRadio();
               
                // Is radio on or off?
                System.out.println("Radio is: " + newRadio.getOnOff());
                newRadio.setOnOff(1);
                System.out.println("Radio is: " + newRadio.getOnOff());

               
        }
       
       
        // Set on off status of radio
        public int setOnOff(int statusValue){
                return onOff = statusValue;
        }
        // Get on off status of radio
        public int getOnOff(){
                return onOff;
        }
}

05_radio_paa_av.jpg

Assignments

1. Programs into methods to change the volume (currentVolume). The methods shall have the names setCurrentVolume and getCurrentVolume.

2. Programs into methods to change the radio station (currentStation). The methods shall be named setCurrentStation and getCurrentStation.

3. Modify the program such that the user can enter 1 to turn on the radio, or 0 to turn off the radio. Let the user change the volume between 0 and 20. Allow the user to enter the desired radio station by selecting between NRJ, NRK mp3, p3, The Beat, P4 and Radio Norway.

Add comment

avatar_blank_60.png

Comment:

Name:

Email:

Comments

No comments yet. You can be the first one to comment. Just write your comment in the form and click on the submit button.