0% found this document useful (0 votes)
50 views

Write A Program To Demonstrate The Use of Constructor and Method

The document demonstrates various methods to work with strings in Java, including: 1) Taking input from the standard input/command line, finding length, reversing, copying strings 2) Extracting bytes, getting substrings, checking starts/ends with 3) Converting to/from other types like int, splitting using regex, replacing, finding indexes

Uploaded by

Aaditya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views

Write A Program To Demonstrate The Use of Constructor and Method

The document demonstrates various methods to work with strings in Java, including: 1) Taking input from the standard input/command line, finding length, reversing, copying strings 2) Extracting bytes, getting substrings, checking starts/ends with 3) Converting to/from other types like int, splitting using regex, replacing, finding indexes

Uploaded by

Aaditya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

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() {
System.out.println("Length is : " + l);
System.out.println("Length is : " + w);
System.out.println();
}
}

class Constructer {
public static void main(String[] args) {
Rectangle a = new Rectangle();
System.out.println("For a object : ");
a.displayDim();
Rectangle b = new Rectangle(3, 4);
System.out.println("For b object : ");
b.displayDim();
Rectangle c = new Rectangle(b);
System.out.println("For c object : ");
c.displayDim();
Rectangle d = new Rectangle(10.0);
System.out.println("For d object : ");
d.displayDim();
}
}
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() {
System.out.println("Length is : "+l);
System.out.println("Width is : "+w);
}
}
class Overloding{
public static void main(String[] args) {
Rectangle a=new Rectangle();
System.out.println("For a object:");
a.displayDim();

Rectangle b=new Rectangle(3);


System.out.println("For b object:");
b.displayDim();

Rectangle c=new Rectangle();


c.setDim(1,3);
System.out.println("For c object:");
c.displayDim();

Rectangle d=new Rectangle();


d.setDim(4,5);
System.out.println("For d object:");
d.displayDim();
}
}
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(){
System.out.println("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(){
System.out.println("k is :"+k);
}
}
class Overriding{
public static void main(String []args){
B b=new B(1,2,3);
b.show();
}
}

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){
this.name=name;
this.enno=enno;
this.per=per;
}
protected void finalize(){
this.name="";
this.enno="";
this.per=0;
}
public void show(){
System.out.println(name+" : "+per);
}
public static void main(String []args){
Student s=new Student("Aaditya","17090107056",88);
System.out.println("Before finalized() called");
s.show();
s.finalize();
System.out.println("After finalized() called");
s.show();
}
}

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 java.util.Scanner;
import java.io.*;
class string{
public static void main(String []args){
Scanner sc=new Scanner(System.in);
System.out.println("Enter String : ");
String s1=sc.nextLine();
System.out.println("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 java.util.Scanner;
import java.io.*;
class string{
public static void main(String []args){
System.out.println("Command line argument:");
for(int i=0;i<args.length;i++){
System.out.println(args[i]);
}
}
}

OUTPUT

$ java string Aaditya Tailor


Command line argument:
Aaditya
Tailor
CO2150704.1

C. find the length of it.

import java.util.Scanner;
import java.io.*;
class string{
public static void main(String []args){
String s="Aaditya";
System.out.println("Length of s is : "+s.length());
}
}

OUTPUT

Length of s is : 7

CO2150704.1

D. Reverse a string

import java.util.Scanner;
import java.io.*;
class string{
public static void main(String []args){
String s1="Aaditya";
StringBuffer s2=new StringBuffer(s1).reverse();
System.out.println("Revers String is :"+s2);
}
}

OUTPUT

Revers String is :aytidaA


CO2150704.1

E. Copy a string to another string

import java.util.Scanner;
import java.io.*;
class string{
public static void main(String []args){
String s="Aaditya";
String s1=s;
System.out.println("copy String : "+s1);
}
}

OUTPUT

copy String : Aaditya

CO2150704.1

G. Extract some bytes from string

import java.util.Scanner;
import java.io.*;
class string{
public static void main(String []args){
String s="Aaditya";
byte []arr=s.getBytes();
System.out.println("Extrected byte:");
for(byte bt:arr){
System.out.println(bt);
}
}
}

OUTPUT
Extrected byte:
65
97
100
105
116
121
97
CO2150704.1

H Get Substring

import java.util.Scanner;
import java.io.*;
class string{
public static void main(String []args){
String s="Aaditya";
String s1=s.substring(2,7);
System.out.println("Substring is : "+s1);
}
}

OUTPUT

Substring is : ditya

CO2150704.1

I. Check string starts and ends with particular string

import java.util.Scanner;
import java.io.*;
class string{
public static void main(String []args){
String s6="India is the best country in the word";
System.out.println("String is start with : "+s6.startsWith("India"));
System.out.println("String is end with : "+s6.endsWith("word"));
}
}

OUTPUT
String is start with : true
String is end with : true
CO2150704.1

J. Convert any data type object/variable to string

import java.util.Scanner;
import java.io.*;
class string{
public static void main(String []args){
int n=101;
String s=String.valueOf(n);
System.out.println("Converted version of int is"+s);
}
}

OUTPUT

Converted version of int is101

CO2150704.1

K. Split a string using regular expression

import java.util.Scanner;
import java.io.*;
class string{
public static void main(String []args){
System.out.println("Splited String is :\n");
String s="10101-10101-920219";
String part[]=s.split("-");
for(int i=0;i<part.length;i++){
System.out.println("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 java.util.Scanner;
import java.io.*;
class string{
public static void main(String []args){
String s1="Aaditya";
String s=s1.replaceAll("Aa","aa");
System.out.println("New repplace string is :"+s);
}
}

OUTPUT

New repplace string is :aaditya

CO2150704.1

M. Find the indexes of a string in another string

import java.util.Scanner;
import java.io.*;
class string{
public static void main(String []args){
String s="Hi Aaditya";
int index=s.indexOf("Aadity");
System.out.println("Index of Aaditya is :"+index);
}
}

OUTPUT

Index of Aaditya is :3
CO2150704.1

N. Convert string to other types(byte and character array)

import java.util.Scanner;
import java.util.Arrays;
import java.io.*;
class string{
public static void main(String []args){
String s="Aaditya";
byte as[]=s.getBytes();
System.out.println("Converted String is : "+Arrays.toString(as));
}
}

OUTPUT
Converted String is : [65, 97, 100, 105, 116, 121, 97]

CO2150704.1

O. Convert into uppercase and lowercase

import java.util.Scanner;
import java.util.Arrays;
import java.io.*;
class string{
public static void main(String []args){
String s="Aaditya";
String upper=s.toLowerCase(),lower=s.toUpperCase();
System.out.println("Lower case String is : "+upper);
System.out.println("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 java.util.Scanner;
import java.util.Arrays;
import java.io.*;
class string{
public static void main(String []args){
String s="Aaditya";
String upper=s.toLowerCase(),lower=s.toUpperCase();
System.out.println("upper.equals(upper) : "+lower.equals(upper));
System.out.println("s.equalsIgnoreCase(upper) : "+lower.equalsIgnoreCase(upper));
}
}

OUTPUT
upper.equals(upper) : false
s.equalsIgnoreCase(upper) : true

CO2150704.1

Q. Print the hashcode of string

import java.util.Scanner;
import java.util.Arrays;
import java.io.*;
class string{
public static void main(String []args){
String s="Aaditya";
System.out.println("hashcode of s : "+s.hashCode());
}
}

OUTPUT
hashcode of s : 430820247
CO2150704.1

R. Print a string in canonical format

import java.util.Scanner;
import java.util.Arrays;
import java.io.*;
class string{
public static void main(String []args){
String s="Aaditya";
String s1="Aaditya";
String s2 = new String("Test");
final String s3 = s1.intern();
System.out.println("Canonical formate is "+s3);
System.out.println("s.equals(s1):"+s.equals(s3));
}
}

OUTPUT

Canonical formate is Aaditya


s.equals(s1):true
CO2150704.1

6. Print the execution time of a function in millisecond, microsecond and nanosecond.

import java.util.concurrent.TimeUnit;
class Timeutil{
public static void main(String []args){
System.out.println("it is for time execution time ");
long st=System.nanoTime();
for(int i=0;i<100000;i++){
}
long et=System.nanoTime();
System.out.println("Time for execution is : "+(et-st)+" nanoseconds");
System.out.println("Time for execution is : "+(et-st)/1000+" microsecond");
System.out.println("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(){
System.out.println("i is "+i);
}
}
class B extends A{
int k,j;
void getjk(int x,int y){
j=x;
k=y;
}
void showjk(){
System.out.println("j is "+j);
System.out.println("k is "+k);
}
}
class C extends B{
int l;
void getl(int x){
l=x;
}
void showijkl(){
System.out.println("i is "+i);
System.out.println("j is "+j);
System.out.println("k is "+k);
System.out.println("l is "+l);
}
}
class Multilevel{
public static void main(String []args){

B b=new B();
b.getI(1);
b.showi();
b.getjk(1,3);
b.showjk();
C c=new C();
c.getl(2);
c.showijkl();
}
}
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(){
System.out.println("implementation of method1");
}
public void method2(){
System.out.println("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(){System.out.println("Get Number");}
public void printNo()
{System.out.println("Print Number");}

}
class Mple{
public static void main(String []args){
Multiple M=new Multiple();
M.getNo();
M.printNo();
}
}

OUTPUT

Get Number
Print Number
CO2150704.2

11. Implement multipath and hybrid inheritance in java.

class A{
void display(){
System.out.println("A");
}
}
class B extends A{
void display(){
System.out.println("B");
}
}
class C extends A{
void display(){
System.out.println("C");
}
}
class D extends A{
void display(){
System.out.println("D");
}
}

class Multipath{

public static void main(String []args){


D d=new D();
d.display();
System.out.println("");
}
}

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(){
System.out.println("Name is : "+str);
System.out.println("Roll no is : "+rn);
}
}
class AbsDemo{
public static void main(String []args){
stu s=new stu();
s.setName("Aaditya");
s.setInt(56);
s.display();
}
}

OUTPUT

Name is : Aaditya
Roll no is : 56

You might also like