I like to define a class as a collection of common items that make-up an object's functionality. You'll also hear definitions like, "a class is the blueprint from which individual objects are created."
We'll create a class called TV. The TV class will have regular TV operations such as Turn On, Turn Off, Volume Up, Volume Down, Channel Up, Channel Down, Set Channel and even Set Volume.
We'll then instantiate that class from the TestTV class.
You may notice that in order to instantiate the TV class, we set the data-type as the class name:
TV tv1 = new TV();
"A class is essentially a programmer-defined type."
Fun Fact: Arrays in Java are treated as objects.
The code should be pretty straightforward.
Overview of TV.java:
- Initialize the channel variable and set it to 1
- Initialize the volumeLevel variable and set it to 1
- Set the on variable to false so that the TV is in the default off position.
- turnOn() method to turn the TV on
- turnOff() method to turn the TV off
- setChannel() method to set the TV to a specific channel. Note that the TV must be on
- setVolume() method to set the TV to a specific volume level.
- channelUp() to increment the channel by 1
- channelDown() to decrement the channel by 1
- volumeUp() to increment the volume by 1
- volumeDown() to decrement the volume by 1
TestTV.java overview
- Instantiate the TV class to tv1 and tv2
- For tv1
- Turn on the TV
- Set the channel to 30
- Set the volume to 3
- For tv2
- Turn on the TV
- Increment the channel by 1
- Increment the channel by 1
- Increment the volume by 1
- Display current channel and volume levels for tv1 and tv2
Informative. Thanks for sharing.
ReplyDeleteBenefits of cryptocurrency exchange script
ReplyDeletebinance clone script V2.0 enhanced
Crypto Exchange Software
Cryptocurrency Exchange Script
binance clone Software
bitcoin Exchange Script
binance clone Script
Read More: Mobile App Development Trends
ReplyDelete