JAVA PROGRAMS

 


public class FIRST {

public static void main(String[] args) {

// TODO Auto-generated method stub

System.out.println("WELCOME TO JAVA");

}

}

==========================================

public class SECOND {

public static void main(String[] args) {

// TODO Auto-generated method stub

System.out.println("Asha\n Asha Bhavan\n Pothencode \n Trivandrum");

}

}

================================================

public class THIRD {

public static void main(String[] args) {

// TODO Auto-generated method stub

System.out.println(" 1\n 1\t2\n 1\t2\t3\n 1\t2\t3\t4 \n 1\t2\t3\t4\t5 ");

}

}

=================================================

public class fourth {

public static void main(String[] args) {

// TODO Auto-generated method stub

System.out.println("\t\t*\n\t*\t\t*\n*\t\t*\t\t*"); 

}

}

============================================================

public class calculation {

public static void main(String[] args) {

// TODO Auto-generated method stub

int a=2;//variable declaratn

int b=4;

int c=a+b;

int d=a-b;

int e=a*b;

int f=a/b; 

//System.out.println("sum="+c);

//System.out.println("difference="+d);

//System.out.println("multiplication="+e);

//System.out.println("division="+f);

System.out.println("sum="+c+ "\ndifference="+d+ "\nproduct="+e+ "\ndivision="+f);

}

}

===================================================================

public class areasquare {

public static void main(String[] args) {

// TODO Auto-generated method stub

int a=3;

int c=a*a;

System.out.println("area of square="+c);

}

}

==============================================================

public class areasquare {

public static void main(String[] args) {

// TODO Auto-generated method stub

int a=3;

int c=a*a;

System.out.println("area of square="+c);

}

}

=============================================================

public class operator {

public static void main(String[] args) {

// TODO Auto-generated method stub

int x=10;

System.out.println(x++);

System.out.println(++x);

System.out.println(x--);

System.out.println(--x);

int a=8;  

int b=10;  

System.out.println(a++ + ++a);//8+10=18

System.out.println(b++ + b++);//10+11=21 

 } }

============================================================

public class unary {

public static void main(String[] args) {

// TODO Auto-generated method stub

int a=22;

int b=-25;

boolean c=true;

boolean d=false;

System.out.println(~a);

System.out.println(~b);

System.out.println(!c);

System.out.println(!d);

}}

======================================================================

public class modulus {

public static void main(String[] args) {

// TODO Auto-generated method stub

int a=9;

int b=3;

int c=a%b;

System.out.println(c);

}}

=======================================================================

public class logical {

public static void main(String[] args) {

// TODO Auto-generated method stub

int a=6;

int b=2;

int c=8;

System.out.println(a<b||a<c);

System.out.println(a<b&a<c); 

}}

===============================================================

public class logical {

public static void main(String[] args) {

// TODO Auto-generated method stub

int a=6;

int b=2;

int c=8;

System.out.println(a<b||a<c);

System.out.println(a<b&a<c); 

}}

===============================================================

public class assignment {

public static void main(String[] args) {

// TODO Auto-generated method stub

int a=10;

int b=20;

a*=4;

b/=2;

System.out.println(a);

System.out.println(b);

}}

====================================================================

public class ifff {

public static void main(String[] args) {

// TODO Auto-generated method stub

int age=20;

if(age>18) {

System.out.print("age is greater than 18");

}}}

================================================================

public class vote {

public static void main(String[] args) {

// TODO Auto-generated method stub

int age=80;

if (age>=18)

{

System.out.println("Eligible for vote");

}

else

{

System.out.println("Not eligible for vote");

)}}

==============================================================


public class positive {

public static void main(String[] args) {

// TODO Auto-generated method stub

 int a=0;

 if (a>=0)

 {

System.out.println("It is a positive number");

 }

 else

 {

System.out.println("It is a negative number");  

 }}}

====================================================================

public class big {

public static void main(String[] args) {

// TODO Auto-generated method stub

int a=10;

int b=8;

if(a<b)

{

System.out.println("a is the smallest number");

}

else

{

System.out.println("b is the smallest number");

}}}

===============================================================

import java.util.Scanner;

public class odd {

public static void main(String[] args) {

// TODO Auto-generated method stub

Scanner input=new Scanner(System.in);

System.out.println("enter a number");

int x=input.nextInt();

if(x%2==0)

{

System.out.println("It is an even number");

}

else {

System.out.println("It is an odd number");

}}}

