Why we need Constructor in Dart

Tags

, ,

Why we need Constructors
The first and the foremost task of constructors is construction of objects. Whenever we try to create an object and write this line:
var fatherBear = Bear();

laern-dart-the-hard-way

https://leanpub.com/learndartthehardway

We actually try to arrange a spot in the memory for that object. The real work begins when we connect that spot with class properties and methods.
Using ‘constructor’ we can do that job more efficiently. Not only that, Dart allows to create more than one ‘constructor’, which is a great advantage.
Let us write our ‘Bear’ class in a new way of using constructor:

//code 3.10
class Bear {
int numberOfFish;
int hourOfSleep;
int weightGain;
Bear(this.numberOfFish, this.hourOfSleep);
int eatFish(int numberOfFish) => numberOfFish;
int sleepAfterEatingFish(int hourOfSleep) => hourOfSleep;
int weightGaining(int weightGain) => weightGain = numberOfFish * hourOfSleep;
}
main(List arguments){
var fatherBear = Bear(6, 10);
fatherBear.weightGain = fatherBear.numberOfFish * fatherBear.hourOfSleep;
print(“Father bear eats ${fatherBear.eatFish(fatherBear.numberOfFish)} fishes. And he sleeps for ${fatherBear.sleepAfterEatingFish(fatherBear.hourOfSleep)} hours.”);
print(“Father bear has gained ${fatherBear.weightGaining(fatherBear.weightGain)} pounds of weight.”);
}

Creating ‘constructor’ is extremely easy. Watch this line:
Bear(this.numberOfFish, this.hourOfSleep);
The same class name works as a function or method and we have passed two arguments through that method. Once we get those values, we would calculate the third variable. Writing constructor this way is known as “Syntactic Sugar”. In the later section of the book, we will know more about the constructor.
Now it gets easier to pass the two values while creating the object. We could have done the same by creating constructor this way, which is more traditional:

//code 3.11
class Bear {
int numberOfFish;
int hourOfSleep;
int weightGain;
Bear(int numOfFish, int hourOfSleep){
this.numberOfFish = numOfFish;
this.hourOfSleep = hourOfSleep;
}
//Bear(this.numberOfFish, this.hourOfSleep);
int eatFish(int numberOfFish) => numberOfFish;
int sleepAfterEatingFish(int hourOfSleep) => hourOfSleep;
int weightGaining(int weightGain) => weightGain = numberOfFish * hourOfSleep;
}
main(List arguments){
var fatherBear = Bear(6, 10);
fatherBear.weightGain = fatherBear.numberOfFish * fatherBear.hourOfSleep;
print(“Father bear eats ${fatherBear.eatFish(fatherBear.numberOfFish)} fishes. And he sleeps for ${fatherBear.sleepAfterEatingFish(fatherBear.hourOfSleep)} hours.”);
print(“Father bear has gained ${fatherBear.weightGaining(fatherBear.weightGain)} pounds of weight.”);
}

In both cases, the output is same as before:

//output of code 3.11
Father bear eats 6 fishes. And he sleeps for 10 hours.
Father bear has gained 60 pounds of weight.

In the above code, you can even get the object’s type very easily. We can change the type of value quite easily. Watch the main() function again:

//code 3.12
main(List arguments){
var fatherBear = Bear(6, 10);
fatherBear.weightGain = fatherBear.numberOfFish * fatherBear.hourOfSleep;
print(“Father bear eats ${fatherBear.eatFish(fatherBear.numberOfFish)} fishes. And he sleeps for ${fatherBear.sleepAfterEatingFish(fatherBear.hourOfSleep)} hours.”);
print(“Father bear has gained ${fatherBear.weightGaining(fatherBear.weightGain)} pounds of weight.”);
print(“The type of the object : ${fatherBear.weightGain.runtimeType}”);
String weightGained = fatherBear.weightGain.toString();
print(“The type of the same object has changed to : ${weightGained.runtimeType}”);
}

And here is the output:

//output of code 3.12
Father bear eats 6 fishes. And he sleeps for 10 hours.
Father bear has gained 60 pounds of weight.
The type of the object : int
The type of the same object has changed to : String

