Monday, August 21, 2017

Generics Learning Step by Step

List Interface:
---------------
The List interface represents a list of Object instances. This means that we could put any object into a List.

List list = new ArrayList();
list.add(new Integer(2));
list.add("a String");

Here we have inserted integer and string in list. If we want to read, then we need to cast as follows.

Integer integer = (Integer) list.get(0);
String string   = (String) list.get(1);

Why it is required to cast?
Ans:
Because multiple type of data are inserted in the list. So it is required to cast in access time. If we want to eradicate this casting we should use generics which confirms only single type of data will be inserted in the collection.

Advantages of Generic Features:
1. With the Java Generics features you can set the type of the collection to limit what kind of objects can be inserted into the collection.
2. You don't have to cast the values you obtain from the collection.

Example of Java Generic feature:
List<String> strings = new ArrayList<String>();
strings.add("a String");

String aString = strings.get(0);

What is diamond operator(<>)?
When you just write a diamond operator as generic type, the Java compiler will assume that the class instantiated is to have the same type as the variable it is assigned to. In the example, that means String because the List variable has String set as its type.
List<String> strings = new ArrayList<>();
=======================================================================================================

Iterator without generics example:
---------------------------------
    ArrayList names = new ArrayList();
    names.add("Chaitanya");
    names.add("Steve");
    names.add("Jack");

    Iterator it = names.iterator();

    while(it.hasNext()) {
      String obj = (String)it.next();
      System.out.println(obj);
    }

Iterator with generics example:
-------------------------------
    ArrayList<String> names = new ArrayList<String>();
    names.add("Chaitanya");
    names.add("Steve");
    names.add("Jack");

    Iterator<String> it = names.iterator();

    while(it.hasNext()) {
      String obj = it.next();
      System.out.println(obj);
    }

Difference between Iterator and Enumeration:
--------------------------------------------

An iterator over a collection. Iterator takes the place of Enumeration in the Java Collections Framework. Iterators differ from enumerations in two ways:
1) Iterators allow the caller to remove elements from the underlying collection during the iteration with well-defined semantics.
2) Method names have been improved. hashNext() method of iterator replaced hasMoreElements() method of enumeration, similarly next() replaced nextElement().

=======================================================================================================
while vs for-each loop in generics:
-----------------------------------
List<String> strings = new ArrayList<String>();

//... add String instances to the strings list...

for(String aString : strings){
  System.out.println(aString);
}
For each loop:
-------------
1. This for-each loop iterates through all String instances kept in the strings list.
2. For each iteration, the next String instance is assigned to the aString variable.
3. This for-loop is shorter than original while-loop

while loop:
-----------
1.  you would iterate the collections "Iterator" and call Iterator.next() to obtain the next instance.
=======================================================================================================

Resource Link:
http://tutorials.jenkov.com/java-generics/generic-list.html
https://beginnersbook.com/2014/06/java-iterator-with-examples/

Straight Line Graphs y=mx+c

Saturday, August 19, 2017

Meaning of “exclusive” and “inclusive” when describing number ranges?

Inclusive Vs Exclusive  >>

First Answer:


The following function prints the powers of 2 from 1 through n (inclusive).
This means that the function will compute 2^i where i = 1, 2, ..., n, in other words, i can have values from 1 up to and including the value n. i.e n is Included in Inclusive
If, on the other hand, your book had said:
The following function prints the powers of 2 from 1 through n (exclusive).
This would mean that i = 1, 2, ..., n-1, i.e. i can take values up to n-1, but not including, n, which means i = n-1 is the highest value it could have.i.e n is excluded in exclusive.

Second Answer:

In Computer Science, inclusive/exclusive doesn't apply to algorithms, but to a number range (more specifically, to the endpoint of the range):
1 through 10 (inclusive)
1 2 3 4 5 6 7 8 9 10

1 through 10 (exclusive)
1 2 3 4 5 6 7 8 9
In mathematics, the 2 ranges above would be:
[1, 10]
[1, 10)
You can remember it easily:
  • Inclusive - Including the last number
  • Exclusive - Excluding the last number

