Diary (Java)

From LiteratePrograms

Jump to: navigation, search

This program is under development.
Please help to debug it. When debugging
is complete, remove the {{develop}} tag.


Contents

Diary

This program is a simulation of a diary system where users can enter personal entries about their thoughts and events in a chronological order. Users can also check the number of entries in thier diary, list all entires in the diary and delete any unwanted entries from it. This program is a good program to learn java from as it introduces the idea of using data structures to store information.


<<Defining the Class>>=
import java.util.ArrayList;
public class Diary
{
    private ArrayList<String> entries;
    Class Body
}
<<Class Body>>=
Constructor
Adding Entries
Returning number of entries
Deleting an entry
Listing all entries

Notice that there is an import statement before the class definition. An import statement must always come before a class definition. This import statement gives access to a library class, called ArrayList, which is required as the class uses a data structure known as an array list. This is described below. After writing this important import statement we can use the ArrayList class as if it were one of our own, calling and implementing the methods that belong to it.

It is sensible to call a class a name that indicates what it does or how it behaves. This class is caled Diary as it is easy to identify what it does from its name.

The first line of code in the body is a definition of the entries field. This field is an ArrayList. An ArrayList is known as a collection, meaning it can store an arbitrary number of elements, with each element being another object. This is useful as we wish to store an arbitrary number of diary entries. Because its purpose is to store entries, it has been called entries. Notice it is a private ArrayList, which means it cant be seen or used by other classes. It is also important to notice the <String>. Because ArrayList is a collection we must define what type of element is to be stored in it, which is achieved by placing <String> in the definition.

Constructor

<<Constructor>>=
public Diary()
    {
        entries = new ArrayList<String>();
    }

Constructors should have the same name as the class they belong too. In this case the constructor is called Diary, like the class it belongs too. Notice that the constructor takes no parameters as the ( and ) are empty.

An object of type ArrayList is created in the first line of the body of the constructor for the Diary class. This is stored in the entries field. Notice that there is a need to specify what type of data is going to be stored in the ArrayList as the <String> is present. This is followed by ( and ), to signify that there are no parameters being passed.

Before describing the methods used in the Diary class it is useful to understand the benefits of using an ArrayList for this simulation.

1)It can increase its capacity as required. This is useful because the user doesnt know how many entries they will be entering.

2)It keeps its own private count of how many items it is storing. The reason why this is useful will become aparant when a method is described later on.

3)It maintains the order of entries you enter into it. This is useful because a user of a diary often likes to look back in a diary to see previous entries at a certain date, eg thier 18th birthday.

Adding Entries

The main idea of a diary system is to add entries. The method described below performs this task.

<<Adding Entries>>=
public void storeEntry(String entry)
    {
        entries.add(entry);
    }

Looking at the method signature storeEntry, it is easy to see this method is going to add an entry to the diary and is of type void, meaning it is not going to return any value to the user. It has a parameterentry, which is of type String, this is passed from the user and represents the entry they wish to add to the diary.

The body uses the add method of the ArrayList class to store the value of the entry parameter in the entries ArrayList.

Returning number of entries

It may also be useful to find out how many entries the diary is holding, which is achieved by the following method.

<<Returning number of entries>>=
public int numberOfEntries()
    {
        return entries.size();
    }

The method signature has a realistic name numberOfEntries as it is going to tell the user how many entries are in the diary. The method is of type int, this indicates that it is going to return an integer value to the user by the end of the method. It has no parameters passed to it, as the ( and ) are empty.

The body of the method is very basic. It uses the size method of the ArrayList to return the number of entries in the entries ArrayList.

Deleting an entry

There may come a time when the user wishes to delete an entry from the diary, the method below allows them to do this.

<<Deleting an entry>>=
public void removeEntry(int entryNumber)
    {
        if(entryNumber < 0) {
            // This is not a valid entry number, so do nothing.
        }
        else if(entryNumber < numberOfEntries()) {
            // This is a valid entry number.
            entries.remove(entryNumber);
        }
        else {
            // This is not a valid entry number, so do nothing.
        }
    }

The method signature indicates what the method is going to perform as it is called removeEntry.It is of type void, so it wont return any value to the user. There is a parameter passed to it, called entryNumber, which is of type int, or integer.

The first line of the method body is an if statement. This allows execution of one of two actions depending on the result of a check. In this case the check is to see if entryNumber, which is the a number passed into the method by the user, is less than zero. If the number is less than 0 the method does nothing. If the number is greater than zero, the next line is executed, which is another check in the form of an else if statement.

This checks if the entryNumber is less than the value returned by the numberOfEntries method. If entryNumber is less than the result then the remove method of the ArrayList class is used to remove the entry corresponding to the entryNumber variable from the entries ArrayList.

If the entryNumber value is greater than the value returned by the numberOfEntries method, the following else statement is executed, which does nothing, as the value input by the user is not valid.

It is important to understand how the process of deleting an entry from the diary affects the index values of other entries in the collection. If an item with a low index number is removed, all following items are moved along by one position to fill the gap. This means thier index numbers will be decreased by 1.

Listing all entries

Because a user may delete entries from the diary over time the index numbers may change quite significantly. It may be useful for the user to be able to list all entries, along with thier corresponding index values so they can see an up to date picture of the whole diary. The following method achieves this.

<<Listing all entries>>=
public void listEntries()
    {
        for(String entry : entries) {
            System.out.println(entry);
        }
    }

This method has a realistic name listEntries, which indicates what it does. It is of type void, so it wont return any value to the user. It also takes no parameters, which is evident by ( and ) being empty.

The body is taken up by a for loop. A problem faces the programmer when it comes to writing a method such as this because the user wants to list all the entries in a collection, but at the time of writing code the programmer doesnt know how long the collection is going to be. A for loop solves this by allowing a set of actions to be performed repeatedly, but without writing the actions out for everytime they need to be performed.

A for loop contains two parts:

  1. Header- for(String entry : entries). This is where the loop details are defined. The first piece of code in the ( and ), String entry is known as a loop variable. This is a local variable that will be used to hold the list elements(in this case diary entries). This must be of type String because the collection that is being used is of type String. A colon is then used, followed by entries, which is the collection that is being used in the Diary class. This code stores one element in the collection entries into the local variable entry, starting at index 0, and increasing by 1 everytime the body of the for loop is executed.
  2. Body- System.out.println(entry);. This statement prints the value of the local variable entry to the user each time the loop is executed.
<<diary.java>>=
Defining the Class
Download code
Views