Showing posts with label class. Show all posts
Showing posts with label class. Show all posts

Monday, March 10, 2014

Anonymous class in Java

//Anonymous class
class outer{
int x=80;
public void display(){
System.out.println("i am in outer class ");
}
}
class inner{
int y=60;

outer out=new outer(){
public void display(){
System.out.println("i am in inner class "+x);
System.out.println("i am in outer class "+y);
}
};
}
class inmain{
public static void main(String arg[]){
inner in=new inner();
in.out.display();

}
}

OUTPUT

Java program to copy one directory filres to another directory

import java.io.*;
class dirfile{

      public static void main(String arg[])throws IOException{
  BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
  System.out.println("entre the location ");
  String s1=br.readLine();
  System.out.println("entre the  new location ");
  String s2=br.readLine();
File f1=new File(s1);
   File f2=new File(s2);
   copy(f1,f2);

  }
public static void copy(File f1,File f2)throws IOException{
                if(f1.isDirectory()){
    if(!f2.exists()){
                       f2.mkdir();
}
                       String f[]=f1.list();
                            for(int k=0;k<f.length;k++){
             File f3=new File(f1,f[k]);
             File f4=new File(f2,f[k]);
             copy(f3,f4);
 }
 }
 else{
  FileInputStream fis=new FileInputStream(f1);
  FileOutputStream fos=new FileOutputStream(f2);
                  PrintStream fs=new PrintStream(fos);
int ch;
                byte []b=new byte[1024];
                while((ch=fis.read(b))!=-1){
                  fs.write(b,0,ch);
                   }
  }
}
}


OUTPUT



Sunday, March 2, 2014

Compare two strings in java

import java.util.Scanner;
class reg2{
public static void main(String []arg){
String a2 = null;
String b2 = null;

Scanner sr = new Scanner(System.in);
System.out.print("Enter first string :: ");
a2 = sr.nextLine();
System.out.print("Enter second string :: ");
b2 = sr.nextLine();

if(scheck(a2,b2)==1)
System.out.println("strings \""+a2+"\" and \""+b2+"\" were matched");
else System.out.println("sorry , strings \""+a2+"\" and \""+b2+"\" were unmatched");
}
static int scheck(String a,String b){
int a1 = a.length();
int b1 = b.length();
int i=0,j=0;

if(a1<b1){
String temp = a;
a = b ;
b = temp ;
int t = a1;
a1 = b1 ;
b1 = t ;
}

while(j!=b1){
while(i!=a1){
if(a.charAt(i)==b.charAt(j)){
i++;
j++;

if(j==b1){
return 1;
}
}
else {
i++;
j=0;
if(i==a1)
return 0;
}
}
j++;
}


return 0;
}
}




OUTPUT







Sunday, February 23, 2014

Word Guessing Game using Java

import java.io.*;
import java.util.*;
class file4{
static int kc = 0;
public static void main(String []arg) throws IOException{
FileReader fr = new FileReader("D:\\java\\f1.txt"); 

    //the file should contain words separated by spaces !
BufferedReader br2 = new BufferedReader(fr);
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
Random r = new Random();

String st = br2.readLine();    

//Works for single line.Use while loop till null if file contains multiple lines.
//System.out.println(st);


String t[] = st.split(" ");
String t1= t[r.nextInt(t.length)];
char ch[] = new char[t1.length()];
char ch3[] = new char[t1.length()];
for(int j=0;j<t1.length();j++){
ch3[j]=t1.charAt(j);
}
String t2;

int i,k;

for(i=0;i<t1.length();i++){
ch[i]='-';
}

// System.out.println("\nenter ur guess");
char ch2;
String x2;
printchar(ch,t1.length());
while(true){
int j;
System.out.println("\nenter ur guess\n");
x2=br.readLine();
System.out.println();

if(scheck(x2,t1)==1)
for(j=0;j<x2.length();j++){

ch2=x2.charAt(j);
//ch2=(char)br.read();
for(k=0;k<t1.length();k++){

if(ch3[k]==ch2){
ch[k]=ch2;
}


}

}
printchar(ch,t1.length());
check2(ch,t1.length());


}
}


static void check2(char c[],int k){

int i,k1=0;
for(i=0;i<k;i++){
if(c[i]=='-'){
k1++;
}
}
kc++;
if(k1==0){
System.out.println(" is correct \nCongrats ! Guess has been completed in attempt no "+kc+"!");
System.exit(0);
}
else System.out.println("\tattempts so far :: "+kc);
//return;
//System.exit(0);
}
static void printchar(char c[],int k){
for(int i=0;i<k;i++)
System.out.print(c[i]);

}
static int scheck(String a,String b){
int a1 = a.length();
int b1 = b.length();
int i=0,j=0;

if(a1<b1){
String temp = a;
a = b ;
b = temp ;
int t = a1;
a1 = b1 ;
b1 = t ;
}

while(j!=b1){
while(i!=a1){
if(a.charAt(i)==b.charAt(j)){
i++;
j++;

if(j==b1){
return 1;
}
}
else {
i++;
j=0;
if(i==a1)
return 0;
}
}
j++;
}


return 0;
}
}





