Showing posts with label class. Show all posts
Showing posts with label class. Show all posts

Saturday, 15 September 2018

Getting Input From User




In this post, we are going to learn how to get input from the User. Then will do some programming exercises to enhance our programming knowledge.

There are Different - Different ways to get an input from the console -- Go HERE.
 but right now will stick to Scanner Class, because it is the most common and easiest way.

using Scanner Class will get the input from the user,

   
- The Scanner is a  class in java.
- present in java.util package
- which allows the user to read values of various types.
- There are far more methods in class Scanner than you will need in this course.

Syntax:- 


Scanner - is a class in java.

sc - is an object of Scanner class (you can give any name).

new - is an operator which is used to make an object of a class.

Scanner() - is a constructor in Scanner Class(Don't Worry will talk it later).

System - is a class in java, represents your computer system (helps to provide us system-level methods)

in - stands for input (s).

System.in - getting input from the system and it passes as an argument to Scanner Constructor.(Don't Worry, if you are unable to get it). 



# Scanner Class has Somes Methods to get Different kinds of values from the console.

- For different types of values different, types of methods are available.
- For int type nextInt() method is used,
- For double type nextDouble() method used,
- All the methods called by using Scanner Object (sc.method name).
- for example (String value) -- we have to use next() or nextLine().


String s; // variable -- where data store
Scanner sc = new Scanner(System,in); // creating Scanner class object 
s = sc.next(); // getting input from user and assgin it to "s" variable  
sc.close(); // good practic to close resourses.







Question -- WAP to Take two number from the user and return its sum.
Answer -- 



# Code 

package scannerClass;

import java.util.Scanner;

public class Sum {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);
System.out.println("Enter 1st number : ");
int num1 =sc.nextInt();
System.out.println("Enter 2nd number : ");
int num2 = sc.nextInt();
int add=num1+num2;
System.out.println("Sum = "+add);
}
}








Tuesday, 11 September 2018

Class & Objects




Now, we are trying to understand the CLASS & OBJECTS...

Let's Take an Example, and Try to Understand it.

Suppose, You want a 10 Table & Chair for your Tuition Class
You just go to the near Carpenter Shop and said I want 10 Tables and chairs for my tuition classes.
So, They offer you a chair and asks you some questions about your table and chair requirements like-  size of the table (length and width), glass or wooden table, what about the table legs wooden or iron,
the colour of the table or leg, etc.
and he wrote down all the details in the piece of paper for remembrance.



and After 10 Days your Chair is ready .. Just go to the shop, pay the balance (Money) get all the tables and chairs.

Now,

The piece of paper (in that all the details are written about table and chair )  is called -- CLASS.

And

The real Tables and Chair That you receive after 10 Days is called -- OBJECTS.


 ********************************************************************************

One more STORY, To understand the concept better...

Hey, Friends!,  Meet my friend JONE.


First Name                      - Jone
Middle Name                  - Sonu
Last Name                       - Sena
Father's Name                 - Harish Sena
Mother's Name               - Sumitha Sena
Gender                             - Male
Qualification                    - B.Tech (CSE)
DOB                                - 12th - December - 1990
Height                              -  163 cm
Weight                              -  78 kg
Address                            - #45, 23/430 , Delhi
Contact No.                      - 8950XXXXXX
Working in                       -  XYZ MNC
Status                               - Un married
Bank Account Name         - ABC Bank
Bank Account Number    - 335957XXXXXX455

(like that Jone has many more Details/Properties ...)


Question - If you are a developer and you have to make a software for Bank, Tuition Insitute and Hospital to Know Your Customer, then what kind of details you are looking for?

Answer -

# For Bank

class KYC
{
Customer Name -
Customer Father's Name -
Bank Account No. -
Address -
Contact No. -
}

# For Tuition Institute 

class KYC
{
Student Name -
Father's Name -
Course -
Fee Status -
Address -
Contact No. -
}

# For Hospital

class KYC
{
Patient Name -
Father's Name -
Age -
Height -
Weight -
Problem -
Address -
Contact No. -
}


Question - Now, Question arise Which kind of information you have to write inside the class?

Answer - We have to write that information which is important for that application (it depends on the perspective of a viewer).
Jone has many properties but in a different application, we acquire (take) different kind of information, so it depends on application to application.




Definition Of A CLASS

A class is a templet where we describe the properties and behaviour of an object.

0r

A class is a collection of objects which similar properties and behaviour. 

0r

A class is a group of a common object. It is a user defined data type. It Contains variables and functions. The class defines all the common properties of the different objects that belong to them.
0r

A class is a blueprint or prototype that defines the variables and the methods (functions) common to all objects.

0r 

Class a template/blueprint that describes the behaviour/state that the object of its type support.




Definition Of AN OBJECT


*Object: It is the instance of a class. 

0r 

An object is a specimen of a class. Software objects are often used to model real-world objects you find in everyday life.

0r 

An object is nothing but a self-contained component which consists of methods and properties to make a particular type of data useful. Object determines the behaviour of the class. When you send a message to an object, you are asking the object to invoke or execute one of its methods.



# Syntax of class


For Example - 