Thursday, August 17, 2017

Sequence Diagram tutorial

CV writing approach by Niaz Ahmed

একজন টার্মিনাল ম্যানেজারের সাথে ওয়ান টু ওয়ান ডিস্কাশনঃ ভাই, আপনাদের কোম্পানির ওয়ার্কশপ আছে? -জি আছে। আপনাদের যে মেশিনগুলো আছে, সেগুলোর জন্য স্পেয়ার পার্টস লাগে না? - জি ভাই, লাগে। স্পেয়ার পার্টস গুলো কি ইমপোর্ট করতে হয়? - জি ভাই, ইম পোর্ট করা লাগে। কারা করে? - সাপ্লাই চেইন ডিপার্টমেন্ট। আচ্ছা, প্রতি বছর কি পরিমান পার্টস ইম পোর্ট করতে হয়? - প্রায় ৬০ লাখ টাকার। যদি সময় মতো পার্টস না আসে, তাহলে কি মেশিন বন্ধ থাকে? - না, তখন আমরা নিজেরা ওয়ার্ক শপে বানায়ে নিয়ে কাজ চালাই। আচ্ছা, এমন কি কখনো হয়েছে, সময় মতো স্পেয়ার পার্টস আসেনি, তখন আপনি অবস্থাটা সামাল দিয়েছিলেন? - জি ভাই, আছে তো। গত বছর ও এরকম হইছিলো, আমি নিজে পার্টসের মেজারমেন্ট নিয়ে তৈরি করছি। এতে কি হইছে? - এক দিনের জন্যই মেশিন বন্ধ থাকে নাই। তো, আপনি যেহেতু এই পার্টস বানায়ে মেশিন রানিং রাখতে পারছেন, তো কোম্পানিকি এখন আর সেই পার্টস ইম পোর্ট করে? - না তো। তো, এতে করে কত টাকা সেভ হচ্ছে? - ৫ লাখের মতো। এভাবেই প্রশ্ন করে করে আমরা আপনার ভিতরের কথা বের করে আনি। "Saved BDT 5 lac of the company per year by developing spare parts for machine at company's workshop & ensured smooth production" মনে রাখবেন, একজন ম্যানেজার কি করেছে, তা গুগোল করলে পাওয়া যাবে, কিন্তু ম্যানেজার হিসেবে আপনি কি করেছেন, তা কোথাও পাওয়া যাবে না।

Wednesday, August 9, 2017

How to check mongo process ID? How to check mongo status?

How to check mongo process ID:

ps -ax | grep -v grep | grep mongod
or
ps -aux | grep mongod
or

pgrep mongod

> 1017
--------------------------------------------------
Kill the process:

sudo kill -9 <PID>

Example:
sudo kill -9 1017
--------------------------------------------------
Check mongo Status:

Sudo systemctl status mongod
or
Sudo service mongod status

--------------------------------------------------
Start mongo service:

Sudo systemctl start mongod
or
Sudo service mongod start

--------------------------------------------------
Stop mongo service:

Sudo systemctl stop mongod
or
Sudo service mongod stop

Tuesday, August 8, 2017

Monday, August 7, 2017

Sequence Diagram learning step by step

Sequence Diagram:


i) SD shows the interactions between objects in the sequential order.
ii) During the requirements phase of a project, analysts can take use cases to the next level by providing a more formal level of refinement. When that occurs, use cases are often refined into one or more sequence diagrams.
iii) how a future system should behave is documented using SD.
iv)

Why SD is required?

One of the primary uses of sequence diagrams is in the transition from requirements expressed as use cases to the next and more formal level of refinement. Use cases are often refined into one or more sequence diagrams.

In addition to their use in designing new systems, sequence diagrams can be used to document how objects in an existing (call it "legacy") system currently interact. This documentation is very useful when transitioning a system to another person or organization.

What is Notation?

