Methods in java

 Methods:

  • A method is a set of instructions that are used to perform specified tasks that repeatedly occur in the main program.
  • It helps to avoid duplication of work and also breaks down a large program into a number of small functions.
  • It is used to divide the complex task into a manageable task.

Syntax:

  1. return_type method_name (parameter-list)

    {

    .......................

    .......................

    }

Where,

  • Return_type – Data types.
  • Method_name – User-defined name.
  • Parameter-list – Parsing variables into methods.

Types of methods:

  • There are three types of methods used in java.

1. Methods that do not return anything and without any parameters.

2. Methods with parameters passed to them and returning.

3. Methods with parameters and that are returning data.

1. Java program without parameters and not returning data:

  1. package exampleprogram;

    import java.util.Scanner;

    public class Method_Example {

        int a,b;//Global variable

        void sum()

        {

            int c;//Local variable

            c=a+b;

            System.out.println("The sum of two element is :"+c);

        }

    }

     

    class main_method

    {

     public static void main (String args[]){

        Method_Example obj=new Method_Example();

        Scanner sc = new Scanner(System.in);

        System.out.println("Enter two numbers : ");

        obj.a = sc.nextInt();

        obj.b = sc.nextInt();

        obj.sum();

        }

    }

Output:

2. Java program with parameters and not returning data:

  1. package exampleprogram;

    public class Method_Example {

        void sum(int a, int b) {

            int c;

            c = a + b;

            System.out.println("The sum of two element is :" + c);

        }

        public static void main(String args[]) {

            Method_Example obj = new Method_Example();

            obj.sum(10, 20);

        }

    }

Output:

3. Java program with parameters and returning data:

  1. package exampleprogram;

    public class Method_Example {

        int sum(int a, int b) {

            int c;

            c = a + b;

            return c;

        }

        public static void main(String args[]) {

            Method_Example obj = new Method_Example();

            int c = obj.sum(10, 20);

            System.out.println("The sum of two element is :" + c);

        }

    }

Output:
Share:

Query to insert more than one record at a time in SQL

Table creation:

Table name: Customers

  1. CREATE TABLE customers ( customer_no NUMBER,

                            customer_name VARCHAR2(100));

Insertion:

  1. INSERT ALL

          INTO customers VALUES (2000, 'Sharma')

          INTO customers VALUES (2001, 'Saniya')

          INTO customers VALUES (2000, 'Mohan')

       SELECTFROM DUAL;

Output:



 

 


Share:

Class and Object in Java

Introductions of Class and Object:

Class:
  • Class is used to hold data members and member function i.e. variables and functions.
  • It can be easily accessed anywhere and used to reduce the reusability of the code.
For example,
  • Let’s take student,
  • Student properties are all called variables and student performance or student-related calculations(i.e. student result based on total and it may be pass or fail) is called as a function.
Where,
The student is a class.
Student properties (variables) : Roll_num,name,age,ect.
Student behaviours(function)  : total,average,result,fees details ect.
Note:
  • If one or more variables declared within the class, called data members.
  • If one or more functions declared within the class, called member function.
Object:
  • An object is used to access the members of the class.
  • It simply said as the short name of the class.
Syntax:
  1. <import packages>
    Access_specifiers class class_name //class declaration
    {
              Variable_declaration;
              Method declaration or function_declaration()
              {
              //Statements
              }
    public static void main(String[] args)
    {
    //object declaration
    class_name obj_name new=class_name();
    //accessing member function using object
    obj_name.function_name();
    }
  2. }
Note:
The class name and object name is a user-defined word.
Access specifiers:
  • Java access specifiers are used for determining or setting the boundary for the availability of class members (data members and member functions) beyond that class.
Types of access specifiers:
  • Private – It is used to access the data member and member function within the classes.
  • Public – It is used to access the data member and member function out of the class. That means any class can be accessible.
  • Protected – It members cannot be accessed from outside the class, however, they can be accessed in inherited classes.

Example:
  1.  package oops_examples;

    import java.util.Scanner;

    public class Class_and_obj_Example_program {

        int sno, m1, m2, m3, m4, m5, tot;

        String name;

        float avg;

        void get()//Method for value get from user

        {

            Scanner sc = new Scanner(System.in);

            System.out.println("Enter student number : ");

            sno = sc.nextInt();

            System.out.println("Enter student name : ");

            name = sc.next();

            System.out.println("Enter Five subject mark : ");

            m1 = sc.nextInt();

            m2 = sc.nextInt();

            m3 = sc.nextInt();

            m4 = sc.nextInt();

            m5 = sc.nextInt();

        }

        void result()//Method for calculating total and average

        {

            tot = m1 + m2 + m3 + m4 + m5;

            avg = tot / 5;

        }

        void display()//Method for displaying full details

        {

            System.out.println("Student details............");

            System.out.println("Student id is : " + sno);

            System.out.println("Student name is : " + name);

            System.out.println("Five subject marks : \t" + m1 + "\t" + m2 + "\t" + m3 + "\t" + m4 + "\t" + m5);

            System.out.println("Total mark for five subject :" + tot);

            System.out.println("Average mark for five subject :" + avg);

        }

        public static void main(String[] args) {

            Class_and_obj_Example_program c = new Class_and_obj_Example_program();//object declaration

    //accessing member function using object

            c.get();

            c.result();

            c.display();

        }

    }

Output:
Share:

Constants in PL/SQL

Definition:

  • Constants are user defined identifier whose values remain unchanged throughout the program
  • Constants needs to be declared prior to their use like variables in PL/SQL
  • Constants can declared only in the declaration section in the PL/SQL block.

Syntax:

  1. Constant_name CONSTANT data_type(data_width) := value;

Explanation:

  •    Constant_name valid constant name (user defined)
  •    Constant_name followed CONSTANT keyword
  •    Data_type of the value
  •    := Assignment operator
  •    Value (constant value)

Note: You must initialize the constant at the DECLARATION section. You cannot initialize the constant anywhere else.

Example:

  1. DECLARE

       var_pi   CONSTANT NUMBER (7,6) := 3.1415;

    BEGIN

       DBMS_OUTPUT.put_line (var_pi);

    END;

Output:

Attributes of constants:

                 1.    Default

               2.    Not Null

Default:

Here, we are used DEFAULT keyword Instead of assignment operator (:=) for initializing the constant.

  1. DECLARE

       var_pi   CONSTANT NUMBER (7, 6) DEFAULT 3.1415;

    BEGIN

       DBMS_OUTPUT.put_line (var_pi);

    END;

Output:

Not Null:

       NOT NULL for preventing Null values.

  1. DECLARE

       var_pi   CONSTANT NUMBER (7, 6) NOT NULL DEFAULT 3.1415;

    BEGIN

       DBMS_OUTPUT.put_line (var_pi);

    END;

Output:


Example for Initialize constant in the BEGIN section:

  1. DECLARE

       var_pi   CONSTANT NUMBER (7, 6) ;

    BEGIN

       var_pi := 3.1415;

       DBMS_OUTPUT.put_line (var_pi);

    END;

Explanation:

  Above type of declaration and initialization is legally accepted in PL/SQL variable but comes to constant this is not accepted by the compiler.

The error report is,



 

 

Share:

Popular Posts

Search This Blog

Powered by Blogger.

Blog Archive

Service Support

Need our help to Learn or Post New Concepts Contact me

Blog Archive

Recent Posts

Service Support

Need our help to Learn or Post New Concepts Contact me