C#DOTNETCONSOLEAPPLICATION

JAVA

JAVA

 Java is a programming language and a platform. Java is a high level, robust, object-oriented and secure programming language.

Java was developed by Sun Microsystems (which is now the subsidiary of Oracle) in the year 1995. James Gosling is known as the father of Java. Before Java, its name was Oak. Since Oak was already a registered company, so James Gosling and his team changed the Oak name to Java.

 

Platform: Any hardware or software environment in which a program runs, is known as a platform. Since Java has a runtime environment (JRE) and API, it is called a platform.

 

Java Platforms / Editions

There are 4 platforms or editions of Java:

                         1) Java SE (Java Standard Edition)

                          2) Java EE (Java Enterprise Edition)

                          3) Java ME (Java Micro Edition)

                          4) JavaFX

 

Features of Java

Simple

 Object-Oriented

Portable

Platform independent

Secured

Robust

Architecture neutral

Interpreted

High Performance

Multithreaded

Distributed

Dynamic

Token

Smallest individual unit in a java program is known as Token.

1.keywords(Reserved Words)

2.Identifiers (variables)

3.Literals(Constant)

4.Operators

5.Symbols

 

1. Java Keywords

Java keywords are also known as reserved words. Keywords are particular words which acts as a key to a code. These are predefined words by Java so it cannot be used as a variable or object name.

 

eg: abstract, assert (since Java 1.4), Boolean, break, byte, case, catch, char, class, const (not used), continue, default, do, double, else, enum (since Java 5.0), extends, final, finally, float, for, goto (not used), if, implements, import, instanceof, int, interface, long, native, new, package, private, protected, public, return, short, static, strictfp (since Java 1.2), super, switch, synchronized, this, throw, throws, transient, try, void, volatile, while

 

2. Java Variables

 

A variable is a container which holds the value while the Java program is executed. A variable is assigned with a data type.

Variable is a name of memory location. There are three types of variables in java: local, instance and static.

There are two types of data types in Java: primitive and non-primitive.

 

Variable is name of reserved area allocated in memory. In other words, it is a name of memory location. It is a combination of "vary + able" that means its value can be changed.

 

3. Constants:  Cannot alter its value during execution of program.

 

4. Operators :Operator in Java is a symbol which is used to perform operations.

 

There are many types of operators in Java

 

Unary Operator,

Arithmetic Operator,

Shift Operator,

Relational Operator,

Bitwise Operator,

Logical Operator,

Ternary Operator and

Assignment Operator.

 

5. Punchuators: Symbols

 

, ; : ( ) [ ] { }

public class first {

 

public static void main(String[] args) {

// TODO Auto-generated method stub

System.out.println("WELCOME \nTO \nTECHZ MATRIX");

}

 

}

 public class Second {

 public static void main(String[] args) {

// TODO Auto-generated method stub

int a=10; 

int b=5; 

int c=a+b;  

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

} 

}

 

public class Third {

 public static void main(String[] args) {

// TODO Auto-generated method stub

int a=10; 

int b=5;

int c=2;

 int d=a*b*c;

 System.out.prig=a%b;

System.out.println("sum="+d);

System.out.println("Difference="+c);

System.out.println("Product="+e);

System.out.println("Qstem.out.println("Reminder="+g);

}

 

}

 

 ASSIGNMENT OPERATORS

+=

-=

*=

/=

Program:

public class Assignment_Operators {

 

public static void main(String[] args) {

// TODO Auto-generated method stub

int a=10;

 

int b=20;

        int c=5;

        c+=2;

        int d=20;

        d-=5;

        a*=4;

 

b/=2;

       int e=21;

       e%=2;

System.out.println(a);

System.out.println(c);

System.out.println(b);

System.out.println(d);

System.out.println(e);

}

 

}

 

INCREMENT OPERATOR

++

 

Program: public class Increment_Operator {

 

public static void main(String[] args) {

// TODO Auto-generated method stub

int x=10;

        x++;

System.out.println(x);

}

 

}

 

DECREMENT OPERATOR

--

Program:

public class Decrement_Operator {

 

public static void main(String[] args) {

// TODO Auto-generated method stub

int x=10;

        x--;

System.out.println(x);

 

}

 

}

 

UNARY OPERATORS

 

~ (Tilde)

! (NOT)

 

Program:

public class Unary_Operators {

 

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);

}

 

}

 

LOGICAL OPERATORS

 

&& (and)

|| (or)

Program:

public class Logical_Operators {

 

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);

}

 

}

 

RELATIONAL OPERATORS

 

<

>

<=

>=

==

!=

Control Structures

1.IF

Syntax:

if(condition)

{

stmts;

}

2.IF ELSE

Syntax:

if(condition)

{

stmts;

}

else

{

stmts;

}

Program:

public class prog_ifelse {

 

public static void main(String[] args) {

// TODO Auto-generated method stub

int age=10;

 

if (age>=18)

 

{

 

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

 

}

 

else

 

{

 

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

 

}

}

 

}

public class Positive_Negative_conversion {

 

public static void main(String[] args) {

// TODO Auto-generated method stub

int a=-5;

if(a>=0) {

System.out.println("Positive");

}

else

{

System.out.println("Negative");

}

}

 

}

