Monday, September 9, 2013

HOW JAVA IS PLATFORM INDEPENDENT

HOW JAVA IS PLATFORM INDEPENDENT ?

The developers of java have made compilers and java virtual machines(also known as JVM). The function of compiler is to convert the program written by the programmer in the syntax of java into byte-code. Though the programmers don't have to do anything with the byte-codes so produced by the compilers, but for the sake of general knowledge, the programmer must know that it is the compilers which are platform dependent. You must have noticed while downloading java-sdk kit from the oracle website. The website provides different kinds of compilers for different platforms such as windows, LINUX, UNIX, Machintosh. So, this means java developers bore this extra responsibility of writing different compilers for different platforms, so that you can both write and run a java program on any platform. All you need to worry about is just learning the syntax of java. 

2nd PHASE AFTER BYTE-CODE FORMATION

When compilers perform their part of converting a program written in java to the byte-code, then this byte-code is fed to JVM(Java Virtual Machine) whose function is work on the byte-code and to convert it into such a form which can be understood by the computer hardware or the underlying machine. This form is referred as machine language. Java developers also take care to develop different JVM for different machines. So that your program written in java could run on any hardware. Your computer manufacturer could be any company such as Sony, Samsung, Lenovo, ASUS, etc. So there are JVMs available for all different machines.
The appropriate compilers and JVMs makes life easier for the programmers who write code in java and want their code to run everywhere irrespective of different platforms.
These striking features of java made the founders of java to raise the slogan :
                   "Write once run anywhere"

Friday, September 6, 2013

ACCESS MODIFIERS IN JAVA

WHAT ARE ACCESS MODIFIERS IN JAVA ?

Access modifiers are the special keywords which when applied provide different types of access to the content of the class. By content I mean, the data members/ fields and methods defined in the class. There might come situations where we don't want other classes to access the fields or methods or the class and some situations where we want all the elements of the class to be accessed by any other class.
Therefore, to provide different kinds of access for different kind of situations, Java has Access Modifiers, there are 3 kind of Access Modifiers :
1. public
2. private
3. protected

WHEN 'public' ACCESS MODIFIER IS USED :





DIFFERENCE BETWEEN METHOD OVERRIDING AND METHOD HIDING

METHOD OVERRIDING

When an instance method is written again with the same name, same number and type of parameters and same return type, in the sub-class as that of the parent-class/super-class, then this phenomenon is called as method overriding.

METHOD HIDING

It occurs in case of class-methods( also popularly known as static methods ). When any static-method is written again with the same name, same number and type of parameters and same return type, in the sub-class as that of the parent / super-class, then this phenomenon is called as method hiding.


Thursday, September 5, 2013

INTRODUCTION TO INHERITANCE IN JAVA

INHERITANCE

The concept of inheritance much more similar as in the real world itself. Inheritance means the passing of similar traits and features from older ones to younger ones. This same concept is taken from the real life and applied in the object oriented principles of programming languages too.
Necessity is the mother of invention fits better here, as in the earlier days of programming when there was no inheritance at all, programmers have to rewrite whole of the complex methods again and again which they wrote earlier in some other programme. They felt very exhausted writing these common but complex methods again and again whenever they have to write new code. They started to think of ways :
1. what if they introduce some way , by which they could be able to use previously written methods from previously written codes.
2. what if they find a solution to introduce a link among different piece of codes.
These necessities gave birth to the concept of inheritance.
With the advent of inheritance , the above problems found solutions. Now, if some code has to re-use the previously written methods and fields in some other code, it just inherits all of them by just writing a keyword 'extends'.

The programming life has become much easier with the introduction of inheritance. Java allows only single inheritance, though some other programming languages allow multiple inheritance too(e.g. C++ ).

Single inheritance

when a class can inherit only one other class i.e. having only one parent class. This kind inheritance is called Single inheritance.

Multiple inheritance

when a class inherits more than one class i.e. having multiple parent classes. This kind of inheritance is calles Multiple inheritance.

e.g.

public class Vehicle {
int numOfWheels;
int topSpeed;
int engineCapacity;
int gears;
void shiftGear(int gear) {
/ / / / /
/ / /
}
void speedUpBy(int mark){
/ / / / /
 / / /
}
}

