It is Java, Please don’t write complete code in main() method.


object oriented programming vs procedural programming

Recently I had chance to meet my college junior, he said he started learning Java.  I appreciated him for learning new programming skill.  He is from C programming background, he said it is easy to learn java and he said java looks same like “C programming”.  I was shocked. How he claims “Java programming same as C”.  Java is object oriented programming and C is procedural programming. I am not trying to say Java is better than C.  Each language has its own advantages.  But I was not able to agree his claim “Java programming same as C”.

I asked him to write simple interest program in java. He wrote the program and calculates the simple interest properly. Great. His simple interest programming as below,

import java.util.Scanner;
public class Simple_Interest{

public static void main(String args[]){

float p, r, t;
Scanner s = new Scanner(System.in);
System.out.print(“Enter the Principal : “);
p = s.nextFloat();
System.out.print(“Enter the Rate of interest : “);
r = s.nextFloat();
System.out.print(“Enter the Time period : “);
t = s.nextFloat();
float si;
si = (r * t * p) / 100;
System.out.print(“The Simple Interest is : ” + si);

}

}

Now I understood why he claims “Java is same as C”. I suggested him to get any alternative way of writing simple interest program in java.  He searched “simple interest program in java” in Google.   He said all the programming forum and java tutorial provides same kind of solution. No alternate design suggested for simple interest. It is strange that the best java programming online tutorials suggested the same for calculating the simple interest.

Object oriented programming is design paradigm in programming world.  It is not just only to provide solution or to get results. The design proposed to write low coupling and high cohesion architecture. Design all the real world object as class object with attributes and methods. We already discussed on OOPS basics encapsulation, abstraction and inheritance. With all these basics, we follow the procedural programming approach to write code in object oriented programming. I feel the simple interest programming should be like below in Java,

package info.javaarch;
import java.util.Scanner;

public class SimpleInterestExample {

public static void main(String[] args) {

//Get input values from Scanner p,n,r

float si = InterestCalculator.calculateSI(p,r,t);  // Utility InterestCalculator and static method calculateSI.
System.out.print(“\nSimple Interest = ” + si);

}

}

final class InterestCalculator{ //Utility class as final

private static final Integer HUNDRED = 100; // Constants as static final

public static float calculateSI(float principal,float rate, float years) { // Method as static.

return principal * rate * years / HUNDRED;

}

}

We could say that earlier program provided for learning purpose, so following procedural language approach would be fine.  But I feel programmer mindset should change to object oriented design from day one of learning  java. It would give insight to different view of solving the problem with objects.  Let us revisit the basics and advantages of object oriented programming.

Basics of object oriented programming:

1. Everything is an object.
2. Computation is performed by objects communicating with each other, requesting that other objects perform actions. Objects communicate by sending and receiving messages. A message is a request for action, bundled with whatever arguments may be necessary to complete the tasks.
3. Each object has its own memory, which consists of other objects.
4. Every object is an instance of a class. A class simply represents a grouping of similar objects, such as Integers or lists.

Advantages of Object Oriented Programming:

1. Modularity: The source code for a class can be written and maintained independently of the source code for other classes. Once created, an object can be easily passed around inside the system.

2. Information-hiding: By interacting only with an object’s methods, the details of its internal implementation remain hidden from the outside world.

3. Code re-use: If a class already exists, you can use objects from that class in your program. This allows programmers to implement/test/debug complex, task-specific objects, which you can then use in your own code.

4. Easy Debugging: If a particular object turns out to be a problem, you can simply remove it from your application and plug in a different object as its replacement. This is analogous to fixing mechanical problems in the real world. If a bolt breaks, you replace it, not the entire machine.

Kindly share your view on changing mindset to procedural programming to object oriented programming. In simple terms, C to Java.!!

You may also like