In this lab, we are going to talk about Object-Oriented programming and the idea of this concept, and how it helps us in making our codes more readable and scalable, the concept itself doesn’t change the way the programmes get executed, but it helps the developers in building much greater applications, and it makes it easier to reuse old codes, all the concepts in this course will not teach you any new logic thinking, it will teach you how to organize your code more.
Organizing the code is a very important step in writing it, after writing some codes if you didn’t organize it well, you won’t be able to modify or write more code in the same project, even if you see these concepts as extra steps you will do (and you can solve the problem in an old way easier), you should understand that without these concepts you won’t be able to write a medium-size program.
The class is one of the basic concepts in object-oriented world, the idea of class is a template for the data (and what we can do with it), so the class is the definition of the data that each object has, for example, an email has title, body, and from, you can create a class named Email that has a from as string, title as string, and body as string!
For now, think about class as something to define your own custom data type.
If you have a class you have a template for data, now the data itself is called an object, so if you have a specific email, you can create an object from the previous class and fill its data with the real data for that email.
Now you can create a method in that class to present or manipulate the data in that object, so for our previous class, we can create a method named printShort, this method will print the form field and title field in one row for that object.
If you want to create an instance of the class (object), you may need to initialize the data in the object when you create it, so that is why there’s a constructor, the constructor is just a special method that got called when you create a new object of a specific class, the construct in Java should be named like the class name, and it doesn’t have any return type (as the construct should return the new object), a single class can have many constructors, these constructors should be different in the header.
So now we can create a new email object like this:
Now you can create an array of emails if you want, and you could write another code to retrieve the emails from your email and fill that array.
What I need you to understand now, is that a class is a template for data, and the methods in that class are the thing that controls the data, now each instant of the class (object) is what contains the real data that got manipulated by the methods.
In this task, you should build a simple blog, which allows you to add posts, view posts, and list all the posts in it, the posts should be saved as text files in a folder name data
, you should use the following structure to build your project.
Task 1 Expected Ouptut
Post.java:
Blog.java:
Programming
Java
IUG