/* Swapping-call by reference-2nd way */
class Swappingref{
Integer x;
Integer y;
Swappingref(Integer x,Integer y){
this.x=x;
this.y=y;
}
void swapping(Swappingref obj){
Integer temp=obj.x;
obj.x=obj.y;
obj.y=temp;
}
public static void main(String[]args){
Integer x=new Integer(5);
Integer y=new Integer(6);
Swappingref object=new Swappingref(x,y);
object.swapping(object);
System.out.println("After swap:\n"+"X:"+object.x+" Y:"+object.y);
}
}
Thursday, October 6, 2016
Swapping(Call by reference)
/*Swapping-call by reference */
class Swappingref{
int x,y;
Swappingref(int x,int y){
this.x=x;
this.y=y;
}
void swapping(Swappingref obj){
int temp=obj.x;
obj.x=obj.y;
obj.y=temp;
}
public static void main(String[]args){
Swappingref object=new Swappingref(5,6);
object.swapping(object);
System.out.println("After swap:\n"+"X:"+object.x+" Y:"+object.y);
}
}
class Swappingref{
int x,y;
Swappingref(int x,int y){
this.x=x;
this.y=y;
}
void swapping(Swappingref obj){
int temp=obj.x;
obj.x=obj.y;
obj.y=temp;
}
public static void main(String[]args){
Swappingref object=new Swappingref(5,6);
object.swapping(object);
System.out.println("After swap:\n"+"X:"+object.x+" Y:"+object.y);
}
}
Swapping(call by value)
/*Swapping-pass by value */
class Swapping{
void swapping(int x,int y){
int temp=x;
x=y;
y=temp;
System.out.println("After swap by value: X:"+x+" y:"+y);
}
public static void main(String[] args){
Swapping object=new Swapping();
object.swapping(4,5);
}
}
Sunday, October 2, 2016
objectTo Numeric Primitive type
/***object to numeric primitive type***/
class objectToNumeric{
public static void main(String[] args){
Integer object=new Integer(4);
Double object2=new Double(10.4);
System.out.println(object);
float value=object.floatValue();
System.out.println("float:"+value);
int val=object.intValue();
System.out.println("int:"+val);
String str=new String("10");
int x=Integer.parseInt(str);
double d=Double.parseDouble(str);
System.out.println("double to integer:"+object2.intValue());
System.out.println("String to integer:"+x);
System.out.println("String to double:"+d);
System.out.println("Integer to string:"+object.toString());
}
}
variable(2)
/*In a class there are three types of variable*/
class Variable{int x=0;//instance variable
static int y=9;//static variable
void function(){
int var=1;//Local variable of function method
System.out.println("Local variable of function method:"+var);
}
public static void main(String[]args){
Variable obj1=new Variable();
System.out.println("instance variable:"+obj1.x);
System.out.println("static variable:"+Variable.y);
System.out.println("static variable:"+y);
obj1.function();
}
}
Monday, June 27, 2016
String(uppercase to lowercase,replace)
package stringuppertolower;
public class StringUpperToLower {
public static void main(String[] args) {
String name="Abdul Mukit";
System.out.println("Before change:"+name);
String change=name.toLowerCase();
System.out.println("After change:"+change);
char c=name.charAt(1);
System.out.println("CharAt:"+c);
String replace=name.replace("Abdul Mukit","Mukit ahmed");
System.out.println("replace name:"+replace);
}
}
string(CompareTo and indexOf)
package stringcompare;
public class StringCompare {
public static void main(String[] args) {
String myName="Bangladesh";
String YourName="Africa";
char c='k';
System.out.println("Index of k from myName:"+myName.indexOf(c));
System.out.println("Index of k from yourName:"+YourName.indexOf(c));
int result=myName.compareTo(YourName);
if(result>0){
System.out.println("greater:"+result);
}
else if(result<0){
System.out.println("less:"+result);
}
else if(result==0){
System.out.println("Equal:"+result);
}
}
}
Sunday, June 26, 2016
for loop and array
package arrayloop;
import java.util.Arrays;
public class Arrayloop {
public static void main(String[] args) {
int []arr={3,21,43,4,0};
int i;
for(i=0;i<arr.length;i++){
System.out.println(arr[i]);
}
Arrays.sort(arr);
System.out.println("After sorting");
for(i=0;i<arr.length;i++){
System.out.println(arr[i]);
}
}
}
Saturday, June 25, 2016
if else and switch case
import java.util.Scanner;
import java.lang.*;
class condition{
public static void main(String args[]){
char c;
Scanner in=new Scanner(System.in);
System.out.println("Please enter a positive negaive or zero");
c=in.next().charAt(0);
if(c=='+'){
System.out.println("This is positive character");
}
else{
switch(c){
case '-':
System.out.println("This is negative character");
break;
default:
System.out.println("404 invalid input");
}
}
}
}
import java.lang.*;
class condition{
public static void main(String args[]){
char c;
Scanner in=new Scanner(System.in);
System.out.println("Please enter a positive negaive or zero");
c=in.next().charAt(0);
if(c=='+'){
System.out.println("This is positive character");
}
else{
switch(c){
case '-':
System.out.println("This is negative character");
break;
default:
System.out.println("404 invalid input");
}
}
}
}
Friday, June 24, 2016
Taking User Input
import java.util.Scanner;
import java.lang.*;
class userinput{
public static void main(String args[]){
Scanner in=new Scanner(System.in);
System.out.println("Please enter a Integer value:");
int x=in.nextInt();
System.out.println("Integer value: "+x);
System.out.println("Please enter a character value:");
char c=in.next().charAt(0);
System.out.println("Character value: "+c);
System.out.println("Please enter a float value:");
float f=in.nextFloat();
System.out.println("float value:"+f);
System.out.println("Please enter a double value:");
double d=in.nextDouble();
System.out.println("Double value:"+d);
System.out.println("Please enter a String value:");
String str;
str=in.nextLine();
System.out.print("String Value:"+str);
}
}
import java.lang.*;
class userinput{
public static void main(String args[]){
Scanner in=new Scanner(System.in);
System.out.println("Please enter a Integer value:");
int x=in.nextInt();
System.out.println("Integer value: "+x);
System.out.println("Please enter a character value:");
char c=in.next().charAt(0);
System.out.println("Character value: "+c);
System.out.println("Please enter a float value:");
float f=in.nextFloat();
System.out.println("float value:"+f);
System.out.println("Please enter a double value:");
double d=in.nextDouble();
System.out.println("Double value:"+d);
System.out.println("Please enter a String value:");
String str;
str=in.nextLine();
System.out.print("String Value:"+str);
}
}
variable
class hello
{
void add(Integer a,Integer b){
Integer res=a+b;
System.out.println(res);
}
public static void main(String[] args)
{
byte a;
a=27;
System.out.println("BYTE: "+a);
short b;
b=1201;
System.out.println("SHORT: "+b);
int x;
x=13256;
System.out.println("INT: "+x);
long z;
z=236464;
System.out.println("LONG: "+z);
float f;
f=(float)23.25;
System.out.println("FLOAT: "+f);
double d;
d=54.12546;
System.out.println("Double: " +d);
char c;
c='J';
System.out.println("CHAR: "+c);
boolean k;
k=false;
System.out.println("BOOLEAN: "+k);
System.out.println(new Integer(10));
Integer j=new Integer(10);
j=2;
System.out.println(j);
Integer j2=100;///Integer j2=new Integer(100);
System.out.println(j2);
hello h=new hello();
h.add(j,j2);
}
}
{
void add(Integer a,Integer b){
Integer res=a+b;
System.out.println(res);
}
public static void main(String[] args)
{
byte a;
a=27;
System.out.println("BYTE: "+a);
short b;
b=1201;
System.out.println("SHORT: "+b);
int x;
x=13256;
System.out.println("INT: "+x);
long z;
z=236464;
System.out.println("LONG: "+z);
float f;
f=(float)23.25;
System.out.println("FLOAT: "+f);
double d;
d=54.12546;
System.out.println("Double: " +d);
char c;
c='J';
System.out.println("CHAR: "+c);
boolean k;
k=false;
System.out.println("BOOLEAN: "+k);
System.out.println(new Integer(10));
Integer j=new Integer(10);
j=2;
System.out.println(j);
Integer j2=100;///Integer j2=new Integer(100);
System.out.println(j2);
hello h=new hello();
h.add(j,j2);
}
}
Subscribe to:
Comments (Atom)