Thursday, 23 August 2018

USER INPUT IN JAVA

USER INPUT IN JAVA

Hello Friends,
                         Today I am going to tell you two ways to take input from user in java.
As we know that java is an object oriented language so any work is based on classes and objects.
First Way :

                 In this method we use two java classes named as : 

               InputStreamReader  and BufferedReader 

There are 2 type of streams : Input Stream and Output Stream 
Input Stream are those stream which receive or read data coming from some other place. and Output Stream are those stream which send and write data 

All the streams are represented by the classes in java.io package, so we need to import this package in our program.
import java.io.*;
 To take input from the keyboard we use System.in 
System.in: it represent InputStream object, which by default represent standard input device i.e. Keyboard

Now first step to create object of InputStreamReader class and connect keyboard to it

1)  InputStreamReader obj = new InputStreamReader(System.in);

Second we have to create the object of BufferedReader class and connect the previous object to that

2)  BufferedReader  br = new  BufferedReader(obj);

Now to read data we have 2 methods of BufferedReader Class 

    read()     and readLine();

read() :    this method is used to read a single character from the keyboard, but it returns the ASCII value of that character. So to print the character on screen we have to convert integer (ASCII value) to char data type 

char ch = (char) br.read();

readLine():  this method read a string from keyboard such as :

String s = br.readLine();

 If we want to take integer we have to convert it 

int n = Integer.parseInt(br.readLine());

 In the same way we can convert string in any of primitive data type.

Example :


import java.io.*;
class First
{
       public static void main(String args[]) throws IOException
        { 
             //above two lines in article can be combimed and written as :
              BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
              System.out.print("enter your name, gender and age");
              String name = br.readLine();
                char gender = (char)br.read();
              int age = Integer.parseInt(br.readLine());

             // display details 
             System.out.println("name = " + name);
             System.out.println("gender = " + gender);
             System.out.println("age = " + age);
}

The only thing which is not discussed till now is Throws IOException  i have written right after main method. it is because :
read() method of BufferedReader class throw an runtime exception named IOException and our program stuck bcaz of the exception . there are 3 ways to handle exception 
1) put the code generating exception in try block and define a catch block below
2) throw the exception using throw keyword
3) throws keyword is used at the method definition, which is generating Exception
so we are using 3rd way in this program ..

I hope it will be helpful for those who is studying java first time ...... 
If you have any question, u can ask in comment ..

Thank You ...






Friday, 20 July 2018

Introduction to android

ANDROID BASICS  


All you need to know before starting android programming ...

ANDROID is an open source  operating system that is based on a modified version of linux.

About its architecture 

1) LINUX KERNEL  -- It contains device driver for display, camera, memory .....

2)  LIBRARIES  -- developed in C/C++

some of them are --

SURFACE MANAGER - for display management

SQLITE   -- for database support

SSL --or security

WEBKIT  -- for web browsing

OpenGL/ES -- graphic library

FREETYPE -- it is used as font render in android

SGL -- scalable graphic library

LIBC -- standard C library

SSL -- secure socket layer

3) ANDROID RUNTIME 

Core libraries    &   DALVIK VIRTUAL MACHINE (DVM)

*   Program for android are written in java and compiled to byte code for JVM, which is then translated to Dalvik bytecode and  stored in .dex (dalvik executable) and .odex (optimized dalvik executable)

Succesor of Dalvik is ART Android Runtime which uses the same byte code and  .dex file but not .odex.

* Each app is a different user for  android OS.
* Each app is assigned a unique linux user ID which is used by the system & unknown to the app.
* It is possible to arrange for  2 apps to share the same linux user ID, in which they aare able to access each others files
* App with same user ID can also arrange to run in same linux process & share the same VM.


To be continued .....



Thursday, 19 July 2018

Matrix & ITS Types

MATRIX :

"Matrix is nothing but the rectangular representation of data in the form of rows and columns inside square brackets "


  • A row matrix is a matrix that has only one row.
  • A column matrix is a matrix that has only one column.
Example :

Image result for matrix examples in maths
To convert linear equations into matrix :

Each equation in the system becomes a row. Each variable in the system becomes a column. The variables are dropped and the coefficients are placed into a matrix. If the right hand side is included, it's called an augmented matrix. If the right hand side isn't included, it's called a coefficient matrix.

The system of linear equations ...

 x +  y -  z = 1
3x - 2y +  z = 3
4x +  y - 2z = 9

becomes the augmented matrix ...


x
y
z
rhs


1
1
    -1 
        1


3
-2
1
3


4
1
-2
9


Type of Matrix :

1. ZERO/NULL Matrix :

Image result for zero matrix examples in maths

2. UPPER  & LOWER TRIANGULAR MATRIX :

Image result for upper triangular matrix examples in maths


