Thursday, October 30, 2014

Java Used Pro



Java program to find factorial

import java.util.Scanner;
class Factorial
{
 public static void main(String args[])
{
      int n, c, fact = 1;
      System.out.println("Enter an integer to calculate it's factorial");
      Scanner in = new Scanner(System.in);
      n = in.nextInt();
         if ( n < 0 )
         System.out.println("Number should be non-negative.");
        else
        {
         for ( c = 1 ; c <= n ; c++ )
            fact = fact*c;
        System.out.println("Factorial of "+n+" is = "+fact);
      }
   }
}

Enhanced for loop java

class EnhancedForLoop 
{
 public static void main(String[] args) 
{
    int primes[] = { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29};
 
    for (int t: primes) 
     {
      System.out.println(t); 
    }
  }
}

Java program to find largest of three numbers

import java.util.Scanner;
class LargestOfThreeNumbers
{
public static void main(String args[])
{
      int x, y, z;
      System.out.println("Enter three integers ");
      Scanner in = new Scanner(System.in);
      x = in.nextInt();
      y = in.nextInt();
      z = in.nextInt();
      if ( x > y && x > z )
         System.out.println("First number is largest.");
      else if ( y > x && y > z )
         System.out.println("Second number is largest.");
      else if ( z > x && z > y )
         System.out.println("Third number is largest.");
      else   
         System.out.println("Entered numbers are not distinct.");
   }
}

Java program to swap two numbers

import java.util.Scanner;
class SwapNumbers
          {
public static void main(String args[])
{
      int x, y, temp;
      System.out.println("Enter x and y");
      Scanner in = new Scanner(System.in);
      x = in.nextInt();
      y = in.nextInt();
      System.out.println("Before Swapping\nx = "+x+"\ny = "+y);
      temp = x;
      x = y;
      y = temp;
      System.out.println("After Swapping\nx = "+x+"\ny = "+y);
   }
}
 

Swapping without temporary variable


import java.util.Scanner;
class SwapNumbers
{
 public static void main(String args[])
 {
      int x, y;
      System.out.println("Enter x and y");
      Scanner in = new Scanner(System.in);
      x = in.nextInt();
      y = in.nextInt();
      System.out.println("Before Swapping\nx = "+x+"\ny = "+y);
      x = x + y;
      y = x - y;
      x = x - y;
     System.out.println("After Swapping\nx = "+x+"\ny = "+y);
   }
}

Java exception handling tutorial

import java.util.Scanner;
class Division 
{
public static void main(String[] args) 
{
     int a, b, result;
  Scanner input = new Scanner(System.in);
  System.out.println("Input two integers");
  a = input.nextInt();
  b = input.nextInt();
        result = a / b;
 System.out.println("Result = " + result);
  }
}

Java constructor tutorial

class Language 
{
  String name;
Language() 
{
    System.out.println("Constructor method called.");
 }
Language(String t) 
{
    name = t;
 }
 public static void main(String[] args) 
{
    Language cpp  = new Language();
    Language java = new Language("Java");
    cpp.setName("C++");
    java.getName();
    cpp.getName();
  }
  void setName(String t)
 {
    name = t;
  }
  void getName() 
  {
    System.out.println("Language name: " + name);
  }
}

Using multiple classes in Java program

class Computer 
{
 Computer() 
{
    System.out.println("Constructor of Computer class.");
 }
 void computer_method() 
 {
    System.out.println("Power gone! Shut down your PC soon...");
  }
  public static void main(String[] args) 
 {
    Computer my = new Computer();
    Laptop your = new Laptop();
    my.computer_method();
    your.laptop_method();
  }
}
class Laptop 
 {
  Laptop() 
  {
    System.out.println("Constructor of Laptop class.");
   }
  void laptop_method() 
  {
    System.out.println("99% Battery available.");
  }
} 

Java static method

class Languages 
{
 public static void main(String[] args) 
 {
    display();
  }
 static void display() 
 {
    System.out.println("Java is my favorite programming language.");
  }
}

No comments:

Post a Comment