Earn by twitter on twivert

Sign up for PayPal and start accepting credit card payments instantly.
Showing posts with label java program. Show all posts
Showing posts with label java program. Show all posts

Thursday, March 29, 2012

WAP of Switch case in Java

/*WAP to capture the two integer values from user and perform following actions according to user Requierement
1. Addition
2. Substraction
3. Multiplication
4. Division
5. Exit
*/
import java.lang.*;
import java.io.*;
class oprations
{
 public void InputMsg()
 {System.out.println("Enter Two Integer numbers. ");}
 
 public void continueMsg()
 {System.out.println("Do you want to Continue. \n1. Yes \n0. No");}
 
 public static void main (String args[]) throws IOException
 {
  int a , b, choice = -1 ;
  
  do
  {
    System.out.println("Choose a Opration");
    System.out.println("1. Addition \n2. Substraction \n3. Multiplication \n4. Division \n5. Exit \nYour Choice: ");

    BufferedReader br = new BufferedReader(new 
         InputStreamReader(System.in));
    oprations e = new oprations();
    choice = Integer.parseInt(br.readLine());
    int repeate = -1;
    
   do
   {
    
    switch(choice)
    {
     
     case 1: e.InputMsg();
        a = Integer.parseInt(br.readLine());
        b = Integer.parseInt(br.readLine());
     System.out.println("Addition = "+(a+b));
    
     e.continueMsg();
     repeate = Integer.parseInt(br.readLine());
     break;
     case 2: e.InputMsg();
        a = Integer.parseInt(br.readLine());
        b = Integer.parseInt(br.readLine());
     System.out.println("Substraction = "+(a-b));
     
     e.continueMsg();
     repeate = Integer.parseInt(br.readLine());
     break;
     case 3: e.InputMsg();
        a = Integer.parseInt(br.readLine());
        b = Integer.parseInt(br.readLine());
     System.out.println("Multiplication = "+(a*b));
     
     e.continueMsg();
     repeate = Integer.parseInt(br.readLine());
     break;
     case 4: e.InputMsg();
        a = Integer.parseInt(br.readLine());
        b = Integer.parseInt(br.readLine());
     System.out.println("Division = "+(a/b));
     
     e.continueMsg();
     repeate = Integer.parseInt(br.readLine());
     break;
     case 5: System.out.println("BYE!"); 
     break;
     default: System.out.println("Invalid Input.");
    }
    
   }while(repeate != 0 && choice != 5);
   
  }while(choice != 5); 
 }
 
}

WAP of Matrix Multiplication in Java


/*WAP of Matrix Multiplication in Java*/
import java.lang.*;
import java.io.*;
import java.util.*;
class matrix
{
 public static void main (String args[]) throws IOException
 {
  int a[][] = new int[2][2];
  int b[][] = new int[2][2];
  int c[][] = new int[2][2];
  
  Scanner sc = new Scanner(System.in);
  System.out.println("Enter Elements of Matrix 1");
  for(int i=0; i<2; i++)
  {
   for(int j=0; j<2; j++)
   {
   a[i][j] = sc.nextInt();
   }
  }

  System.out.println("Enter Elements of Matrix 2");
  for(int i=0; i<2; i++)
  {
   for(int j=0; j<2; j++)
   {
   b[i][j] = sc.nextInt();
   }
  }
  // calculation
  for(int i=0; i<2; i++)
  {
   for(int j=0; j<2; j++)
   {
    for(int k=0; k<2; k++)
    {
   c[i][j] += a[i][k] * b[k][j];
    }
   }
  }
  System.out.println("Matrix After multiplication");
  for(int i=0; i<2; i++)
  {
   for(int j=0; j<2; j++)
   {
   System.out.println( "\t" + c[i][j] );
   }
  }
 }
}

Monday, September 27, 2010

WAP to that includes a method inside a class



/*WAP to that includes a method inside a class*/
class Box {
double width;
double height;
double depth;
// display volume of a box
void volume() {
System.out.print("Volume is ");
System.out.println(width * height * depth);
}
}
class BoxDemo3 {
public static void main(String args[]) {
Box mybox1 = new Box();
Box mybox2 = new Box();
// assign values to mybox1's instance variables
mybox1.width = 10;
mybox1.height = 20;
mybox1.depth = 15;
/* assign different values to mybox2's
instance variables */
mybox2.width = 3;
mybox2.height = 6;
mybox2.depth = 9;
// display volume of first box
mybox1.volume();
// display volume of second box
mybox2.volume();
}
}

WAP to declares two Box class objects




/*WAP to declares two Box class objects*/
class Box {
double width;
double height;
double depth;
}
class BoxDemo2 {
public static void main(String args[]) {
Box mybox1 = new Box();
Box mybox2 = new Box();
double vol;
// assign values to mybox1's instance variables
mybox1.width = 10;
mybox1.height = 20;
mybox1.depth = 15;
/* assign different values to mybox2's
instance variables */
mybox2.width = 3;
mybox2.height = 6;
mybox2.depth = 9;
// compute volume of first box
vol = mybox1.width * mybox1.height * mybox1.depth;
System.out.println("Volume is " + vol);
// compute volume of second box
vol = mybox2.width * mybox2.height * mybox2.depth;
System.out.println("Volume is " + vol);
}
}