class Fan {
// properties 
//and
// behaviour
}


# Syntax of Object

- It is a two step process...

1st Step -


For Example -  

Fan f ;


2nd Step - 


For Example - 

f = new Fan();



These two steps you can also do in a single line - By combining



For Example -

Fan f  =  new Fan() ;







Friday, 7 September 2018

Interrogation Of First JAVA Program




Class FirstJavaProgram
{
      public static void main (String [] args)
     {
               System.out.println("Hello World!");
      } // main
} // class


In Java Program EveryThing is a CLASS.

1- First we write class and class_name
2- main() method
3- method/function to display something to the screen

--> These three steps are needed to follow for understanding & execution of the program.




******************************************************

# Syntax Of CLASS




Accessibility_Modifier are -

  • public
  • private
  • protected
Modifiers are - 
  • final
  • static
  • abstract
  • many more (Will see all of them later)


class - class is a keyword. It should be written as it is. [all the keyword are written in small letters in java ]

Class_Name - Here "FirstJavaProgram"  is a name of the class.


******************************************************

Now, Main() Method


*This Is Very Important (It may chance to asks question on this in an interview room)
we will see this later when you are in a position to understand these concepts.
As of now I just introduce these words to you.

[Note - by heart this line (without this line you cannot run any program in java)]

public - is an access modifier.

static - is a keyword.

void - it is a DATA TYPE.

main() -

  • it is the name of main() a method. 
  • JVM is always looking for the main() method.
  • it is the parametrized method, it takes one argument of string type of array.
  • You can't change its name, if you want to do so - for that you have to customize your JVM and it is almost impossible. 

String [] args - String is a class in java and args is the name of string type array.


As of now, it is enough but still, you want to know more about " public static void main(Strings [] args) "

Then I am going to give you video link - that will help you to understand this line clearly.


 [ Video -public static void main(Strings [] args) == 1st part ]


 [ Video -public static void main(Strings [] args) == 2nd part ]


 [ Video -public static void main(Strings [] args) == 3rd part ]


******************************************************
Now, Output Stream


System.out.println(); - helps to display something on the screen.

System - is a class. (I told you before every thing in java is a class)

out - is a static variable.

println() - is a method of System Class.  


As of now, it is enough but still you want to know more about System.out.println();
Then i am going to give you video link - that will help you to understand this line clearly.




[ Video - Explanation about System.out.println(); ]


******************************************************

Now, Comments 


  • Comments are something that is unreadable in java.
  • it is used to increase the readability of the program.
  • Java Compile ignores it.
  • Two Types of Comment in Java
    • Single Line Comment
    • Multiple Line Comment
    • Documentation Comment
// double forward slash used for single line comment


/*

this is used 
for multi-line comment, 
what every written inside these (  /*  _comment_   */  )  is never
executed at all.

*/


/**
it appears in blue color
*  this comment is for other developers
* These comments have some "@" words
 used for -- last modification date, developed by, modified by etc
*/







Object Oriented Programming





Question - Feature Of Java?

Answer -



These are the features of Java, will see all of them one by one but right now we are going to understand  Object Oriented Programming Language.




*Question - What is Object Oriented Programming?

Answer - Object Oriented Programming is a paradigm ( way of writing a program ).There are many ways of programming like

  1. Procedure Programming (structural programming)
  2. Object-Oriented Programming *
  3. Constraint Programming
  4. Logical Programming
  5. Functional Programming 
  6.  and more ...
Note -- None of the styles is perfect. We use Object Oriented Style because of many benefits of object-oriented and it is the most popular and used.


Let's try to Define & Understanding of OOP Language  -

- It's Abbreviation of OOPs

- OOPs Stands for OBJECT ORIENTED PROGRAMMING System

- It is a kind of Model or Concept or Method of implementation of code

- Used to define implement (wright) programs

- methodology (way of developing a program) based on class and objects instead of just functions and procedures.

- In these types of programming, you have to make CLASS because without a class you can't make  OBJECTs. OOPs is based on OBJECTS that is generated from the CLASS.

- It has some important features that we will be using during developing our code. Some are
    ○ Encapsulation
    ○ Abstraction
    ○ Polymorphism
    ○ Inheritance etc (there are more features will see later)


- Examples of OOP's languages are C++, C#, JAVA, PHP, SMALL TALK  etc.


-Objects are created from Classes. (also say that object is an instance of the class)



" OBJECT-ORIENTED PROGRAMMING refers to a programming methodology based on objects, instead of just functions and procedures. These objects are organized into classes, which allow individual objects to be group together. Most modern programming languages including  C++, C#, JAVA, PHP, etc are object-oriented languages." 

0r 

" OBJECT-ORIENTED PROGRAMMING is a method of implementation in which software is organized (written) as a discrete collection of objects."





Question - What are the characteristics/features of OOP?
Answer - 

  • Class
  • Objects
  • Inheritance 
  • Abstraction 
  • Encapsulation
  • Polymorphism
  • Modularity
  • Concurrency 
  • Persistence 
Note -These are the features must be satisfied for any programming language then only that language is known as object-oriented programming language.


(Don't Worry we will see all there features one by one...for now, just remember these names)