Friday 27 February 2015

hhggthy

pulkit: Cupid Love's Test Result: t and g Got 10 Do you have any selfestime? Then stop thinking about it.

Sunday 20 April 2014

Replacing a text from file

import java.io.*;
import java.util.*;

public class ReplaceText
{
    public static void main(String args[]) throws Exception
    {
        if(args.length != 4)
        {
            System.out.println("Usage: java ReplaceTest sourceFile targetFile oldStr newStr");
            System.exit(0);
        }
        File sourceFile = new File(args[0]);
        if(!sourceFile.exists())
        {
            System.out.println("Source file "+args[0]+" doesn't exist !");
            System.exit(0);
        }
        File targetFile = new File(args[0]);
        if(!targetFile.exists())
        {
            System.out.println("Target file "+args[1]+" doesn't exist !");
            System.exit(0);
        }
       
        Scanner input = new Scanner(sourceFile);
        PrintWriter output = new PrintWriter(targetFile);
       
        while(input.hasNext())
        {
            String s1 = input.nextLine();
            String s2 = s1.replaceAll(args[2],args[3]);
            output.println(s2);
        }
        input.close();
        output.close();
    }

}

Thursday 17 April 2014

Pascal Triangle in Java !

Pascal Triangle in Java code:


import java.util.Scanner;

class Pascal
{
public static void main(String arg[])
{
Scanner sc=new Scanner(System.in);             //making object of Scanner for input
int n;
System.out.println("Enter the no of rows to print : ");
n=sc.nextInt();                                                     // taking integer input
int p[][];                                                               // declaring 2-d Array
p= new int[n][];                                                   // specifying size
for(int i=0;i<n;i++)
p[i] = new int[i+1];                                                // allocating memory
for(int i=0;i<n;i++)
{
p[i][0]=p[i][i]=1;                                                // The edges are set by 1
for(int j=1;j<i;j++)
p[i][j]= p[i-1][j-1]+ p[i-1][j];                             // logic of pascal triangle
}
System.out.println("\n\n");
for(int row[] : p)                                                // for each row
{
for(int trm : row)                                              // & inside for each column
System.out.printf("%5d",trm);                           // print result
System.out.println ();
}
}
}

Difference between int []arr; v/s int arr[];

Difference between int []arr; v/s int arr[];

 

Suppose take an example :

int []a,b;    //   Both ‘a’ and ‘b’ are array references ! 
                                     v/s
int a[],b;   //  In this ‘a’ is an array reference while ‘b’ is a value type variable !

Therefore,
int []arr;   is array reference !
while
int arr[];  is a value type variable !

I hope u guys got the difference !!!

 


The java.lang Package

The String Class

In java String is implemented through String class.
Remember that any keyword starting with a capital letter is a class.
The String class is defined in the package ‘java.lang’, which is automatically imported in all java programs.
The ‘String’ class is declared as final, hence cannot be sub-classed.
The ‘String’ object represents a fixed length immutable character sequence.


String Constructors

  • String()
  • String(char arr[])
  • String(char arr[],int startIndex,int numChars)
  • String(String s)
  • String(byte arr[])
  • String(byte arr[], int startIndex, int numChars)

String s;   // way to make reference of String
 

1. String()

s = new String(); // creates an empty String


2.String(char str[])
 
char str[] = {'t','e','n','d','u','l','k','a','r'};
s = new String(str);   //s is referring to "tendulkar"


3. String(char arr[],int startIndex,int numChars)


s = new String (str, 2 , 3 );          // s is now referencing to "dul"

4. String(String s)            // - - - > copy constructor

s = new String("Vivek");               // invokes copy constructor, hence creates a new object

                                                v/s

s = "Vivek";          // does not invokes constructor. The String is created in String pool & its reference is assigned to s;

5. String(byte arr[])

byte arr[] = {118,105,118,101,107};
 
s = new String(arr);                       // s is referred to "vivek" 

5. String(byte arr[], int startIndex, int numChars)

s= new String(arr, 1 , 3 );      //  s refers to "vek"

String representation of Undefined (class) types :

The toString() method

It is a good programming practice to provide the toString method for the class so that whenever the String representation of the object of the class is required, Java automatically looks foe the toString methods inside the class & invokes it.
The toString method specifies the conversion policy for the information of the class into String.
Take a look at this example :

//program without toString() class Record
{
private String name;
private int age;
private float basic;
Record(){}
Record(String name,int age , double basic)
{
this.name = name;
this.age = age;
this.basic = (float)basic;
}
void print()
{
System.out.printf(“\n %-15s %2d %8.2f”,name,age,basic);
}
}
class Main
{
public static void main(String arg[])
{
Record r1,r2;
r1=new Record(“vivek warde”,20,3800.75);
r2=new Record(“kapil”,21,3535.55);
r1.print();
r2.print();
System.out.println(“\n”);
System.out.println(“\n r1 : “+r1);
System.out.println(“\n r2 : “+r2);
}
}

///////////////////////////// v/s ////////////////////////
 
//program with toString()

class Record
{
private String name;
private int age;
private float basic;
Record(){}
Record(String name,int age , double basic)
{
this.name = name;
this.age = age;
this.basic = (float)basic;
}
void print()
{
System.out.printf(“\n %-15s %2d %8.2f”,name,age,basic);
}
public String toString()
{
//      return name+” “+age+” “+basic;
String s = String.format(“%-15s %2d %8.2f”,name,age,basic);
return s;
}
}
class Main
{
public static void main(String arg[])
{
Record r1,r2;
r1=new Record(“vivek warde”,20,3800.75);
r2=new Record(“kapil”,21,3535.55);
r1.print();
r2.print();
System.out.println(“\n”);
System.out.println(“\n r1 : “+r1);
System.out.println(“\n r2 : “+r2);
}
}
//Just copy paste the code in ur compiler , & u can see the outputs

History of Java

Java was conceived in 1991 at Sun Micro-systems by:
James Gosling
Patrick Naughton
Ed Frank
Chris Warth
Mike Sheridan

Java was initially called as OAK, it was renamed Java in 1995. Much of the characteristics of Java is inherited from C and C++. Java derives its syntax from C & Object Oriented features from C++.
Originally, the aim of developing Java was to develop a platform independent language that could be used to create softwares to be embedded in various consumer electronic devices. At about the same tme world-wide-web (internet) came into existence & was facing the problem of portability. This realization caused focus on Java to switch from consumer electronics to internet programming. Thus environmental change that prompted Java was the read from platform independent programs destined for distribution on the Internet.