Java4teachus

Monday, August 27, 2018

Define a Java Program With Class Syntax

In previous post we have already learnt about on the Syntax of a class. But we would like known about the program of class syntax.

Q. Write a class definition for computing subtraction of two number?

program

class Sub
{
int a, b, c ;
void accept( )
{
a=40;
b=30;
}
void add( )
{
int c=a-b;
}
void  display( )
{
System.out.println(“value of a="+a);
System.out.println(“value of b=”+b);
System.out.println(“sub=”+c);
}
}

Q . Write a class definition for computing multiplication of  three number?.

program

class Multiplication
{
int a, b, c ,d  ;
void accept( )
{
a=30;
b=60;
c=30
}
void mul( )
{
d= a*b*c;
}
void  display( )
{
System.out.println(“value of a="+a);
System.out.println(“value of b=”+b);
System.out.println("value  of  c=" +d);
System.out.println(“Mul=”+c);
}
}

Q . Write a class definition for computing (a+b)2?

program

class Sqr
{
int a,b,c;
void accept()
{
a=10;
b=20;
}
void sqr()
{
c=a*a+b*b+2*a*b;
}
void display()
{
System.out.println(“value of a="+a);
System.out.println(“value of b="+b);
System.out.println(“square="=c);
}
}

Q . Write a class definition for computing a 2 ?

program

class Square
{
int a,b;
void accept()
{
a=10;
}
void square()
{
b=a*a;
}
void display()
{
System.out.println(“value of a="+a);
System.out.println(“square="+b);
}
}

Q . Write a class definition for computing area of triangle ?

program

class Triangle
{
int b,h;
float i;
void accept()
{
b=1;
h=2;
}
void triangle()
{
i=0.5*b*h;
}
void display()
{
System.out.println(“value of b="+b);
System.out.println(“value of h="+h);
System.out.println(“area of triangle="+i);
}
}

Q. Write a class definition for computing area of circle ?.

program

class AreaOfCircle
{
int r;
double pi,s;
void accept()
{
r=10;
pi=3.14;
}
void circle()
{
s=pi*r*r;
}
void display()
{
System.out.println(“value of r="+r);
System.out.println(“area of circle="+s);
}
}

Q. Write a class definition for computing area of Rectangle ?.

program


class AreaOfRectangle
{
int a,l,b;
void accept()
{
l=100;
b=200;
}
void areaOfRectangle()
{
a=l*b;
}
void display()
{
System.out.println(“value of l="+l);
System.out.println(“value of b="+b);
System.out.println(“areaofrectangle="+a);
}
}

Q. Write a class definition for computing the circumstance of circle ?.

program

class CircumferenceOfCircle
{
int r;
double pi,c;
void accept()
{
pi=3.14;
r=20;
}
void circumferenceOfCircle()
{
c=2*pi*r;
}
void display()
{
System.out.println("value of r="+r);
System.out.println("value of pi="+pi);
System.out.println("CircumferenceOfCircle="+c);
}
}

Q. Write a class definition for computing the Volume of  a cube?.

program

class VolumeOfCube
{

int b,h,v;

void accept()

{

b=1;

h=2;

}

void volumeOfCube()
{
v=b*h;
}
void display()
{
System.out.println("value of b="+b);
System.out.println("value of h="+h);
System.out.println("VolumeOfCube="+v);
}
}

Q .Write a java with class definition for computing sum of two number?

program

class Sum
{
int a, b, c ;
void accept()
{
a=10;
b=20;
}
void add( )
{
int c=a+b;
}
void  display( )
{
System.out.println (“value of a="+a);
System.out.println (“value of b=”+b);
System.out.println(“sum =”+c);
}
}

In the above program, Whenever we write the java program then we must have to define the class name by using the keyword called as "class".  Here the class name what we defined that is Sum

A logic of the above program is c= a+b. Here  a,b  are the two input variables which is used to store the values  and c is the output variable of a java program. 

accept(), add(), display() are the methods which is always shows Business Logic of a java program.

Thursday, August 23, 2018

Syntax Of a Class in Java

Definition Of Class


A class is collection of Data members where members can be defined as member variable and member methods within a Block of statements is called class. In java a class can be written by using the keyword called "Class". A class is related to properties and Fields is also known as Abstract data type.

Syntax Of a Class

   class  <ClassName>
   {
        Variable declaration (Or) Data members ;
        Methods definition ;
   }

➠ The members of a class can be public or public. In java, we cannot have global variables or stand alone functions. If a variable or a function has to be written, It must be a member of some or the other class. This is also reason java is a "Object oriented programming language".

➠ Method definition has four parts. They are:
  • The name of the Method( Method Name)
  • The type of the value the method return (type)
  • A list of parameters .
  •  The body of the method.                                                                 
In the below diagram shown as class name called Tree. But a Tree is one of the class name and different type of trees is called  "Object".

Different-type-of-classes-with-class-name-called-Mobile.png


In the above diagram shows class name called Mobile. But here Mobile is one of the class name and different type of Mobiles is called  "Object".

Different-type-of-classes-with-class-name-called-Tree.png

Explanation

  • Class is one of the keyword which is used for developing "Programmer Defined Data type" and to model the new world applications.
  • <class name> is one of the java valued  variable name treated as name of the class. In java if any  class contains either one word more than one word then words all first must be capital. This rule is also known as Hungarian rule in java.
               Example:-  class StudentInfo, class EmployeeInfo .
  • In java each and every name  is treated as  Programmer Defined Data Type.
  • Variable declaration represents set of data members which is selected from based on <class name>.  
  • Methods definition represents the set of methods meant for performing different type of operation. In java all the Methods must be defined inside the class only but not outside of a class.
  • In oops concepts we have 2 types of methods they are:    
           a) Member method             
           b) Non-Member method
  • A Member method is always available with in the scope of the class and it can access the values present in the data member.
  • A Non- member is not available within the scope of the class and they cannot access values present in the data member.
  • Java programmer permits only member method for providing high security and never permits non-member methods.
  • The definition of the class must be embedded within open bracket ( { ) and closing bracket ( } ). The definition of the class may are may not be terminated in semicolon ( ; ).
  • Hence all the class definition contains "logical existence" and acts of formula.
  • Define a class for representing student information.    
class Student
{
int std no :
String std name;
float M 1 ,M 2 ,M 3;
int getNoOfHoursStudy ( )
{
Return (3)
}
float getTotalMarks ( )
{
float total =m 1 +m 2 +m 3 ;
return total ;
}
String getGrade ( )
{
Return” distinction” ;
}
}

Saturday, August 18, 2018

Definition Of Class in Java

Purpose Of a Class

In previous post we already learnt about the OOPS Principles. Now  we discuss about on the first concept in oops principle called as "class".
  • The purpose of class concept  is used to  develop the  programmer defined data type and also to develop the  "REAL WORLD APPLICATIONS".
  • Each and every java program must be start with concept of class. Without class concept there is no java program.
  • The purpose of developing programmer defined data type is that to store "Multiple value of same " or " different type or both the types " .
  • To develop programmer defined data type with class concept, we use the keyword called "class".
  • Programatically each & every class name is treated as "programmer defined data type".

Definition :-

"A class is a collection of data member & method" is known as class.
                                                 (or)
"The process of  binding the data  members and associate methods in a single unit " is called class .
  • In java we must have  to define a class keyword. Because the memory space is not created for the Data Members and Methods. But whose memory space is created only when we create an object with respect to corresponding "class name" .
  • Data member of a class are also known as "Attributes (or) Fields (or) Properties ". Where as "Methods" are also called as "Behavior (Or) Accessories".
  • Programatically data members and methods are associated with in a class. But  without class name the memory space and  available as it is with object along with memory space.
In real world the definition of class expressed by class diagrams in the helps of xml software like rational rows, turbos annalist (IBM pro). Structure of a class diagram is shown below. 

Structure Of a  Class :-

Structure-Of-A-Class-in-Java.png
                                     
Q . Draw a class diagram  or storing student information ?

Diagram-of-a-class.png

Class : Class is a collection of Data Members where members can be members variables or members functions. Classes are used to prepare ADTs. In the above example, "Student" is the class.

Properties : The members of a class are called Properties Or Attributes . These are also called as "Data Members" . In the above diagram 'Number, Name, Marks' are properties.

Methods : The member function of a class can be called as "Methods". In the above diagram getNoOfHoursStudy(), getTotolMArks(), getGrade() are the methods or behavior. 

Monday, August 13, 2018

OOPS Principle's|Feature's|Concept's in Java

Object Oriented Programming Language

OOPS means "Object Oriented Programming Language" available as a part of Java language. It is one of the most important concept in java which is useful for developing any Software or Applications easily. Today the real software industry are using only OOPS concepts rather then POP concepts. Suppose  if any programming languages satisfies objected oriented principles then it is called OOPS.

Example:-   
  • Small Talk 
  • ADA                                               
  • Object pascal            
  • Object COBOL
  • C++
  • Java
  • .Net
  • Oracle 
  • Perl .
If we use Object Oriented Programming Language (Java) for development of  distributed application we get the following points.
  1. It Posses platform independent 
  2. The data is visiting between client & server side application in the form of hyper text.
  3.  It Provides high security to the data by default.
  4. The data is visiting between client & server side application in the form  of  Objects and leads to effective communication.
  5. The data is represented around the object only.
In the real industry if any programming language is Object Oriented Programming then it has to be  satisfy 8 principles. They are:- 
  1. Classes
  2. Object
  3. Data encapsulation 
  4. Data abstraction 
  5. Inheritance 
  6. Polymorphism
  7. Dynamic binding
  8. Message Passing

class:

  • The class is at the core of java. It is the logical code which is based upon on the entire java language is built because it defines the shape and nature of object.
  • So that, a class can forms the basis on object oriented programming in java.
  • The most important thing to understand that a class defines a new data types. After that can be used to create the objects of that type only.
  • A class is a template for an object, and object is an instance of a class. Thus a class is a collection of objects.
          Example:- Mango, Orange, Apple 

Object:

  • Objects are the basics on the run time entities in an object-oriented system. They may be represented anything a program may handle. 
  • Any programming language problem is analyzed in terms of objects and the nature of communication between them.
  • program objects should be chosen such that they match closely with the real-world objects. 

Data abstraction:


The wrapping up of data and methods into a single unit called class is also known as encapsulation. Data encapsulation is the important features of a class. The data is not accessible to outside world and only those methods, which are wrapped in the class then we can access it.

Encapsulation:


Data Encapsulation is the mechanism that combined to together the code and the data it manipulates, and keep the both safe and from outside interference and misuse. One way to think about encapsulation is a protective wrapper that prevents the code defined outside the wrapper.

Inheritance:


Inheritance is the process of which objects of one class acquire the object properties of another class. Inheritance supports the concept of hierarchical diagram. In oop, the concept of inheritance provides the ideas of runnability. This means that we can add the additional features to an existing class without modifying it. This is possible to deriving a new class from the existing one. The new class will have the combined features of both the classes.

polymorphism:


Polymorphism mean the ability that to take one or more than one form is called polymorphism. For Example : An operation may exhibit different types of behavior in different instances. The behavior depends upon on the type of data used in the operation.

Polymorphism plays an important role in allowing objects which is having different internal structures to shares the same external interface. Polymorphism is extensively used in implementing inheritance.

Dynamic binding:


Dynamic Binding refers to the linking of a produce call to the executed in response to the call. Dynamic binding means that the code associated with a given procedure call is not known until the time of call of run-time. It is associated with polymorphism and inheritances.

Messaging Passing:


An object - oriented programming language consists of a set of objects that communicate with each other. The process of programming in an object oriented language involves the following steps.
    1. It creates the class that define the objects and their behavior.
    2. Creating the objects from the class definitions.
    3. It establishing the communication among the objects.

    Sunday, August 12, 2018

    Introduction Of OOPS

    Introduction of OOPS  

    In the previous post we learnt about on the Method Overriding in java, At present we are learning about the OOPS  features in java.     

    In the real time coding to develop any project we need to choose languages called technology. It can satisfy either "Procedure Oriented principle or Object Oriented  Principles". In other words  we have 2 type of languages.
    1. Procedure Orientated Programming Language 
    2. Object Orientated Programming Language

    Procedure Oriented Programming Language 

    In any programming language satisfy the procedure oriented principle then it is called "POP". Some of the example for POP languages like .

    Example :- 

    ADA
    FOR TAN  
    PASCAL 
    CABAL
    8086 ( Micro Controller & Processor )
    C Language
    Oracle up to 7.3
    ASP.Net (Visual Basic)
    Developer 2000
    Perl  ETC ....

    In the real time example we can use the Procedure Oriented language languages for developing  projects related to the System SoftwareApplication Software  and we cannot use for developing related to the Internet  or Distributes software . 

    If we use procedure oriented language for developing the Distributed Application then we can get the Following Limitation .

    1. It posses Platform Independent
    2. The data is visiting between client & server side application  in the form of plane test , but not in the form of  hyper test .
    3. Security problems are  more.
    4. The data is visiting between client side application & server side  application  in the form of byte to byte and leads to poor communication.
    5. The data is represented in the form of "Function".
    To overcome the above  limitations industry recommended to use Objective Oriented Programming Language  for developing of distributed Application.

    Tuesday, August 07, 2018

    Definition of Method Overriding

    Method overriding 

    In previous post we already learnt about on the MethodOverloading concept. Now we discuss on about the Method Overriding in Java.  

    A Method is said to be overriding if and only if  Method Heading is same and the method body is different then it is called Method Overriding.
                                                        (or)
    The process of predefined the original method for performing different type of is called "Method Overriding".

    Definition :-

    Method heading is same  +  Method body is different  then it is  called the Method Overriding.

    Example :- 

    Method-Overriding-in-java.png

    In the Example of Method Overriding, Method Heading is same but the Method Body is Different  is called Method Overriding.
              

    Monday, August 06, 2018

    Definition Of Method Overloading

    Method Overloading 

    In previous post we already discuss about on Syntax Of Method  in java. Now we  learn about on the Method overloading  in java.

    A method is said to be overloaded if and only if  Method Name is Same  but  Signature is different . This is also known as Method Overloading.  Here Signature  represents the following points but one thing must be different  i.e..

    a) Number of parameters
    b) Type of parameters
    c) Order of parameters

    The purpose of method overloading concept is mainly used on the development of constructor in java. So this concept must be followed by methods for developing the Method overloading  and Method overridingConsider the following example show on the Method Overloading concept. 

    Method-Overloading.png
    Examples:- 

    sum(10);
    sum(10,20);
    sum(10,20,30);
    sum(10.5f);
    sum(10.5f,20.5f);
    sum(java,kvr);
    sum(java4teachus,Lucky);

    Definition 

    In java programming it is possible to define the two or more methods within the same class with the same class. But the parameters declarations are different, this process is referred to as "Method Overloading". Method overloading is one of the way to achieve polymorphism in java. Overloaded method must be differ in the type or number of their parameters. 

    Example:- 

    class OverloadDemo
    {
    void test()
    {
    System.out.println("No Parameters");
    }
    void test(int a)
    {
    a=10;
    System.out.println("value of a="+a);
    }
    void test(int a, int b)
    {
    a=10;
    b=20;
    System.out.println("value of a="+a);
    System.out.println("value of b="+b);
    }
    void test(float a)
    {
    a=20.1f;
    System.out.println("value of a="+a);
    }
    void test(float a, float b)
    {
    a=20.0f;
    b=20.3f;
    System.out.println("value of a="+a);
    System.out.println("value of b="+b);
    }
    void test(char a, char b, char c, char d, char e)
    {
    a='L';
    b='U';
    c='C';
    d='K';
    e='Y';
    System.out.println("value of a="+a);
    System.out.println("value of b="+b);
    System.out.println("value of c="+c);
    System.out.println("value of d="+d);
    System.out.println("value of e="+e);
    }
    }

    ⏩ Constructor Overloading is a kind of Method Overloading. This concept is useful when objects are required to perform the similar tasks but using different parameters.

    ⏩ Constructor Overloading is having any number of constructors provided they take different number or type of arguments.

    ⏩ The following program has three constructors that demonstrate constructor overloading.

    Example:-

    class Box
    {
    private int l,b,h;
    box()
    {
    l=b=h=0;
    }
    box()
    {
    l=b=h=x;
    }
    box(int ll, int bb, int hh)
    {
    l=ll;
    b=bb;
    h=hh;
    }
    }

    Explanation:- In the above example, there are three constructors called constructor without arguments, constructor with one argument, constructor with three arguments.