Continue with For Loop in Dart

You have just seen how we have explicitly broken the inner loop and stopped one cycle of the inner loop. So ‘break’ is a very important concept while using ‘for loop’. At the same breath the ‘continue’ keyword also plays a very key role in ‘for loop’.
Let us consider this code snippet:

void loopContinue(){
for(var num = 1; num <= 5; num++){
if(num % 2 == 0 ){
print("These are all even numbers. $num");
continue;
} print("These are all odd numbers. $num");
}
}
main(List arguments){
loopContinue();
}

Watch the output and you will understand how the keyword ‘continue’ works.

These are all odd numbers. 1
These are all even numbers. 2
These are all odd numbers. 3
These are all even numbers. 4
These are all odd numbers. 5

Let us change the above code a little bit.

void loopContinue(){
for(var num = 1; num <= 5; num++){
if(num % 2 == 0 ){
//print("These are all even numbers. $num");
continue;
} print("These are all odd numbers. $num");
}
}

Here is the output:

These are all odd numbers. 1
These are all odd numbers. 3
These are all odd numbers. 5

According to the context, the keyword ‘continue’ means when the value is divisible by 2 and there is no remainder, just skip printing.
Let us change the code again and see the output.

void loopContinue(){
for(var num = 1; num <= 5; num++){
if(num % 2 == 0 ){
print("These are all even numbers. $num");
continue;
} //print("These are all odd numbers. $num");
}
}

Have a look at the output:

These are all even numbers. 2
These are all even numbers. 4

Now our context has changed. When the value is divisible by 2 and there is no remainder, the keyword ‘continue’ tells the program to continue with printing the value as long as the ‘if clause’ stays true.
‘Break and Continue’ are two very important concepts not only in Dart, but in every programming language.

Control the flow of your code in Dart

Tags

, , , ,

laern-dart-the-hard-way

https://leanpub.com/learndartthehardway

Controlling the flow of your code is very important. Every programmer wants to control the logic for many reasons; one of the main reasons is the user of the software should have many options open to them.
You do not know the conditions beforehand. You can only guess and as a developer, you should open as many avenues before the user as possible. There are several techniques adopted for controlling the flow of the code. The ‘if and else’ logic is very popular.
If and Else
Let us see an example where it works to control the flow of the code:

//code 2.37
main(List<String> arguments) {
bool firstButtonTouch = true;
bool secondButtonTouch = false;
bool thirdButtonTouch = true;
bool fourthButtonTouch = false;
if(firstButtonTouch) print("The giant starts running.");
else print("To stop the giant please touch the second button.");
if(secondButtonTouch) print("The giant stops.");
else print("You have not touched the second button.");
print("Touch any button to start the game.");
if(thirdButtonTouch) print("The giant goes to sleep.");
else print("You have not touched any button.");
if(fourthButtonTouch) print("The giant wakes up.");
else print("You have not touched any button.");
}
//output of code 2.38
The giant starts running.
You have not touched the second button.
Touch any button to start the game.
The giant goes to sleep.
You have not touched any button.

Now you can make this small code snippet more complicated.

//code 2.39
main(List<String> arguments) {
bool firstButtonTouch = true;
var firstButtonUntouch;
bool secondButtonTouch = false;
bool thirdButtonTouch = true;
bool fourthButtonTouch = false;
firstButtonUntouch ??= firstButtonTouch;
firstButtonUntouch = false;
if (firstButtonUntouch == false || firstButtonTouch == true) print("The giant is sleeping.");
else print("You need to wake up the giant. Touch the first button.");
if(firstButtonTouch == true && firstButtonUntouch == false) print("The giant starts running.");
print("To stop the giant please touch the second button.");
if((secondButtonTouch == true && thirdButtonTouch == true) || fourthButtonTouch == false) print("The giant stops.");
else print("You have not touched the second button.");
print("Touch any button to start the game.");
if(thirdButtonTouch) print("The giant goes to sleep.");
else print("You have not touched any button.");
if(fourthButtonTouch) print("The giant wakes up.");
else print("You have not touched any button.");
}

