Labels

Showing posts with label Beginners. Show all posts
Showing posts with label Beginners. Show all posts

Monday, December 11, 2017

Access Google gmail Inbox via 3rd Party Application

Hi folks,

Today, I’m come up with a pretty impressive application, which is allow you to access to your gmail inbox and get the feed about the latest email. This simple application based on Atom Google gmail inbox feed. Before I go to development session, let me explain about the atom, according to Google,
Atom is a system which makes it easy for you to receive, in one place, regular updates from news websites, blogs, and/or Gmail. You can use Atom with an aggregator (also known as a newsreader, feed reader, or RSS/Atom reader) to receive new message alerts.Here we go, this is the way to do it.

Pre Requirements.

In order to access gmail account, you have to allow to access ‘less secure Application’. You can easily do it by referring following screen shots.  

Go to Settings and then click Other Google Account settings

Click Apps with account access

Allow to Access Allow less secure apps

I’ll create a separate class to represent email object, and another separate class to get the data from atom the feed.




Get the sample project from GitHub Repository  


Happy Coding,


Best Regards,
Denuwan Himanga

Monday, April 18, 2016

JAVA Interface Implementation

Hi folks,

In this post, I decide to cover little bit confused area in programming, most of the candidates are little bit straggle in when they faced a job interview. On OOP inheritance concept depend on these two methodologies. I explained OOP & Abstract concept, in the previous post. Today I’m figuring out major differentiations between Interfaces & Abstract concepts.


Basically Interfaces & Abstract class are looks like same, when most of the programmers deal with those both methodologies in very first time. But the reality is these two techniques are introduced for the two different things. As well as in a very complex scenario, we must identify which technique implements in to the system. Otherwise, most probably we face difficulties in developing/maintaining stages in SDLC.   


Use Case Diagram

Ok, let’s try to implement above scenario, for the implementation process I use to unique features of the interface. Following list listed down those features. 

1.                  Interface variables are by default Constant.
2.                  The interface allows only method signatures / doesn’t allow method body (Above JAVA 8)
3.                  The interface allows to implement multiple interfaces.
4.                  Interface, Interface implementation can use extends keyword
5.                  Class, Interface implementation can using implements keyword
6.                  An interface can extend multiple interfaces.

Following code segments depict how Mobile Phone class extending using interface wise implementation. For the demonstration I used highlighted segment in above UML diagram. Try to implement whole scenario by yourself.  

package com.interfaces;

public interface Secondary {
    
    public double MEGA_PIXELS = 13;
    public int Resolution = 1080;
   
}
Secondary.java
package com.interfaces;

public interface Primary {
    
    public double MEGA_PIXELS = 13;
    public double FLASH_RANGE = 4;
    public int [] SENSITIVITY = { 100, 200, 400, 800, 1600 };
    public double ZOOM = 3;
    
    public boolean zoomLence(int zoomRange);
    
    public void onFlash();
    
    public void offFlash();
    
    public void autoFocus();
    
    public int changeSensitivity(int Sensitivity);
}
Primary.java

package com.interfaces;

public interface Display {
    
    public String TYPE = "CAPACITIVE TOUCHSCREEN, 16M COLORS";
    public double SIZE = 5.0;
    public int[] RESOLUTION = {720 , 1280};
    
    public boolean multiTouch();
    
    public boolean turnOnDisplay();
    
    public boolean turnOffDisplay();
    
}
Display.java
package com.interfaces;

public interface Camera extends Primary, Secondary{
    
    public String FILE_FORMAT = ".jpeg";
    
    public boolean saveFile(String fileName);
    
    public boolean capture();

}
Camera.java
package com.application;

import com.interfaces.Camera;
import com.interfaces.Display;

public class MobilePhone implements Camera, Display{
    
    private String brandName;
    private int year;
    private String price;
    
    @Override
    public boolean zoomLence(int zoomRange){
        return true;
    }
    
    @Override
    public boolean saveFile(String fileName){
        return true;
    }
    
    @Override
    public void onFlash(){
    }
    
    @Override
    public void offFlash(){
    }
    
    @Override
    public int changeSensitivity(int Sensitivity){
        return 0;
    }
    
    @Override
    public boolean turnOnDisplay(){
        return true;
    }
    
    @Override
    public boolean multiTouch(){
        return true;
    }
    
    @Override
    public boolean turnOffDisplay(){
        return true;
    }
    
    @Override
    public boolean capture(){
        return true;
    }
    
    @Override
    public void autoFocus(){
    }    
}
MobilePhone.java
Additionally, JAVA 8 allow to implement method body inside interface, it looks like the evolution of Interface concept. Following example, I explained how to implement a method body in the interface. Using default & static key word we can implement a method body inside an interface scoop. Also override a default method is not a mandatory, but two interfaces, content same name compiler doesn’t identify which method belongs to which interface. So mitigate demanding issues if there any method to contain the same method signature in two different interfaces, overriding is mandatory. But method which are implements using the static key word can’t override in any concern.
 interface Java8Interface {  
   static void playCaptureSound(){  
     System.out.println("Click !!!");  
   }  
   default void formatMemory(){  
     //Do stuff here;  
   }  
 }  


Happy coding,
Regards,
Denuwan Himanaga

Monday, March 21, 2016

String vs. StringBuffer/StringBulider

Hi all,

In this post, I’ll demonstrate how memory behave while string type data involving scenario in our JAVA program. Basically, when we execute JAVA program on our PC, Java mange PCs main memory in its own way. For string type operations are handled through following 3type of memory segments.
  • Stack
  • Heap
  • Pool






String data type is immutable, mean when changes happen to literals particular literals never change, instead of that reference variable pointed to new value. This behavior helps to manage literals which have more than one reference.

