.

Arrays in JAVA

Saturday, 20 April 2013
After building basics in java ,now its time to pack your thoughts and come in the world of arrays.
you have already wanted to learn arrays but due to some way or another you didn't do that.your.probably your instructor or your friends call you dumb who can not learn arrays.so here is goes THE MAKING OF ARRAYS

An array is like a tray of cups.
Steps to declare an array.
step 1. Declare an int array variable .an array variable is a remote control to an array object.
            int[] nums;
step 2.  create a new int array with length of 7,and assign it to the previously declared int[] variable nums
             nums=new int[7];
step 3.  give each element in the array an int value.
              remember elements in an int array are just int variables.
    nums[0]=6;
    nums[1]=7;
    nums[2]=12;
    nums[3]=16;
    nums[4]=62;
    nums[5]=36;
    nums[6]=55;
you can also make arrays of Dogs  instead of nums..............

declare a DOG array variable
Dog[] pets;
create a new name array with a length of five ,and assign it to previously declared name[] variable names.
pets=new Dog[7];
create a new dog objects and assign them to the array elements.
pets[0]=new Dog();
pets[1]=new Dog();
............................
you can also access them as
Dog fido=new Dog();
fido.name="FIDO";
fido.bark(0;
fido.chasecat(0;
*********************************************************************************
programs
MATRIX ADDITION
import java.util.scanner.*;

class Addm
{
 public static void main(String[] arg)
 {
  int i,j;
 int a[][]=new int[3][3];
 Scanner sc=new Scanner(System.in);
 for(i=0;i<3;i++)
 {
   for(j=0;j<3;j++)
 {
  a[i][j]=sc.nextInt();
 }
}
  int b[][]=new int[3][3];
 for(i=0;i<3;i++)
 {
   for(j=0;j<3;j++)
 {
  b[i][j]=sc.nextInt();
}
}
int c[][]=new int[3][3];
 for(i=0;i<3;i++)
 {
   for(j=0;j<3;j++)
 {
  c[i][j]=a[i][j]+b[i][j];
 System.out.println("Matrix Addition="+c[i][j]);
}
}
}
}

*********************************************************************************
DIAGONAL OF MATRIX


import java.util.Scanner;
class Diagonalm
{
 public static void main(String[] arg)
 {
  int i,j;
 int a[][]=new int[3][3];
 Scanner sc=new Scanner(System.in);
 for(i=0;i<3;i++)
 {
   for(j=0;j<3;j++)
 {
  a[i][j]=sc.nextInt();
 }
}
System.out.println("Left diagonal elements=0");
 for(int k=0;k<3;k++)
 {
   for(int l=0;l<3;l++)
 {
 if(k==l)
 {
 System.out.println(a[k][l]);
}
}
}
 System.out.println("Right diagonal elements=0");
 for(int m=0;m<3;m++)
 {
   for(int n=0;n<3;n++)
 {
  if(m+n==2)
 {
  System.out.println(a[m][n]);
 }
}
}
}
}
*********************************************************************************
example to see a dog barking

class Dog
{
String name;
public static void main(string[] args)
{
//make a dog object and access it
Dog dog1=new Dog();
dog1.bark();
dog1.name="bart";
// now make a dog array
Dog[] mydogs=new Dog[3];
// put some dogs in it
mydogs[0]=new dog();
mydogs[1]=new dog();
mydogs[2]=dog1;
//now access dogs using arrays and references
mydog[0].name="fred";
mydogs[1].name="marge";
//now loop through the array
// nad tell all the dogs to bark
int x=0;
while(x<mydogs.length)
{
mydogs[x].bark();
x=x+1;
}
}

public void bark(0
{
System.out.println(name+"says ruff|");
}
public void eat()
{
}
public void chasecat()
{}
}


******************************************************************************
this was just enough to get your hands in array for nay further information comment below.



                   

Read more ...

How to learn java easily?

Thursday, 18 April 2013
so this is the basic thought come in the mind of most people who are going to learn java or want to make this language as a profession.the first thing you need to understand that java is not a language which you can learn just by writing some codes given by your teacher or practicing some books example this is a language which need to be understand by heart ,to achieve greater goals you have to start early ,same is the case with java,to learn from heart this language,you have to start eagerly.
the basic steps involved in learning java are-
1.try to understand the concepts of oops before getting your hand in java.
2.you should have to know the basics like, methods,classes,identifiers,access identifiers,objects,inheritance,constructors,polymorphisms and many more basics.
3.you have to go through step by step in java by building some basic programs to larger applications.
4.java is a very simple language if you have a zeal in you to learn it,it just need an hour of daily practicing on the codes.
5.data structures are the basic building block of any language as the bricks for building so you have to understand the advantages of java over other languages,features of java,how can you go through step by step success in java.
6. core java,j2ee,j2me,androids there are lot to more to cover so start in a passionate way.
7.core java is the basic building of advanced java so get yourself dirty in this language in a very drastic way.
8.side by side learn the concepts of html,jsp,jstl which is very helpful in learning advanced java quickly.
9.if you are following the steps above get yourself ready to get a high check in the market.
10. start doing projects as soon as you learn some basic concepts related to it.

will give you further details related to java projects,programming skills,java examples,gui apps and more.

Read more ...

more examples on basics of java

Thursday, 18 April 2013
class Example
 {
 public static void main(String args[])
 {
 char ch; ch = 'X';
 System.out.println("ch contains " + ch);
 ch++; // increment ch
 System.out.println("ch is now " + ch);
 ch = 90; // give ch the value Z
System.out.println("ch is now " + ch);
 }
 }

Predicted Output: ch is now X ch is now Y ch is now Z
*********************************************************
control statements


class Example
 {
 public static void main(String args[])
 {
 boolean b; b = false;
System.out.println("b is " + b);
 b = true;
System.out.println("b is " + b); // a boolean value can control the if statement if(b)
 System.out.println("This is executed.");
 b = false;
if(b) System.out.println("This is not executed."); // outcome of a relational operator is a boolean value System.out.println("10 > 9 is " + (10 > 9));
}
}

 Predicted output: b is false b is true This is executed 10 > 9 is true
**********************************************************************
math class
class Example {
 public static void main(String args[])
{
double x, y, z; x = 3; y = 4;
z = Math.sqrt(x*x + y*y);
 System.out.println("Hypotenuse is " +z);
}
 }

 Predicted Output: Hypotenuse is 5.0
..............................................................................
this was just a basic for more programs get yourself stick to this blog sure you will be master in java.

Read more ...

Famous programs in java

Thursday, 18 April 2013

Fibonacci program

class fib
 {
 public static void main(String[] arg)
 {
  int i,a=0,b=0,c=1;
  int n=8;
   System.out.println("fibonacci series"+n);
  for(i=1;i<=n;i++)
  {
   a=b;
  b=c;
  c=a+b;
  System.out.println(c);
 
  }
  }
  }
********************************************************
        armstrong numbers

class fivearms
{
public static void main(String args[])
{
int r,temp,count;
for(count=1;count<500;count++)
{
int sum=0;
temp=count;
while(temp!=0)
{
r=temp%10;
temp=temp/10;
sum=sum+(r*r*r);
}
if(sum==count)
System.out.println("no.s are"+count);}
}
}

**********************************************************
prime number

class prime{
public static void main(String []args){
int num=6;
int fac=0;
for(int i=1;i<=6;i++)
{
 if(num%i==0)
   fac++;
}
if(fac==2)
System.out.println("Is a prime no");
else
 System.out.println("Not a prime no");
}
}

***************************************************
factorial

class factorial{
public static void main(String args[])
{
 int f=1,i;
 for(i=1;i<=5;i++)
 f=f*i;
 System.out.println("factorial="+f);
}
 }
*****************************************************
swap without using third variable

class swap
{
   public static void main(String []args)
   {
      int a=25;
      int b=10;
      System.out.println("Initially a="+a+"b="+b);
      a=a+b;
      b=a-b;
      a=a-b;
      System.out.println("a="+a+"\nb="+b);
   }
}
***********************************************





Read more ...

Logic table program in java

Thursday, 18 April 2013

Make a program which creates a sort of truth table to show the behaviour of all the logical operators mentioned. Hints:
 You need two Boolean type variables which you will initially set both to false
 Use character escape codes to tabulate the results
The following program can be used as a guide:
class LogicTable
 {
public static void main(String args[])
{
 boolean p, q;
 System.out.println("P\tQ\tPANDQ\tPORQ\tPXORQ\tNOTP");
 p = true;
 q = true;
 System.out.print(p + "\t" + q +"\t");
 System.out.print((p&q) + "\t" + (p|q) + "\t");
System.out.println((p^q) + "\t" + (!p));
 p = true; q = false;
 System.out.print(p + "\t" + q +"\t");
 System.out.print((p&q) + "\t" + (p|q) + "\t");
 System.out.println((p^q) + "\t" + (!p));
p = false; q = true;
System.out.print(p + "\t" + q +"\t");
 System.out.print((p&q) + "\t" + (p|q) + "\t");
 System.out.println((p^q) + "\t" + (!p));
 p = false; q = false;
 System.out.print(p + "\t" + q +"\t");
 System.out.print((p&q) + "\t" + (p|q) + "\t");
 System.out.println((p^q) + "\t" + (!p));
 }
 }
Read more ...

examples of basic program in java

Thursday, 18 April 2013
class Example
 {
public static void main(String args[])
{
int iresult, irem;
double dresult, drem;
iresult = 10 / 3;
 irem = 10 % 3;
 dresult = 10.0 / 3.0;
 drem = 10.0 % 3.0;
 System.out.println("Result and remainder of 10 / 3: " + iresult + " " + irem);
 System.out.println("Result and remainder of 10.0 / 3.0: " + dresult + " " + drem);
}
 }

 Predicted Output: Result and Remainder of 10/3: 3 1 Result and Remainder of 10.0/3.0: 3.3333333333333335 1

*********************************************************************************

class Example
{
public static void main(String args[])
 {
int n, d;
n = 10;
d = 2;
 if(d != 0 && (n % d) == 0)
System.out.println(d + " is a factor of " + n);
 d = 0; // now, set d to zero // Since d is zero, the second operand is not evaluated.
 if(d != 0 && (n % d) == 0)
System.out.println(d + " is a factor of " + n);
/* Now, try same thing without short-circuit operator. This will cause a divide-by-zero error. */
if(d != 0 & (n % d) == 0)
 System.out.println(d + " is a factor of " + n);
}
 }



Predicted Output: *Note if you try to execute the above program you will get an error (division by zero). To be able to execute it, first comment the last two statements, compile and then execute. 2 is a factor of 10
Trying to understand the above program is a bit difficult, however the program highlights the main difference in operation between a normal AND (&) and the short-circuit version (&&). In a normal AND operation, both sides of the expression are evaluated, e.g. if(d != 0 & (n % d) == 0) – this returns an error as first d is compared to 0 to check inequality and then the operation (n%d) is computed yielding an error! (divide by zero error) The short circuit version is smarter since if the left hand side of the expression is false, this mean that the output has to be false whatever there is on the right hand side of the expression, therefore: if(d != 0 && (n % d) == 0) – this does not return an error as the (n%d) is not computed since d is equal to 0, and so the operation (d!=0) returns false, causing the output to be false. Same applies for the short circuit version of


Read more ...

how to compile and run java programs

Thursday, 18 April 2013
you have to set path first---
for this
click on Mycomputer->properties->advanced->environmentvariables->pselect new paste the url of bin file C:\Program Files\Java\jdk1.7.0\bin in path and click okk.

then write first the program given below in a note pad

class Example
{
 public static void main(String args[])
 {
 int var1;  // this declares a variable
 int var2; // this declares another variable
var1 = 1024; // this assigns 1024 to var1
 System.out.println("var1 contains " + var1);
 var2 = var1 / 2;
 System.out.print("var2 contains var1 / 2: ");
 System.out.println(var2);
}
 }

then 1.compile by typing  "javac filename.java"  in command prompt
        2. run by typing "java filename" in command prompt
in the above example for a correct output you must put filename and class name to be same  for eg-the filename for the above program is Example.java


Predicted Output: var2 contains var1 / 2: 512

then


Read more ...

My first java program

Thursday, 18 April 2013
Welcome all the geeks and nerds who want to develop some huge applications,all the college students,school goings and those who want something interesting in life.
so here is your first program..............
don't be upset if you don't know data structure and c,java is a language which you can learn easily only by entertaining yourself and the world ,you have to know only the basics.


here is a program----------------
.
                                                           
Class Hello
    {
public static void main(String[] args)
   {
System.out.println("hello world");
     }
}

class name-first letter with capital. eg Dog
method name-first with small letter then capital letter. eg myName()
object-what you want.

public is used to tell the scope,static is used its value is same everywhere,void is return type,string array is used to pass the arguments,System is a class,out is a object,println is a method to print line by line.

its your first program so give sometime to understand it then go further to make programs of other types..










Read more ...

.

Comment here