According to your complexity of the code, you should arrange your ‘if and else’ logic. And your output varies.

//output of code 2.39
The giant is sleeping.
The giant starts running.
To stop the giant please touch the second button.
The giant stops.
Touch any button to start the game.
The giant goes to sleep.
You have not touched any button.

For ‘if and else’ logic always remember these golden rules.

    1. 1. When both conditions are true, the result is true.

 

    1. 2. When both conditions are false, the result is false.

 

    1. 3. When one condition is true and the other condition is false, the result is false.

 

    4. When one condition is true or one condition is false, the result is true.

In the above code, I just try to give you an idea about how you can use ‘if and else’ logic where you really need it. However, this example is too simple. It can be complex when relational operators get added and the logic may become complex.
Finally before leaving this section, I would like to show you another code snippet where the existing set of rules or principles has been changed.


//code 2.40
main(List<String> arguments) {
bool firstButtonTouch = true;
var firstButtonUntouch;
bool secondButtonTouch = false;
bool thirdButtonTouch = true;
bool fourthButtonTouch = false;
firstButtonUntouch ??= firstButtonTouch;
firstButtonUntouch = false;
if (firstButtonUntouch == false || firstButtonTouch == true) print("The giant is sleeping.");
else if (thirdButtonTouch) print("You need to wake up the giant. Touch the first button.");
else if(firstButtonTouch == true && firstButtonUntouch == false) print("The giant starts running.");
else if (secondButtonTouch) print("To stop the giant please touch the second button.");
else if((secondButtonTouch == true && thirdButtonTouch == true) || fourthButtonTouch == false) print("The giant stops.");
else if (thirdButtonTouch) print("You have not touched the second button.");
else if (secondButtonTouch) print("Touch any button to start the game.");
else if(thirdButtonTouch) print("The giant goes to sleep.");
else if (firstButtonUntouch) print("You have not touched any button.");
if(fourthButtonTouch) print("The giant wakes up.");
else print("You have not touched any button.");
}

And here is the output of code 2.40:

The giant is sleeping.
You have not touched any button.
You can change the pattern and see what happens.

Learn Dart the Hard Way

Tags

, , , ,

laern-dart-the-hard-way

https://leanpub.com/learndartthehardway

Just published!

Just get the feel:

1. Why Dart Language?

You can answer this question in one sentence.
Dart is a great fit for both – Mobile Apps and Web Apps. Dart is free and open source and the repository is available at https://github.com/dart-lang. And at the same time you may get the feel of the language at the official website: https://www.dartlang.org/.
In this introductory chapter, let us try to understand why learning Dart language is important for us to build a mission critical Mobile Apps on iOS and Android.
It is important to note, in the very beginning, the developers around the world use Dart to create high-quality apps for iOS and Android and the web. It is feature rich so that client-side development is also possible. As we progress step-by-step, we will see how correct this statement is.

Continue reading

Interfaces and Method Injection in Laravel 5.8

Tags

, ,


Get my all books here….

https://leanpub.com/u/sanjibsinha

Laravel Authentication

Abstraction in OOP involves extraction of relevant details. Consider the role of a car salesman. There are many types of consumers. Everyone wants to buy a car, no doubt, but each one has differences in their criteria. Each of them is interested in one or two certain features. This attribute varies accordingly. Shape, color, engine power, power steering, price … the list is endless. The salesman knows all the details of the car but does he repeat the list one by one until someone finds his or her choice?
No.
He presents only the relevant information to the potential customer. As a result the salesman practices “Abstraction” and presents only relevant details to customer. Now consider abstraction from the perspective of a programmer who wants a user to add items to list.
Abstraction does not mean that information is unavailable but it assures that the relevant information is provided to the user. PHP 5 had introduced abstract classes and methods much before. And PHP 7 enhances it now making the general purpose language completely object oriented. Classes defined as abstract may not be instantiated and any class that contains at least one abstract method must also be abstract.
Remember that abstract methods can not define the implementation. On the other hand, object interfaces allow you to create code which specifies which methods a class must implement, without having to define how these methods are handled.
Interfaces are defined with the interface keyword, in the same way as a standard class, but without any of the methods having their contents defined.
All methods declared in an interface must be public; this is the nature of an interface. In Laravel, you will find the injection of interfaces to the classes quite frequently. In Laravel Interface is considered as a Contract.
Contract between whom? And why? Because an interface does not contain any code but it only defines a set of methods that an object implements. In SOLID design principle we have talked about maintaining a library of small classes with clearly defined scopes, and it is achievable with the help of Interfaces.