3. Diagonal Matrix  :

Image result for diagonal matrix example
4. IDENTITY MATRIX:

Image result for diagonal matrix example
5. SYMMETRIC MATRIX : 


Image result for diagonal matrix example

* If a matrix is equal to its transpose matrix then it is called symmetric matrix.

                                                                  A = AT

6. SKEW SYMMETRIC 

* If a matrix is equal to the negative of its transpose then it is called SKEW SYMMETRIC 


 A = -AT


Example of skew symmetric :

Image result for skew symmetric  matrix example


7. Idempotent Matrix :

Image result for idempotent matrix example

* A square matrix A is called idempotent matrix if   

A2 = A

8. Involutory Matrix 
 A2 = I






Thursday, 16 July 2015

JSP DIRECTIVES


JSP DIRECTIVES

Basically JSP have 3 type of directives :
  1. Page Directives
  2. Include Directive
  3. Taglib Directive
Here we will discuss about Include Directive :
Include directive provide us the facility to reuse the elements.
* Included  elements can contain jsp code so they are inserted into the the page at the time the page is translated  to servlet.
So the main problem with include directive is if we modify the included page content, we have to update the all the main jsp pages that include that page .
There is a another way to overcome this limitation.

How to include file at Page Translation Time with Include Directive

Syntax     <% include file = "relative url"  %>


Example : 

there are 2 files : IncludeDirective.jsp : this page is included into other jsp page.
IncludeDirective2.jsp : Main Jsp page that include IncludeDirective.jsp page.

IncludeDirective.jsp :

<%--  this page will be included into other jsp page   --%>
<html>
<head>
<title>Insert title here</title>
</head>
<body>
<%@ page import="java.util.Date" %>
<%!
private int accessCount = 1;
private Date accessDate = new Date();
private String accessHost = "<I>No previous access</I>";
%>
<P>
<HR>
This page &copy; 2000
<A HREF="http//www.my-company.com/">my-company.com</A>.
This page has been accessed <%= ++accessCount %>
times since server reboot. It was last accessed from
<%= accessHost  %>  at  <%= accessDate %>.
<%  accessHost = request.getRemoteHost();   %>
<%=  request.getContentType()  %>
<%   accessDate = new Date();  %>
</body>
</html>


IncludeDirective2.jsp

<%--  the main jsp page which will include other jsp   --%>


<html>
<head>
<title>Insert title here</title>
</head>
<body>
<P>
Information about our products and services.
<P>
Blah, blah, blah.
<P>
Yadda, yadda, yadda.
<%-- 
this file will be included at the time of translation of jsp page into servlet    --%>
<%@ include file="IncludeDirective.jsp" %>
</body>
</html>


Output  :







 

JSP : Java Server Pages


JAVA SERVER PAGES



"JSP technology is an extension of the servlet technology."


JavaServer Pages (JSP) technology enables you to mix regular, static HTML with dynamically generated content from servlets. 
we write the regular HTML in the normal manner, then enclose the code for the dynamic parts in special tags, most of which start with <%and end with %>

HOW JSP WORKS

The process of making JavaServer Pages accessible on the Web is much simpler than that for servlets. all we need is a web server that support JSP pages . we save our file with .jsp extension and thats all no compiling, no packages, no classpath required.
Behind the scene our JSP page is translated into a servlet where static HTML content being printed to the output stream associated  with servlet's service method.

Inside a JSP Page :

Aside from the regular HTML, there are three main types of JSP constructs that you embed in a page: scripting elements, directives, and actions.

SCRIPTING ELEMENTS :   java code that will become part of                                                      resultant servlet.
DIRECTIVES :    control the overall structure of servlet.

ACTION :    control the behaviour of JSP engine.

SCRIPTING ELEMENTS 

  1.  EXPRESSIONS :  which are evaluated and then inserted into servlet's output .                                                                                                           General form :   <%=           %>                                                                      Alternate XML syntax :                                                                                                                                                              <jsp : expression>                                                                        Java Expression                                                                         </jsp : expression>                                                                      
  2. SCRIPTLETS :  which are inserted into servlet's  _service method.                                                                                                   General form:    <%               %>                                               Alternate XML syntax :                                                                                                     <jsp : scriptlets>                                                                             code                                                                                          </jsp : scriptlets>                                                                          
  3. DECLARATION : which are inserted into body of servlet's class outside any method.                                                                         General form:     <%!       %>                                             Alternate XML syntax :                                                                                                     <jsp : declaration>                                                                         code                                                                                         </jsp : declaration>

Static HTML inside a JSP page is called Template Text.

JSP Comments are in the form   <%--   jsp comment        --%> 

A simplest example of 

EXAMPLE :   

ScriptingElement.jsp 

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<html>
<head>
<title>Insert title here</title>
</head>