public class Odd_Even {

 

public static void main(String[] args) {

// TODO Auto-generated method stub

int a=10;

if(a%2==0)

{

System.out.println("Number is even");

}

else

{

System.out.println("Number is odd");

}

}

 

}

 

public class Leap_year {

 

public static void main(String[] args) {

// TODO Auto-generated method stub

int a=2020;

if(a%4==0)

{

System.out.println("Leap Year");

}

else

{

System.out.println("Not a leap year");

}

}

 

}

 

public class Biggest_of_Two_numbers {

 

public static void main(String[] args) {

// TODO Auto-generated method stub

int a=1,b=5;

if(a<b)

{

System.out.println(b+ " is Bigger");

}

else

{

System.out.println(a+ " is Bigger");

}

}

 

}

 

IF ELSE IF

if(condition)

{

stmts;

}

else if(condition)

{

stmts;

}

else if(condition)

{

stmts;

}

.

.

...

else

{

stmts;

}

public class prog_if_else_if {

 

public static void main(String[] args) {

// TODO Auto-generated method stub

int y=4;

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");

}}

}

 

 

SWITCH CASE

Syntax:

switch(optional variable)

{

case option1:

stmt;

break;

case option2:

stmt;

break;

::

:

:

default:

stmt;

break;

}

 

 

public class Switch_Case {

 

public static void main(String[] args) {

// TODO Auto-generated method stub

int y=0;

switch(y)

{

case 1:

{

System.out.println("sunday");

break;

}

case 2:

{

System.out.println("monday");

break;

}

case 3:

{

System.out.println("tuesday");

break;

}

case 4:

{

System.out.println("wednesday");

break;

}

case 5:

{

System.out.println("thursday");

break;

}

case 6:

{

System.out.println("friday");

break;

}

case 7:

{

System.out.println("saturday");

break;

}

default:

{

System.out.println("Invalid Option");

break;

}

}

}

 

}

Check the alphabet is vowel or not

 

public class Vowel_or_not {

 

public static void main(String[] args) {

// TODO Auto-generated method stub

char x='A';

switch(x)

{

case 'a':

case 'A':

{

System.out.println("Vowel");

break;

}

case 'e':

case 'E':

{

System.out.println("Vowel");

break;

}

 

case 'i':

case 'I':

{

System.out.println("Vowel");

break;

}

case 'o':

case 'O':

{

System.out.println("Vowel");

break;

}

case 'u':

case 'U':

{

System.out.println("Vowel");

break;

}

 

default:

{

System.out.println("Not Vowel");

break;

}

}

}

 

}

public class Vowel_or_not {

 

public static void main(String[] args) {

// TODO Auto-generated method stub

char x='A';

switch(x)

{

case 'a':

case 'A':

case 'e':

case 'E':

case 'i':

case 'I':

case 'o':

case 'O':

case 'u':

case 'U':

{

System.out.println("Vowel");

break;

}

 

default:

{

System.out.println("Not Vowel");

break;

}

}

}

 

}

 

public class Or_Example {

 

public static void main(String[] args) {

// TODO Auto-generated method stub

char x='e';

if((x=='a')||(x=='e')||(x=='i')||(x=='o')||(x=='u')||(x=='A')||(x=='E')||(x=='I')||(x=='O')||(x=='U'))

{

System.out.println("Vowel");

}

else

{

System.out.println("Not Vowel");

}

}

 

}


java.util is a package, while Scanner is a class of the java.util package.

 To use the Scanner class, create an object of the class and use any of the available methods found in the Scanner class documentation. In our example, we will use the nextLine() method, which is used to read a complete line

PROGRAM:

import java.util.Scanner;

public class scannerprog {

 

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");

 

}

 

}

 

}

Output:

enter a number

12

It is an even number

 

 

Java Packages

A package in Java is used to group related classes.A java package is a group of similar types of classes, interfaces and sub-packages.

Package in java can be categorized in two form, built-in package and user-defined package.

There are many built-n packages such as java, lang, awt, javax, swing, net, io, util, sql etc.


import java.util.Scanner;

 

public class scannerpositivenegative {

 

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>=0)

 

{

 

System.out.println("It is Positive number");

 

}

 

else {

 

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

 

}

 

}

 

}

 Advantage of Java Package

1) Java package is used to categorize the classes and interfaces so that they can be easily maintained.

 

2) Java package provides access protection.

 

3) Java package removes naming collision.

 

Packages are divided into two categories:

 

Built-in Packages (packages from the Java API)

User-defined Packages (create your own packages)

Built-in Packages

 

These packages consist of a large number of classes which are a part of Java API.Some of the commonly used built-in packages are:

1) java.lang: Contains language support classes(e.g classed which defines primitive data types, math operations). This package is automatically imported.

2)  java.io: Contains classed for supporting input / output operations.

3)  java.util: Contains utility classes which implement data structures like Linked List, Dictionary and support ; for Date / Time operations.

4)  java.applet: Contains classes for creating Applets.

5)  java.awt: Contain classes for implementing the components for graphical user interfaces (like button , ;menus etc).

6)  java.net: Contain classes for supporting networking operations.

 

import java.util.Scanner;

 