public class Motorbike extends Vehicle{
int seats;
int mileage;
void start() {
/ / / /
/ / /
}
void stop() {
/ / / /
/ / /
}
}

We can clearly see in above example, that Motorbike class extends Vehicle class in its first statement, by doing this, the Motorbike class is able to inherit all the features of Vehicle class, without having it to write all those methods again. This is how inheritance makes the coding easier than before.
Here the Motorbike class is called subclass. 
The Vehicle class is called superclass.


SUB-CLASS

The class that is derived from the another class is called as sub-class (also called as child-class, derived class or extended class).

SUPER-CLASS

The class from which sub-class is derived is called as super-class ( also called as a base-class or a parent class ).


NOTE : there exist

DIFFERENCES AND SIMILARITIES BETWEEN INTERFACES AND ABSTRACT CLASSES

DIFFERENCES BETWEEN INTERFACES AND ABSTRACT CLASSES

Here I have compiled the points of both interfaces and abstract classes, and presented them in the image form . So, that anybody can save it and make it as his/her wallpaper, which enables to see it regularly and revise it.


Wednesday, September 4, 2013

SOLVING ANY PROBLEM FROM THE OBJECT ORIENTED POINT OF VIEW

 Objective: Analysis of the following Case Study from the perspective of Object Oriented Approach


Problem Description: Course Registration System



A Course Registration System needs to be developed for an engineering college. The college wants an automated system to replace its manual system for the purpose of registration of students to branches and calculation of fees for each year. The engineering college provides graduation courses in various branches of engineering.
The system will be used by the admin staff to register students, admitted to the college to the branches, at the time of joining the college and also to calculate the yearly fees for the students. The student has to register every year for the next academic year. The Admin takes care of the yearly registration of the students and the calculation of yearly fees. The system needs to be authenticated with a login id and password.
Registration of a student to a branch is based on the qualifying exam marks and the entrance counseling. For every branch, a yearly branch fee is applicable. Discounts are given to the branch fees of the first year based on the qualifying exam marks. In addition to this, there is a registration fee which is applicable to the first year students. Students can opt to be a day scholar or hostelite. Annual bus fee is applicable to the day scholars based on the distance they travel. Annual hostel fee is applicable for all the hostelites. Annual infrastructure fees and library fee is also applicable to all the students. Admin calculates the yearly college fees for each student and the college fees include all the fees specified earlier based on the type of student. Admin will provide a printed receipt of the fees to the students once the annual college fees have been paid. At the time of registration, student has to provide the permanent address and in case the student is opting to be a day scholar, he/she has to provide the residential address also.

SOLUTION :

Now, after thorough reading the above problem, we need to extract some crucial points, without writing them into sentences again, because those points will take us closer to the object oriented design phase. 
Lets get started :

Step 1 :
First of all we need to extract the purpose from the whole description  given above in just few points, as it will enable us to have a clear cut target. So the purpose of above long description is as follows :

1.  Registration of the students to the available branches.
2. Calculation of annual fees.


Step 2 :
Since, we are seeing the problem from the object oriented point of view, therefore, we need to make out what kind of actors / entities / objects are involved in our problem.

The whole problem revolves around one important object and i.e. 'Student'.

So, 'Student' can be seen as an actor or entity  or an object .

Tuesday, September 3, 2013

TOPIC 3 : INTRODUCTION TO DATA STRUCTURES

3.1 Data structures - Stack

Assignment 3.1.1: Application of Stack Data Structure


Objective: To understand how to apply Stack Data Structure.


Problem Description: Write a pseudo code to find the binary value of a given integer and 
display it.
Hint: Consider the integer 5. To convert 5 to binary, the following procedure needs to be 
adopted.



               2|_5____

               2|_2_-_1_

                   _1__- 0_





Each time, store the remainder in an appropriate data structure and use LIFO to get the 

binary value.

TOPIC 2: LOGIC BUILDING USING PSEUDO CODE part-3

2.3 Iteration Control Structures

Assignment 2.3.1 Pseudo code - Iterational pattern (1)


Objective: Write pseudo code for iterational pattern problems.

Problem 1 :

The problem given in Assignment 2.1.2 is extended to check whether 
the Number of Hours accepted from the user is less than or equal to 0. If so display an error 

message. Reenter the data the Number of Hours until valid data is entered.