i) Notation is a frame for drawing.
ii) The frame element is used as a basis for many other diagram elements in UML 2, but the first place most people will encounter a frame element is as the graphical boundary of a diagram.
iii) A frame element provides a consistent place for a diagram's label, while providing a graphical boundary for the diagram.

What is Namebox for a notation?

The diagram's label is placed in the top left corner in what I'll call the frame's "namebox," a sort of dog-eared rectangle.

What is "Interaction"?

On sequence diagrams incoming and outgoing messages (a.k.a. interactions) for a sequence can be modeled by connecting the messages to the border of the frame element.

How to give name for sequence diagram?

Diagram Type | Diagram Name

specific text values for diagram types
sd = Sequence Diagram, activity = Activity Diagram, and use case = Use Case Diagram

Main Purpose of sequence diagram:

i) The main purpose of a sequence diagram is to define event sequences that result in some desired outcome.
ii) The focus is less on messages themselves and more on the order in which messages occur; nevertheless, most sequence diagrams will communicate what messages are sent between a system's objects as well as the order in which they occur.
iii) The diagram conveys this information along the horizontal and vertical dimensions: the vertical dimension shows, top down, the time sequence of messages/calls as they occur, and the horizontal dimension shows, left to right, the object instances that the messages are sent to.

What is lifeline?

When drawing a sequence diagram, lifeline notation elements are placed across the top of the diagram. Lifelines represent either roles or object instances that participate in the sequence being modeled.
Lifelines are drawn as a box with a dashed line descending from the center of the bottom edge (Figure 3). The lifeline's name is placed inside the box.
An example of the Student class used in a lifeline whose instance name is freshman
The UML standard for naming a lifeline follows the format of:
Instance Name : Class Name

Asynchronus Message:

 An asynchronous message is drawn similar to a synchronous one, but the message's line is drawn with a stick arrowhead.

Resource Link:

  1. https://www.ibm.com/developerworks/rational/library/3101.html
  1. https://www.ibm.com/developerworks/rational/library/3101-pdf.pdf

UML Interview Questions Step by Step

UML:

Q1: What is UML?
Ans:
  1. UML is Unified Modeling Language.
  2. It is a graphical language for visualizing, specifying, constructing and documenting the artifacts of the system.
  3. It allows you to create a blue print of all the aspects of the system, before actually physically implementing the system.


Q2: What is modeling?
Ans:
Modeling is a proven and well-accepted engineering technique which helps build a model. Model is a simplification of reality; it is a blueprint of the actual system that needs to be built.

Q3: What are the advantages of creating a model?
Ans:
  1. Model helps to visualize the system.
  2. It helps to specify the structural and behavior of the system.
  3. It helps to make templates for constructing the system.
  4. It also helps to document the system.

Q4: What are the different views that are considered when building an object-oriented software system?
Ans:
Normally there are 5 views.
  1. Use Case view - This view exposes the requirements of a system.
  2. Design View - Capturing the vocabulary.
  3. Process View - modeling the distribution of the systems processes and threads.
  4. Implementation view - addressing the physical implementation of the system.
  5. Deployment view - focus on the modeling the components required for deploying the system.

Q5: What are diagrams?
Ans:
Diagrams are graphical representation of a set of elements most often shown made of things and associations.

Q6: What are the major three types of modeling used?
Ans:
Major three types of modeling are structural, behavioral, and architectural.

Q7: Mention the different kinds of modeling diagrams used?
Ans:
There are 9 types of Modeling diagrams.
  1. Use case diagram,
  2. Class Diagram,
  3. Object Diagram,
  4. Sequence Diagram,
  5. statechart Diagram,
  6. Collaboration Diagram,
  7. Activity Diagram,
  8. Component diagram,
  9. Deployment Diagram.
Q8:What is Architecture?
Ans:
Architecture is not only taking care of the structural and behavioral aspect of a software system but also taking into account the software usage, functionality, performance, reuse, economic and technology constraints.

Q9: What is SDLC?
Ans:

SDLC is Software Development Life Cycle. It includes various processes that are Use case driven, Architecture centric, Iterative and Incremental.

