Friday, April 12, 2019

this operator in java

code:


class cl {

public double a,b;

// A normal parametrized constructor
public cl(int a,int b) {
this.a = a;
this.b = b;
}

// copy constructor
cl(cl c) {
System.out.println("Copy constructor called");
a = c.a;
b = c.b;
}



void f()
{
    System.out.println(a+" "+b);
}
}

public class Main {

public static void main(String[] args) {
cl c1 = new cl(10, 15);

// Following involves a copy constructor call
cl c2 = new cl(c1);
c1.f();
c2.f();
System.out.println("xxxxxxxxxxxxx");
c1.a=5;
c1.f();
c2.f();

// Note that following doesn't involve a copy constructor call as
// non-primitive variables are just references.
cl c3 = c2;
System.out.println("xxxxxxxxxxxxx");
c3.f();
c2.f();
c1.f();
System.out.println("xxxxxxxxxxxxx");
c2.a=6;
c1.f();
c2.f();
c3.f();
System.out.println("xxxxxxxxxxxxx");
        c3.a=7;
c1.f();
c2.f();
c3.f();
System.out.println("xxxxxxxxxxxxx");
        c1.a=8;
c1.f();
c2.f();
c3.f();

}
}


output::
$javac Main.java
$java -Xmx128M -Xms16M Main
Copy constructor called
10.0 15.0
10.0 15.0
xxxxxxxxxxxxx
5.0 15.0
10.0 15.0
xxxxxxxxxxxxx
10.0 15.0
10.0 15.0
5.0 15.0
xxxxxxxxxxxxx
5.0 15.0
6.0 15.0
6.0 15.0
xxxxxxxxxxxxx
5.0 15.0
7.0 15.0
7.0 15.0
xxxxxxxxxxxxx
8.0 15.0
7.0 15.0
7.0 15.0

Friday, April 5, 2019

roots of equation in java

roots of equation                                    edited by diya


equals() in java

equals() in java                                                                            edited by diya


import java.util.Scanner;
class p19
{
public static void main(String args[])
{
Scanner n=new Scanner(System.in);

System.out.println("Enter a string");
String s1=n.nextLine();
System.out.println("Enter a string");
String s2=n.nextLine();
System.out.println(s1.equals(s2));
}

}


D:\>javac p19.java

D:\>java p19
Enter a string
diya
Enter a string
diya
true

D:\>java p19
Enter a string
dil
Enter a string
diya
false

D:\>

compareTo()

compareTo()                                                         edited by diya


import java.util.Scanner;
class p19
{
public static void main(String args[])
{
Scanner n=new Scanner(System.in);

System.out.println("Enter a string");
String s1=n.nextLine();
System.out.println("Enter a string");
String s2=n.nextLine();
int a=s1.compareTo(s2);
System.out.println("after comparesion");
System.out.println(a);

}

}



D:\>javac p19.java

D:\>java p19
Enter a string
diya
Enter a string
sayan
after comparesion
-15

D:\>

string concatenation

string concatenation                                                                                edited by diya


import java.util.Scanner;
class p19
{
public static void main(String args[])
{
Scanner n=new Scanner(System.in);

System.out.println("Enter a string");
String s1=n.nextLine();
System.out.println("Enter a string");
String s2=n.nextLine();
s1=s1.concat(s2);
System.out.println("after concatenation");
System.out.println(s1);

}

}


D:\>javac p19.java

D:\>java p19
Enter a string
dil
Enter a string
diya
after concatenation
dildiya

D:\>

vowel counting

vowel counting                                                                       edited by diya


import java.util.Scanner;
class p19
{
public static void main(String args[])
{
Scanner n=new Scanner(System.in);
int v=0;
System.out.println("enter a string");
String s=n.nextLine();
for(int i=0;i<s.length();i++)
{
char ch=s.charAt(i);
if(ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u')
{
v++;
}
}
System.out.println("total vowel="+v);

}

}




D:\>java p19
enter a string
kolkata
total vowel=3

D:\>

length of a string

//length of a string                                                                    edited by diya


class p19
{
public static void main(String args[])
{
String s="rittik";
System.out.println("length="+s.length());
for(int i=0;i<s.length();i++)
{
    char ch=s.charAt(i);
    System.out.println(ch);
}

}

}



D:\>java p19
length=6
r
i
t
t
i
k

D:\>

list of functions String class