<body>

<%--   expression example     --%>
current date and time is :
<%=   new java.util.Date()    %>
<hr>
<%= request.getRemoteHost() %>
<hr>

<%--   delaration example     --%>
<%!   int count =1;    %>

<%--    scriptlet example     --%>
<%      count++;
       out.println(count);   %>
<hr>
</body>
</html>


OUTPUT



Thursday, 8 May 2014

ARRAY  IN  DATA STRUCTURE





There are 2 type of implementations of Ordered List data structure :     
                                                                     1.     Array
                                                                     2     Pointer
  •  Ordered list structures that are implemented with arrays are known as Sequential List.
  • Arrays are collection of elements where each element is identified by an index or key.
  • Memory Address of the first element of the array is called Base Address.
  • Two dimensional arrays are also called :    Tables Array   or Matrix Array.
  • especially Vector Processors are often optimized for array operations.
  • Arrays can be used to determine partial or complete control flow of program so also known as Control Tables.
  • Array indices (subscript)  may begin with 0,1,n. and other data types like enumeration or characters may be used as array index.
  • The number of indices needed to specify an element is called Dimension  or Rank of the array. 
  • Address Formula for the element at index i 
        :  for one dimensional array = B+C.i         
                                                             where B= Base address
                                                      C= constant (add increment)

       :  for two dimensional array   B+C.i+d.j
  • Dope Vector or Array's descriptor or Stride Vector 
Dope Vector is a record that include all the parameters as dimension d, base add. b, and the increment c1,c2 ....ck.





Wednesday, 31 July 2013

MCQ on OSI Model in networking

 MCQ

OSI MODEL

Q.1) The Internet model consist of -----------  layer 

Ans ...            Five.

Q.2)  which of the following is an application layer service ..

A) mail system
B) File Transfer
C) Remote Log In 
D) All of them

Ans .......     D

Q.3) When a host on n/w A send a message to the host on n/w B which address does the router look at ?

Ans ...       Logical


Q.4 )   IPV6 has ........  bit address ?

Ans ........     128

Q.5) ICMPv6   includes .............

Ans .........   IGMP     and ARP

Q.6)  ............. is a process-to-process protocol that adds only port addresses, checksum error control, and length information to the data from the upper layer.

Ans ..........     UDP

Q.7)  The ________ address, also known as the link address, is the address of a node as defined by its LAN or WAN.

Ans .....     physical

Q.8)   Ethernet uses a ______ physical address that is imprinted on the network interface card (NIC).

Ans .....      6 byte

Q.9)   A port address in TCP/IP is ______ bits long.

Ans ....   16

Q.10)  The TCP/IP _______ layer is equivalent to the combined session, presentation, and application layers of the OSI model.

Ans ...........   Application

 

Q.11)   The ____ address uniquely defines a host on the Internet.

Ans ......    IP

Q.12)  The_____ address identifies a process on a host. 

Ans .....    port

Q.13)  Routers operate at which layer of the OSI Model?

Ans ...  Network

Q.14) Which of the following operates at the Presentation layer?

Ans .....  MIDI & JPEG

Q.15)   Which of the following are Transport layer protocols?

Ans .....     TCP  and UDP

Q.16)  Flow Control take place at which layer ?

Ans ....   Transport

Q.17)  Repeaters & hubs operate at whaich layer?

Ans .....    Physical 

Q.18)  Bit synchronization is handled at which layer of the OSI Model?

Ans ..... Session

Q.19)   Bridges operate at which layer of the OSI Model?

Ans .........Network

Q.20) What are the sublayers of the Data Link layer?

Ans ....   MAC   and LLC

Q.21)  Which layer is responsible for packet sequencing, acknowledgements, & requests for retransmission?

Ans ....   Transport

Q.22) Which layer translates between physical & logical addresses?

Ans ....   Network

Monday, 8 July 2013

ARRAY EXAMPLE IN JAVA

Hello Friends,
                        It is very common to have objective question about Array creation in any competition exam.
and it is also possible to be confused and do mistake. 
So here I am giving you an program as an example.
This program illustrate correct as well as incorrect array syntax :



public class ArrayExample {

