<< Chapter < Page Chapter >> Page >

Save the frequency value

The code in the method stores the frequency value in an element of the array object discussed earlier.

The element number is specified by the value of index shown in square brackets in the assignment expression. (This syntax is similar to storing a value in an array element in most programming languages that I am familiar with.)

Pressing a radio button to select a station

Listing 8 shows the playStation method. This is the method that simulates the result of having the user press a button on the front of the radio to select a particular radio station for play.

Listing 8 . The playStation method.
public void playStation(int index){ System.out.println("Playing the station at " + stationNumber[index]+ " Mhz"); }//end method playStation

Selecting and playing a radio station

The method receives an integer index value as an incoming parameter. This index corresponds to the number of the button pressed by the user.This method simulates the playing of the radio station by

  • extracting the appropriate frequency value from the array object, and
  • displaying that value on the computer screen along with some surrounding text.

When called by code in the main method of this program, this method produces the message shown in Figure 1 on the computer screen

That summarizes the code and the behavior of this simple program.

Class definition syntax

There are a number of items that can appear in a class definition, including the following:

  • Instance variables
  • Class variables
  • Instance methods
  • Class methods
  • Constructors
  • Static initializer blocks
  • Inner classes

Let's keep it simple

In order to make these modules as easy to understand as possible, the first several modules will ignore the possibility of class variables, class methods, static initializer blocks, and inner classes.

As mentioned in the earlier discussion of class variables, these elements aren't particularly difficult to use, but they create a lot of complications when attempting to explain OOP from the viewpoint of Java programming.

Therefore, the first several modules in the series will assume that class definitions are limited to the following elements:

  • Instance variables
  • Instance methods
  • Constructors

A constructor

A constructor is used only once in the lifetime of an object. It participates in the task of creating (instantiating) and initializing the object. Following instantiation, the state and behavior of an object depends entirely on instance variables, class variables, instance methods, and class methods.

Instance variables and methods

The class named Radio discussed earlier contains

  • one instance variable named stationNumber , and
  • two instance methods named setStationNumber and playStation .

Summary

This module has concentrated primarily on a discussion of the Java class.

A simple Java program was discussed to illustrate the definition and use of two different classes. Taken in combination, these two classes simulate the manufacture and use of the car radio object introduced in an earlier module.

You saw how to write code to create a new Radio object by applying the new operator to the class named Radio .

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Object-oriented programming (oop) with java. OpenStax CNX. Jun 29, 2016 Download for free at https://legacy.cnx.org/content/col11441/1.201
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Object-oriented programming (oop) with java' conversation and receive update notifications?

Ask