ArrayOfObjects HTML
ArrayOfObjects HTML
html
Array of Objects
Basic operators| File I/O|
Array of Objects
A systematic technique for accessing objects of different dynamic type within the same hierarchy is through an array of
pointers of their static type. The executable code dereferences each pointer at run time based on its object's dynamic type.
[read dynamic & static type again]
In the previous topic, A antique shop that sells antique items, namely vases, statues, and paintings. For now, we want to
manage the list of objects such as vases, statues, paintings in an array. The objects pointed to by the array elements may
be of differing dynamic type, but are of the same static type(type of the superclass).
for example, we create the file named ItemList.java, it contains an array as follows:
we will implement these later. For now, the main method to test some basic functions in the antique shop.
}
}
After starting, the obj variable points to address 1000, this block contains the reference variable list that is pointing to
address 2000. It is an array of pointers. All elements are initiated to null
To complete the program, we add a method named addItem to the ItemList class.
//this method removes the item at the specified position in this list.
//Shifts any subsequent elements to the left
//input: the index you wish to remove
pulic boolean removeItem(int index){
if( index >= 0 && index < numOfItem){
for(int j=index; j< numOfItem; j++ ){
list[j]=list[j+1];
}
numOfItem --;
return true;
}
return false;
}
//this method prints out all items that belong to the given type in the list.
public void displayItemsByType(String type){
if (type.equals("Vase")){
for(int i=0; i < numOfItem; i++)
if ( list[i] instanceof Vase) System.out.println( list[i]);
}
else if (type.equals("Statue")){
for(int i=0; i < numOfItem; i++)
if ( list[i] instanceof Statue) System.out.println( list[i]);
}
else {
for(int i=0; i < numOfItem; i++)
if ( list[i] instanceof Painting) System.out.println( list[i]);
}
}
//this method sorts items in ascending order based on their values.
public void sortItems(){
for(int i=0; i< numOfItem; i++)
for(int j=numOfItem-1; j>i ;j--){
if( list[i].getValue()< list[j-1].getValue()){
Item tmp=list[j];
list[j]=list[j-1];
list[j-1]=tmp;
}
}
file:///C:/Users/Admin/Downloads/1_PRO192/ArrayOfObjects.html 4/19
6/8/23, 10:34 PM ArrayOfObjects.html
break;
case 6:
int index=2;
if( obj.updateItem(index)){
System.out.println("After updating: ");
obj.displayAll();
}
else System.out.println("can not update the item");
break;
case 7:
int index=1;
if( obj.removeItem(index)){
System.out.println("After removing: ");
obj.displayAll();
}
else System.out.println("can not remove the item");
break;
case 8:
String type="Painting";
obj.displayItemsByType(type);
break;
case 9:
obj.sortItems();
obj.displayAll();
}//end switch
...
}
}
file:///C:/Users/Admin/Downloads/1_PRO192/ArrayOfObjects.html 5/19
6/8/23, 10:34 PM ArrayOfObjects.html
The findItemIndex method finds the item within the list and returns the position of the first occurrence in the list. For
example, if you wish to get the position of the item which value is 100 then the method returns 0
The findItem method finds the item within the list and returns the address of the first occurrence in the list. For example, if
you wish to get the item which creator is Paris then the method returns 300
If you wish to remove the item at index 1, the method removeItem(index) will shift any subsequent elements to the up. After
shifting, the old item that was at position 1 becomes garbage and Java will deallocate it.
I/O Stream
Input and output data sources of a program can be the keyboard, monitor, file in an external disk. In this lesson, basic
concepts and tools for handling all kinds of data, from primitive values to advanced objects are introduced.
File is a group of related data which are stored in external memory (disk) for the common use between some programs.
The file designer will decide the format of stored data. In general, a thing containing data is called a file. So, a directory is a
file, a device( keyboard, monitor, network interface card, disk,…) is a file. Data in a normal file can include text, images,
sounds. Information stored in a file must be persistent. This means that data in a file must be survived even when the
process, which created it, terminated.
Text format: a data unit is a character or digit (ASCII code). So, all numbers in a
program (numeric variables) must be transferred to a string of digits before they are
written to file.
Binary format: data in a file is a figure of a variable’s memory bitmap in the program.
Text format is flexible than binary format but costs must be paid for type conversions. Files in text format can be viewed,
updated using any normal editor such as Notepad, MS Word, …
The binary format is efficient because no data type transfer is needed but data in a file can be accessed by appropriate
programs only.
1. Open file
2. Read data from file to a program’s variable or write the value in the program’s variable to file
3. Close file
STREAM
An I/O Stream represents an input source or an output destination. A stream can represent many different kinds of sources
and destinations, including disk files, devices, other programs, and memory arrays.
I/O Stream contains a reference to a data source, methods for accessing data in the source, and methods for type
conversions
Streams support many different kinds of data, including simple bytes, primitive data types, localized characters, and
objects. Some streams simply pass on data; others manipulate and transform the data in useful ways (type conversions
may be needed).
No matter how they work internally, all streams present the same simple model to programs that use them: A stream is a
sequence of data. A program uses an input stream to read data from a source, one item at a time and uses an output
stream to write data to a destination, one item at a time.
file:///C:/Users/Admin/Downloads/1_PRO192/ArrayOfObjects.html 6/19
6/8/23, 10:34 PM ArrayOfObjects.html
In the following program, the user will give a pathname, the program will show information about this
pathname in the file system.
file:///C:/Users/Admin/Downloads/1_PRO192/ArrayOfObjects.html 7/19
6/8/23, 10:34 PM ArrayOfObjects.html
Result in 3 cases:
When a text file is used, the file designer will decide text organization (data meanings) in a file. So, all read/write operations
must perform appropriately.
Two following pictures depict ways we can use to access a text file:
file:///C:/Users/Admin/Downloads/1_PRO192/ArrayOfObjects.html 8/19
6/8/23, 10:34 PM ArrayOfObjects.html
users can choose an operation at a time using a simple menu. Operations are required:
- Add a product
- Search a product based on its ID
- Print products based on a part of its name. For example, “TV” or “soundbar”
- Print all products in descending order of prices.
- Save the current list to file, named Products.txt
Constraints:
- The ID of a product must be in the “P000” format.
- The product name is not blank and there is no extra blank.
- The product’s price must be >0.
- The product’s ID is unique.
Class design
- Class Inputter for inputting data to ensure that they satisfy the problem’s constraints.
- Class Menu for program’s menu
- Class Product for a product
- Class ProductList for a list of products
- Class ProductManager for the program.
file:///C:/Users/Admin/Downloads/1_PRO192/ArrayOfObjects.html 9/19
6/8/23, 10:34 PM ArrayOfObjects.html
Software structure and menu:
Note:A class that uses classes in a package should be outside of the package. The ProductManger class is put outside of
the products package.
Implementations
The Inputter class is the tool for inputting data based on the program’s constraints.
file:///C:/Users/Admin/Downloads/1_PRO192/ArrayOfObjects.html 10/19
6/8/23, 10:34 PM ArrayOfObjects.html
file:///C:/Users/Admin/Downloads/1_PRO192/ArrayOfObjects.html 11/19
6/8/23, 10:34 PM ArrayOfObjects.html
file:///C:/Users/Admin/Downloads/1_PRO192/ArrayOfObjects.html 12/19
6/8/23, 10:34 PM ArrayOfObjects.html
file:///C:/Users/Admin/Downloads/1_PRO192/ArrayOfObjects.html 13/19
6/8/23, 10:34 PM ArrayOfObjects.html
file:///C:/Users/Admin/Downloads/1_PRO192/ArrayOfObjects.html 14/19
6/8/23, 10:34 PM ArrayOfObjects.html
EXERCISE
Upgrade the Product Manager program above. Add the following extra operations to this program:
- Print products whose prices are between min Price and max Price
- Update name and price of a product based on its ID.
- Remove a product based on its ID
Each programming language chooses a way to serialize an object. Following are some concerns:
- Whether class information of the object is stored in a file or not.
- What is the order of fields that will be stored? Down order (field 1, filed 2,…) is applied or up order should be used?
To serialize data of objects to a stream, a class of objects must implement the java.io.Serializabe interface. This interface
has no method. The Java compiler will add systematic codes needed to serialize and de-serialize between objects and a
binary stream.
Because object files are binary, users can not use text editors to see or edit data. All read and write operations must be
performed by suitable programs.
Not all data of an object are serialized to a stream. static and transient fields are not serialized.
Note:
file:///C:/Users/Admin/Downloads/1_PRO192/ArrayOfObjects.html 15/19
6/8/23, 10:34 PM ArrayOfObjects.html
- the static modifier is used when a field is the common field of a class.
- the transient modifier is used when a field having value as a result of computation from other fields. [read more]
Model for reading objects from and writing objects to an object stream.
The following image helps us explore the structure of an object file in Java.
1. Package and class information including field names, field types (I: int) are put at the beginning of the file.
2. Fields are written to file in reverse order.
Each time objects are written to file, information about package and class will be written. We are noticed about properties
when manipulating object files as below:
Problem:The following demonstration will depict the way to access an object file containing some books.
file:///C:/Users/Admin/Downloads/1_PRO192/ArrayOfObjects.html 16/19
6/8/23, 10:34 PM ArrayOfObjects.html
file:///C:/Users/Admin/Downloads/1_PRO192/ArrayOfObjects.html 17/19
6/8/23, 10:34 PM ArrayOfObjects.html
The result:
file:///C:/Users/Admin/Downloads/1_PRO192/ArrayOfObjects.html 18/19
6/8/23, 10:34 PM ArrayOfObjects.html
EXERCISE
Develop the above demonstration program for managing books with the following specifications:
- Book .
- Filename: books.dat
- Menu of the program:
- Add a book
- Search a book based on ID
- Update a book
- Remove a book
- Print books of an author
- Print books having titles containing a sub-string
- Print books in ascending order of authors then ascending prices
- Save the list to file
SUMMARY
File is a group of related data which are stored in external memory (disk) for the common use between some
programs.
A thing containing data is called a file. So, a directory is a file, a device (keyboard, monitor, network interface card,
disk,…) is a file.
An I/O Stream represents an input source or an output destination. A stream can represent many different kinds of
sources and destinations, including disk files, devices, other programs, and memory arrays.
The java.io package contains interfaces and almost of classes for accessing text files and binary files including
object files.
The java.io.File supports methods for accessing basic information of a file or folder.
Course Slide
Array of Objects.pdf
File I/O.pdf
file:///C:/Users/Admin/Downloads/1_PRO192/ArrayOfObjects.html 19/19