CO2150704.
1
1 Write a program to demonstrate the use of constructor and method.
class Rectangle {
double l, w;
Rectangle() {
l = w = 0;
}
Rectangle(double l, double w) {
this.l = l;
this.w = w;
}
Rectangle(Rectangle A) {
l = A.l;
w = A.w;
}
Rectangle(double x) {
this(x, x);
}
void displayDim() {
[Link]("Length is : " + l);
[Link]("Length is : " + w);
[Link]();
}
}
class Constructer {
public static void main(String[] args) {
Rectangle a = new Rectangle();
[Link]("For a object : ");
[Link]();
Rectangle b = new Rectangle(3, 4);
[Link]("For b object : ");
[Link]();
Rectangle c = new Rectangle(b);
[Link]("For c object : ");
[Link]();
Rectangle d = new Rectangle(10.0);
[Link]("For d object : ");
[Link]();
}
}
OUTPUT:
For a object :
Length is : 0.0
Length is : 0.0
For b object :
Length is : 3.0
Length is : 4.0
For c object :
Length is : 3.0
Length is : 4.0
For d object :
Length is : 10.0
Length is : 10.0
CO2150704.1
1 Write a program to demonstrate method overloading and constructor overloading
class Rectangle {
double l,w;
Rectangle(){
l=w=0;
}
Rectangle(double x){
l=w=x;
}
void setDim(double x, double y){
l=x;
w=y;
}
void displayDim() {
[Link]("Length is : "+l);
[Link]("Width is : "+w);
}
}
class Overloding{
public static void main(String[] args) {
Rectangle a=new Rectangle();
[Link]("For a object:");
[Link]();
Rectangle b=new Rectangle(3);
[Link]("For b object:");
[Link]();
Rectangle c=new Rectangle();
[Link](1,3);
[Link]("For c object:");
[Link]();
Rectangle d=new Rectangle();
[Link](4,5);
[Link]("For d object:");
[Link]();
}
}
OUTPUT:
Length is : 0.0
Width is : 0.0
For b object:
Length is : 3.0
Width is : 3.0
For c object:
Length is : 1.0
Width is : 3.0
For d object:
Length is : 4.0
Width is : 5.0
CO2150704.1
3 Write a program to demonstrate method overriding.
class A{
int i,j;
A(int x,int y){
i=x;
j=y;
}
void show(){
[Link]("i and j are :"+i+" "+j);
}
}
class B extends A{
int k;
B(int x,int y,int z){
super(x,y);
k=z;
}
void show(){
[Link]("k is :"+k);
}
}
class Overriding{
public static void main(String []args){
B b=new B(1,2,3);
[Link]();
}
}
OUTPUT:
k is :3
CO2150704.1
4 Write a program to demonstrate destructor like functionality in Java.
class Student{
String name,enno;
double per;
Student(String name,String enno,double per){
[Link]=name;
[Link]=enno;
[Link]=per;
}
protected void finalize(){
[Link]="";
[Link]="";
[Link]=0;
}
public void show(){
[Link](name+" : "+per);
}
public static void main(String []args){
Student s=new Student("Aaditya","17090107056",88);
[Link]("Before finalized() called");
[Link]();
[Link]();
[Link]("After finalized() called");
[Link]();
}
}
OUTPUT:
Before finalize() calling
Aaditya:85.0
After finalize() called
:0.0
CO2150704.1
5 Write a program to demonstrate the following for String:
A. Input a string from standard input
import [Link];
import [Link].*;
class string{
public static void main(String []args){
Scanner sc=new Scanner([Link]);
[Link]("Enter String : ");
String s1=[Link]();
[Link]("Input from standerd input is :"+s1);
}
}
OUTPUT
Enter String :
Aaditya
Input from standerd input is :Aaditya
CO2150704.1
B. Input a string from command line argument
import [Link];
import [Link].*;
class string{
public static void main(String []args){
[Link]("Command line argument:");
for(int i=0;i<[Link];i++){
[Link](args[i]);
}
}
}
OUTPUT
$ java string Aaditya Tailor
Command line argument:
Aaditya
Tailor
CO2150704.1
C. find the length of it.
import [Link];
import [Link].*;
class string{
public static void main(String []args){
String s="Aaditya";
[Link]("Length of s is : "+[Link]());
}
}
OUTPUT
Length of s is : 7
CO2150704.1
D. Reverse a string
import [Link];
import [Link].*;
class string{
public static void main(String []args){
String s1="Aaditya";
StringBuffer s2=new StringBuffer(s1).reverse();
[Link]("Revers String is :"+s2);
}
}
OUTPUT
Revers String is :aytidaA
CO2150704.1
E. Copy a string to another string
import [Link];
import [Link].*;
class string{
public static void main(String []args){
String s="Aaditya";
String s1=s;
[Link]("copy String : "+s1);
}
}
OUTPUT
copy String : Aaditya
CO2150704.1
G. Extract some bytes from string
import [Link];
import [Link].*;
class string{
public static void main(String []args){
String s="Aaditya";
byte []arr=[Link]();
[Link]("Extrected byte:");
for(byte bt:arr){
[Link](bt);
}
}
}
OUTPUT
Extrected byte:
65
97
100
105
116
121
97
CO2150704.1
H Get Substring
import [Link];
import [Link].*;
class string{
public static void main(String []args){
String s="Aaditya";
String s1=[Link](2,7);
[Link]("Substring is : "+s1);
}
}
OUTPUT
Substring is : ditya
CO2150704.1
I. Check string starts and ends with particular string
import [Link];
import [Link].*;
class string{
public static void main(String []args){
String s6="India is the best country in the word";
[Link]("String is start with : "+[Link]("India"));
[Link]("String is end with : "+[Link]("word"));
}
}
OUTPUT
String is start with : true
String is end with : true
CO2150704.1
J. Convert any data type object/variable to string
import [Link];
import [Link].*;
class string{
public static void main(String []args){
int n=101;
String s=[Link](n);
[Link]("Converted version of int is"+s);
}
}
OUTPUT
Converted version of int is101
CO2150704.1
K. Split a string using regular expression
import [Link];
import [Link].*;
class string{
public static void main(String []args){
[Link]("Splited String is :\n");
String s="10101-10101-920219";
String part[]=[Link]("-");
for(int i=0;i<[Link];i++){
[Link]("part "+(i+1)+" "+part[i]);
}
}
}
OUTPUT
Splited String is :
part 1 10101
part 2 10101
part 3 920219
CO2150704.1
L. Replace string with other
import [Link];
import [Link].*;
class string{
public static void main(String []args){
String s1="Aaditya";
String s=[Link]("Aa","aa");
[Link]("New repplace string is :"+s);
}
}
OUTPUT
New repplace string is :aaditya
CO2150704.1
M. Find the indexes of a string in another string
import [Link];
import [Link].*;
class string{
public static void main(String []args){
String s="Hi Aaditya";
int index=[Link]("Aadity");
[Link]("Index of Aaditya is :"+index);
}
}
OUTPUT
Index of Aaditya is :3
CO2150704.1
N. Convert string to other types(byte and character array)
import [Link];
import [Link];
import [Link].*;
class string{
public static void main(String []args){
String s="Aaditya";
byte as[]=[Link]();
[Link]("Converted String is : "+[Link](as));
}
}
OUTPUT
Converted String is : [65, 97, 100, 105, 116, 121, 97]
CO2150704.1
O. Convert into uppercase and lowercase
import [Link];
import [Link];
import [Link].*;
class string{
public static void main(String []args){
String s="Aaditya";
String upper=[Link](),lower=[Link]();
[Link]("Lower case String is : "+upper);
[Link]("Upeer case String is : "+lower);
}
}
OUTPUT
Lower case String is : aaditya
Upeer case String is : AADITYA
CO2150704.1
P. Check the equality of two strings(with and without consideration of case)
import [Link];
import [Link];
import [Link].*;
class string{
public static void main(String []args){
String s="Aaditya";
String upper=[Link](),lower=[Link]();
[Link]("[Link](upper) : "+[Link](upper));
[Link]("[Link](upper) : "+[Link](upper));
}
}
OUTPUT
[Link](upper) : false
[Link](upper) : true
CO2150704.1
Q. Print the hashcode of string
import [Link];
import [Link];
import [Link].*;
class string{
public static void main(String []args){
String s="Aaditya";
[Link]("hashcode of s : "+[Link]());
}
}
OUTPUT
hashcode of s : 430820247
CO2150704.1
R. Print a string in canonical format
import [Link];
import [Link];
import [Link].*;
class string{
public static void main(String []args){
String s="Aaditya";
String s1="Aaditya";
String s2 = new String("Test");
final String s3 = [Link]();
[Link]("Canonical formate is "+s3);
[Link]("[Link](s1):"+[Link](s3));
}
}
OUTPUT
Canonical formate is Aaditya
[Link](s1):true
CO2150704.1
6. Print the execution time of a function in millisecond, microsecond and nanosecond.
import [Link];
class Timeutil{
public static void main(String []args){
[Link]("it is for time execution time ");
long st=[Link]();
for(int i=0;i<100000;i++){
}
long et=[Link]();
[Link]("Time for execution is : "+(et-st)+" nanoseconds");
[Link]("Time for execution is : "+(et-st)/1000+" microsecond");
[Link]("Time for execution is : "+(et-st)/1000000+" miliseconds");
}
}
OUTPUT
it is for time execution time
Time for execution is : 2087188 nanoseconds
Time for execution is : 2087 microsecond
Time for execution is : 2 miliseconds
CO2150704.2
8. Explain single and multilevel inheritance and demonstrate the same using program.
class A{
int i;
void getI(int x){
i=x;
}
void showi(){
[Link]("i is "+i);
}
}
class B extends A{
int k,j;
void getjk(int x,int y){
j=x;
k=y;
}
void showjk(){
[Link]("j is "+j);
[Link]("k is "+k);
}
}
class C extends B{
int l;
void getl(int x){
l=x;
}
void showijkl(){
[Link]("i is "+i);
[Link]("j is "+j);
[Link]("k is "+k);
[Link]("l is "+l);
}
}
class Multilevel{
public static void main(String []args){
B b=new B();
[Link](1);
[Link]();
[Link](1,3);
[Link]();
C c=new C();
[Link](2);
[Link]();
}
}
OUTPUT
i is 1
j is 1
k is 3
i is 0
j is 0
k is 0
l is 2
CO2150704.2
9. Demonstrate the use of “interface” using a program.
interface MyInterface{
public void method1();
public void method2();
}
class Demo implements MyInterface
{
public void method1(){
[Link]("implementation of method1");
}
public void method2(){
[Link]("implementation of method2");
}
public static void main(String arg[]){
MyInterface obj = new Demo();
obj.method1();
}
}
OUTPUT
implementation of method1
CO2150704.2
10. Write a program to demonstrate multiple inheritances.
interface if1{
void getNo();
}
interface if2{
void printNo();
}
class Multiple implements if1,if2{
//int n;
public void getNo(){[Link]("Get Number");}
public void printNo()
{[Link]("Print Number");}
}
class Mple{
public static void main(String []args){
Multiple M=new Multiple();
[Link]();
[Link]();
}
}
OUTPUT
Get Number
Print Number
CO2150704.2
11. Implement multipath and hybrid inheritance in java.
class A{
void display(){
[Link]("A");
}
}
class B extends A{
void display(){
[Link]("B");
}
}
class C extends A{
void display(){
[Link]("C");
}
}
class D extends A{
void display(){
[Link]("D");
}
}
class Multipath{
public static void main(String []args){
D d=new D();
[Link]();
[Link]("");
}
}
OUTPUT
D
CO2150704.2
12. Demonstrate abstract class using the program.
abstract class abs{
String str;
int rn;
abstract void setName(String s);
abstract void setInt(int n);
}
class stu extends abs{
void setName(String s){
str=s;
}
void setInt(int n){
rn=n;
}
void display(){
[Link]("Name is : "+str);
[Link]("Roll no is : "+rn);
}
}
class AbsDemo{
public static void main(String []args){
stu s=new stu();
[Link]("Aaditya");
[Link](56);
[Link]();
}
}
OUTPUT
Name is : Aaditya
Roll no is : 56