>

Sunday, December 16, 2012

File Creation in java

import java.io.File;
import java.io.IOException;

public class FileCreatin {

    public static void main(String args[]) throws IOException
    {
        File f=new File("F:/java.txt");
        if(!f.exists())
        {
            f.createNewFile();
            System.out.println("file created successfully");
        }
        else
            System.out.println("File already exists");
       
    }
}

Simple Java programs

1.
Question :
Given two non-negative int values, return true if they have the same last digit, such as with 27 and 57. Note that the % "mod" operator computes remainders, so 17 % 10 is 7.

Ans:
public boolean lastDigit(int a, int b) {
          int r=a%10;
        int r1=b%10;
        if(r==r1)
        {
            System.out.println(true);
        }  // These programs for educational purpose only. Dont copy and publish in your blog or website..
        else
            System.out.println(false);
}

2.

Question :Given 2 int values, return true if one is negative and one is positive. Except if the parameter "negative" is true, then return true only if both are negative.

Ans:
public boolean posNeg(int a, int b, boolean negative) {
  if((a<0&b>0&negative==false)|(a>0&b<0&negative==false))
        {
            System.out.println(true);
        }
        else if(a<0&b<0&negative==true)
            System.out.println(true); 
        else
            System.out.println(false);
} // These programs for educational purpose only. Dont copy and publish in your blog or website..


3.
 Question :
Given two temperatures, return true if one is less than 0 and the other is greater than 100.

icyHot(120, -1) → true
icyHot(-1, 120) → true
icyHot(2, 120) → false

Answer:
public boolean icyHot(int temp1, int temp2) {
  if((temp1<0&temp2>100)|(temp1>100&temp2<0))
        {
            System.out.println(true);
        }
        else
            System.out.println(false);
} // These programs for educational purpose only. Dont copy and publish in your blog or website..

OCJP Constructor and method overloading....

1.An java program with  Constructor and method overloading

public class Workout {

    public void workout(){
        System.out.println("Constructor in Workout");
    }
    public Workout(){
        Workout.main("hi");
        System.out.println("Over loaded constructor in Workout");
    }    public static void main(String string) {

System.out.println(string);
new Workout();
       
    }
    public static void main(Integer args[])
    {
        Workout.main("hi");
    }
public static void main(String args[])
{
    new Workout();//An program with constructor and method overloading......by k.saravanan
// program for educational purposes only.. Dont copy and post in your blog or ur website ....
}
}


What is output?

Prints hi continuously and stack overflow error will occur.....

SCJP Questions with Answers Part -1

1. String compare using == operator


public void stringConvert(){
    if("java".toUpperCase()=="java".toUpperCase())
    {
        System.out.println(true);
    }
    else
        System.out.println(false);
}


2. String compare using equals :

public void stringEquals(){
    if("java".toLowerCase().equals("JAVA".toLowerCase()))
    {
        System.out.println("true");
    }
    else
        System.out.println(false);
           
}

3.


public void stringSubstring(){
    String str="java programming";
    str.substring(0,4);
    str.replace('a', 'j');
    String stra="welcome to "+str;
    System.out.println(stra);
   
}

4.
public void zero(){
        if(-00.0==+0.000)
        {
            System.out.println(true);
        }
        else
            System.out.println(false);



ANSWERS:

1.false -> == operator check objects not contents...
2.true  -> equals modifier checks only content...
3.welcome to java programming-> value of string replaced but not stored in anywhere.... So sting original contents not replaced...
4.true...-> there is nothing called negative and positive zero in java.. all zeros are treated equally..

Java program using If-Else Condition



WAP :Given 2 int values, return true if one is negative and one is positive. Except if the parameter "negative" is true, then return true only if both are negative. 


public class NegativePositive {

    public static void posNeg(int a,int b, boolean neg)
    {
        if((a<0&b>0&neg==false)|(a>0&b<0&neg==false))
        {

            System.out.println(true);

        }
        else if(a<0&b<0&neg==true)
            System.out.println(true);

        else
            System.out.println(false);

    }    public static void main(String[] args) {
     
            posNeg(-5,-1,true);
    }

}

Travelling Guide -ROUTES


ROUTES-AN NGO
Every day numerous people travelling towards Chennai. But most of them were not aware of Chennai Bus routes. For those who new to Chennai or People do not have idea about Chennai bus routes can utilize this facility provided by an NGO called ROUTES.
Just call this number from Chennai. You will get help about all bus routes in Chennai. This service is provided at free of cost...
8695959595

...

Also do not forgot to post your comments...

Java String Program

Given a string, take the first 2 chars and return the string with the 2 
chars added at both the front and back, so "kitten" yields"kikittenki". 
If the string length is less than 2, use whatever chars are there.

My Own program follows:

public class FrontTwo {


    public static void frontT(String str)
    {
        if(str.length()>=2){
          
          
            String temp=str.substring(0, 2);
            System.out.println(temp+""+str+""+temp);
            }
            else if(str.length()<2)
            {
                String temp=str.substring(0);
                System.out.println(temp+""+str+""+temp);
            }
            else
            {
                System.out.println(str);
            }
      
    }

         public static void main(String[] args) {

        frontT("hai");

    }

}