public class stringreading {

 

public static void main(String[] args) {

// TODO Auto-generated method stub

Scanner input = new Scanner(System.in);

     System.out.print("Enter your name: ");

         // Takes input from the keyboard

         String name = input.nextLine();

         // Prints name

         System.out.println("My name is " + name);

}

 }

 Output:

Enter your name: asha

My name is asha

import java.util.Scanner;

 public class sum_using_scanner {

 public static void main(String[] args) {

// TODO Auto-generated method stub

int num1, num2, sum;

        Scanner sc = new Scanner(System.in);

        System.out.println("Enter First Number: ");

        num1 = sc.nextInt();

    System.out.println("Enter Second Number: ");

        num2 = sc.nextInt();

       sc.close();

        sum = num1 + num2;

        System.out.println("Sum of these numbers: "+sum);

} 

}

 

Program:

import java.util.Scanner;

 

public class double_value_using_scanner {

 

public static void main(String[] args) {

// TODO Auto-generated method stub

Scanner scanner = new Scanner(System.in);

   System.out.println("Enter the length of Rectangle:");

   double length = scanner.nextDouble();

   System.out.println("Enter the width of Rectangle:");

   double width = scanner.nextDouble();

   //Area = length*width;

   double area = length*width;

   System.out.println("Area of Rectangle is:"+area);

}

 

}

 Output:

Enter the length of Rectangle:

23.2

Enter the width of Rectangle:

12.3

Area of Rectangle is:285.36

Arrays

Arrays are used to store multiple values in a single variable, instead of declaring separate variables for each value. To declare an array, define the variable type with square brackets:

String[] cars;

public class array {

 

public static void main(String[] args) {

// TODO Auto-generated method stub

String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};

 

    System.out.println(cars[0]);

    System.out.println(cars[1]);

    System.out.println(cars[2]);

    System.out.println(cars[3]);

    System.out.println(cars.length);

}

 

}

Output:

Volvo

BMW

Ford

Mazda

4

LOOP:

Syntax: WHILE LOOP

while(condition)

{

stmts;

}

Program:

Program to print numbers upto 15

public class while_loop {

 

public static void main(String[] args) {

// TODO Auto-generated method stub

int i=1;

while(i<=15)

{

System.out.println(i);

i++;

}

}

 

}

 

Program to print number upto 10 in reverse order

public class while_loop {

 

public static void main(String[] args) {

// TODO Auto-generated method stub

int i=10;

while(i>=1)

{

System.out.println(i);

i--;

}

}

 

}

Program to print even numbers upto 20

public class while_loop {

 

public static void main(String[] args) {

// TODO Auto-generated method stub

int i=2;

while(i<=20)

{

System.out.println(i);

i+=2;

}

}

 

}

Program to print even numbers upto 20 in reverse order.

public class while_loop {

 

public static void main(String[] args) {

// TODO Auto-generated method stub

int i=20;

while(i>=2)

{

System.out.println(i);

i-=2;

}

}

 

}

Program to print odd numbers upto 10

public class while_loop {

 

public static void main(String[] args) {

// TODO Auto-generated method stub

int i=1;

while(i<=10)

{

System.out.println(i);

i+=2;

}

}

 

}

 

 Do While Loop

Syntax:

do

{

stmts;

}

while(conditions);

 

Write a program to print multiples of 7 less than 70

public class do_while {

 

public static void main(String[] args) {

// TODO Auto-generated method stub

int i=7;

 

do

 

{

 

System.out.println(i);

 

i+=7;

 

}

 

while(i<=50);

 

 

}

 

}

Program to print odd numbers upto 20

public class do_while {

 

public static void main(String[] args) {

// TODO Auto-generated method stub

int i=1;

 

do

 

{

 

System.out.println(i);

 

i+=2;

 

}

 

while(i<=20);

 

 

}

 

}

Program to print odd numbers upto 20 in reverse order

public class do_while {

 

public static void main(String[] args) {

// TODO Auto-generated method stub

int i=19;

 

do

 

{

 

System.out.println(i);

 

i-=2;

 

}

 

while(i>=1);

 

 

}

 

}

FOR LOOP

Syntax:

for(initialization;condition;inc/dec) { stmts; }

public class For_Loop {

 

public static void main(String[] args) {

// TODO Auto-generated method stub

for (int i = 0; i < 10; i++) {

        System.out.println(i);

}

}

 

}

public class For_Loop {

 

public static void main(String[] args) {

// TODO Auto-generated method stub

for (int i = 10; i > 0; i--) {

        System.out.println(i);

}

}

 

}

WAP even numbers upto 20

public class For_Loop {

 

public static void main(String[] args) {

// TODO Auto-generated method stub

for (int i = 0; i<=20; i+=2) {

        System.out.println(i);

}

}

}

Branching Statements(Jumping Statements)

1. Break

2. Continue

3. Return

 

1. BREAK

The break statement can also be used to jump out of a loop.

 

public class Break {

 

public static void main(String[] args) {

// TODO Auto-generated method stub

for (int i = 0; i < 10; i++) {

  if (i == 4) {

    break;

  }

  System.out.println(i);

}

}

 

}

2.CONTINUE

The continue statement breaks one iteration (in the loop), if a specified condition occurs, and continues with the next iteration in the loop.

 

