Try   HackMD

Programming II Lab 1

Introduction to Object-Oriented Programming

The Islamic University of Gaza
Engineering Faculty
Department of Computer Engineering
Author: Mohammed Nafiz ALMadhoun2021/02/12

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.

What is a class

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!

public class Email { String from; String title; String body; }

For now, think about class as something to define your own custom data type.

What is an Object

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.

Class constructor

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.

class Email { String from; String subject; String body; public Email (String from, String subject, String body){ this.from = from; this.subject = subject; this.body = body; } }

So now we can create a new email object like this:

Email email = new Email ("mohelm97@gmail.com", "This is a title", "This is the body"); 

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.

Lab Tasks

Task 1: Build a simple blog!

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.

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Task 1 Expected Ouptut

Post.java:

import java.util.*; import java.io.*; public class Post { private String title; private String content; public Post(File file) throws Exception{ // Complete this section! // You should read the post from a file! } public Post(String title, String content) { this.title = title; this.content = content; } public String getTitle(){ return this.title; } public String getContent(){ return this.content; } public void save(String fileName) throws Exception { PrintWriter pw = new PrintWriter("data/"+fileName+".txt"); pw.println(title); pw.println(content); pw.close(); } }

Blog.java:

import java.util.*; import java.io.*; public class Blog { public static void main (String[] args) { Blog blog = new Blog(); blog.askUser(); } private ArrayList<Post> posts = new ArrayList<Post>(); public void askUser () { try { updatePosts(); } catch (Exception ex) { System.out.println("Can't find data folder!!"); System.exit(1); } System.out.println("Welcome to our blog!"); System.out.println("Please select one of the following options:"); System.out.println(" 1. List Posts"); System.out.println(" 2. View Post"); System.out.println(" 3. Add Post"); System.out.println(" 4. Exit"); Scanner input = new Scanner(System.in); while (true) { System.out.print("Enter Selection: "); int selection = input.nextInt(); switch (selection) { case 1: listPosts(); break; case 2: System.out.print("Enter the post index: "); int index = input.nextInt(); viewPost(index); break; case 3: addPost(); break; case 4: System.out.println("Bye bye!"); System.exit(0); break; default: System.out.println("unknown command"); } } } public void updatePosts () throws Exception { File f = new File("data/"); // Place your code here! for(File pf : files) { posts.add(new Post(pf)); } } public void listPosts (){ for(int i=0;i<posts.size();i++) { System.out.printf ("%d: %s%n", i, posts.get(i).getTitle()); } } public void viewPost (int index) { // Place your code here! } public void addPost () { Scanner input = new Scanner(System.in); System.out.print("Enter post title: "); // Place your code here! try { post.save(posts.size() + ""); posts.add(post); System.out.println("Post saved!"); } catch (Exception ex) { System.out.println("Couldn't save the post!"); } } }
tags: Programming Java IUG
End Of Lab 1