This Life cycle is divided into phases. Phase is a time span between two milestones.

The milestones are Inception, Elaboration, Construction, and Transition. Process Workflows that evolve through these phase are Business Modeling, Requirement gathering, Analysis and Design, Implementation, Testing, Deployment. Supporting Workflows are Configuration and change management, Project management.

Q10: What are Relationships?
Ans:
There are different kinds of relationships:
  1. Dependencies,
  2. Generalization,
  3. and Association.

Dependencies:

Dependencies are relationships between two entities that a change in specification of one thing may affect another thing. Most commonly it is used to show that one class uses another class as an argument in the signature of the operation.

Generalization:

Generalization is relationships specified in the class subclass scenario, it is shown when one entity inherits from other.

Association:
Associations are structural relationships that are: a room has walls, Person works for a company.

Aggregation(Association):

Aggregation is a type of association where there is a has a relationship, That is a room has walls, if there are two classes room and walls then the relationship is called an association and further defined as an aggregation.

Aggregation is the typical whole/part relationship. This is exactly the same as an association with the exception that instances cannot have cyclic aggregation relationships (i.e. a part cannot contain its whole).

Composition:
Composition is exactly like Aggregation except that the lifetime of the 'part' is controlled by the 'whole'. This control may be direct or transitive. That is, the 'whole' may take direct responsibility for creating or destroying the 'part', or it may accept an already created part, and later pass it on to some other whole that assumes responsibility for it.

Q11: How are the diagrams divided?
Ans:

Classification of diagram:
9 diagrams are divided into 2 types.

  • static diagrams
  • and dynamic diagrams.

Static Diagrams (Also called Structural Diagram):
  1. Class diagram,
  2. Object diagram,
  3. Component Diagram,
  4. Deployment diagram.

Dynamic Diagrams (Also called Behavioral Diagrams):
  1. Use Case Diagram,
  2. Sequence Diagram,
  3. Collaboration Diagram,
  4. Activity diagram,
  5. Statechart diagram.

Q12: What are Messages?
Ans:

A message is the specification of a communication, when a message is passed that results in action that is in turn an executable statement.

Q13: What is an Use Case?
Ans:

  • A use case specifies the behavior of a system or a part of a system,
  • It is used to capture the behavior that need to be developed.
  • It involves the interaction of actors and the system.

Resource Link:

Q14: Can you explain ‘Extend’ and ‘Include’ in use cases?
Ans:
‘Extend’ and ‘Include’ define relationships between use cases. Below figure ‘Extend and Include’ shows how these two fundamentals are implemented in a project. The below use case represents a system which is used to maintain customer. When a customer is added successfully it should send an email to the admin saying that a new customer is added. Only admin have rights to modify the customer. First lets define extend and include and then see how the same fits in this use case scenario.

Include: Include relationship represents an invocation of one use case by the other. If you think from the coding perspective its like one function been called by the other function.

Extend: This relationship signifies that the extending use case will work exactly like the base use case only that some new steps will inserted in the extended use case.


Scenerio#1:

I often use this to remember the two:

My use case: I am going to the city.

includes -> drive the car

extends -> fill the petrol

"Fill the petrol" may not be required at all times, but may optionally be required based on the amount of petrol left in the car. "Drive the car" is a prerequisite hence I am including.

Scenerio#2:
Extend is used when a use case adds steps to another first class use case.

For example, imagine "Withdraw Cash" is a use case of an ATM machine. "Access Fee" would extend Withdraw Cash and describe the conditional "extension point" that is instantiated when the ATM user doesn't bank at the ATM's owning institution. Notice that the basic "Withdraw Cash" use case stands on its own, without the extension.

Include is used to extract use case fragments that are duplicated in multiple use cases. The included use case cannot stand alone and the original use case is not complete without the included one. This should be used sparingly and only in cases where the duplication is significant and exists by design (rather than by coincidence).

For example, the flow of events that occurs at the beginning of every ATM use case (when the user puts in their ATM card, enters their PIN, and is shown the main menu) would be a good candidate for an include.