public class continue_ {

 

public static void main(String[] args) {

// TODO Auto-generated method stub

for (int i = 0; i < 10; i++) {

  if (i == 4) {

    continue;

  }

  System.out.println(i);

}

}

 

}

3.RETURN

The return keyword in Java is used to exit from a method and optionally return a value. It is essential for methods that have a return type other than void.

Java methods-A method is a block of code which only runs when it is called. You can pass data, known as parameters, into a method. Methods are used to perform certain actions, and they are also known as functions.

 Why use methods?

 To reuse code: define the code once, and use it many times. Methods are mainly two types

1.Bulit in Methods

2.User Defined Methods

Built in Methods in Java Categories of Built in Methods

i) String Methods ii) Number Methods iii) Character Methods iv) Array Method

 

Java String Methods

1) compareTo() Method :-It compares two strings

ASCII Code(American Standard Code for Information Interchange)

 

ASCII Code for A=65

ASCII Code for a=97

ASCII Code for S=83

ASCII Code for s=115

Diff:= 115-83=32

public class Compare_to {

 

public static void main(String[] args) {

// TODO Auto-generated method stub

String str1 = "selenium";

String str2 = "SELENIUM";

String str3 = "seleniuma";

String str4 = "selenium";

 

System.out.println(str1.compareTo(str2));

System.out.println(str1.compareTo(str3));

System.out.println(str1.compareTo(str4));

 

}

 

}

Output:

32

-1

0

2.Equals Method

public class equals {

 

public static void main(String[] args) {

// TODO Auto-generated method stub

String str1 = "selenium";

String str2 = "SELENIUM";

String str3 = "seleniuma";

String str4 = "selenium";

System.out.println(str1.equals(str2));

System.out.println(str1.equals(str4));

}

 

}

 

2) concat() Method

(It concatenates two strings /Joins two strings)

public class concat {

 

public static void main(String[] args) {

// TODO Auto-generated method stub

String str1 = "Selenium";

String str2 = " Testing";

System.out.println(str1.concat(str2));

}

 

}

3) charAt() Method

 (Returns a character by index position)

public class char_at {

 

public static void main(String[] args) {

// TODO Auto-generated method stub

String str1 = "Selenium";

System.out.println(str1.charAt(1));

}

 

}

 

Output: e

Date: 30/01/2025

4)Equals Ignore Case Method

public class Equals {

public static void main(String[] args) {

// TODO Auto-generated method stub

String str1 = "SELENIUM";

String str2 = "selenium";

String str3 = "UFT";

System.out.println(str1.equalsIgnoreCase(str2));

System.out.println(str1.equalsIgnoreCase(str3));

}

}

Output:

true

false

5)toUpperCase () – Converts values to Upper case

public class touppercase {

public static void main(String[] args) {

// TODO Auto-generated method stub

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());

}

}

 

Output:

SELENIUM

SELENIUM

SELENIUM

SELENIUM123

 

 

7)toLowerCase() -Converts values to Lower case

public class to_lowercase {

public static void main(String[] args) {

// TODO Auto-generated method stub

String str1 = "SELENIUM";

String str2 = "selenium";

String str3 = "SELEnium";

String str4 = "selenium123";

System.out.println(str1.toLowerCase());

System.out.println(str2.toLowerCase());

System.out.println(str3.toLowerCase());

System.out.println(str4.toLowerCase());

}

}

Output:

selenium

selenium

selenium

selenium123

 

 

8) trim() Method

 (Removes spaces from both sides of a String)

public class trim {

public static void main(String[] args) {

// TODO Auto-generated method stub

String str1 = "  Selenium ";

System.out.println(str1);

System.out.println(str1.trim());

}

}

Output:

Selenium

Selenium

 

9)substring () Method

public class sub_string {

public static void main(String[] args) {

// TODO Auto-generated method stub

String str = "Welecome to Selenium Testing";

System.out.println(str.substring(12));

System.out.println(str.substring(21));

System.out.println(str.substring(12, 20));

System.out.println(str.substring(9, 11));

}

}

Output:

Selenium Testing

Testing

Selenium

to

 

10)endsWith() -Ends with specified suffix

public class ends_with {

public static void main(String[] args) {

// TODO Auto-generated method stub

String str = "Welcome to Selenium Testing";

System.out.println(str.endsWith("Selenium Testing"));

System.out.println(str.endsWith("Testing"));

System.out.println(str.endsWith("Selenium"));

}

}

output:

true

true

false

 

11)length()

 (returns string length)

public class Length {

public static void main(String[] args) {

// TODO Auto-generated method stub

String str = "Selenium Testing";

String str2 = "Selenium";

System.out.println(str.length());

System.out.println(str2.length());

}

}

output:

16

8

 

Number Methods

ii) Java Number Methods

1) compareTo() Method:

 

public class Compareto {

public static void main(String[] args) {

// TODO Auto-generated method stub

int x = 5;

Integer a =x;

System.out.println(a.compareTo(5));

System.out.println(a.compareTo(6));

System.out.println(a.compareTo(4));

}

}

output:

0

-1

1

 

2) equals() Method

