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

Creation and using one-dimensional and multi-dimensional arrays in the programming language Java.

Procedural programming | Object oriented programming | Classification of data types in the programming language Java. | Floating-point numbers | Generics in the programming language Java. | Continue Statement | Declaring Pointers |


Читайте также:
  1. A BRIEF OUTLINE OF THE DEVELOPMENT OF THE ENGLISH LITERARY (STANDARD) LANGUAGE
  2. A Dictionary of the English language
  3. A foreign language serves the aim and the means of teaching
  4. A general model for introducing new language
  5. A humorous drawing, often dealing with something in an amusing way
  6. A) Read the text below to find out about using gestures in different cultures.
  7. A) the language style of poetry; b) the language style of emotive prose; c) the language style of drama.

Java, as with most languages, supports multi-dimensional arrays - 1-dimensional, 2-dimensional, 3-dimensional,... In practice most arrays are one-dimensional, and two-dimensional (rows and columns) are also quite common. Higher dimensional arrays are less common so they aren't used in the examples, but there's nothing mysterious about them and the same principles apply.

Two-dimensional arrays are used whenever the model data is best represented with rows and columns, or has two varying aspects (eg, gender and age, weight and height,...). It's also the idea, altho not the implementation, of graphics that specifies a two (or three) dimensional position with an x and y (and z) coordinate.

 

Java builds multi-dimensional arrays from many one-dimensional arrays, the so-called "arrays of arrays" approach.

There are a couple of interesting consequences of this: Rows may be different sizes. Also, each row is an object (an array) that can be used independently.

Multi-dimensional arrays are built from multiple one-dimensional arrays

As with all arrays, the new keyword must be used to allocate memory for an array. For example,

int[][] a = new int[2][4];

This two-dimensional array will have two rows and four columns.

This actually allocates 3 objects: a one-dimensional array of 2 elements to hold each of the actual row arrays, and a two one-dimensional arrays of 4 elements to represent the contents of the rows.

  In Java two-dimensional arrays are implemented is a one-dimensional array of one-dimensional arrays -- like this.

 

12) Inheritance in the programming language C++

inheritance is the process by which new classes called derived classes are created from existing classes called base classes. The derived classes have all the features of the base class and the programmer can choose to add new features specific to the newly created derived class.

For example, a programmer can create a base class named fruit and define derived classes as mango, orange, banana, etc. Each of these derived classes, (mango, orange, banana, etc.) has all the features of the base class (fruit) with additional attributes or features specific to these newly created derived classes. Mango would have its own defined features, orange would have its own defined features, banana would have its own defined features, etc.

Inheritance Example:

Sample Code

#include <iostream>

using namespace std;

class exforsys

{

public:

exforsys(void) { x=0; }

void f(int n1)

{

x= n1*5;

}

 

void output(void) { cout << "n" << "x=" << x; }

 

private:

int x;

};

 

class sample: public exforsys

{

public:

sample(void) { s1=0; }

 

void f1(int n1)

{

s1=n1*10;

}

 

void output(void)

{

exforsys::output();

cout << "n" << "s1=" << s1;

}private:

int s1;

}; int main(void)

{ sample s;

s.f(10);

s.f1(20);

s.output();

 

13) Working with I / O system in the programming language Java

Java I/O Overview

To understand the issues associated with performing I/O in Java, it is necessary to briefly review the Java

I/O model.

When discussing Java I/O, it is worth noting that the Java programming language assumes two distinct

types of disk file organization. One is based on streams of bytes, the other on character sequences. Byteoriented I/O includes bytes, integers, floats, doubles and so forth; text-oriented I/O includes characters and

text. In the Java language a character is represented using two bytes, instead of the one byte representation

in C/C++. Because of this, some translation is required to handle characters in file I/O. In this project, since our major concern is to compare Java I/O to that of C/C++, we will focus on the byte-oriented I/O.

In Java, byte-oriented I/O is handled by input streams and output streams, where a stream is an ordered

sequence of bytes of unknown length. Java provides a rich set of classes and methods for operating on byte input and output streams. These classes are hierarchical, and at the base of this hierarchy are the abstract classes InputStream and OutputStream. It is useful to briefly discuss this class hierarchy in order to clarify the reason why we are interested in FileInputStream/FileOutputStream, BufferedInputStream/

BufferedOutputStream, and RandomAccessFile in our test cases. Figure 2.1 provides a graphical

representation of this I/O hierarchy. Note that we have not included every class that deals with byteoriented I/O but only those classes that are pertinent to our discussion.

 

2.1 InputStream and OutputStream Classes

The abstract classes InputStream and OutputStream are the foundation for all input and output streams.

They define methods for reading/writing raw bytes from/to streams. For example, the InputStream class provides methods for reading a single byte, a byte array, or reading the available data into a particular region of a byte array. The OutputStream class provides methods for writing that are analogous to those of InputStream.

 

14. Definition and declaration of class methods in the programming language C++.

Each function that you declare for your class must have a definition. The definition is also called the function implementation. Like other functions, the definition of a class method has a function header and a function body.

The definition must be in a file that the compiler can find. Most C++ compilers want that file to end with.C or .CPP. This book uses .CPP, but check your compiler to see what it prefers.

The declaration of a class tells the compiler what the class is, what data it holds, and what functions it has. The declaration of the class is called its interface because it tells the user how to interact with the class. The interface is usually stored in an .HPP file, which is referred to as a header file. The function definition tells the compiler how the function works. The function definition is called the implementation of the class method, and it is kept in a .CPP file. The implementation details of the class are of concern only to the author of the class. Clients of the class--that is, the parts of the program that use the class--don't need to know, and don't care, how the functions are implemented.

15. Access specifiers: public, private and protected in the programming language C++.


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


<== предыдущая страница | следующая страница ==>
The return Statement| Access labels

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