Software Engineering for Students A Programming Approach

Software Engineering for Students A Programming Approach Software Engineering for Students A Programming Approach

web.firat.edu.tr
from web.firat.edu.tr More from this publisher
21.08.2013 Views

without difficulty. For example: JButton button = new JButton("go"); 16.3 Using packages 225 If the import statement was omitted, we could still use the class JButton, but we would need to refer to it by its full name – javax.swing.JButton. This would be inconvenient and cumbersome. Hence we see the value of the import statement. If we only need to import an individual class, say JButton, from within a package, we can spell it out: import javax.swing.JButton; Using * means that we want to import all the classes within the named package. So if we need to use more than one from a package, it is simpler to use the * notation. SELF-TEST QUESTION 16.1 Classes called Monday, Tuesday, Wednesday, Thursday, Friday, Saturday and Sunday are grouped in a package called week. Write down the Java import statement that will be needed to use the class Friday. Write the statement to create an object friday of the class Friday. Write down the import statement that will be needed to use all the classes in the package. In C and C++, a program almost always begins with an include declaration. For a system package the declaration is typically: #include or, for a programmer’s own package: #include "myFile.h" The include statement is a directive to the compiler to include within the current source file a copy of the named file. A file with the suffix .h is, by convention, a header file and it is files of this type that normally appear in the include statement. A header file is a source code file that contains the declaration of the methods to be found within a package. These declarations are, in C terminology, the prototypes of the available methods – their names and parameters. The C and C++ languages have a rule that a method has to be declared (textually) before it can be used. When a declaration like this is present, the program can refer to the methods within the package described and it will compile successfully. Subsequently the object code of the packages is linked.

226 Chapter 16 ■ Programming in the large 16.4 ● Creating packages Suppose a system consists of three groups of classes: 1. classes that handle the user interface 2. classes that access the database 3. classes that handle the central logic of the program. We create three packages, named gui, database and logic. Next we need to ensure that individual classes are in the correct package. In Java this is accomplished using the package statement written at the head of the class. So, if we have a class named Login that handles the login part of the GUI, we write the following at the head of the class: package gui; public class Login If you omit a package statement, it means that the class is placed, by default, in a package with no name. SELF-TEST QUESTION 16.2 A class named Backup is to be placed in a package named database. Write the package statement and the class heading. 16.5 ● Scoping in large programs We have already seen how a program can access other packages using import or include statements. The issue is: What packages, methods and (rarely) variables are accessible to any given package? When you create and use packages, some new scope rules come into play. The essence is that classes within the same package can access each other very easily. When you write a method in Java, you specify that it is private, public, protected or simply give it no prefix. The prefix determines who can access the method: public means that the method is available to all. private means that it is accessible only within the class. protected means that the method can be used by any subclass. If you give a method no prefix, it means that the method is accessible from anywhere within the same package, but inaccessible from outside. This is also true of classes, constructors and variables. This means that the programmer can establish a close relationship between methods in the same package. This accords with the idea that classes in the same package are related to each other.

without difficulty. For example:<br />

JButton button = new JButton("go");<br />

16.3 Using packages 225<br />

If the import statement was omitted, we could still use the class JButton, but we<br />

would need to refer to it by its full name – javax.swing.JButton. This would be<br />

inconvenient and cumbersome. Hence we see the value of the import statement.<br />

If we only need to import an individual class, say JButton, from within a package,<br />

we can spell it out:<br />

import javax.swing.JButton;<br />

Using * means that we want to import all the classes within the named package. So<br />

if we need to use more than one from a package, it is simpler to use the * notation.<br />

SELF-TEST QUESTION<br />

16.1 Classes called Monday, Tuesday, Wednesday, Thursday, Friday,<br />

Saturday and Sunday are grouped in a package called week. Write<br />

down the Java import statement that will be needed to use the class<br />

Friday. Write the statement to create an object friday of the class<br />

Friday. Write down the import statement that will be needed to use all<br />

the classes in the package.<br />

In C and C++, a program almost always begins with an include declaration. For a<br />

system package the declaration is typically:<br />

#include <br />

or, <strong>for</strong> a programmer’s own package:<br />

#include "myFile.h"<br />

The include statement is a directive to the compiler to include within the current<br />

source file a copy of the named file. A file with the suffix .h is, by convention,<br />

a header file and it is files of this type that normally appear in the include statement.<br />

A header file is a source code file that contains the declaration of the methods<br />

to be found within a package. These declarations are, in C terminology, the prototypes<br />

of the available methods – their names and parameters. The C and C++ languages<br />

have a rule that a method has to be declared (textually) be<strong>for</strong>e it can be used.<br />

When a declaration like this is present, the program can refer to the methods within<br />

the package described and it will compile successfully. Subsequently the object<br />

code of the packages is linked.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!