An example about making a Java application using NetBeans. Before reading this post, you may read the following two posts:

A Java application may consist of one or more classes. A class with the main method can be designated as the Main class.

This example has the following parts.

  1. Problem Statement

  2. Step 1: Set up a Java Project MyApp

  3. Step 2: Create three Packages

  4. Step 3: Create a new Java Class classes.Person

  5. Step 4: Create a new Java Class managers.PersonManager

  6. Step 5: Create a new Java Class apps.StaffManagement

  7. Step 6: Configure and Run the Project

  8. Step 7: Build a Distribution

  9. Step 8. Run the Application

  10. Step 9. Share the Distribution

  11. Exercise

  12. Possible Further Improvements

Problem Statement

The purpose of this example is about illustrating one structural design in an application prototype. The prototype contains three classes.

The class, Person, is designed to meet the following object specifics:

  • It possesses the following private property members: firstName, lastName, gender, email, bDate

  • It must have a constructor Person() that initializes lastName, firstName and gender for each new Person instance.
  • It allows outsiders to read its property members.
  • Person.setEmail: Person allows outsides to update the email value.

  • Person.toString: Person can custom a string representation by overriding the default Object.toString method.
  • Person.print: Person can print its String representation to the console.

The class, PersonManager, will perform the following functions to manage a collection of Person objects:

  • PersonManager has a property managerName.
  • PersonManager has a property staff which stores a collection of Person in an ArrayList.

  • Each new PersonManager instance can be initialized with a managerName value by constructor.
  • PersonManager provides interfaces to read its property values.

  • PersonManager.addPerson: create and add a Person instance into staff.
  • PersonManager.addPeople: add a collection of Person instances into staff.
  • PersonManager.printStaff: print each Person by calling Person.print method.

The class, StaffManagement, is the Main class that will perform the following functions that have been designed for the application prototype:

  • Instantiate a new PersonManager with a managerName “staffManager”.
  • Call staffManager to add three new Person instances, each with specific firstName, lastName and gender values.
  • Call staffManager to print the staff list.
  • Update the email value for each staff.
  • Call staffManager to print the staff list after email update.

The following instructions are based on NetBeans IDE 8.2 for Windows.

Step 1: Set up a Java Project

  1. Launch NetBeans.
  2. Create a new Java project by following the menu path: File -> New Project.
  3. Then choose Java Application. Click Next.
  4. Enter the name “MyApp”. Specify a different project location if needed. Uncheck the box “Create Main Class”.
  5. Click Finish.

A new project will open under the Project tab, showing the following project tree.

image-title-here

Step 2: Create three Packages

Create three packages named “classes”, “managers” and “apps”.

To create a new package in a project, right click the node “Source Packages” in the project tree. Follow the menu path: New->Java Package. Enter a name for the package. Click Finish.

Up to now, the project tree should look like the picture below, except you won’t have three .java file in each package. In the following parts, these classes will be added into each package.

image-title-here

Step 3: Create a new Java Class classes.Person

Right click the target package “classes”. Follow New -> Java Class. Enter the name “Person”. Click Finish.

An editor tab titled Person.java will immediately open with the default code.

Enter the following source for classes.Person.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package classes;

public class Person {

    private String firstName;
    private String lastName;
    private String gender;
    private String bDate;   
    private String email;

    public Person(String firstName, String lastName, String gender) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.gender = gender;
    }

    public void print(){
        // Print Person in its String format
        System.out.println(this.toString());
    }
    
    @Override
    public String toString() {
        // Custom a string representation 
        return "Person{" 
                + "firstName=" + firstName 
                + ", lastName=" + lastName 
                + ", gender=" + gender 
                + ", bDate=" + bDate 
                + ", email=" + email 
                + '}';
    }
        
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
    public String getbDate() {
        return bDate;
    }

    public String getGender() {
        return gender;
    }

    public String getLastName() {
        return lastName;
    }

    public String getFirstName() {
        return firstName;
    }

}// End Class

Step 4: Create a new Java Class managers.PersonManager

In the package “managers”, add the Java class PersonManager.

Enter the following source for managers.PersonManager.

(For Java stream function, refer to the post * Processing Collection Data with Java Streams )

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package managers;

import classes.Person;
import java.util.ArrayList;
import java.util.Arrays;

public class PersonManager {

    private String managerName;
    private ArrayList<Person> staff;

    public PersonManager(String managerName) {
        this.managerName = managerName;
        this.staff = new ArrayList<>();
    }

    public void addPerson(String firstName, String lastName, String gender) {
        // Add a new person
        Person temp = new Person(firstName, lastName, gender);
        this.staff.add(temp);
    }