public class equals_numbers {

public static void main(String[] args) {

// TODO Auto-generated method stub

int x = 5;

Integer a =x;

System.out.println(a.equals(5));

System.out.println(a.equals(6));

System.out.println(a.equals(4));

}

}

output:

true

false

false

 

3) abs() -Returns absolute value

public class abs {

public static void main(String[] args) {

// TODO Auto-generated method stub

double a =10.234;

double b =-10.784;

System.out.println(Math.abs(a));

System.out.println(Math.abs(b));

}

}

output:

10.234

10.784

 

4) round() -It rounds the value to nearest integer

public class round {

public static void main(String[] args) {

// TODO Auto-generated method stub

double a =10.234;

double b =-10.784;

double c =10.51;

System.out.println(Math.round(a));

System.out.println(Math.round(b));

System.out.println(Math.round(c));

}

}

output:

10

-11

11

 

5) min() – Returns minimum value between two numbers

public class min_numbers {

public static void main(String[] args) {

// TODO Auto-generated method stub

int a=10, b=20;

double c =10.234, d =10.345;

System.out.println(Math.min(a, b));

System.out.println(Math.min(c, d));

System.out.println(Math.min(7, 9));

System.out.println(Math.min(1.23, 1.234));

}

}

output:

10

10.234

7

1.23

 

6) max()-Returns maximum value between two numbers

public class max_numbers {

public static void main(String[] args) {

// TODO Auto-generated method stub

int a=10, b=20;

double c =10.234, d =10.345;

System.out.println(Math.max(a, b));

System.out.println(Math.max(c, d));

System.out.println(Math.max(7, 9));

System.out.println(Math.max(1.23, 1.234));

}

}

output:

20

10.345

9

1.234

 

Java Character Methods

1) isLetter() – Checks whether the value is Alphabetic or not?

public class alphabet_is {

public static void main(String[] args) {

// TODO Auto-generated method stub

char a ='A';

char b ='1';

System.out.println(Character.isLetter(a));

System.out.println(Character.isLetter(b));

System.out.println(Character.isLetter('Z'));

System.out.println(Character.isLetter('1'));

System.out.println(Character.isLetter('*'));

}

}

 

output:

true

false

true

false

false

 

2) isDigit() -Checks weather the value is Number or not?

public class Is_Digit {

public static void main(String[] args) {

// TODO Auto-generated method stub

char a ='A';

char b ='1';

System.out.println(Character.isDigit(a));

System.out.println(Character.isDigit(b));

System.out.println(Character.isDigit('Z'));

System.out.println(Character.isDigit('1'));

System.out.println(Character.isDigit('*'));

}

}

output:

false

true

false

true

false

 

3) isUpperCase() – Checks weather the value is Upper case or not?

public class Is_Uppercase {

public static void main(String[] args) {

// TODO Auto-generated method stub

char a ='A';

char b ='z';

char c ='1';

System.out.println(Character.isUpperCase(a));

System.out.println(Character.isUpperCase(b));

System.out.println(Character.isUpperCase(c));

}

}

 

output:

true

false

false

 

4) isLowerCase()-Checks weather the value is Lower case or not?

public class is_lowercase {

public static void main(String[] args) {

// TODO Auto-generated method stub

char a ='A';

char b ='z';

char c ='1';

System.out.println(Character.isLowerCase(a));

System.out.println(Character.isLowerCase(b));

System.out.println(Character.isLowerCase(c));

}

}

output:

false

true

false

 

 

Java Array Methods

Array: Array is a collection of objects of same data type referred by common name.

1) length -It returns length of the Array.

public class length_array {

public static void main(String[] args) {

// TODO Auto-generated method stub

int [] array1 = {10, 20, 30, 40};

System.out.println(array1.length);

}

}

 

output:

4

2) toString() -It prints an Array.

import java.util.Arrays;

public class to_String {

public static void main(String[] args) {

// TODO Auto-generated method stub

String [] array1 = {"Selenium", "UFT", "LoadRunner", "RFT"};

String str = Arrays.toString(array1);

System.out.println(str);

}

}

 

output:

[Selenium, UFT, LoadRunner, RFT]

 

3) contains() – Checks if the Array contains certain value or not?

import java.util.Arrays;

public class contains_array {

public static void main(String[] args) {

// TODO Auto-generated method stub

String [] array1 = {"Selenium", "UFT", "LoadRunner", "RFT"};

boolean a = Arrays.asList(array1).contains("UFT");

boolean b = Arrays.asList(array1).contains("Java");

System.out.println(a);

System.out.println(b);

}

}

output:

true

false

 

DATE:31/1/2025

User Defined Methods

public class USER_DEFINED {

static void myMethod() {

    System.out.println("I just got executed!");

    }

public static void main(String[] args) {

// TODO Auto-generated method stub

myMethod();

myMethod();

}

}

OUTPUT:

I just got executed!

I just got executed!

 

public class Methods {

static int myMethod(int x) {

    return 5 + x;

  }

public static void main(String[] args) {

// TODO Auto-generated method stub

System.out.println(myMethod(2));

System.out.println(myMethod(10));

}

}

output:

7

15

 

public class method2 {

public static void main(String[] args) {

// TODO Auto-generated method stub

 myMethod("Asha",30);

 myMethod("Iniyah",4);

}

static void myMethod(String fname,int age) {

    System.out.println(fname + " is "+  age);

}

}