Step 1: Write the following Pseudo code

CALC_GROSS_PAY
1. input No_of_Hours, Pay_Rate
2. while(------------) do
3. display "---------------------"
4. -------------------------------------
5. end while
6. Gross_Pay =  No_of_Hours * Pay_Rate
7. display " The gross pay is equal to : ", Gross_Pay
8. end

Step 2 : Fill in the missing (------) parts in the Pseudo code
We need to put a condition in the parenthesis of while ( ) loop, which could test the validity of the data input by the user for the number of hours. We need to keep the loop running displaying a message as long as the condition in the parenthesis remains true. Therefore the complete form the above pseudocode is given as :



CALC_GROSS_PAY
1. input No_of_Hours, Pay_Rate
2. while(No_of_Hours < = 0) do
3. display "Invalid Entry, Enter Valid Number of hours. "
4. input No_of_Hours
5. end while
6. Gross_Pay =  No_of_Hours * Pay_Rate
7. display " The gross pay is equal to : ", Gross_Pay
8. end


Problem 2 :
Write a pseudo code to calculate the Loan Balance, where a person borrows an amount A and in return he/she agrees to make N payments per year, each of amount P. While the person is repaying the loan, interest will accumulate at an annual percentage rate of R, and this interest will be compounded N times a year (along with each payment). Therefore, the person must continue paying these installments of amount P until the original amount and any accumulated interest is repaid.

NOTE: The formula to calculate the amount that the person needs to repay after T years is :

Balance amount after T years = 

TOPIC 2: LOGIC BUILDING USING PSEUDO CODE

2.2 Selection Control Structures


Assignment 2.2.1: Pseudo code - Selectional pattern (1)


Objective: Write pseudo code for selectional pattern problems

Problem Description: The problem given in Assignment 2.1.2 should be extended to check whether the Number of Hours accepted from the user is less than or equal to 0. If so an appropriate error message should be displayed.

The problem given in assignment 2.1.2 is here.
The pseudo-code with required changes can be written again as :


Calculate-Gross-Pay

1. input No_of_Hours, Pay_Rate

2. if  No_of_Hours < = 0 then
3. Display " The Number of hours are invalid."
4. else
5. Gross_Pay = No_of_Hours * Pay_Rate
6. Display "Gross Pay is : ", Gross_Pay
7. end



Assignment 2.2.2: Selectional pattern (2)

Objective: To develop pseudo code writing skills for a given problem using certain logic.

Case study: An institution offers several programs. Each program has several courses. There 
are students who have opted for these courses. There are Educators who offer these courses.



Problem 1:
At a given point of time we need to allocate a course to an educator for a given
period. Before allocating a course to an educator the following conditions are to be checked.
1. Enough number of students have registered for the course
AND
2. The course is added into the educator’s skill set
AND
3. Educator’s calendar is free for the given period
AND
4. Educator is not on leave for the given period
And
5. Educator has not yet completed his delivery targets Or There is no other alternative

SOLUTION :

