Showing posts with label Java Programming. Show all posts
Showing posts with label Java Programming. Show all posts

18 June, 2017

Java Programming Basics to Brilliance Part 3

This post is a progression to previous post Java Programming Basics to Brilliance Part 2.



Before we make things complicated lets first get to know what is an array and how we can use it.

Quoting from Google,
array
əˈreɪ/
noun
  1. 1.
    an impressive display or range of a particular type of thing.
    "there is a vast array of literature on the topic"
verb
  1. 1.
    display or arrange (things) in a particular way.


In Java, an array is an object which holds a specified number of similar number, strings or objects. This depends on the type of array you are making.
The size of an array is set when it is initialized.
For e.g.
 int listOfNumbers[] = {1,2,3,12,2};

This is one way of making an array with values in it.
Now recall how everything in Java is nothing but an object. Considering that you can also make a blank array as follows:

String myHeroes[] = new String[5];

Now the above line of code initialized a blank array of String type of length 5.
Note that this array is of type String, so you cannot feed values other than String into it.
Let's feed some values into this array.

myHeroes[0] = "Lanaya";
myHeroes[1] = "Lina";
myHeroes[2] = "Rylai";
myHeroes[3] = "Mercurial";
myHeroes[4] = "Lyralei";

Now that we have an array of numbers listOfNumbers and an array of string myHeroes now obviously if we want to make use of this array we need a way to access these values. 
To do that we loop through the array and do whatever we want accordingly.
But before we loop through an array take note of how the length of array myHeroes is 5 but we added values up to myHeroes[4] only. This is simply because array index starts from 0 not 1 and that makes the size of array as 5.

To further clear up the confusion you can use 
arrayName.length; to find out the length of the array.

For e.g.

         System.out.println(myHeroes.length);


Now back to looping.
The simplest and easy to understand form of loop is a for loop. And once you understand for loop there another version of it called foreach loop which is really handy and saves lots of confusion.

for loop

for(int i=0; i<10; i++){
            System.out.println(i);
}

This simple loop will print numbers 1 to 9. How?

You can see the above for loop defined as follows

for(initial values; condition checking; update){
//loop body that executes if condition check is true
}

Now the update part of for loop need not necessarily be increment only. You can instead decrease the values, divide, multiply etc.
Rest should be pretty much self explanatory.
Lets use this loop to print the list of heroes we made.

         for(int i=0; i<myHeroes.length; i++){
            System.out.println(myHeroes[i]);
}

Notice how we ran the loop for values starting with to to just less than the length of array. Explained above.

Now a rather simplified version of for which is foreach loop.
For the array myHeroes[]
we can print the array values using for each as follows:

 for(String i: myHeroes){
            System.out.println(i);
        }

Think of foreach loop as defining a variable i that will be holding values from the array one by one.

To better understand this looping consider following lines of code

for(typeOfValuesInArray variableName: arrayName){
variableName; //this will access all of the values inside the array one by one in the order
}
Having done all that lets move to a fairly complex yet very useful part of Java called Collection.

Collection.

Collection are a way for storing objects in Java.
Collection provide necessary features which help in ordered storage of data and easier manipulation of the same.
There are various collection interfaces in Java predefined and you can create you own collection as well.
Namely, there is
List,
ArrayList, Vector,
Queue,
LinkedList, PriorityQueue,
Set,
Sorted Set, TreeSet, HashSet, LinkedHashSet,
Map,
HashTable, HashMap, LinkedHashMap, and TreeMap.

Various Collections

Various Map Collection

Unless you are giving some exam there is no absolute need for remembering all the names.
For the sake of this tutorial and get going with Java Programming I will be using just the ArrayList with reference of List collection and might cover others in future.

Consider the program below:

import java.util.ArrayList;
import java.util.List;

public class ListExample {




public static void main(String[] args) {

List<Dog> dogsList = new ArrayList<Dog>();

String dogName;

int dogId;

for(int i=0; i<10; i++){

dogName = "Dog"+Integer.toString(i);

dogId = i;

dogsList.add(new Dog(dogId, dogName));

}

for (Dog dog : dogsList) {

System.out.println(dog.getDogId());
System.out.println(dog.getDogName());

}

}

}