Output:

Asha is 30

Iniyah is 4

 

 

public class method3 {

public static void main(String[] args) {

// TODO Auto-generated method stub

System.out.println(mymethod(2,3,5));

}

static int mymethod(int x,int y,int z) {

return x*y*z;

}

}

Output:

30

 

import java.util.Scanner;

public class method3 {

public static void main(String[] args) {

// TODO Auto-generated method stub

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));

}

static int mymethod(int x,int y,int z) {

return x*y*z;

}

}

 

OUTPUT:

Enter 3 numbers

1

5

2

10

import java.util.Scanner;

public class ODD_OR_EVEN_FUNCTION {

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");

}

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();

mymethod (x);

}

}

 

Output:

Enter the number

10

num is an even number

 

Program to calculate addition,subtraction, multiplication and division of two numbers using user defined functions?

import java.util.Scanner;

public class calculator {

static int mymethod(int x,int y) {

return x+y;

}

static int mymethoddiff(int x,int y) {

return x-y;

}

static int mymethodproduct(int x,int y) {

return x*y;

}

static int mymethoddivi(int x,int y) {

return x/y;

}

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("Sum=" +mymethod(x,y));

System.out.println("Difference=" +mymethoddiff(x,y));

System.out.println("Product=" +mymethodproduct(x,y));

System.out.println("Quotient=" +mymethoddivi(x,y));

}

}

 

 

output:

20

5

Sum=25

Difference=15

Product=100

Quotient=4

 

CONSTRUCTOR

It is an member function that invoke automatically when the object is created.

Constructor name and class name is always same.

Types of constructors:

1)Default Constructor

2)Parameterized Constructor

1)Default Constructor

Constructor with no parameters is called default constructor.

Eg:

public class Main {

int x;

public Main() {

   x = 5;

 }

public static void main(String[] args) {

// TODO Auto-generated method stub

Main myObj = new Main();

    System.out.println(myObj.x);

}

}

 

Output:

5

public class Main {

int x,y;

public Main() {

   x = 5;

   y=2;

 }

public static void main(String[] args) {

// TODO Auto-generated method stub

Main myObj = new Main();

    System.out.println(myObj.x+myObj.y);

}

}

 

Output:

7

2
)Parameterized Constructors

Constructor with parameters is called parameterized constructors

Eg:

public class Parameterized {

int x;

public Parameterized(int y) {

   x = y;

 }

public static void main(String[] args) {

// TODO Auto-generated method stub

Parameterized myObj = new Parameterized(10);

    System.out.println(myObj.x);

}

}

 

Output:

10

Date:03/02/2025

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.

 

 

 

 

ARITHMETIC EXCEPTION

public class exception1 {

public static void main(String[] args) {

// TODO Auto-generated method stub

try{

      int data=100/0;

   }catch(ArithmeticException e){System.out.println(e);}

   System.out.println("rest of the code...");

  }

}

 

Output:

java.lang.ArithmeticException: / by zero

rest of the code...

 

 

ARRAY INDEX OUT OF BOUND EXCEPTION

public class Array_exception {

public static void main(String[] args) {

// TODO Auto-generated method stub

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");

}

}

 

Output:

java.lang.ArrayIndexOutOfBoundsException: Index 10 out of bounds for length 4

rest of the code

 

OOPS(Object Oriented Programming Language)

Java is a object oriented programming language

The followings are the features of OOPs

1.Class

2.Object

3.Abstraction

4.Encapsulation

5.Inheritance

6.Polymorphism

1.Class

Class is a blue print of a program

Class is used to create object. A class is a group of similar objects

A Class in Java can contain:

Data member

Method

Constructor

Nested Class

Interface

 

Class Declarations

Syntax:

The keyword ‘class’ is used to define a class.

 

 

access_modifier class <class_name>

{      

 variable declarations;

method declarations

nested class;     

 }

 

Program:

public class student {

int id=10;

String name="Asha";

public static void main(String[] args) {

// TODO Auto-generated method stub

student s1=new student();

System.out.println(s1.id);

System.out.println(s1.name);

}

}

 

output:

10

Asha

 

 

2. Object:

Objects are the instances of a class that are created to use the attributes and methods of a class.

Syntax:

The keyword new is used to create an object.

Class_name object_name= new class_name();

 

Program:

public class oops_object {

int a=10;

int b=20;

public static void main(String[] args) {

// TODO Auto-generated method stub

oops_object obj=new oops_object();

System.out.println(obj.a+obj.b);

}

}

 

Output:

30

 

5.Inheritance

Inheritance in Java is a mechanism in which one object acquires all the properties and behaviors of a parent object.The main advantage of inheritance is code resuability.

Sub Class/Child Class: Subclass is a class which inherits the other class. It is also called a derived class, extended class, or child class.

Super Class/Parent Class: Superclass is the class from where a subclass inherits the features. It is also called a base class or a parent class.

Advantages Of Inheritance in Java:

Code Reusability: Inheritance allows for code reuse and reduces the amount of code that needs to be written. The subclass can reuse the properties and methods of the superclass, reducing duplication of code.