    public void addPeople(String[] fNames, String[] lNames, String[] genders) {
        // Add a group of people
        for (String x : fNames) {
            int pos = Arrays.asList(fNames).indexOf(x);
            this.addPerson(x, lNames[pos], genders[pos]);
        }
    }

    public void printStaff() {
        // Print all person items in ArrayList staff
        this.staff
            .stream()
            .forEach((person) -> {person.print();});
    }

    public String getManagerName() {
        return managerName;
    }

    public ArrayList<Person> getStaff() {
        return staff;
    }

}//End Class

Step 5: Create a new Java Class apps.StaffManagement

In the package “apps”, add the Java class StaffManagement.

Enter the following source for apps.StaffManagementn.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package apps;

import managers.PersonManager;

public class StaffManagement {
    
    public static void main(String[] args) {
        // Instantiate a manager
        PersonManager staffManager = new PersonManager("Staff");
        // Operation set 1
        System.out.println("~~~Initializing three new staff... ");        
        String[] fNames = {"Alpha","Betta","Cita"};
        String[] lNames = {"Cat","Dog","Pig"};
        String[] genders = {"f","m","f"};
        staffManager.addPeople(fNames, lNames, genders);        
        staffManager.printStaff();   
        
        // Operation set 2
        String[] emails = {"acat@org.com", "bdog@org.com", "cpig@org.com"};
        staffManager
                .getStaff()
                .stream()
                .forEach(p ->{
                    int pos = staffManager.getStaff().indexOf(p);
                    p.setEmail(emails[pos]);
                });
        System.out.println("~~~After updating emails: ");
        staffManager.printStaff();
    }//End main
    
}// End staffManagement

Step 6: Configure and Run the Project

Right click the project name in the Projects tab. Follow the menu path: Set Configuration -> Customize….

In the Main Class: field, click Browse… button to select apps.StaffManagement. Click Select Main Class button.

Click OK button to finish configuration.

Again right click the project name in the Projects tab. Click Run to run the project.

image-title-here

Step 7: Build a Distribution

In the Projects tab, right click Clean and Build. In the %PROJECT_HOME% folder, a new folder ‘dist’ will be created for storing at least two files: MyApp.jar and README.TXT.

Step 8: Run the Application

To run the project jar file from the command line, go to the dist folder in the %PROJECT_HOME% directory and type the following:

java -jar "MyApp.jar" 

Or run the jar file anywhere by providing the full path:

java -jar "%PROJECT_HOME%\dist\MyApp.jar" 

A possible screenshot is shown below for Windows console.

C:\> java -jar G:\Projects\MyApp\dist\MyApp.jar
~~~Initializing three new staff...
Person{firstName=Alpha, lastName=Cat, gender=f, bDate=null, email=null}
Person{firstName=Betta, lastName=Dog, gender=m, bDate=null, email=null}
Person{firstName=Cita, lastName=Pig, gender=f, bDate=null, email=null}
~~~After updating emails:
Person{firstName=Alpha, lastName=Cat, gender=f, bDate=null, email=acat@org.com}
Person{firstName=Betta, lastName=Dog, gender=m, bDate=null, email=bdog@org.com}
Person{firstName=Cita, lastName=Pig, gender=f, bDate=null, email=cpig@org.com}

Step 9: Share the Distribution

Compress the entire dist folder into a compressed file (such as a zip file). Send the zip file to the target people.

Exercise:

Make a new format of printing/reporting each Person instance from PersonManager.staff.

Possible Further Improvements:

The class managers.PersonManager can further be augmented to support more functions including, but not limited to:

  • PersonManager.sortStaffByName: Sort people by their names
  • PersonManager.groupStaffByGender: Partition people into two groups for male and female
  • PersonManager.searchStaffByName: Search people for a given name

Hint: Java Streams API. You may read the post Processing Collection Data with Java Streams

Workout

Make a Java project that contains a Main class and the Triangle class (Triangle.java is available at the post Java Review: Classes, Instances, Properties, Methods. )

The main class has the main method that will take three arguments from the user, create a new Triangle instance and assign the three arguments as integers to three side lengths a, b and c in order. Then the user should be notified by the text in the console, telling the user if his triangle is valid or not.

Note: Each command-line argument in the main method is of type String. In order to take the numerical value from an argument, apply Integer.parseInt to the argument.

For example, to convert (parse) the second argument in args to a numerical value, the expression is

Integer.parseInt(args[1])

Submission (in a zip file):

  1. Your application in a .jar file

  2. The source file of all the classes (Triangle.java and the Main class)