Monday, May 4, 2015

JAVA - Override

JAVA - Override

 

public class main extends A{
public static void main(String[] args) {
main obj = new main();
}    
public main()
{
    printinfo();
}
    @Override
        public void printinfo() {
            super.printinfo();
            System.out.println("HI");
        }
}


 
public class A {
    
        int w=10;
        public void printinfo()
        {
            System.out.println("super class  "+w);
        }
}

JAVA–Exception

JAVA–Exception

 

 

import java.util.Scanner;
 
public class a {
 
    public static void main(String[] args) {
        int x=1;
        Scanner a = new Scanner(System.in);
        do{
        try{
        System.out.println("enter two number : ");
        int n= a.nextInt();
        int m= a.nextInt();
        int sum = n/m;
        System.out.println(sum);
        
    }
        catch(Exception e)
        {
            System.out.println("you can do that fix that");
        }
        }while(x==1);
 
}}

File–Writing

File–Writing

 

 

main

 
public class writing2 {
    public static void main(String[] args) {
        writing obj=new writing();
        obj.openFile();
        obj.add();
        obj.close();
    }
}


import java.io.File;
import java.util.Formatter;
 
public class writing {
        private Formatter x;
        public void openFile()
        {
            try {
                x =new  Formatter("shadhi.txt");
            }
            catch(Exception e){
                System.out.println("you have an error");
            }
        }
        
        public void add()
        {
            x.format("%s%s%s","20"," new ","number");
        }
        public void close()
        {
            x.close();
        }
 
}

File–Reading

 

File–Reading

 
public class reading2 {
    public static void main(String[] args) {
        Reading obj = new Reading();
        obj.openfile();
        obj.readfile();
        obj.closefile();
        
        
    }
}

 



import java.io.File;
import java.util.Scanner;
 
 
public class Reading {
 
private Scanner x;
    public void openfile()
    {
        try {
            x= new Scanner(new File("Catch.txt"));
        }
    catch(Exception e)
        {
        System.out.println("you got an error");
        }
    }
    public void readfile()
    {
        while (x.hasNext())
        {
            String a = x.next();
            String b = x.next();
            String c = x.next();
            
            System.out.println(a + b + c);
        }
    }
    public void closefile()
    {
        x.close();
    }
    }
 

JAVA–FILE exists or Not !

JAVA–FILE exists or Not !

 

 

 

//main method

import java.io.File;
public class A {
public static void main(String[] args) {
File xFile = new File("D:\\ONLINE\\Dropbox\\CSE - WORLD\\Java File\\tx.txt");
if(xFile.exists())
{
System.out.println(xFile.getName()+" exist");
}
else {
System.out.println("no");
}
}
}


//AnotherClass


import java.util.Formatter;

public class B {

public static void main(String[] args) {
final Formatter x;
try {
x = new Formatter("shadh.txt");
System.out.println("you create a file");
}
catch (Exception e)
{
System.out.println("you got an error");
}
}

}