Abstraction: Inheritance allows for the creation of abstract classes that define a common interface for a group of related classes. This promotes abstraction and encapsulation, making the code easier to maintain and extend.

Class Hierarchy: Inheritance allows for the creation of a class hierarchy, which can be used to model real-world objects and their relationships.

Polymorphism: Inheritance allows for polymorphism, which is the ability of an object to take on multiple forms. Subclasses can override the methods of the superclass, which allows them to change their behavior in different ways.

 

 

Syntax:

class Subclass-name extends Superclass-name  

{  

   //methods and fields  

}

The keyword extends used to inherit.

 

 

a)Single Inheritance

class Employee{ 

float salary=40000; 

}

public class programmer extends Employee{

int bonus=5000;

public static void main(String[] args) {

// TODO Auto-generated method stub

programmer pg=new programmer();

System.out.println(pg.salary+pg.bonus);

}

}

 

output:

45000.0

b)Multi level Inheritance

class animal

{

public void eat()

{

System.out.println("Eating");

}

}

class dog extends animal

{

public void bark()

{

System.out.println("Barking");

}

}

public class puppy extends dog {

public void play()

{

System.out.println("Playing");

}

public static void main(String[] args) {

// TODO Auto-generated method stub

puppy pu=new puppy();

pu.bark();

pu.eat();

pu.play();

}

}

 

output:

Barking

Eating

Playing

 

Date:05/02/2025

Hierarchical Inheritance

 

Program:

class vehicle

{

public void run()

{

System.out.println("Running");

}

}

class car extends vehicle

{

public void drive()

{

System.out.println("Driving");

}

}

class bus extends vehicle

{

public void beep()

{

System.out.println("Beeping");

}

}

class scooter extends vehicle

{

public void horn()

{

System.out.println("Horns...");

}

}

public class hierarchical_inheritance {

public static void main(String[] args) {

// TODO Auto-generated method stub

car ob=new car();

ob.drive();

ob.run();

bus obj=new bus();

obj.beep();

obj.run();

scooter objj=new scooter();

objj.horn();

objj.run();

}

}

 

output:

Driving

Running

Beeping

Running

Horns...

Running

 

 

Hybrid Inheritance

Program:

class mother

{

public void cook()

{

System.out.println("Cooks Food");

}

}

class girl extends mother

{

public void play()

{

System.out.println("Play hockey");

}

}

class babygirl extends girl

{

public void help()

{

 System.out.println("Help Mother");

}

}

class boy extends mother

{

public void sing()

{

System.out.println("Sing songs..");

}

}

class babyboy extends boy

{

public void sleep() {

System.out.println("Sleep with father");

}

}

public class hybrid_inheritance {

public static void main(String[] args) {

// TODO Auto-generated method stub

babyboy bb=new babyboy();

bb.sleep();

bb.sing();

bb.cook();

babygirl bg=new babygirl();

bg.help();

bg.play();

bg.cook();

}

}

 

output:

Sleep with father

Sing songs..

Cooks Food

Help Mother

Play hockey

Cooks Food

 

Multiple Inheritance:

Java doesn't support multiple inheritance(Interface)

Abstraction

● It is a concept of simplifying a real world object into its essential features without including background details.

● The keyword abstract is used to abstract class

● It can have abstract and non-abstract methods.

● It cannot be instantiated.

● It can have constructors and static methods also.

● It can have final methods which will force the subclass not to change the body of the method.

Program:

abstract class bike{

  abstract void run();

}

public class Abstraction extends bike {

void run(){System.out.println("running safely..");}

public static void main(String[] args) {

// TODO Auto-generated method stub

bike obj = new Abstraction();

  obj.run();

}

}

 

output:

running safely..

 

Program:

abstract class Shape{

abstract void draw();

}

//In real scenario, implementation is provided by others i.e. unknown by end user

class Rectangle extends Shape{

void draw(){System.out.println("drawing rectangle");}

}

class Circle1 extends Shape{

void draw(){System.out.println("drawing circle");}

}

public class Abstraction2 {

public static void main(String[] args) {

// TODO Auto-generated method stub

Shape s=new Circle1();

s.draw();

Shape s1=new Rectangle();

s1.draw();

}

}

output:

drawing circle

drawing rectangle

 

program:

abstract class Animal {

 // Abstract method (does not have a body)

 public abstract void animalSound();

 // Regular method

 public void sleep() {

   System.out.println("Zzz");

 }

}

// Subclass (inherit from Animal)

class Pig extends Animal {

 public void animalSound() {

   // The body of animalSound() is provided here

   System.out.println("The pig says: wee wee");

 }

}

public class abstraction3 {

public static void main(String[] args) {

// TODO Auto-generated method stub

Pig myPig = new Pig(); // Create a Pig object

    myPig.animalSound();

    myPig.sleep();

}

}

output:

The pig says: wee wee

Zzz

 

 

POLYMORPHISM

The word ‘polymorphism’ means ‘having many forms’. In Java, polymorphism refers to the ability of a message to be displayed in more than one form.

1.Method Overloading(Compile time polymorphism)

2.Method overriding(Run time polymorphism)

1.Method Overloading:

Same Function name with different parameters

Program:

class Helper {

   // Method with 2 integer parameters

   static int Multiply(int a, int b)

