chapter_15_arraylists_javabook

Exercise 15 5 an arraylist based array is

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: on of the program that information is ignored. (It is possible to call a non-void method in the same way as methods of type void are called.) You could declare a variable like boolean word_has_been_translated = false ; in method main() and set that to value true when a translation has been made. Exercise 15-2. Improve program Translate.java so that it is capable of translating between four different languages. You can derive a class named FourLanguageTranslation from class TrilingualTranslation. Exercise 15-3. Make program Translate.java to read its translation data from a file. You could convert the program to a translator application with which it would be possible to add new word combinations to the translation data. The program could be a menu-based application similar to program Collect.java. If you can find, for example from the Internet, an existing file which contains translations of words from one language to another, you could make a translation program which uses those existing translations. To make this task simpler, it might be best to translate only between two languages. Exercises related to ArrayList-based arrays Exercise 15-4. In Chapter 7, a program named Reverse.java is introduced. That program uses a conventional Java array to store values of type int. Rewrite the program so that int values are stored to an ArrayList array instead of a conventional array. Exercise 15-5. An ArrayList-based array is particularly useful in an application in which objects are dynamically inserted to an array and removed from an array. Program Collect.java is this kind of application. Rewrite program Collect.java so that you use inside class Collection an ArrayList array in place of the conventional array. This modification should simplify the methods of class Collection. (If you have developed some other program that is similar to Collect.java, it might be a useful idea to use an ArrayList-based array in that program.) 514 Chapter 15: More standard Java types 15.2 Comparable and other interfaces A Java class can inherit the members of only one immediate superclass. A class cannot have several immediate superclasses. Sometimes, however, it is necessary that classes can be specified so that they possess certain qualities in addition to the qualities that are inherited from the immediate superclass and classes that are superclasses of the immediate superclass. The concept of interface has been invented to specify additional qualities of classes. When a class is declared, it can inherit one class, and in addition it can implement one or more interfaces. Java has the reserved keyword implements which can be used in the following way class SomeClass extends SomeSuperclass implements SomeInterface, SomeOtherInterface { ... In this case SomeClass implements two named interfaces SomeInterface and SomeOtherInterface. When a class implements several interfaces, the names of the interfaces are separated by commas in the class declaration. An interface usually specifies a set of methods. In addition an interface can specify constants. An interface contains only method declarators (method headers). It does not provide implementations (i.e. method bodies) for the specified methods. It is the responsibility of the class that implements an interface to provide implementations for the specified methods. To explore the nature of interfaces, let’s suppose that the interface SomeInterface, which is implemented by SomeClass above, is declared in the following way: interface SomeInterface { int calculate_something( int given_value ) ; void do_something() ; } When SomeClass implements this interface, it means that SomeClass must have a method named calculate_something() which takes an int value as a parameter and returns an int value, and it must have a method named do_something() that neither takes parameters nor returns anything. As the above SomeClass also implements the other interface SomeOtherInterface, it means that SomeClass contains also all the methods specified by that interface. Interface declarations clearly resemble class declarations. The keyword interface is used in place of the class keyword. Like classes, interfaces are usually written to their own source program files, and the file name must correspond to the name of the interface. An interface named SomeInterface should be kept in a file named SomeInterface.java. Java provides standard interfaces in addition to standard classes. Many of the standard interfaces are generic interfaces, which means that when a class implements an interface, it is necessary to specify the type of objects with which the methods of the interface will operate. The class Event in program Events.java implements the standard interface Comparable in the following way class Event extends Date implements Comparable<Event> { ... This class declaration means that class Event has a method named compareTo() which can compare Event objects. By writing <Event> after the interface name we specify that the compareTo() method, that is the only method required by the Comparable interface,...
View Full Document

Ask a homework question - tutors are online