WAP that calculate volume of Box


/*WAP that calculate volume of Box
Save as BoxDemo.java
*/
class Box {
double width;
double height;
double depth;
}
// This class declares an object of type Box.
class BoxDemo {
public static void main(String args[]) {
Box mybox = new Box();
double vol;
// assign values to mybox's instance variables
mybox.width = 10;
mybox.height = 20;
mybox.depth = 15;
// compute volume of box
vol = mybox.width * mybox.height * mybox.depth;
System.out.println("Volume is " + vol);
}
}

Friday, September 24, 2010

WAP Using a do-while loop to process a menu selection


/*WAP Using a do-while loop to process a menu selection
save file as: Menu.java
*/
class Menu {
public static void main(String args[])
throws java.io.IOException {
char choice;
do {
System.out.println("Help on:");
- 84 -
System.out.println(" 1. if");
System.out.println(" 2. switch");
System.out.println(" 3. while");
System.out.println(" 4. do-while");
System.out.println(" 5. for\\n");
System.out.println("Choose one:");
choice = (char) System.in.read();
} while( choice < '1' || choice > '5');
System.out.println("\\n");
switch(choice) {
case '1':
System.out.println("The if:\\n");
System.out.println("if(condition) statement;");
System.out.println("else statement;");
break;
case '2':
System.out.println("The switch:\\n");
System.out.println("switch(expression) {");
System.out.println(" case constant:");
System.out.println(" statement sequence");
System.out.println(" break;");
System.out.println(" // ...");
System.out.println("}");
break;
case '3':
System.out.println("The while:\\n");
System.out.println("while(condition) statement;");
break;
case '4':
System.out.println("The do-while:\\n");
System.out.println("do {");
System.out.println(" statement;");
System.out.println("} while (condition);");
break;
case '5':
System.out.println("The for:\\n");
System.out.print("for(init; condition; iteration)");
System.out.println(" statement;");
break;
}
}
}

WAP to An improved version of the season program


/*WAP to An improved version of the season program*/
class Switch {
public static void main(String args[]) {
int month = 4;
String season;
switch (month) {
case 12:
case 1:
case 2:
season = "Winter";
break;
case 3:
case 4:
case 5:
season = "Spring";
break;
case 6:
case 7:
case 8:
season = "Summer";
break;
case 9:
case 10:
case 11:
season = "Autumn";
break;
default:
season = "Bogus Month";
}
System.out.println("April is in the " + season + ".");
}
}

WAP to check that In a switch, break statements are optional


/*WAP to check that In a switch, break statements are optional*/
class MissingBreak {
public static void main(String args[]) {
for(int i=0; i<12; i++)
switch(i) {
case 0:
case 1:
case 2:
case 3:
case 4:
System.out.println("i is less than 5");
break;
case 5:
case 6:
case 7:
case 8:
case 9:
System.out.println("i is less than 10");
break;
default:
System.out.println("i is 10 or more");
}
}
}

Tuesday, September 21, 2010

WAP to Demonstrate switch statements


/*WAP to Demonstrate switch statements*/
class SampleSwitch {
public static void main(String args[]) {
for(int i=0; i<6; i++)
switch(i) {
case 0:
System.out.println("i is zero.");
break;
case 1:
System.out.println("i is one.");
break;
case 2:
System.out.println("i is two.");
break;
case 3:
System.out.println("i is three.");
break;
default:
System.out.println("i is greater than 3.");
}
}
}

WAP to Demonstrate if-else-if statements


/*WAP to Demonstrate if-else-if statements*/
class IfElse {
public static void main(String args[]) {
int month = 4; // April
String season;
if(month == 12 || month == 1 || month == 2)
season = "Winter";
else if(month == 3 || month == 4 || month == 5)
season = "Spring";
else if(month == 6 || month == 7 || month == 8)
season = "Summer";
else if(month == 9 || month == 10 || month == 11)
season = "Autumn";
else
season = "Bogus Month";
System.out.println("April is in the " + season + ".");
}
}

WAP to Demonstrate ? : Ternary operator


/*WAP to Demonstrate ? : Ternary operator*/
class Ternary {
public static void main(String args[]) {
int i, k;
i = 10;
k = i < 0 ? -i : i; // get absolute value of i
System.out.print("Absolute value of ");
System.out.println(i + " is " + k);
i = -10;
k = i < 0 ? -i : i; // get absolute value of i
System.out.print("Absolute value of ");
System.out.println(i + " is " + k);
}
}

WAP to Demonstrate the boolean logical operators


