Coding the Note class

This is the fundamental data structure of the app. It is a class that we will write ourselves from scratch and has all the member variables we need to represent one user note. In Chapter 13, Handling and Displaying Arrays of Data, you will learn some new Java to understand how we can let the user have dozens, hundreds, or thousands of notes.

Create a new class by right-clicking on the folder with the name of your package, the one that contains the MainActivity.java file. Select Java class under New and name it Note. Click on OK to create the class.

Add the highlighted code to the new Note class:

public class Note {

    private String mTitle;
    private String mDescription;
    private boolean mIdea;
    private boolean mTodo;
    private boolean mImportant;

}

Note that our member variable names are prefixed with m as per the Android convention. Furthermore, as there is no reason for any other class to access these variables directly, they are all declared private.

We will, therefore, require a getter and a setter method for each of our members. Android Studio can quickly do this for us:

  1. Right-click below the last member variable declaration, but above the closing curly brace of the class.
  2. From the context menu, select Getter and setter under Generate.
  3. As we want to generate getters and setters for all the members, in the Select fields to generate getters and setters window, select each of the members individually by holding the Shift key and clicking on each of the members in turn. After this step, this window will look like as shown in the next screenshot:
    Coding the Note class
  4. Now, click on OK and all the getter and setter methods will be generated for us.

Take a look at the code that was just generated, and then we can discuss it. Note that Android Studio has saved us a lot of time by generating many methods. Also note that the names of the methods are imperfect. They have been generated as getmTitle, setmTitle, and so on. We want them to be neater and easier to read. So, quickly modify each of the method names by removing m from between the middle of each method name so that they are like this: getTitle and setTitle.

Here is what our getter and setter methods should look like at this point:

public String getTitle() {
  return mTitle;
}

public void setTitle(String mTitle) {
  this.mTitle = mTitle;
}

public String getDescription() {
  return mDescription;
}

public void setDescription(String mDescription) {
  this.mDescription = mDescription;
}

public boolean isIdea() {
  return mIdea;
}

public void setIdea(boolean mIdea) {
  this.mIdea = mIdea;
}

public boolean isTodo() {
  return mTodo;
}

public void setTodo(boolean mTodo) {
  this.mTodo = mTodo;
}

public boolean isImportant() {
  return mImportant;
}

public void setImportant(boolean mImportant) {
  this.mImportant = mImportant;
}

There is quite a lot of code here but nothing complicated. Each of the methods has public access specified so that it can be used by any other class that has a reference to an object of the Note type. Furthermore, for each variable, there is a method with the name get… and a method of the name set…. The getters for the Boolean type variables are named is…. This is a logical name if you think about it because the returned answer will be either true or false.

Each of the getters simply returns the value of the variable and each of the setters sets the value of the variable to whatever value/parameter is passed in to the method.

In fact, we should really enhance our setters a little to do a bit of checking to make sure that the values passed in are within reasonable limits. For example, we might want to set a maximum or minimum length for String mTtile and String mDescription. We won't do so here, however, as this extraneousness will only serve to cloud the real learning objectives of this project.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.119.104.238