    /**
     * @param args
     */
    public static void main(String[] args) {

        //first way to create array. declairing,constructing and initializing in the same line
    int[] array1={1,2,4,6};
   
     //retrieving array elements
    for(int i=0;i<array1.length;i++)
    {
    System.out.println(array1[i]);
    }

    //second way to create array
    //declaire and construct an array
    int[] array2;
     array2 = new int[3];
 
     int x= -2;
     array2[x] = 7;
     System.out.println(array2[x]);
       //  Run time exception

   
   
     //it is not valid to give size at the time of declaration
   
    int[3] array3;
   
     //so above statement gives compilation error.
   
     int[] array4 = new int[3];
     byte b = 4;
     char c = 'a';
     short d = 7;
     array4[0] = b;
     array4[1] = c;
     array4[2] = d;
     for(int i=0; i<array4.length;i++)
     {
         System.out.println(array4[i]);
         
     }
 

     // constructing Multidimentional array
    String array2d[][] = new String[3][];
    //this is a valid syntax but
   
     String sw1[][] = new String[][3];
    //is not valid .
   
   
    //Second way to create 2-d array
    String array2d1[][] = {   {"ram","shiv","bholenath"}, {"o","m","n","a","m"}};
   
    //Second way to initialize 2-d array
    int array2d3[][] = new int[3][];
    array2d3[0]= new int [2];
    array2d3[0][0]=6;
    array2d3[0][1]=7;
   array2d3[0][2]=9; 

// run time exception 
    array2d3[1]= new int [1];
    array2d3[1][0] = 3;
    }

}

Friends just copy and paste this code and save the file ArrayExample.java , and Run it.
Code in blue colour work correctly but code in red colour gives u error.  So comment the code in red and then run it. and look carefully because it may lead you to a mistake.


Thank you ..........

Sunday, 7 July 2013

ARRAYS in JAVA


                                                                  ARRAYS  IN JAVA


Some interesting facts about arrays in java :

A)  It is never legal to include the size of array at the time of array    declaration in JAVA.
int[7] marks;    is invalid
Because jvm does not allocate space until you actually initiate array object, then size matters.


B) Array objects created on heap. 

C) int array[][] = new int[3][];  is acceptable in java 
    but
    int[][] array = new int[][3];   is not acceptable 

   because jvm needs to know the size of object assigned to the     variable array.

D) Anonymous Array Creation :

  int[] test;
  test = new int {4,7,5};

In this anonymous array we construct and initialize and array and then assign the array to previously declare array reference variable.
      The preceding code create a int array object with three elements 4,7,5 and assign this object to previously declared array reference test.
       We do not specify the size  of anonymous array obj. , size is derived from the number of elements in the curly braces .
So  :   new Object[3] {null, new object(), new object()};
is invalid  size must not be specified.

E)  Just in Time array argument :

       public class Foo {
        void takeArray(int[] someArray)
        { 
             //  use array parameter
        }

       public static void main (String[] args) {
              Foo f = new Foo();
              f.takeArray(new int[]  {1,2,3,4,5});
        }
    }

      Here we are not assigning array object to any reference variable, just-in-time array is used as an argument to method.







Friday, 21 June 2013

JAVA MCQ EXERCISE


HEY FRIENDS ......
                            Try to answer  ..................... 



1.  Which four options describe the correct default values for array elements of the types indicated?
  1. int -> 0
  2. String -> "null"
  3. Dog -> null
  4. char -> '\u0000'
  5. float -> 0.0f
  6. boolean -> true
A. 1, 2, 3, 4B. 1, 3, 4, 5
C. 2, 4, 5, 6D. 3, 4, 5, 6



Q.2  Which one of these lists contains only Java programming language keywords?
A. class, if, void, long, Int, continue
B. goto, instanceof, native, finally, default, throws
C. try, virtual, throw, final, volatile, transient
D. strictfp, constant, super, implements, do
E. byte, break, assert, switch, include




Q.3)  Which is a reserved word in the Java programming language?
A. methodB. native
C. subclassesD. reference
E. array

Q.4) Which three are legal array declarations?
  1. int [] myScores [];
  2. char [] myChars;
  3. int [6] myScores;
  4. Dog myDogs [];
  5. Dog myDogs [7];
A. 1, 2, 4B. 2, 4, 5
C. 2, 3, 4D. All are correct.


Q.5)  
public interface Foo 
{ 
    int k = 4; /* Line 3 */
}
 
Which three piece of codes are equivalent to line 3?
  1. final int k = 4;
  2. public int k = 4;
  3. static int k = 4;
  4. abstract int k = 4;
  5. volatile int k = 4;
  6. protected int k = 4;   


A . 1, 2  and 3                                    B.   2, 3, 4
C.  3, 4  and 5                                    D.   4, 5, 6


9.  Which three are valid declarations of a char?
  1. char c1 = 064770;
  2. char c2 = 'face';
  3. char c3 = 0xbeef;
  4. char c4 = \u0022;
  5. char c5 = '\iface';
  6. char c6 = '\uface';
A. 1, 2, 4
B. 1, 3, 6
C. 3, 5
D. 5 only


10.   Which is the valid declarations within an interface definition?
A. public double methoda();
B. public final double methoda();
C. static void methoda(double d1);
D. protected void methoda(double d1);




 
 
 

Search This Blog

Total Pageviews