 SL NO CLASS    NAME FUNCTION     NAME PROGRAM     NAME LINK
 1.  String     string initialization in java  clickhere
 2. String    string concate using + in java                                                     click here
 3. String   String concat(String str)   string concat using function   clickhere
 4. String    string compare using== in java   click here
 5. String   boolean equals(string object) string compare using equals() in java  click here 
 6. String   static String equalsIgnoreCase(String another)  string compare equalsignorecase()  in java  clickhere
 7. String   int compareTo(Object ob) compare string lexicographically  click here
 8. String   int length() string length() in java  click here
 9. String   char charAt(int index) string charAt() in java example  click here
 10. String    vowel count in java  click here
 11. String   String trim() string trim() in java  click here
 12. String  String replace(CharSequence old, CharSequence new) string replace() in java   click here
 13. String  static String join(delimiter,delimeter1,....)  string join() in java example click here
 14. String  boolean isEmpty() string isempty() in java clickhere
 15. String  boolean contains(CharSequence s) string contains() in java click here
 16. String   string pattern in java click here
 17. String  int indexOf(int ch)  string indexOf()  click here
 18. String  int indexOf(int ch, int fromIndex) string indexOf() click here
 19.  String  int indexOf(String substring) string indexOf() click here
 20.  String  int indexOf(String substring, int fromIndex) string indexOf() click here
 21.  String  int lastIndexOf(int ch) lastIndexOf() in java click here
 22.  String  int lastIndexOf(int ch, int fromIndex) lastIndexOf() in java click here
 23.  String  int lastIndexOf(String substring) lastIndexOf() in java click here
 24.  String  int lastIndexOf(String substring, int fromIndex) lastIndexOf() in java click here
 25.  String  String substring(int beginIndex) substring() in java  click here
 26.  String  String substring(int beginIndex, int endIndex) substring() in java  click here
 27.  String  String toUpperCase() String toLowerCase() toUpperCase(),toLowerCase() in java click here
 28.  String  string pallindrome checking in java click here


1. length of a string. -  link
2. count total vowels in a string. - link
3. concatenation of two strings. - link
4. compareTo () - link
5. equals() - link

Sunday, March 31, 2019

SUM AND AVERAGE OF N ELEMENTS IN DYNAMIC ARRAY IN CORE JAVA


Sum and average of n numbers using dynamically array                           EDITED BY SAYANI


Code:
import java.util.Scanner;
class arr
{
int a[],length;
void iplength(int x)
{
length=x;
}
void iparray()
{
a=new int [length];
Scanner j=new Scanner(System.in);
for(int i=0;i<length;i++)
{
a[i]=j.nextInt();
}
}
void op()
{
 int sum=0;
for(int i=0;i<length;i++)
{
sum=sum+a[i];
}
System.out.println("Sum="+sum);
double avg=(double)sum/(double)length;
System.out.println("Average="+ avg);
}
}
class arr1
                {
                                public static void main(String args[])
                                {
                                                arrob=new arr();
                                                ob.iplength(5);
                                                ob.iparray();
                                                ob.op();
                                }
                }

Output:E:\>javac arr1.java

E:\>java arr1
2
4
5
7
9
Sum=27
Average=5.4

E:\>


CONSTRUCTOR OVERLOADING


CONSTRUCTOR OVERLOADING WITHOUT PARAMETER, SINGLE PARAMETER, MULTIPLE PARAMETER

import java.util.Scanner;
class day
                {
                int a;
               
                day()
                {
                System.out.println("Sunday");
                }
                day(int x)
                {
                System.out.println("Monday");
                }
                day(int x,int y)
                {
                System.out.println("Friday");
                }
                }
class day1
                {
                public static void main(String args[])
                {
                day ob1=new day();
                day ob2=new day(5);
                day ob3=new day(7,9);
                }
                }

Output:
E:\>javac day1.java

E:\>java day1
Sunday
Monday
Friday

E:\>