My Journey Through Java PART 1

Introduction

Usually, people make travel blogs as they enjoy traveling very much and they want to share their experiences with others so that everyone gets a piece of the pie. Here I’m gonna repeat the same story but with a bit of a twist. Instead of an actual journey through a place, I would be talking about my journey through Java. Whats my intention? (one might think), well mostly I think if I could explain how I did it it would be very easy for someone who is looking forward to learning java to understand the concepts better.

Disclaimer

I assume that you to be familiar with java or programming basics as I am going to start with Object Oriented Programming. I have referred lots of documentation from oracle and watched lots of videos on Youtube to help myself learn. If you prefer such methods to learn OOP I would recommend you to do so.

Let’s Begin…….

So the First Step in understanding OOP or Java, is knowing the difference between a ‘Class’ And an ‘Object’. To put it simply just imagine that you want to build a house, the First thing that you need is a blueprint. In this example, we can consider the blueprint to be the class of the program and the build as the object you want to build. The blueprint of the class has all the structures you need to get your build or you object to being created.

Objects

Let us talk a real-world example A television, A TV has a state and a behavior similarly an object also has a state and a behavior. The color of your TV, its brand, the resolution are all examples of states of your TV. Well beyond this your TV does something it changes channels, increases volume, decreases volume etc.. these are known as behaviors. Just as your TV objects also have behaviors. We are talking too much about real-world objects as examples as objects in Java are very similar to that of real-world examples, if you look around for real-world objects you might also see that there are lots of amusing thinks that objects could do this is where the magic happens in java ie..similarly java object could work wonders this is the beauty of object-oriented programming in Java. The objects stores data as variables and shows there behavior by taking advantage of what is known as methods.

Classes

Classes are generally described as the blueprints of an object. Let us go back to our first example TV. Just like how you have a TV in your house there might be thousands of similar TV’s made they all have been made from the same blueprint. To put it in technical terms we can say that the TV in your house is an instance of a class of objects called TVs. Now let me repeat that in simple terms class is something from which each and every object is created.

Let’s take a look at a picture representation

class-and-object-in-java-3

Here the class is Dog (the set of three dogs) and the objects in the class are each individual dog’s let’s name them Dog1Object, Dog2Object, Dog3Object.

Now that we are clear about the differences between an Object and a class lets discuss a simple program that I did useing Object Oriented Programming Concepts.

  1. I want you to click here
  2. Download the code compile it and run it

 

Screenshot from 2018-11-01 23-41-57

The logic is pretty simple you enter two integer numbers and you get, sum difference, product, and quotient of the numbers.

Simple Programming in Java can achieve that, but here the difference is I have used ObjectOrientedProgramming to do so.

In the first 50 lines of code what I have done is basically creating a class that has all my variables and methods which my objects will utilize it simply means that I have created a blueprint for my object to store three variables ( int number1 int number2 and int number3) these are the states for my objects and made four behaviors (methods)  for my objects to add, subtract, multiply and divide. Thus the blueprint (Class) has step by step illustration of how the object should be made. Now you might have noticed that the methods that I have used have void this is because the methods do not return anything it simply does an instruction which it has been assigned.

Now let us jump into the wonders of ObjectOrintedProgramming

This is how my code continues.Screenshot from 2018-10-31 23-11-51

What I have done here is that 1st in my main class I have created an object ob (line 59). The compiler automatically allocates a reserved space for my object to work on. line 65-71 I am taking an input from the user, I hope you see that it is not the traditional way since I have created an object ob using my Calculator class (blueprint) I know that this object that I am creating have three states my intention here is to store my accepted data in two of those states (int num1 and int num2) for that I have 1st called my object and then it calls the variable inside the object, hence calculator.number1 and calculator.number2 . Now that assigning the variable is done I want to do something with it for this I have to use my methods. On line 75 I have performed addition what this basically does is run the part under method add() which is assigning the value of number1 + number2 to result. (note that all of this is done in the memory which the compiler has made for the object ob and if another object is created the steps followed by this object would not interfere with the other object). In the next step, I am outputting the result. The same procedure is now followed for subtraction, multiplication, and division.

Conclusion

Now that you have seen a small program done using OOP I want you to try out the same with ideas of your own. If you are really interested in Learning Java to an advanced level I do recommend using the Oracle Documentation. As I have said this is just a blog on my journey through java and I want it to be educative so if you are enjoying this do tag along as I would be back with another part of this blog.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s