==================================================================

import java.util.Scanner;

public class onetofive {

public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input=new Scanner(System.in);
System.out.println("Enter the number");

int y=input.nextInt();
if(y==1)
{
System.out.println("sunday");
}
else if(y==2)
{
System.out.println("monday");
}
else if(y==3)
{
System.out.println("tuesday");
}
else if(y==4)
{
System.out.println("wednesday");
}
else if(y==5)
{
System.out.println("thursday");
}
else if(y==6)
{
System.out.println("friday");
}
else if(y==7)
{
System.out.println("satur  day");
}}}
====================================================================

import java.util.Scanner;
public class addscanner {

public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input=new Scanner(System.in);

System.out.println("enter the first number, a=");

System.out.println("enter the second number, b=");

int a=input.nextInt();
int b=input.nextInt();
int c=a*b;
System.out.println("enter the answer=" +c);
}}
==============================================================

public class methodsssss {

  static int myMethod(int x) {
    return 5 + x;
  }

  public static void main(String[] args) {
    System.out.println(myMethod(3));
  }
}
==========================================

//public class method {
  //static void myMethod() {
   // System.out.println("I just got executed!");

//public static void main(String[] args) {
    //myMethod(); 
   
    
public class method {
  static void myMethod(String fname,int age) {
    System.out.println(fname + " is"+  age);
  }

  public static void main(String[] args) {
    myMethod("LEO", 5);
    myMethod("ASHA", 31);
    myMethod("VEDHA", 8);
  }
}  
===================================
import java.util.Scanner;

public class methodsum {
static int mymethod(int x,int y,int z) {
//int x=5;
//int y=6;
return x*y*z;
}

public static void main(String[] args) {
// TODO Auto-generated method stub
//int x=3;
//int y=5;
Scanner input=new Scanner(System.in);
System.out.println("Enter 3 numbers");
int x=input.nextInt();
int y=input.nextInt();
int z=input.nextInt();
System.out.println(mymethod(x,y,z));
}

}
==========================================

import java.util.Scanner;

public class oddevenmethod {
static void mymethod (int x) {
if(x % 2==0)
System.out.println("num is an even number");
else
System.out.println("It is an odd number");
//return;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input=new Scanner(System.in);
System.out.println("Enter the number");
int x=input.nextInt();
//System.out.println("It is an even number");
mymethod(x);

}

}
=====================================
import java.util.Scanner;
public class calculationmethod {
static int add(int x,int y) {
int sum=x+y;
return sum;
}
static int subt(int x,int y) {
int diff=x-y;
return diff;
}
static int multi(int x,int y) {
int multi=x*y;
return multi;
}
static int divi(int x,int y) {
int div=x/y;
return div;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input=new Scanner(System.in);
System.out.println("enter 2 numbers");
int x=input.nextInt();
int y=input.nextInt();
System.out.println(add(x,y));
System.out.println(subt(x,y));
System.out.println(multi(x,y));
System.out.println(divi(x,y));
}

}
=================================
public class cncatstring {

public static void main (String [] args){
String str1 = " Selenium";
String str2 = " Testing";

System.out.println(str1.concat(str2));//SeleniumTesting
System.out.println(str1 + str2);//SeleniumTesting
}
}
================================================================
public class equaltoinbuilt {

public static void main (String [] args){
String str1 = "selenium";
String str2 = "SELENIUM";
String str3 = "selenium";

System.out.println(str1.equals(str2));//false
System.out.println(str1.equals(str3));//true
}
}
=================================================================
public class upperrcase {

public static void main (String [] args){
String str1 = "SELENIUM";
String str2 = "selenium";
String str3 = "SELEnium";
String str4= "selenium123";
System.out.println(str1.toUpperCase());
System.out.println(str2.toUpperCase());
System.out.println(str3.toUpperCase());
System.out.println(str4.toUpperCase());
}

}
=============================================================
public class concaat {

public static void main(String[] args) {
// TODO Auto-generated method stub
String s1="java";
String s2="hai";
s1=s1.concat(s2);
System.out.println(s1);
}}
=============================================================
CONSTRUCTOR

// Create a Main class
public class Main {
  int x;

