//classroom discussions on 1.MAR.2012 by Mr.Dr
//IMPLEMENTATION OF MEHTOD OVERLOADING
//this program also explains the narrowing and widening of built in data type
class math
{
int f,s;
//constructor
math()
{
f=1;
s=2;
}
//method-1
void add(double d1, double d2)
{
f=(int)d1; //narrowing down casting
s=(int)d2; //narrowing
int sum=f+s;
System.out.println("The sum is "+sum);
}
//METHOD-2
void add()
{
int sum=f+s;
System.out.println("The sum is "+sum);
}
//METHOD-3
void add( int d1)
{
f=d1;
s=d1;
int sum=f+s;
System.out.println("The sum is "+sum);
}
//METHOD-4
void add(int d1, int d2)
{
f=d1;
s=d2;
int sum=f+s;
System.out.println("The sum is "+sum);
}
//METHOD-5
double add(double d1, int d2)
{
f=(int)d1;
s=d2;
int sum=f+s;
System.out.println("The sum is "+sum);
return sum; //widening up casting
}}
class polymorph
{
public static void main(String args[])
{
math m=new math();
m.add(1.1,1.2); //TWO FLOATS ADDITION
m.add(1,1); //TWO INTEGERS ADDITION
m.add(); //NO PARAMETERS
m.add(1); //ONE PARAMETER PASSING
double d=m.add(1.1,1); //ONE FLOAT AND ONE INTEGER
System.out.println("the sum vaule from the math class is "+d);
}}
OUTPUT:-
//IMPLEMENTATION OF MEHTOD OVERLOADING
//this program also explains the narrowing and widening of built in data type
class math
{
int f,s;
//constructor
math()
{
f=1;
s=2;
}
//method-1
void add(double d1, double d2)
{
f=(int)d1; //narrowing down casting
s=(int)d2; //narrowing
int sum=f+s;
System.out.println("The sum is "+sum);
}
//METHOD-2
void add()
{
int sum=f+s;
System.out.println("The sum is "+sum);
}
//METHOD-3
void add( int d1)
{
f=d1;
s=d1;
int sum=f+s;
System.out.println("The sum is "+sum);
}
//METHOD-4
void add(int d1, int d2)
{
f=d1;
s=d2;
int sum=f+s;
System.out.println("The sum is "+sum);
}
//METHOD-5
double add(double d1, int d2)
{
f=(int)d1;
s=d2;
int sum=f+s;
System.out.println("The sum is "+sum);
return sum; //widening up casting
}}
class polymorph
{
public static void main(String args[])
{
math m=new math();
m.add(1.1,1.2); //TWO FLOATS ADDITION
m.add(1,1); //TWO INTEGERS ADDITION
m.add(); //NO PARAMETERS
m.add(1); //ONE PARAMETER PASSING
double d=m.add(1.1,1); //ONE FLOAT AND ONE INTEGER
System.out.println("the sum vaule from the math class is "+d);
}}
OUTPUT:-
e:\ramu>javac polymorph.java
e:\ramu>java polymorph
The sum is 2
The sum is 2
The sum is 2
The sum is 2
The sum is 2
the sum vaule from the math class is 2.0
// three techniques for inputting from the keyboard for a java program
method-1 streams
DataInputStream d=new DataInputStream(System.in);
//by default reading is String mode
String s=d.readLine();
//to change the data from String to integer you have to use parseInt() method
int x=Integer.parseInt(d.readLine());
mehtod-2 readers
BufferedReader br=new BufferedReader(new InputStreamReader(System.in))
//by default reading is String mode
String s=br.readLine();
//to change the data from String to integer you have to use parseInt() method
int x=Integer.parseInt(br.readLine());
method-3 commandline mode
int x= Integer.parseInt(args[0] )
for the above technique you need to pass data at interpretation stage of a java program
java cline ramu in this java command and cline is the .classfile ramu is the arg[0]
note all the I/O interactions throws the IOException
constructor overloading
class box
{
//INSTANCE OR CLASS VARIABLES
int length;
int breadth;
int height;
//method-1 constructor
box()
{
length=1;
breadth=1;
height=1;
}
box(int l)
{
length=l;
breadth=l;
height=l;
}
box(int l,int b)
{
length=l;
breadth=b;
height;//no operation
}
box(int l,int b, int h)
{
length=l;
breadth=b;
height=h;
}
write here some methods
like display()
volume()
}//end of the class
class boximpl
{ public static void main(String ramu[])
{
box b=new box(); //call for the 0 parameterized constructor
b.display();
b.volume();
box b=new box(1); //call for the 1 parametrized constructor
b.display();
b.volume();
// three techniques for inputting from the keyboard for a java program
method-1 streams
DataInputStream d=new DataInputStream(System.in);
//by default reading is String mode
String s=d.readLine();
//to change the data from String to integer you have to use parseInt() method
int x=Integer.parseInt(d.readLine());
mehtod-2 readers
BufferedReader br=new BufferedReader(new InputStreamReader(System.in))
//by default reading is String mode
String s=br.readLine();
//to change the data from String to integer you have to use parseInt() method
int x=Integer.parseInt(br.readLine());
method-3 commandline mode
int x= Integer.parseInt(args[0] )
for the above technique you need to pass data at interpretation stage of a java program
java cline ramu in this java command and cline is the .classfile ramu is the arg[0]
note all the I/O interactions throws the IOException
constructor overloading
class box
{
//INSTANCE OR CLASS VARIABLES
int length;
int breadth;
int height;
//method-1 constructor
box()
{
length=1;
breadth=1;
height=1;
}
box(int l)
{
length=l;
breadth=l;
height=l;
}
box(int l,int b)
{
length=l;
breadth=b;
height;//no operation
}
box(int l,int b, int h)
{
length=l;
breadth=b;
height=h;
}
write here some methods
like display()
volume()
}//end of the class
class boximpl
{ public static void main(String ramu[])
{
b.volume();
box b=new box(1); //call for the 1 parametrized constructor
b.display();
b.volume();
box b=new box(1,2); //call for the 2 parametrized constructor
b.display();
b.volume();
b.volume();
box b=new box(1,2,3); //call for the3 parametrized constructor
b.display();
b.volume(); } }
exchange data of 2 memory locations in different ways
first way by using the temporary variable
temp=a;
a=b;
b=temp;
second way logic for with out using the temporary variable
a=a+b;
b=a-b;
a=a-b;
THIS KEYWORD is used to refer Present class
instance variables Present
class methods. Present
class constructor.
b.volume(); } }
exchange data of 2 memory locations in different ways
first way by using the temporary variable
temp=a;
a=b;
b=temp;
second way logic for with out using the temporary variable
a=a+b;
b=a-b;
a=a-b;
The below cited program
to use ‘this’ to refer the current class parametrized constructor
and current class
instance variable.
//this demo
class Person
{ String name;
Person ( )
{ this (“Ramu D”); // calling
present class parametrized constructor
this.display ( ); // calling
present class method
}
Person (String name)
{ this.name = name; // assigning
present class variable with parameter “name”}
void display( )
{ System.out.println
("Person Name is = " + name);
}}
class ThisDemo
{ public static void main(String
args[])
{Person p = new Person ( );}}
కామెంట్లు లేవు:
కామెంట్ను పోస్ట్ చేయండి