When we consider above scenario, step 1 to 4 nameList variable reference to new literals in each step of the loop, at the same time previous values of literals are reaming inside the pool without any references, instead of JAVA garbage collector involving to clear pool, String pool will be full of garbage value.

For mitigate these kind of scenario JAVA 5 introduces StringBuffer & StringBulider both of the classes are not immutable. Let’s see how to implement above code segment using StringBuffer class.


Happy Coding,
Regards,
Denuwan Himanga

Monday, December 21, 2015

Simple JavaScript Timer

Hi folks,

After long time I’m back, I’m really sorry about this two month delay. Because I’m really busy with my new job. By the way, today I’m here to share some simple, but little bit tricky JavaScript activity.
Develop a small JavaScript Timer, It’s a really easy thing. Actually, this may be easier than develop a simple “Hello World!” printing app.

setTimeout

This is the inbuilt method I use to develop this simple timer. Actually, this method gets two values as parameters. First, we need to pass a function name as a first parameter, then we need to pass a time in the millisecond format to mention execution time. In a simple word, setTimeout responsible for executing the passed function after giving time.

Here we go! This is the tricky point I use to build this simple web based timer, when page load I call the onload event to execute the ‘steamer’ function, this function responsible for print hours, minutes, seconds & milliseconds. After that recursively call it again after 1 millisecond its own function again. At that time these numeric values go thought some conditions for increase those values according to corresponding time format.
Additionally, use ‘twoDigitConverter’ function use for converting numeric values into two digit value.


That’s it, is that hard, actually this is what I’m always believe in this field, ‘Software industry depends on concepts & innovation thinking, instead of technology or resources’ isn’t it……..

JavaScript code jegment 
<script> var hour = 0; var minute = 0; var seconds = 0; var milliseconds = 0; function setTimer()  { if(milliseconds == 99) { milliseconds =0 if(seconds == 59) { seconds =0 if(minute == 59) { minute =0 hour++; }else { minute++; } }else { seconds++; } }else { milliseconds++; } document.getElementById('hour').innerHTML = twoDigitConverter(hour); document.getElementById('minute').innerHTML = twoDigitConverter(minute); document.getElementById('seconds').innerHTML = twoDigitConverter(seconds); document.getElementById('milliseconds').innerHTML = milliseconds; setTimeout("setTimer()", 10); } function twoDigitConverter(number) { var numberString = number+''; if(numberString.length == 1) { numberString = '0'+number; } return (numberString); </script>

CSS code segment
<style type="text/css"> body,td,th { color: #FFF; } .TimerTheme{ font-family: Segoe UI Light; background-color:#06F; font-size:36px; width:25%; text-align:center; } </style>

HTML code segment
<body onload="setTimer()">
<table width="100%" border="0">
  <tr>
    <td><table width="50%" border="0" align="center">
      <tr>
        <td class="TimerTheme" id="hour" />
        <td class="TimerTheme" id="minute" />
        <td class="TimerTheme" id="seconds" />
        <td class="TimerTheme" id="milliseconds" />
      </tr>
    </table></td>
  </tr>
</table>

</body>

Final Result
[Sample Code/Recourse]

Regards,
Denuwan Himanga.

Tuesday, September 1, 2015

LINQ (Language Integrated Query)



Hi guys,

The revolution of the computer programming technology rapidly change, Industry leaders such as Microsoft, Adobe try to add some values to their product in each releases. Within large amount of various product Microsoft own “C#” do an incredible revolution change in computer program developing history. They try to build universal language as well as universal IDE instead of different IDEs and different programing languages.   
According to their huge vision, Microsoft introduce Language Integrated Query with their amazing IDE called Visual Studio in 2008. Basically LINQ provide power full language capability to C#. Very simple explanation is, you can use only one programing language instead of SQL, XML etc.


In this demo, I’ll explain how to retrieve a data from SQL data base without using SQL query. LINQ consider table as a class, as well as data tuple as an object. That feature make computer programing technology to an amazing place J

Demo (Retrieving data from SQL database to WPF Application)    

Create a windows presentation foundation (WPF) application.



After that drag and drop a ‘list box’ from tool box, then formatted that list box according to our SQL table. In my case, Vehicle table formatted like this.


Using XAML code, we can easily format list box according to our requirement. Following XAML code segment format list box according to Vehicle table.

<ListBox x:Name="vehicleListBox" ItemsSource="{Binding}" Margin="0,40,0,0" >

            <ListBox.ItemTemplate>

                <DataTemplate>

                    <Grid>

                        <Grid.ColumnDefinitions>

                            <ColumnDefinition/>

                            <ColumnDefinition/>

                            <ColumnDefinition/>

                        </Grid.ColumnDefinitions>

                        <TextBlock Text="{Binding Vehicle_Name}" Grid.Column="0" Width="150"  FontSize="18" />

                        <TextBlock Text="{Binding Vehicle_Price}" Grid.Column="1" Width="150" FontSize="18"/>

                        <Image Source="{Binding Vehicle_Image}" Grid.Column="2" Width="150"/>

                    </Grid>

                </DataTemplate>

            </ListBox.ItemTemplate>

</ListBox>

Now we need to build a connection between SQL server and application, traditionally connection string responsibility for do this part. But in LINQ, add a .dbml Data classes and call when we need.
Add a .dbml data class to the project.


Add tables on to design surface.



After that, need to create a data classes data context object inside the application.

DataClasses1DataContext DB = new DataClasses1DataContext();

Finally we can easily call a values into special data type call ‘var’, this data type formatted according to values, which is assign to the variable. 

var result = from dt in DB.Vehicles
                         select dt;

vehicleListBox.ItemsSource = result.ToList<Vehicle>();
  
Source code/Project Repo.

Happy coding....

Best regards,
Denuwan Himanga.