  // Create a class constructor for the Main class
  public Main() {
    x = 5;
  }

  public static void main(String[] args) {
    Main myObj = new Main();
    System.out.println(myObj.x);
  }
}
===========================
public class Main {
  int x;

  public Main(int y) {
    x = y;
  }

  public static void main(String[] args) {
    Main myObj = new Main(5);
    System.out.println(myObj.x);
  }
}
==============================================

Exception Handling in Java

The Exception Handling in Java is one of the powerful mechanism to handle the runtime errors so that normal flow of the application can be maintained.
Exception Handling is a mechanism to handle runtime errors such as ClassNotFoundException, IOException, SQLException, RemoteException, etc.
An exception is an unwanted or unexpected event, which occurs during the execution of a program i.e at run time, that disrupts the normal flow of the program’s instructions.

What is Exception in Java

Dictionary Meaning: Exception is an abnormal condition.In Java, an exception is an event that disrupts the normal flow of the program. It is an object which is thrown at runtime.


Advantage of Exception Handling

The core advantage of exception handling is to maintain the normal flow of the application. An exception normally disrupts the normal flow of the application that is why we use exception handling. Let's take a scenario: 

Suppose there are 10 statements in your program and there occurs an exception at statement 5, the rest of the code will not be executed i.e. statement 6 to 10 will not be executed. If we perform exception handling, the rest of the statement will be executed. That is why we use exception handling in Java.



Hierarchy of Java Exception classesThe java.lang.Throwable class is the root class of Java Exception hierarchy which is inherited by two subclasses: Exception and Error. A hierarchy of Java Exception classes are given below:
hierarchy of exception handling

Types of Java Exceptions

There are mainly two types of exceptions: checked and unchecked. Here, an error is considered as the unchecked exception. According to Oracle, there are three types of exceptions:

  1. Checked Exception
  2. Unchecked Exception
  3. Error
Types of Java Exceptions

Difference between Checked and Unchecked Exceptions

1) Checked Exception

The classes which directly inherit Throwable class except RuntimeException and Error are known as checked exceptions e.g. IOException, SQLException etc. Checked exceptions are checked at compile-time.

2) Unchecked Exception

The classes which inherit RuntimeException are known as unchecked exceptions e.g. ArithmeticException, NullPointerException, ArrayIndexOutOfBoundsException etc. Unchecked exceptions are not checked at compile-time, but they are checked at runtime.

3) Error

Error is irrecoverable e.g. OutOfMemoryError, VirtualMachineError, AssertionError etc.

Java Exception KeywordsThere are 5 keywords which are used in handling exceptions in Java.
KeywordDescription
tryThe "try" keyword is used to specify a block where we should place exception code. The try block must be followed by either catch or finally. It means, we can't use try block alone.
catchThe "catch" block is used to handle the exception. It must be preceded by try block which means we can't use catch block alone. It can be followed by finally block later.
finallyThe "finally" block is used to execute the important code of the program. It is executed whether an exception is handled or not.
throwThe "throw" keyword is used to throw an exception.
throwsThe "throws" keyword is used to declare exceptions. It doesn't throw an exception. It specifies that there may occur an exception in the method. It is always used with method signature.


public class JavaExceptionExample{
  public static void main(String args[]){
   try{
      int data=100/0;
   }catch(ArithmeticException e){System.out.println(e);}
   System.out.println("rest of the code...");
  }
}

===================================
public class TryCatchExample9 {

public static void main(String[] args) {
try
{
int arr[]= {1,3,5,7};
System.out.println(arr[10]); //may throw exception 
}
            // handling the array exception
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println(e);
}
System.out.println("rest of the code");
}
}
====================================
  1. import java.io.FileNotFoundException;  
  2. import java.io.PrintWriter;  
  3.   
  4. public class TryCatchExample10 {  
  5.   
  6.     public static void main(String[] args) {  
  7.           
  8.           
  9.         PrintWriter pw;  
  10.         try {  
  11.             pw = new PrintWriter("jtp.txt"); //may throw exception   
  12.             pw.println("saved");  
  13.         }  
  14. // providing the checked exception handler  
  15.  catch (FileNotFoundException e) {  
  16.               
  17.             System.out.println(e);  
  18.         }         
  19.     System.out.println("File saved successfully");  
  20.     }  
  21. }

No comments:

Post a Comment

Techzmatrix