COURSE_ALLOCATION
1. input course
2. input num_of_students
3. input educator's_skill_set, spare_time, is_on_leave, delivered_targets, allocation
4. if ( (num_of_students > min_number_students) && (educator's_skill_set = course) && (spare_time = true ) && (is_on_leave = false) && (delivered_targets = true ) ) then
5. set allocation = true
6. end if
7. display "Course is allocated. "
8. end


Problem 2: For a student, following is the course qualifying criteria.
1. Student has registered for the course
2. Attended at-least 80% of the classes
or attended at-least 60% of the classes and submitted medically unfit certificate
3. Submitted all the assignments
4. Completed project
5. Scored 80% marks in the test or the retest

QUALIFYING_STUDENT
1. input classes_attended, min_score
2. input course_reg, medical_certificate, submitted_assignments, completed_project
3. if ( ( course_reg = true ) &&(classes_attended >= 80 || (classes_attended >= 60 && medical_certificate = true ) ) && (submitted_assignments = true ) && (completed_project = true) && ( min_score >= 80)) then
4. set course_qualification = true
5. end if
6. display "Congratulations, course has been qualified. "
7. end 



Assignment 2.2.3: Selectional pattern (3)


Objective: To use the problem solving techniques learnt so far in a real life application.

Problem 1: Take marks of a trainee in a module as input and calculate the grade and
grade points. The grade table is given below:




SOLUTION :

CALC_GRADE
1. input marks_obtained
2. if ( marks_obtained >= 80 ) then
3. grade_point = 5
4. grade = A
5. elseif ( marks_obtained >= 73 && marks_obtained <=79 ) then
6. grade_point = 4.5
7.  grade = B+
8. elseif ( marks_obtained >= 65 && marks_obtained <=72 ) then
9. grade_point = 4
10. grade = B
11. elseif ( marks_obtained >= 55 && marks_obtained <=64 ) then
12. grade_point = 3
13. grade = C
14. elseif ( marks_obtained >= 0 && marks_obtained <=54 ) then
15. grade_point = 2
16. grade = D
17. end if
18. display " The grade points obtained are equal to: ", grade_point
19. display " The grade awarded is : ", grade
20. end


Problem 2 :
Take marks of a trainee in all generic modules (listed below)as input and calculate the
Generic GPA. The method for calculating Generic GPA is as follows:


Grade points for marks are given in previous problem and credit points are as follows:





SOLUTION :

CALC_GPA
1. input marks_obtained
2. if ( marks_obtained >= 80 ) then
3. grade_point = 5
4. grade = A
5. elseif ( marks_obtained >= 73 && marks_obtained <=79 ) then
6. grade_point = 4.5
7.  grade = B+
8. elseif ( marks_obtained >= 65 && marks_obtained <=72 ) then
9. grade_point = 4
10. grade = B
11. elseif ( marks_obtained >= 55 && marks_obtained <=64 ) then
12. grade_point = 3
13. grade = C
14. elseif ( marks_obtained >= 0 && marks_obtained <=54 ) then
15. grade_point = 2
16. grade = D
17. end if
18 GPA = ( marks_obtained * grade_point ) / marks_obtained
19. display " The grade points are : ", grade_point
20. display " The GPA obtained : ", GPA
21. end

PSEUDO-CODES IN PROGRAMMING

WHAT ARE PSEUDO-CODES ?

The pseudo- code is step by step representation written in plain english of an algorithm, which can be perceived as the initial stage of formal coding. Instead of directly digging upon the proper code, it is preferred to write pseudocode to get an essence of the logic applied in the algorithm.

Some of the conventions, keywords etc. followed while writing a pseudo-code are as follows :
1. 'input' keyword is used to take the input from the user.
2. 'display' keyword is used to show the result.
3. Mathematical expressions are used as we do normally.
4. For decision making constructs, 'if' , 'then', 'else' , 'else if' keywords are used.
5. When some statements are to be repeated for a known number of times, 'for', 'to', 'do', 'end for' keywords are used.
6. When some statements are to be repeated as long as some condition remains true then , 'while', 'end while' keywords are used.


TYPES OF PSEUDO-CODES

1. SEQUENTIAL PATTERN ( the statements are executed in the same order as it is written)

2. SELECTIONAL PATTERN ( they involve decision making situations )

3. ITERATIONAL PATTERN ( they involve repitition of the statements )


Monday, September 2, 2013

TOPIC 2: LOGIC BUILDING USING PSEUDO CODE

2.1 Flowchart and Pseudo Code


Assignment 2.1.2: Pseudo code- Sequential pattern


Objective: Writing pseudo code for sequence pattern problems

Problem Description: The finance department of a company wants to calculate the gross pay of an employee in the company. The number of hours worked by the employee and the pay rate should be accepted from the user and the gross pay should be calculated as the below formula.

Calculate-Gross-Pay
1. input No_of_Hours, Pay_Rate
2. Gross_Pay = No_of_Hours * Pay_Rate
3. Display "Gross Pay is : ", Gross_Pay

The above given set of statements represents a pseudo code, which can be perceived as the initial stage of formal coding. It represents a step by step approach to solve any problem in plain english, instead of formal syntax used in any particular programming language. 

There are few conventions which are followed while writing a pseudo code as :
1. use of keyword 'input', whenever data is retrieved from the user
2. use of keyword 'display', whenever result is to be displayed
3. Mathematical expressions are used as it is, as we normally use them.
4. if, then, else, end if keywords are used for decision making situations
5. for, to , do , end for keywords are used when  certain steps are to repeated for a known number of times.
6.while, end while keywords are used when some of the steps are to repeated as long as certain condition remains true.


Assignment 2.1.3: Understanding Variables & Constants


Problem 1 :

Write a pseudo code to take the height of the building in variable 
“Building_Height” and display the same.





SOLUTION :

DISP_HEIGHT
1. input Building_Height
2.display "The height of the building is : ", Buildiing_height
3. end


Problem 2 :
Write a pseudo code to take the constant value 3.142 in a variable “pi” and
display the same.

SOLUTION :

DISP_PI
1. input pi = 3.142
2. display "The value assigned to pi is : " pi
3. end


Assignment 2.1.4: Understanding Operators


Problem 1 : 
Write a pseudo code to take two numbers as input in variables a and b and display
i. Product of the two numbers a and b
ii. Sum of the two numbers a and b
iii. Values of a-b and b-a
iv. Quotient and remainder for a/b and b/a

SOLUTION :

i ) 
PROD_OF_TWO_NUM
1. input a, b
2. Product = a * b
3. Display " The product of 'a' and 'b' is: ", Product
4. end

ii)
SUM_OF_TWO_NUM
1. input a, b
2. Sum = a  + b
3. Display " The sum of 'a' and 'b' is : ", Sum
4. end