NASA scientists, engineers, and technologists: are they spies?

Tags

, , , , , ,

Spies in the Skies

Spies in the Skies


No.
They are not spies. Conventionally.
However, they have indirect links. From the very beginning.
The American Spy and Intelligence agencies CIA and NRO have recently published tons of declassified documents that support that notion.
Tehelka has published a cover story. Spies in the Skies.
I am giving the link below.
Declassified documents released by US government agencies like CIA, NRO are saying so. The truth has an official seal. NASA had designed shuttles that carried US spy satellites to orbit. Declassified documents depict a disgraceful relationship between NASA and CIA, the US Department of Defense (DoD), and National Reconnaissance Office (NRO). These agencies took part in designing NASA’s Shuttles throughout the 1970s.

Spies in the Skies

How to install Composer globally

Tags

, , , ,

First you need to issue this command:
ss@ss-desktop:~$ sudo apt-get update
You get this output:
[sudo] password for ss:
Hit:1 http://ppa.launchpad.net/apandada1/brightness-controller/ubuntu xenial InRelease
Hit:2 http://ppa.launchpad.net/ricotz/unstable/ubuntu xenial InRelease
Hit:3 https://dl.winehq.org/wine-builds/ubuntu xenial InRelease
Hit:4 http://in.archive.ubuntu.com/ubuntu xenial InRelease
Hit:5 http://ppa.launchpad.net/webupd8team/java/ubuntu xenial InRelease
Get:6 http://in.archive.ubuntu.com/ubuntu xenial-updates InRelease [109 kB]
Hit:7 http://ppa.launchpad.net/wine/wine-builds/ubuntu xenial InRelease
Get:8 http://in.archive.ubuntu.com/ubuntu xenial-backports InRelease [107 kB]
Get:9 http://security.ubuntu.com/ubuntu xenial-security InRelease [107 kB]
Fetched 323 kB in 2s (130 kB/s)
Reading package lists... Done

Continue reading

Why Resourceful Controller is Important for Authentication

Tags

, , , , ,

Creating Controllers in Laravel is super simple. In an MVC pattern, controllers play a vital role and defining your all request handling logic as an anonymous function in route file is unthinkable for a big application.

So we always use controllers as a transport medium. The main advantage of a controller is it can group all the request handling into a single class and that class can be stored in the ‘app/HTTP/Controllers’ directory.

There are a few ways to create a controller and we can always add some functionalities into it later to use features such as Middleware.

You can create a basic controller by using this command:
$ php artisan make:controller MyController
However, for a big application such as what we are going to create, it is always wise to create a resourceful controller.

When you issue such command:
$ php artisan make:controller MyController –resource
Laravel creates a controller class that automatically adds typical “CRUD” routes to the controller. This single line of code generates all types of methods that we usually require to create, retrieve, update and delete records. Continue reading

Two Books Coincided

An Amazing day indeed. The last day. I self-published a book on Laravel’s Authentication services on Amazon and Leanpub, simultaneously. The book handles not only the Authentication part alone, it throws light on Middleware filtering and Authorizations also.

In the afternoon while checking my Email I found my Publisher Springer Nature and Apress published another book on Ethical Hacking with Kali Linux. It was due. It’s an early release.

You cannot ignore either of two offspring born at the same time from the same pregnancy. A cute bundle indeed!

Just published! My new book on “Laravel 5.7.*: Middleware, Authentication, Authorization Explained: How to Authenticate a company,projects and tasks management application”.

Just published! My new book on “Beginning Ethical Hacking with Kali Linux: Computational Techniques for Resolving Security Issues”.

https://www.amazon.com/gp/product/B07L2QFBRY/ref=dbs_a_def_rwt_bibl_vppi_i12