<< Chapter < Page Chapter >> Page >

What is tapeIn used for?

As you will see shortly, the value of the variable named tapeIn is used to determine if it is possible to play the tape or to play the radio.

According to that logic:

  • If tapeIn is true, it is possible to play the tape but it is not possible to play the radio.
  • If tapeIn is false, it is possible to play the radio, but it is not possible to play the tape.

tapeIn is not declared in the Combo class

It is also worthy of note that in this version of the program, the variable named tapeIn is not declared in the Combo class (this will change in the next module where the program uses method overriding) . Rather, this variable is inherited from the Radio class that is extended by the Combo class.

The removeTape method

The removeTape method of the Combo class is shown in Listing 3 . Its behavior is pretty much the reverse of the insertTape method, so I won't discuss it further.

Listing 3 . The removeTape method.
public void removeTape(){ System.out.println("Remove Tape");tapeIn = false; System.out.println(" Tape is out"); System.out.println(" Radio is on"); }//end removeTape method\

The playTape method

Listing 4 shows the method named playTape defined in the new Combo class.

Listing 4 . The playTape method .
public void playTape(){ System.out.println("Play Tape");if(!tapeIn){//tapeIn is false System.out.println(" Insert the tape first"); }else{//tapeIn is trueSystem.out.println( " Tape is playing");}//end if/else }//end playTape

Confirm that the tape is ready

Calling the method named playTape can be thought of as sending a message to the Combo object asking it to play the tape. The code in the playTape method checks to confirm that the value stored in the tapeIn variable is true before executing the request to play the tape.

If tapeIn is false , an error message is displayed advising the user to insert the tape first.

If tapeIn is true , the method prints a message indicating that the tape is playing.

Modified Radio class

Listing 5 shows the definition of the modified version of the class named Radio .

Listing 5 . Modified Radio class.
class Radio{ protected double[]stationNumber = new double[5]; protected boolean tapeIn = false

Tape status

The first significant change that was made to the class named Radio is shown in Listing 6 below.

Listing 6 . Tape status.
protected boolean tapeIn = false;

The statement in Listing 6 declares and initializes a new instance variable named tapeIn . As explained earlier, this instance variable is used to indicate whether or not a tape is inserted. (The Combo class inherits this variable.)

Earlier in this module, I explained how the playTape method of the Combo class uses this value to determine whether or not to attempt to play a tape.

Change to the playStation method

The significant change that was made to the method named playStation of the Radio class is shown in Listing 7 below.

Listing 7 . Change to the playStation method.
if(!tapeIn){//tapeIn is false System.out.println(" Playing the station at "+ stationNumber[index] + " Mhz");}else{//tapeIn is true System.out.println(" Remove the tape first")

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