Join Peggy Fisher for an in-depth discussion in this video Flowcharting and UML diagrams, part of Learning Java 8.
- Flowcharts are a great way to create a visual representation of our program flow. UML Diagrams, Unified Modeling Language, also provide a visual of any classes we need to create. When creating flowcharts, it forces us to break our programs down into pieces, otherwise known as stepwise refinement. This shows us the processes needed to create our final solution. From these flowcharts, we can start to encapsulate portions of our code into smaller methods. Here are some of the basic symbols that we will be using during this course.
A start and end symbol, which is a round circle, a process, or a rectangle, a decision, which is a diamond, for data we'll use a parallelogram, and we also have a symbol for files that looks like reports. Let's take a simple problem and use a flowchart to describe our solution. How about we model a cash register at a grocery store? What is the flow of this process? As you can see at the top, we have our start circle. The first thing we do is an action item, or a process that says scan the item to add to total costs.
Now we have a decision box, a diamond. Are there more items? If yes, we loop back up and scan the next item. If no, we go to the next process box and calculate our total. Then we collect the amount due from the customer, and we're done. Now let's look at another diagraming technique that we will use when we get to the section on classes and objects, UML, or Unified Modeling Language. This is used for visualizing your classes. Here's an example of a UML diagram.
This diagram represents a cash register class. A neighborhood grocery store might only have one cash register, but if you want to allow for expansion, we can create multiple cash registers from this one class to represent the different registers at the store. If we look closely at the information contained in the diagram, we notice that there are three sections. The top rectangle contains the class name. The next rectangle represents the instance data for the class. In this example, we have the customer's loyalty card number, the total number of items, and the total cost.
These items will be defined as variables in our class in our program. In the last rectangle, we have a list of actions that need to be performed on objects created using this class as a blueprint. These will become our methods. They are addCost, which adds the cost of the one item scanned to the total, getItemCount returns the number of items in this transaction. getTotal returns the total cost of this transaction. getCustomerNumber returns the customer loyalty card number.
For each of the variables, we indicate the data type. For example, customerCardNumber and itemCount are both integer values. The totalCost is a double because it contains a decimal point for precision. The methods include a similar value, but this is the return type from the call to this method. If no value is returned, it is marked as void. This is also where we specify any additional data needed by the method. For example, the addCost method needs the price of the item so that it can add it to the total.
In this case, that is represented by parentheses, the word "double", which describes the type of number, closed parentheses. Finally, did you notice the symbols in front of the data items and the method headers. The minus sign represents private. The data encapsulated in an object should be declared with the visibility modifier of private. This forces the program to access the data using a method to help maintain data integrity. The plus signs represent items that should be public.
Most of the methods in a class will be declared as public unless they are helper methods, and they only need to be accessible inside the class. We will get more practice with UML diagrams later in the course. For more information on UML diagrams, refer to Foundations of Programming: Object-Oriented Design with Simon Allardice, Introduction to the Unified Modeling Language, UML.
- Downloading and exploring NetBeans
- Understanding Java basics: data types, strings, arrays, and more
- Controlling flow with functions and loops
- Creating classes
- Sorting and searching arrays
- Manipulating files
- Handling errors
- Building GUIs