Simulate a solar toy car – Java Example


Hello everybody, welcome to the Java
example section. In this section, I’m going to simulate a solar toy car.
First, I would create a ToyCar class. And then I would create a SolarPanel
interface. That means I can combine the ToyCar class and the SolarPanel
interface to create my SolarToyCar class, ok. And in my SolarToyCar
implementation, it … the instance coming from this class can have his own
charging approach, which is different from that of the conventional toy car.
And a SolarToyCar instance can also work with the solar energy directly,
which is having a different approach of using energy in … than the way for the ToyCar class.
Now let … let us see the implementation. I would start with the
ToyCar class first. For our toy car, I would have two instance variables A toy car
would have a battery level It also has a variable, which is a final variable
representing the full battery level of a ToyCar object. This final keyword means
that I’m not able to change this value any longer after setting up it with a
final keyword. And we can see that both instance variables are having the
“protected” access modifier. It means that I’m going to set up a SolarToyCar
class which can access these two variables directly with … without using
the getter methods provided by the ToyCar class. It would simply
ease my process of creating the SolarToyCar class, okay Now I create the
constructor of the conventional ToyCar When I set up a ToyCar object, its
battery level would be simply equal to one thousand unit, which is the case when
the battery is fully charged And now I give some methods for a particular ToyCar
object. I can tell it to move forward by a certain distance I can tell
a toy car to go forward for some meters indicated by the formal parameter
called distance Here the battery level would also be reduced by the same amount
as the distance in meters. What does it mean by that? It simply means that one
unit of distance is equal to the consumption of one battery level Okay.
In addition to the movement, a toy car is also able to turn I can turn … left or
right by some angle. And Direction here is simply a data type indicated by the enum
keyword, which is shown here. When I want to set up two constants in … by using the
enum data type, I can do something like this I have two constants. One is LEFT
and one is RIGHT Usually we would use all capital letters
to declare the contents in the enum data type. Let me save it That means I
can turn left or turn right The direction of the turn can be shown in
the printing statement I can also explain the degrees of the
turn When I do the turning action, the battery level would also be decreased The battery level would be dropped by
this same value as the angle. That means one unit of rotation or turning would be
equal to one battery level Okay. I can also create some set getter methods for
the instance variable called batteryLevel This getter method is for the
outside world to understand the battery level of a certain ToyCar object Of course, a toy car needs to recharge. So I set up a method called chargeBattery A conventional toy car would need to be
charged up from a wall socket After charging, the battery level would return
to the full battery level which is simply 1,000 units, okay. So this
completes the ToyCar class. Let me save it And now I can go to the SolarPanel,
which is simply an interface I will have two solar panels for each
solar panel component, okay The solar panel needs to be recharged, so the
charge battery method should be a bit different from the one in the typical
ToyCar class, okay A solar toy car is also able to move with the solar energy
coming from the solar panel directly So I have to set up a method pertaining to
this situation that means I’m not going to use the power coming from the battery
in the conventional toy car A solar toy car is also able to turn by consuming the solar
energy stored in the solar panel So this completes the interface of the
solar panel, okay. So these methods will be implemented in the SolarToyCar
class, which combines the cor …ToyCar class together with the SolarPanel
interface Okay, let me go to the SolarToyCar class,
which extends ToyCar and implements SolarPanel. And we have to remember that
the “extends” key word should come before the “implements” keyword. Now we see that
we have a chargeBattery method in the ToyCar class. But also we have the solar …
chargeBattery method in a solar panel. So what does it mean by that? I
can do the method overriding for the SolarToyCar class I’m going to
override the chargeBattery method coming from ToyCar class So the solar toy car is going to charge
up by using the solar panels. And I can show the number of panels contained in
the particular solar toy car by calling the static variable in this SolarPanel
interface, okay After charging by the solar panel, the battery level would
become the full battery level, which is simply equal to one thousand units.
Because this variable is a protected variable, the subclass coming from the
superclass is also able to access the protected variables directly Now I set up the two methods coming from the SolarPanel interface The solar toy car can move without using
the battery power. It simply uses the pen … the power coming from the solar panel directly. In this case, the battery level would not be lost. So I don’t need to set
up the battery level after the movement I can do similar things to the turnWithSolarEnergy method Ok, this completes the SolarToyCar class, which has a lot
of properties coming from the ToyCar class and the SolarPanel interface. Let
me save it Okay, there should be no problem with the SolarToyCar class. For
now, I can go back to the driver program You’ll see how I can call the SolarToyCar objects Now I’m going to set up a SolarToyCar
object Okay, the SolarToyCar class has no construct … constructor being set up
explicitly. That means I’m going to use the default constructor of my SolarToyCar
class Okay, I can tell the myCar object to give me the
battery level I can also tell the solar toy car to turn right by 45 degrees . So
um the turn method must be simply inherited from the ToyCar class, which
is shown here, okay. When I turn the solar toy car with the turn method, I’m going
to use up the battery. So the battery level would be dropped. And we can see
that the enum data type is employed here by using the Direction dot RIGHT
value coming from the Direction enum data type, okay? After such a turning motion, I can see
the battery level later I can also tell a solar toy car to move forward by 30
meters. When I use the forward method, it simply comes from the ToyCar class,
which would use up the energy coming from the battery So in this case, I can
check the battery level after the movement action Okay, I can also tell my car to turn with
solar energy by 20 degrees After that, I may able to check the
battery level. Because the methods shown here would not consume the energy
coming from the battery, the battery value should be unchanged. We can see
the result later I can also tell myCar to move with solar energy for 100 meters.
And of course, I can show the battery level after this movement with solar
energy I can also tell myCar to charge battery. Since the solar toy car has
already overridden the method coming from the superclass, we would be able to
see that the solar toy car is going to charge the battery a bit differently I can show the battery level after the
charging action. So let me save it, and try to run it. Yes, a lot of output coming out When I set up the constructor, the
battery level would be initially 1,000 units. After turning right by 45
degrees, the battery would be dropped by 45 units. So it remains nine hundred
and fifty five units. And after going forward by thirty meters, the battery
will be further dropped by 30 units. So it becomes 925 units. And when I turn
the solar toy car by 20 degrees without using solar … using the battery power, the
battery power would remain … remain unchanged. Same for the case when
I move with solar energy The energy stored in the battery will
not be lost. So when I charge a battery for my solar toy car, I would charge it
with my two solar panels. And after the charging, it would return to 1,000
immediately. So this completes my solar toy car simulation. My code is intuitive
and straightforward, but it may not be the most efficient approach to the
problem. If you can suggest any idea to improve my code, please do so on the
comment section below the video. If you like this video, please give me a like
and please subscribe to my channel . Thank you for watching

, , , , , , , , , , , , , , , , , ,

Post navigation

Leave a Reply

Your email address will not be published. Required fields are marked *