December 13, 2015

Java Application Exercise 1-1: Define Class And Generate Object

Learning points 


In this exercise we will study about:
  1. Defining class in Java.
  2. Constructor.
  3. Setter and Getter.
  4. Generate object.
  5. Call class function.

Summary


A health food manufacturer is on preparation of opening new sales office in Tokyo and Osaka. First, we will show each sales office information. Following information need to be displayed.

Expected result 


[Shop] This is Tokyo Shop. [Tel] 03-1234-5678
[Shop] This is Osaka Shop. [Tel] 06-1234-5678


Now let's create some java application to fulfill the above specification.


1. Define shop class


First we need to create a java class. Let's call it Shop.java. It will contain shop's name and telephone number information, so we will also add 2 variables in it.

public class Shop {
    //members
    private String shopName;
    private String shopTel;
}


2. Add class constructor.


A constructor is a block of code, which will run when we use new keyword to initiate an object/class. In this exercise the constructor will be triggered when shop.java class is created.

/**
 * Default Constructor
 * It is also called as constructor without arguments/parameter
 */
public Shop() {
    //inside here is the code to initiate class members.
}

/**
 * Parameterized constructor
 * Used when we need to give initial value to class members
 */
public Shop(String shopName, String shopTel) {
    this.shopName = shopName;
    this.shopTel = shopTel;
}

3. Add Setter and Getter methods


Setter and getter in Java is just an ordinary functions that make private members inside a class accessible from methods in another class. Setter and getter functions is not necessary if you set the member to public. The reason for using setter and getter instead of making class members public is that it makes it possible to change the implementation without changing the interface.

/**
 * Setter for shopName
 */
public void setShopName(String shopName) {
    this.shopName = shopName;
}

/**
 * Setter for shopTel
 */
public void setShopTel(String shopTel) {
    this.shopTel = shopTel;
}

/**
 * Getter for shopName
 */
public String getShopName() {
    return shopName;
}

/**
 * Getter for shopTel
 */
public String getShopTel() {
    return shopTel;
}

4. Add method to show shop's information


Next, we need a method to show shop's name and telephone number.

/**
 * Method to show shop's information
 */
public void printShopInfo(){
    System.out.println("[Shop] This is " + shopName + ". [Tel] " + shopTel);
}

Shop.java class parts complete now. This is the full code of Shop.java class.

public class Shop {
    private String shopName;
    private String shopTel;
 
    /**
     * Default Constructor
     * It is also called as constructor without arguments/parameter
     */
    public Shop(){
        //inside here is the code to initiate class members.
    }
    /**
     * Parameterized constructor
     * Used when we need to give initiate value to class members
     */
    public Shop(String shopName, String shopTel) {
        this.shopName = shopName;
        this.shopTel = shopTel;
    }
 
    /**
     * Method to show shop's information
     */
    public void printShopInfo(){
        System.out.println("[Shop] This is " + shopName + ". [Tel] " + shopTel);
    }
 
    /**
     * Setter for shopName
     */
    public void setShopName(String shopName) {
        this.shopName = shopName;
    }

    /**
     * Setter for shopTel
     */
    public void setShopTel(String shopTel) {
        this.shopTel = shopTel;
    }

    /**
     * Getter for shopName
     */
    public String getShopName() {
        return shopName;
    }

    /**
     * Getter for shopTel
     */
    public String getShopTel() {
        return shopTel;
    }
}

Create main method.


The next thing we should do is to create main method. A Java program needs to start its execution somewhere. A Java program starts by executing the main method of some class. we can name the class up to us, but not the name of the method must always be called main. We will create main method in JavaApplication.java class

public class JavaApplication {

    public static void main(String[] args) {
        //create tokyo shop object
        Shop tokyoShop = new Shop("Tokyo Shop","03-1234-5678");
        //create Osaka shop object
        Shop osakaShop = new Shop("Osaka Shop","06-1234-5678");
     
        //Call method to print Tokyo shop's information.
        tokyoShop.printShopInfo();
        //Call method to print Osaka shop's information.
        osakaShop.printShopInfo();
    }
}

Now execute the application and see the result as below.

[Shop] This is Tokyo Shop. [Tel] 03-1234-5678
[Shop] This is Osaka Shop. [Tel] 06-1234-5678

Enjoy your coding.
Share:

0 comments:

Post a Comment