Different type of collection have different properties and usefulness in their domain. Deciding the right collection depending on the requirement is something that affects your application thoroughly. I will leave that you as it requires research depending on the requirement and even I'm not all aware of all of the collections. 

That's all for now. If you go through these 3 tutorials on Java you are likely to get through anything. I'm not entirely sure what will be next in this but in case I do write it will be updated here.
Keep Coding...

28 April, 2017

Java Programming Basics to Brilliance Part 2

This post is a progression to previous post here.



Java Syntax
Every language in this world has a syntax so does a programming language like Java. Now take a look at the following programming which simply prints the all famous "hello world."

public class play {
public static void main(String[] args){
System.out.println("hello world.");
}
}

In this program what we do is create a class by the name of play. In this class we create a function or method by the name of main in which we print out a line "hello world.".
Any program in java will always have a class and a main method. When a java program is executed, the main method is called and all of the statements in this method are executed in progression.
Writing the above lines of code is a piece of cake in Eclipse. To further define what is happening in above program in details, lets break it.

public class play {
States the beginning of the class. We stated class as public, which means it's members and methods will be accessible publicly. Other than public we have private, protected and default when you don't define anything. In details below.

public static void main(String[] args){
main method is always public because JVM has to make a call to this method. 
static methods carry a functionality that shall remain same throughout the program.
void just states that the method need not return anything.
String[] args defines an array of name args which will be holding everything we input to this program in this array. We need not necessarily use this array. 

Access modifiers in slight details.
A private class can have public methods. Why?
Because when we make a class private it is usually to protect it's instance variables from outside sources and public methods are the way to initialize these private instance variables.
If this confuses you, lets learn it with an extensive example.

public class MyDog {

private String tail;

public String waggingTail(String activity){
tail = activity;
return tail;

}

}


public class MyDogActivity {

public static void main(String[] args) {
MyDog tommy = new MyDog();

String tommysTail = tommy.waggingTail("Look at it move!!!");

System.out.println(tommysTail);
}

}

Write the above code line by and use Ctrl+space for suggestions in Eclipse. The two classes MyDog and MyDogActivity are meant to be in different class file. Run the program by using keyboard shortcut Ctrl+F11 for faster access.






What happens in the above program is, we have a class called MyDog with a private variable of String type tail and a public method waggingTail which sets its value to activity.
Furthermore we have a class MyDogActivity which creates an object of MyDog by the name of tommy. Now we make a String type variable tommysTail which has to perform a its task defined in MyDog class. If this confuses you the line below might help.

MyDog is a class which defines the structure of any dog out in the world. Like any dog can wag his tail we made a function waggingTail also no dog likes their tail to be touched so we made the tail private.

Now we have a dog by the name of tommy. Like any other dog tommy has a tail called tommysTail and can perform whatever function any dog can perform like waggingTail.


I hope this clears the picture about MyDog and tommy being one of the Dog. If you still find it confusing read it over and make your own classes and objects to clear it up and feed it in your head. This is by far the most important aspect in Java and everything we do in Java consists of classes and objects one way or the other.

Moving on, lets make a simple program of adding two digits we take as input from the user.

public class Addition {

public static void main(String[] args) {

Scanner inputReceiver = new Scanner(System.in);

System.out.println("Enter first number: ");

int firstNumber = inputReceiver.nextInt();

System.out.println("Enter second number: ");

int secondNumber = inputReceiver.nextInt();

int sum = firstNumber + secondNumber;

System.out.println("The sum is: "+sum);

}

}


In the above program we make an object called inputReceiver of the class Scanner. Now what is Scanner?



Scanner like our
MyDog class in previous example is a class which has certain attributes and functions.
Like our
MyDog had a tail attribute and a function of wagging the tail, Scanner is a class predefined in Java which carries a function to take the input and forward it as required.If you want to see this class just press hold Ctrl and click on Scanner in Eclipse.
In the example above, we make an object of class Scanner by the name of inputReceiver using which we take the input
firstNumber and secondNumber. Rest of the code should be self explanatory as its simple addition and printing. This is pretty much how everything is done in Java, just thing become more complicated depending upon the complexity of a program.
Like most of the object oriented programming languages out there, Java also has methods for looping and condition checking like


for loop,
if else,
while,
do while,
switch.

There are various ways of storing data in Java at run time like
Variables which have types of int, String, char, byte, boolean,
then arrays of same types. Which are still simple and easy to understand and deal with.
Things get complicated in Java with the introduction of Collections, reason being collection consists of objects rather than simple strings and integer values and it's one of the feature that makes Java powerful.

Next up will be Collection, conditional operators and loops.

Go to next. http://www.shiftescape.com/2017/06/java-programming-basics-to-brilliance.html

24 April, 2017

Java Programming Basics to Brilliance Part 1



Let's get our hands dirty with Java Application Development. By the end of this course, you will be able to understand and code in Java programming language. We will also be making some real-life application which I haven't yet decided.
Now, I know there are just too many sites out there which can teach you Java and probably far better than what I am writing here. So what am I gonna do here which is different from them? Nothing.
I am only going to repeat what you have probably already read. But I will keep it short and try to make it as easy as possible.
This is part one, and I haven't planned as for how many parts this series will have.
This tutorial is meant for complete beginners to the Programming world. This will get you an idea of how programming(not just Java) works in the shortest possible way.

Useful links: 
This is my way of tracking my progress. 

First of, 
What is Java?
  1. Java is an OOP based programming language. OOP stands for Object-Oriented Programming. Defined further ahead.
  2. You can make cross-platform(including mobile) applications using Java. 
  3. You can make web applications using Java.
How do Java works?
Broadly
  1. Java code is written by a developer.
  2.  The compiler reads the complete code and generates a .class file which contains the bytecode.
    This .class file is like the instructions for JVM defined next.
  3. JVM, stand for Java Virtual Machine takes the .class file and does whatever it says.
The step 2-3 results in Java being platform independent. JVM is a system specific software and every different kind of system will have a different kind of JVM. 

Things you will need.
I will simplify this for you in one step, just install Eclipse IDE. Download it from here. It will help you setup rest.

To mention is any way you will need to download JDK(Java Development Kit) from Oracle website here. Set the environment variables to run Java files through command prompt or link JDK folder in Eclipse. That's it. 

What is OOP?
Given that we are learning an OOP based programming language it's natural to ask what actually is OOP? 
Object Oriented Programming is a concept of programming in which we deal and do anything and everything by creating objects.
Objects are like real-world entities in the program. Like any object in real life will have certain characteristics, objects in a program is expected to carry some attributes. Like any object in real life can do something, objects in a program can also do something. For example, an Object like myDog has a dogTail as an attribute and myDog can wag his dogTail.

OOP can always be achieved with the 4 features, namely 
Abstraction: which is hiding of details from the viewer. Like f I want to feed myDog I don't need to know how his digestive system works I just have to offer myDog his food.
Encapsulation: which is binding up of certain feature and features to limit their scope of functionality. We use access modifiers like Public, Private, Protected and Default to achieve this. For example, if myDog is Public anyone can pet him, but if myDog is private and not allowed to move out of the home, only the people inside the home can pet him.
Inheritance: which is carrying the similar attributes like that of their parent. This reduces the amount of code written and makes it easier to understand. For example, if myDog has a myPuppy he will carry similar attributes like Tail, Eyes, Ears.
Polymorphism: which is different behaviour performed by the object in different situations. For example, myDog will walk slowly when I take him for a walk but run away when I let him. So the feature is same which is move() but mydog can either walk or run depending on the situation.

Java Syntax
Every language in this world has a syntax so does a programming language like Java.

Continued further here.


Featured Post

Content Writing VS Content Marketing: How are Both Different From Each Other?

Regardless of how long the businesses have been introduced to digital marketing, it is often seen that they confuse the terms of marketing. ...