Posts

What does System.out.printf( "%-15s%03d\n", s1, x) do?

What does System.out.printf( "%-15s%03d\n", s1, x) do?   First   s1   which is formatted using   %-15s . The   %   means that what follows is an argument that will be formatted. Then follows a   -   resulting in left alignment.   15   fills the string up to a length of 15 characters (adding spaces at the end). Finally the   s   means, that you are formatting a string. Second  x  which is formatted using  %03d . Here the  0  is the fill character, meaning that, if necessary, zeros are added. The  3  is again the width, meaning the fill character  0  is added as many times as necessary to make it 3 digits long (this time at the beginning). Finally  d  means, that a integer is formatted. OR "%-15s" means that within 15 blank space, the String "s1" will be filled in the left. (fill in the blanks from the left) "%03d" means that within 3 0s, the integer"x" will be filled in the ...

Day24 : Argument Passing

Argument Passing  In Java we can pass two types of arguments to a method and they are Variables (Primitive Datatypes) References (Array References & Object Reference Passing Variable Whenever we pass any variable as argument to a method then java passes its value. This value is received  by the formal argument declared in the method's argument list. So now both the argument, actual and formal have the same value but their address are different so if we make any change in the value of formal argument then it will never effect the value of actual argument. Thus we say that in Java variables are always passed using pass by value. The Second type of argument which we can pass to any method in Java is reference (Array Reference and Object Reference). Whenever we pass any reference as argument to a method then Java passes the addresses of the object or array pointed by the reference. This address is then received by formal reference declared in the argument list of the method. s...

Day23 : Method Overloading

 Method Overloading The word overloading in object oriented world means multiple version of the same thing. Thus the word Method overloading means providing multiple method in the same scope with the same name. For Example :  The two popular method in java called println and print have multiple version and thus are overloaded.          (1) int a = 10;                  System.out.println(a);          (2) double b = 1.7;                 System.out.println(b);          (3) String name = "Sachin";                  System.out.println("Sachin"); But if we're overloading two or more method then these method must compulsory differ from each other in terms of their argument and this difference can be of three types: Number of Argument can be different. Datatype of ...

Day22 : Object Initialization

Image
 Object Initialization The word object initialization in Java means assigning initial value to instance variable at the time of creation of the object. In Java we've three ways of doing this Using Explicit initialization Using Constructor Using Initializer block 1 - Using Explicit Initialization          class < class-name>          {               <array> <data_type> <var> = value(This is an explicit initialization);          } Example : File Name : Account.java public class Account { private int accId = 101 ; private String custName = "Amit" ; private double balance = 50000.0 ; public void showAccount () { System . out . println ( "accId " + accId ); System . out . println ( "Customer Name " + custName ); System . out . println ( "Balance " + balance ); } } File Name : UseAccount.java public clas...

Day21: Object Oriented Programming System(OOPs)

Object Oriented Programming System(OOPs)  Object Oriented Programming is not a programming Language. Rather it is a methodology approach to design programs which are close to read world and help us visualize real world situation in our software. The biggest advantage of Object oriented programming- Data Security Easy Modeling of Real world situation What are the two basic building block of OOPs? The most important component of object oriented programming are: Classes Objects Class: A class is just like a blueprint Or Template which describes how an object will look. For Example :      Model of building, Develop by civil engineer is like a class while actual building built up by the Labourers is object. In Simple terms we can say classes is logical while object are physical. What does a class contain? A class contain two important element. (1) Instance Variable -          They are also called data member in languages like C++ and they hold the...

Day 20 : Double Dimensional Array

Image
 Double Dimensional Array In Java 2D Array are used in those situations where we have to stored multiple groups of data . For Example :      Suppose we want to record marks of three student, where each student has five subject. In this case we have two solution - We can declare Three 1D array each of size 5 But the code will become difficult because we will have to handle 3 Arrays individually.  A better solution would be to take 2D array of three rows and five column. The benefit will be we will have to handle 1 array which can be handle using only 1 Loop. Type of 2D Array In Java we have two types of 2D Array. Rectangular  2D Array Jagged 2D Array Rectangular 2D Array These are those 2D array where every row contains same number of columns i.e., Length of every row contains same number of column. See Fig1. Jagged 2D Array These are those 2D Array where each rows can be of different length. See Fig2. Creating a Rectangular 2D Array 1. Creating the Array R...

Day19 : Using for Each Loop

Image
 Using For-Each Loop Syntax :                for <data_type><var> : <array_name>               {                    //Statement               } int [ ] arr = {10,20,30,40,50}                                   for(int x : arr)                    {                         System.out.println(x);                    }      Output :                10 20 30 40 50  Advantages of for-each loop: Syntax is easy compared to regular for loop because initial...