Skip to main content
ICT
Lesson A2 - Object Oriented Programming
 
Main   Previous Next
 

B. Program Components page 4 of 10

  1. Programming languages allow the inclusion of comments that are not part of the actual code. Documentation is an important aspect of writing programs as it helps the reader to understand what is going on. Java provides three different styles of comments.

  1. Programmers try to avoid “reinventing the wheel” by using predefined libraries of code. In Java, these predefined classes are grouped into packages. The import statement allows the program to use predefined classes.

  2. Java comes with many packages. There are also many packages created by programmers that you can use. There are two non-standard packages supplied in this curriculum guide. The one we have been using so far is called gpdraw. In our example program, the DrawingTool and SketchPad classes are imported from the gpdraw package with the statement

import gpdraw.*;

  1. Every Java program contains a main method. However, not all classes need to contain a main method, nor should they.

  2. Most classes will contain methods to define a class’s behavior. Here we will explore the basic syntax of a method. You will learn how to utilize all of these parts in Student Lesson A4 - Object Behavior. A method has the following general syntax:

  • The modifiers refer to a sequence of terms designating different kinds of access to methods. (e.g. public, private)
  • The method_name is the name of the method. In the case of Code Sample 2.1, the name of the method is draw.
  • The return_type refers to the type of data a method returns. The data type can be one of the predefined types (e.g., int, double, char, void, String, etc.) or a user-defined type. In Code Sample 2.1, the draw method does not return a value and is therefore designated by the void type.
  • The parameters list will allow us to send values to a method. In our example, we must tell the forward method how far to go. (myPencil.forward(100)).
  • The method_body contains statements to accomplish the work of the method. In this example, there are seven lines of code needed to draw the square.

 

Main   Previous Next
Contact
 © ICT 2006, All Rights Reserved.