   {

       // Returns product of integer numbers

       return a * b;

   }

   // Method 2

   // With same name but with 2 double parameters

   static double Multiply(double a, double b)

   {

       // Returns product of double numbers

       return a * b;

   }

}

public class method_overloading {

public static void main(String[] args) {

// TODO Auto-generated method stub

System.out.println(Helper.Multiply(2, 4));

       System.out.println(Helper.Multiply(5.5, 6.3));

}

}

 

Output:

8

34.65

 

DATE:06/12/2025

2.Method Overriding

● Overriding in Java occurs when a subclass implements a method which is already defined in the superclass or Base Class.

● The method in the subclass must have the same signature as in the superclass. It allows the subclass to modify the inherited methods.

Program:

class Animals {

   // Base class

   void move()

   { System.out.println(

     "Animal is moving."); }

   void eat()

   { System.out.println(

     "Animal is eating."); }

}

class Dog extends Animals {

   @Override void move()

   { // move method from Base class is overriden in this

     // method

       System.out.println("Dog is running.");

   }

   void bark() { System.out.println("Dog is barking."); }

}

public class Method_overriding {

public static void main(String[] args) {

// TODO Auto-generated method stub

Dog d = new Dog();

       d.move();

       d.eat();

       d.bark();

}

}

 

output:

Dog is running.

Animal is eating.

Dog is barking.

 

Encapsulation(Data Hiding)

● Wrapping up of data into a single unit is known as encapsulation.

Access Specifiers

1.Private

2.Public

            3.Protected

private keyword

A Java private keyword is an access modifier. It can be assigned to variables, methods, and inner classes. It is the most restricted type of access modifier.

Program:

class A 

{ 

private String msg="Try to access the private variable outside the class"; 

     

}

public class Private_access {

public static void main(String[] args) {

// TODO Auto-generated method stub

A a=new A(); 

   System.out.println(a.msg);

}

}

 

output:

Exception in thread "main" java.lang.Error: Unresolved compilation problem:

The field A.msg is not visible

at Private_access.main(Private_access.java:11)

 

public:

The public keyword is an access modifier used for classes, attributes, methods and constructors, making them accessible by any other class.

Program:

class A 

{ 

public String msg="Try to access the private variable outside the class"; 

     

}

public class Private_access {

public static void main(String[] args) {

// TODO Auto-generated method stub

A a=new A(); 

   System.out.println(a.msg);

}

}

 

output:

Try to access the private variable outside the class

 

Program:

class Mainn {

 public String fname = "John";

 public String lname = "Doe";

 public String email = "john@doe.com";

 public int age = 24;

}

public class public_1 {

public static void main(String[] args) {

// TODO Auto-generated method stub

Mainn myObj = new Mainn();

    System.out.println("Name: " + myObj.fname + " " + myObj.lname);

    System.out.println("Email: " + myObj.email);

    System.out.println("Age: " + myObj.age);

}

}

 

output:

Name: John Doe

Email: john@doe.com

Age: 24

 

 

PROTECTED

The protected keyword is an access modifier used for attributes, methods and constructors, making them accessible in the same package and subclasses.

Program:

class Person {

 protected String fname = "John";

 protected String lname = "Doe";

 protected String email = "john@doe.com";

 protected int age = 24;

}

public class Protected extends Person {

public static void main(String[] args) {

// TODO Auto-generated method stub

Protected myObj = new Protected();

    System.out.println("Name: " + myObj.fname + " " + myObj.lname);

    System.out.println("Email: " + myObj.email);

    System.out.println("Age: " + myObj.age);

}

}

 

output:

Name: John Doe

Email: john@doe.com

Age: 24

 

Interface

The interface keyword is used to declare a special type of class that only contains abstract methods.

To access the interface methods, the interface must be "implemented" by another class with the implements keyword (instead of extends).

Program:

interface Animall {

 public void animalSound(); // interface method (does not have a body)

 public void sleep(); // interface method (does not have a body)

}

// Pig "implements" the Animal interface

class Pig1 implements Animall {

 public void animalSound() {

   // The body of animalSound() is provided here

   System.out.println("The pig says: wee wee");

 }

 public void sleep() {

   // The body of sleep() is provided here

   System.out.println("good night...");

 }

}

public class interfacee {

public static void main(String[] args) {

// TODO Auto-generated method stub

Pig1 myPig = new Pig1();  // Create a Pig object

    myPig.animalSound();

    myPig.sleep();

}

}

 

output:

The pig says: wee wee

good night...

 

Program:

interface FirstInterface {

 public void myMethod(); // interface method

}

interface SecondInterface {

 public void myOtherMethod(); // interface method

}

// DemoClass "implements" FirstInterface and SecondInterface

class DemoClass implements FirstInterface, SecondInterface {

 public void myMethod() {

   System.out.println("Some text..");

 }

 public void myOtherMethod() {

   System.out.println("Some other text...");aaass

 }

}

public class interface1 {

public static void main(String[] args) {

// TODO Auto-generated method stub

DemoClass myObj = new DemoClass();

    myObj.myMethod();

    myObj.myOtherMethod();

}

}

 

output:

Some text..

Some other text...

 

 

No comments:

Post a Comment

Techzmatrix