Студопедия
Случайная страница | ТОМ-1 | ТОМ-2 | ТОМ-3
АрхитектураБиологияГеографияДругоеИностранные языки
ИнформатикаИсторияКультураЛитератураМатематика
МедицинаМеханикаОбразованиеОхрана трудаПедагогика
ПолитикаПравоПрограммированиеПсихологияРелигия
СоциологияСпортСтроительствоФизикаФилософия
ФинансыХимияЭкологияЭкономикаЭлектроника

Structure of a program. Running an application.

More on operators and expressions | Numeric promotions | Objects and references | The class String | Useful examples | Defining attributes of objects | Defining operations on objects (methods) | Defining methods of object creation (constructors) | Static members | Packages and imports |


Читайте также:
  1. A diverse educational system: structure, standards, and challenges
  2. Adel fell down while he was running quickly. (during)
  3. Atlantic salmon muscle structure
  4. B. SENTENCE-STRUCTURE.
  5. Business organization structure
  6. Ch.2 – OS Structures
  7. Chapter 1. The Scientific Notion of Language and Structure

A Java program is a set of class definitions.

Outside the class body no source code can appear except package declaration, import declarations and comments.

 

The structure of a Java program is depicted on the following framework:

package... // package statement (optional)import... // import statement (not always necessary)import... // this is class A public class A {...} // this is class B class B {...}...


A package declaration specifies to which package belong the classes defined in the source file after compilation.

A program may consist of one or more source files.
(in particular: all the classes of a program may be included in a single file, or each class may be defined in a separate source file).

Execution of an application starts from the method:

public static void main(String[] args)

which must be defined in some class.

Consider file Work.java with definitions of two classes: Work and Other:

public class Work {
public static void main(String[] args) {
...
}
...
}
class Other {
...
}

// End of file Work.java

After compilation (javac Work.java) there are two files:
Work.class
Other.class

Java virtual machine is started with the command java which takes the following arguments:

java Work [optional arguments passed to the main() as String[] args]

Java virtual machine starts up by loading a specified class and then invoking the method main in this specified class.

 

 

Summary


The lecture concentrates on object-oriented matters. The following issues have been described in detail:

Now we are able to create interesting and quite advanced classes. We know too, how to properly locate the classes in the structure of the program.

 

Exercises

 

  1. Add methods to the class Pair allowing subtraction, multiplication and integer division of pairs of integers. Add methods calculating the average and sum of components of a pair.
  2. Define class Triplet (representing triplets of real numbers) with functionality similar to that of the class Pair from the exercise 1.
  3. Modify the method show in both classes so as to make it display using dialog boxes the following information: values in the first line, sum of values in the second line and the average in the third line.
  4. In the class Disk provide summing of gross prices of all objects (disks) being created in the program.

 

Lecture 9
Decisions

This lecture begins with a detailed presentation of control statements. It is impossible to write programs without mastering this type of instructions. We start with a summary of control statements and a presentation of possible ways to define conditions for these statements using relational and logical operators. Next we focus on making decisions. Iteration loops are discussed in the next lecture.


Дата добавления: 2015-11-16; просмотров: 56 | Нарушение авторских прав


<== предыдущая страница | следующая страница ==>
Scope of an identifier. Local variables. Access control.| A brief survey of control statements.

mybiblioteka.su - 2015-2024 год. (0.005 сек.)