iii)
DIFFERENCE_BETWEEN
1. input a, b
2. Diff_A_and_B = a - b
3. Diff_B_and_A = b-a
4. Display "the difference of 'a' and 'b' is : ", Diff_A_and_B
5. Display "the difference of 'a' and 'b' is : ", Diff_B_and_A
6. end

iv) 
QUOTIENT_REM
1. input a, b
2. Quo_A_by_B = a / b
3. Rem_A_by_B = a % b
4. Quo_B_by_A = b / a
5. Rem_B_by_A = b % a
6. Display "the quotient of 'a' by 'b' is : ", Quo_A_by_B
7. Display "the quotient of 'b' by 'a' is : ", Quo_B_by_A
8. Display "the remainder from 'a' by 'b' is : ", Rem_A_by_B
9. Display "the remainder from 'b' by 'a' is : ", Rem_B_by_A
10. end


Problem 2:

Write a pseudo code to take the height and width of a rectangle in variables

height and width respectively and display the rectangle's area and perimeter.

SOLUTION :

RECTANGLE_AREA_PERIMETER
1. input height, width
2. Area = height * width
3. Perimeter = 2*(height +width)
4. Display "The area of rectangle is : ", Area
5. Display "The perimeter of rectangle is : ", Perimeter
6. end


Problem 3:
Write a pseudo code to take the radius of a circle in variable radius and display

its area and perimeter.

SOLUTION :

CIRCLE_AREA_PERIMETER
1. input radius
2. Area = 22/7(radius * radius)
3. Perimeter = 2 *( 22/7) * radius
4. Display "The area of circle is : ", Area
5. Display "The perimeter of circle is : ", Perimeter
6. end


Problem 4:
Write a pseudo code to take the starting value, common difference and the
number of terms of an arithmetic progression in variables a, d and n respectively and display its nth term and sum of n terms.

SOLUTION :

ARITHMETIC_PROGRESSION
1. input a, d, n
2. Nth_term = a+((n-1)*d)
3. Sum_of_terms = (n/2)*((2*a)+((n-1)*d))
4. Display " The nth term is : ", Nth_term
5. Display "The sum of terms is : ", Sum_of_terms
6. end


Problem 5:
Write a pseudo code to take the radius of the sphere in variable radius and

display the volume of the sphere.

SOLUTION :

VOLUME_OF_SPHERE
1. input radius
2. Volume = (4/3)*3.14*(radius*radius*radius)
3. Display " The volume of the sphere is  : ", Volume
4. end




FLOWCHARTS IN PROGRAMMING

WHAT ARE FLOWCHARTS ?

Flowcharts are graphical means of representing the logic to solve any computational problem.
It is a pictorial way and hence give more intense understanding of the solution for the problem at hand.It consist of some symbols which forms its tools to make a complete structure for any problem.

These symbols are : 
 
Flowchart Symbols


An example to show a perfect flowchart :


Flowchart to check whether a number is prime or not