Output




Wednesday, August 28, 2013

C++ program to count member function calls

/* C++ program to count member funtion calls */

#include<iostream>
using namespace std;

class A
 {
  int data;
  static int i,j;
  public:
  void input()
  {
  cout<<"enter num : ";
  cin>>data;
  i++;
  }
  void display()
  {
  cout<<"\nnum is "<<data<<endl;
  j++;
  }
  void count()
  {
  cout<<"\nInput calls = "<<i<<"\nDisplay calls = "<<j<<endl;
  }
 };
int A :: i=0;
int A :: j=0;

int main()
 {
  A A1;
  int c;
  do
  {
  cout<<"\n1.INPUT\n2.OUTPUT\n3.COUNT\n4.EXIT\nENTER CHOICE : ";
  cin>>c;
  switch(c)
  {
  case 1:
  A1.input();
  break;
case 2:
  A1.display();
  break;
case 3:
  A1.count();
  break;
default :
return 0;
  }
  }
  while(c);
  return 0;
 }






C++ program using constructor,destructor

/* C++ program using constructor,destructor */



#include<iostream>
using namespace std;
class t
{
  int hour;
  int min;
 public:
 // void input();
  t(int,int);
  t();
  void display(void)
  {
  cout<<"hour="<<hour<<"\n";
  cout<<"min="<<min<<"\n";
  }
  t(t &t11)
  {
  hour=t11.hour;
  min=t11.min;
  }
  ~t()
  {
   cout<<"\ndestroyed\n";
  }
};
t::t(int h,int m)
  {
   hour=h;
   min=m;
  }
 t::t()
    {
     hour=0;
     min=0;
    }

int main()
{
 t t1;
 cout<<"\nobject1 "<<"\n";
 t1.display();
 t1=t(2,45);
 cout<<"\nobject2"<<"\n";
 t1.display();
 t t2(t1);
 cout<<"\nobject3 "<<"\n";
 t2.display();
 return 0;
}

C++ program to swap numbers between two objects of different class

 /* c++ program to swap no between two objects of diff class */

#include<iostream>
using namespace std;
class A
{
public:

 int num1;

 void input()
 {
 cout<<"\nenter the number:";
 cin>>num1;
 }
 void display()
 {
 cout<<num1<<endl;
 }
friend class B;
};
class B
{
public:

 int num2;

 void input()
{
 cout<<"\nenter the second number:";
 cin>>num2;
}
void display()
{
cout<<num2<<endl;
}
friend class A;
};
void swapping(A &A1,B &B1)
{
int temp;
temp=A1.num1;
A1.num1=B1.num2;
B1.num2=temp;
}
int main()
{
A A11;
B B11;
A11.input();
B11.input();
cout<<"number before swapping:";
A11.display();
B11.display();
swapping(A11,B11);
cout<<"number after swapping:";
A11.display();
B11.display();
return 0;
}