/*WAP to Demonstrate the boolean logical operators*/
class BoolLogic {
public static void main(String args[]) {
boolean a = true;
boolean b = false;
boolean c = a | b;
boolean d = a & b;
boolean e = a ^ b;
boolean f = (!a & b) | (a & !b);
boolean g = !a;
System.out.println(" a = " + a);
System.out.println(" b = " + b);
System.out.println(" a|b = " + c);
System.out.println(" a&b = " + d);
System.out.println(" a^b = " + e);
System.out.println("!a&b|a&!b = " + f);
System.out.println(" !a = " + g);
}
}

WAP of bit


/*WAP of bit*/
class OpBitEquals {
public static void main(String args[]) {
int a = 1;
int b = 2;
int c = 3;
a |= 4;
b >>= 1;
c <<= 1;
a ^= c;
System.out.println("a = " + a);
System.out.println("b = " + b);
System.out.println("c = " + c);
}
}

Sunday, September 19, 2010

WAP to Unsigned shifting a byte value


/*WAP to Unsigned shifting a byte value.
Save As: ByteUShift.java
*/
class ByteUShift {
static public void main(String args[]) {
char hex[] = {
'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
};
byte b = (byte) 0xf1;
byte c = (byte) (b >> 4);
byte d = (byte) (b >>> 4);
byte e = (byte) ((b & 0xff) >> 4);
System.out.println(" b = 0x" + hex[(b >> 4) & 0x0f] + hex[b & 0x0f]);
System.out.println(" b >> 4 = 0x" + hex[(c >> 4) & 0x0f] + hex[c & 0x0f]);
System.out.println(" b >>> 4 = 0x" + hex[(d >> 4) & 0x0f] + hex[d & 0x0f]);
System.out.println("(b & 0xff) >> 4 = 0x" + hex[(e >> 4) & 0x0f] + hex[e & 0x0f]);
}
}

WAP to Masking sign extension


/*WAP to Masking sign extension.
Save As : HexByte.java
*/
class HexByte {
static public void main(String args[]) {
char hex[] = {
'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
};
byte b = (byte) 0xf1;
System.out.println("b = 0x" + hex[(b >> 4) & 0x0f] + hex[b &
0x0f]);
}
}

WAP to Left shifting as a quick way to multiply by 2


/*WAP to Left shifting as a quick way to multiply by 2.
Save As: MultByTwo.java
*/
class MultByTwo {
public static void main(String args[]) {
int i;
int num = 0xFFFFFFE;
for(i=0; i<4; i++) {
num = num << 1;
System.out.println(num);
}
}
}

WAP to Left shifting a byte value


/*WAP to Left shifting a byte value.
Save As ByteShift.java
*/
class ByteShift {
public static void main(String args[]) {
byte a = 64, b;
int i;
i = a << 2;
b = (byte) (a << 2);
System.out.println("Original value of a: " + a);
System.out.println("i and b: " + i + " " + b);
}
}

WAP to Demonstrate the bitwise logical operators


/*WAP to Demonstrate the bitwise logical operators
Save As: BitLogic.java
*/
class BitLogic {
public static void main(String args[]) {
String binary[] = {
"0000", "0001", "0010", "0011", "0100", "0101", "0110",
"0111",
"1000", "1001", "1010", "1011", "1100", "1101", "1110",
"1111"
};
int a = 3; // 0 + 2 + 1 or 0011 in binary
int b = 6; // 4 + 2 + 0 or 0110 in binary
int c = a | b;
int d = a & b;
int e = a ^ b;
int f = (~a & b) | (a & ~b);
int g = ~a & 0x0f;
System.out.println(" a = " + binary[a]);
System.out.println(" b = " + binary[b]);
System.out.println(" a|b = " + binary[c]);
System.out.println(" a&b = " + binary[d]);
System.out.println(" a^b = " + binary[e]);
System.out.println("~a&b|a&~b = " + binary[f]);
System.out.println(" ~a = " + binary[g]);
}
}

WAp to Demonstrate ++ operator


/*WAp to Demonstrate ++ operator
Save as: IncDec.java
*/
class IncDec {
public static void main(String args[]) {
int a = 1;
int b = 2;
int c;
int d;
c = ++b;
d = a++;
c++;
System.out.println("a = " + a);
System.out.println("b = " + b);
System.out.println("c = " + c);
System.out.println("d = " + d);
}
}

WAP to Demonstrate several assignment operators


/*WAP to Demonstrate several assignment operators*/
class OpEquals {
public static void main(String args[]) {
int a = 1;
int b = 2;
int c = 3;
a += 5;
b *= 4;
c += a * b;
c %= 6;
System.out.println("a = " + a);
System.out.println("b = " + b);
System.out.println("c = " + c);
}
}

ADS

How to earn online

How to earn online:
Step - 1 :
signup for PayPal, to recieve your online earning
click here:
https://www.paypal.com/in/mrb/pal=CZ7224TZBMCBL
step - 2 : Singup these 4 sites & earn & enjoy! Read site to know how to earn.
1. trekpay
Earn up to $0.02 (2 cents) per click.
http://www.trekpay.com/?ref=34690
2. neobux
Earn up to $0.02 (2 cents) per click.
Click here:
http://www.neobux.com/?r=Moneyearner786

AddMe

Bookmark and Share