1. Write a program to perform various String Operations.
1) By Using equals() Method
The String class equals() method compares the original content of the string. It
compares values of string for equality. String class provides the following two methods:
class Teststringcomparison1
public static void main(String args[])
String s1="Sachin";
String s2="Sachin";
String s3=new String("Sachin");
String s4="Saurav";
[Link]([Link](s2));//true
[Link]([Link](s3));//true
[Link]([Link](s4));//false
2) By Using == operator
The == operator compares references not values.
class Teststringcomparison3
public static void main(String args[])
String s1="Sachin";
String s2="Sachin";
String s3=new String("Sachin");
[Link](s1==s2);//true (because both refer to same instance)
[Link](s1==s3);//false(because s3 refers to instance created in non
pool)
3) By Using compareTo() method
The String class compareTo() method compares values lexicographically and returns an
integer value that describes if first string is less than, equal to or greater than second
string.
Suppose s1 and s2 are two String objects. If:
s1 == s2 : The method returns 0.
s1 > s2 : The method returns a positive value.
s1 < s2 : The method returns a negative value.
class Teststringcomparison4
public static void main(String args[])
String s1="Sachin";
String s2="Sachin";
String s3="Ratan";
[Link]([Link](s2));//0
[Link]([Link](s3));//1(because s1>s3)
[Link]([Link](s1));//-1(because s3 < s1 )
}
}
String Concatenation Methods in Java
In Java, String concatenation forms a new String that is the combination of
multiple strings. There are two ways to concatenate strings in Java:
By + (String concatenation) operator
By concat() method
1) String Concatenation by + (String concatenation)
operator
Java String concatenation operator (+) is used to add strings. For Example:
class TestStringConcatenation1
public static void main(String args[])
String s="Sachin"+" Tendulkar";
[Link](s);//Sachin Tendulkar
2) String Concatenation by concat() method
The String concat() method concatenates the specified string to the end of current
string. Syntax:
class TestStringConcatenation3
public static void main(String args[])
String s1="Sachin ";
String s2="Tendulkar";
String s3=[Link](s2);
[Link](s3);//Sachin Tendulkar
2. Write a program on class and object in java.
class Student{
int rollno;
String name;
void insertRecord(int r, String n){
rollno=r;
name=n;
}
void displayInformation(){[Link](rollno+" "+name);}
}
class TestStudent4{
public static void main(String args[]){
Student s1=new Student();
Student s2=new Student();
[Link](111,"Karan");
[Link](222,"Aryan");
[Link]();
[Link]();
}
}
3. Write a program on default and parameterized constructor.
Default constructor
//Java Program to create and call a default constructor
class Bike
{
//creating a default constructor
Bike1()
{
[Link]("Bike is created");
}
//main method
public static void main(String args[])
{
//calling a default constructor
Bike b=new Bike();
}
}
Parameterized constructor
//Java Program to demonstrate the use of the parameterized constructor.
class Student
{
int regd_id;
String name;
//creating a parameterized constructor
Student(int i, String n)
{
regd_id = i;
name = n;
}
//method to display the values
void display()
{
[Link](id+" "+name);
}
public static void main(String args[])
{
//creating objects and passing values
Student s1 = new Student(111,"Rasmi");
Student s2 = new Student4(222,"Ranjan");
//calling method to display the values of object
[Link]();
[Link]();
}
}
4. Write a program to illustrate Function Overloading &
Function Overriding methods in Java.
Function Overloading
class Adder{
static int add(int a,int b){return a+b;}
static int add(int a,int b,int c){return a+b+c;}
}
class TestOverloading1{
public static void main(String[] args){
[Link]([Link](11,11));
[Link]([Link](11,11,11));
}}
Function Overriding
//Java Program to demonstrate why we need method overriding
//Here, we are calling the method of parent class with child
//class object.
//Creating a parent class
class Vehicle{
void run(){[Link]("Vehicle is running");}
}
//Creating a child class
class Bike extends Vehicle{
public static void main(String args[]){
//creating an instance of child class
Bike obj = new Bike();
//calling the method with child class instance
[Link]();
}
}
5. Write a program to illustrate the implementation of abstract
class and abstract methods.
abstract class Bike
{
abstract void run();
}
class Honda4 extends Bike
{
void run()
{
[Link]("running safely");
}
public static void main(String args[])
{
Bike obj = new Honda4();
[Link]();
}
}
6. Write a program to implement Exception handling.
public class JavaExceptionExample
{
public static void main(String args[])
{
try
{
//code that may raise exception
int data=100/0;
}
catch(ArithmeticException e)
{
[Link](e);
}
finally
{ //rest code of the program
[Link]("This line will execute always");
}
}
}
7. Write a program to create and access packages in Java.
Creating a package
These are the packages that are defined by the user. First we create a
directory myPackage (name should be same as the name of the
package). Then create the MyClass inside the directory with the first
statement being the package names.
// Name of the package must be same as the directory
// under which this file is saved
package myPackage;
public class MyClass
{
public void getNames(String s)
{
[Link](s);
}
}
Accessing and using the package:
Now we can use the MyClass class in our program.
/* import 'MyClass' class from 'names' myPackage */
import [Link];
public class PrintName
{
public static void main(String args[])
{
// Initializing the String variable with a value
String name = "MPWT";
// Creating an instance of class MyClass in the package.
MyClass obj = new MyClass();
[Link](name);
}
}
8. Write a program on interface in java.
interface MPCS
{
void print();
}
interface MCCS
{
void show();
}
class Groups implements MPCS,MCCS
{
public void print()
{
[Link]("Hello MPCS");
}
public void show()
{
[Link]("Hello MCCS");
}
public static void main(String args[])
{
Groups obj = new Groups();
[Link]();
[Link]();
}
}
9. Write a program to Create Multiple Threads in Java.
// Java program to create multiple threads
class MyThread implements Runnable {
public void run() {
int i = 0;
for (i = 1; i <= 3; i++)
[Link]("Thread " + [Link]().getId() + " is running");
}
}
public class Main {
public static void main(String[] args) {
Thread t1 = new Thread(new MyThread());
Thread t2 = new Thread(new MyThread());
Thread t3 = new Thread(new MyThread());
[Link]();
[Link]();
[Link]();
}
}
10. Write a program to which illustrates the implementation different stream classes.
Java FileOutputStream Class Java FileInputStream Class
import [Link]; import [Link];
public class FileOutputStreamExample public class DataStreamExample
{ {
public static void main(String args[]) public static void main(String args[])
{ {
try try
{ {
FileOutputStream fout; FileInputStream fin;
fout=new FileOutputStream("D:\\[Link]"); fin=new FileInputStream("D:\\[Link]");
[Link](65); int i=[Link]();
[Link](); [Link]((char)i);
[Link]("success..."); [Link]();
} }
catch(Exception e) catch(Exception e)
{ {
[Link](e); [Link](e);
} }
} }
} }
11. Write a program which illustrates the implementation of
multiple Inheritance using interfaces in Java
interface MPCS
{
void print();
}
interface MCCS
{
void show();
}
class Groups implements MPCS,MCCS
{
public void print()
{
[Link]("Hello MPCS");
}
public void show()
{
[Link]("Hello MCCS");
}
public static void main(String args[])
{
Groups obj = new Groups();
[Link]();
[Link]();
}
}