Vous êtes sur la page 1sur 6

Q7 ans

public final class String


extends Object
implements Serializable, Comparable<String>,
CharSequence
The String class represents character strings. All string literals in Java programs, such as "abc", are
implemented as instances of this class.
Strings are constant; their values cannot be changed after they are created. String buffers support mutable strings.
Because String objects are immutable they can be shared. For example:
String str = "abc";

is equivalent to:
char data[] = {'a', 'b', 'c'};
String str = new String(data);

Here are some more examples of how strings can be used:
System.out.println("abc");
String cde = "cde";
System.out.println("abc" + cde);
String c = "abc".substring(2,3);
String d = cde.substring(1, 2);

The class String includes methods for examining individual characters of the sequence, for comparing strings, for
searching strings, for extracting substrings, and for creating a copy of a string with all characters translated to
uppercase or to lowercase. Case mapping is based on the Unicode Standard version specified by
the Character class.
The Java language provides special support for the string concatenation operator ( + ), and for conversion of other
objects to strings. String concatenation is implemented through the StringBuilder(or StringBuffer)
class and its appendmethod. String conversions are implemented through the method toString, defined
by Object and inherited by all classes in Java. For additional information on string concatenation and conversion,
see Gosling, Joy, and Steele, The Java Language Specification.
Unless otherwise noted, passing a null argument to a constructor or method in this class will cause
a NullPointerException to be thrown.
A String represents a string in the UTF-16 format in which supplementary characters are represented
by surrogate pairs (see the section Unicode Character Representations in the Character class for more
information). Index values refer tochar code units, so a supplementary character uses two positions in a String.
The String class provides methods for dealing with Unicode code points (i.e., characters), in addition to those for
dealing with Unicode code units (i.e., char values).
Q8 ans
Java Arrays are data structures containing objects of the same
type. A String array can only contain Strings and a Car array can
only contain car objects. Arrays are used to store objects that
you want to use in other calculations or want to reference to.
For example, an array can contain grades that can be averaged
or referenced.
1.
When you declare a Java array, you need to give it a data type when you
declare it just like you would give a variable a datatype.
For example, we will declare an integer array containing five elements which
we will name arrayName
int[] arrayName = new int[5];
2.
When you need to access an element of the array you can do so directly :
System.out.println(arrayName[3] );
or you can print out the entire array using a for loop - first put some values in it
so we can see what is going on. Remember arrays start numbering with 0 so an
array with a length of five has elements numbered from 0-4 and not 1-5.
arrayName[0] = 000;
arrayName[1] = 111;
arrayName[2] = 222;
arrayName[3] = 333;
arrayName[4] = 444;
To print out the entire array (five is the size of the array):
for (int i=0; i<5; i++)
{
System.out.println(arrayName[i]);
} // end for
This will print out the following:
0
111
222
333
444
7.2.1 Some Processing Examples
To begin, here's an example to remind you to be careful about avoiding array indices
outside the legal range. Suppose that lines is an array of type String[], and we
want to know whether linescontains any duplicate elements in consecutive
locations. That is, we want to know
whether lines[i].equals(lines[i+1]) for any index i. Here is a failed
attempt to check that condition:
boolean dupp = false; // Assume there are no duplicates
for ( int i = 0; i < list.length; i++ ) {
if ( lines[i].equals(lines[i+1]) ) { // THERE IS AN ERROR
HERE!
dupp = true; // we have found a duplicate!
break;
}
}
This for loop looks like many others that we have written, so what's the problem?
The error occurs when i takes on its final value in the loop, i equal
to lines.length-1. In that case, i+1 is equal tolines.length. But the last
element in the array has index lines.length-1, so lines.length is not a
legal index. This means that the reference to lines[i+1] causes an
ArrayIndexOutOfBoundsException. This is easy to fix; we just need to stop the loop
before i+1 goes out of range:
boolean dupp = false; // Assume there are no duplicates
for ( int i = 0; i < list.length - 1 ; i++ ) {
if ( lines[i].equals(lines[i+1]) ) {
dupp = true; // we have found a duplicate!
break;
}
}
This type of error can be even more insidious when working with partially full arrays
(see Subsection 3.8.4), where usually only part of the array is in use, and a counter is
used to keep track of how many spaces in the array are used. With a partially full
array, the problem is not looking beyond the end of the array, but looking beyond the
part of the array that is in use. When your program tries to look beyond the end of an
array, at least the program will crash to let you know that there is a problem. With a
partially full array, the problem can go undetected.

For the next example, let's continue with partially full arrays. We have seen how to
add an item to a partially full array, but suppose that we also want to be able to
remove items? Suppose that you write a game program, and that players can join the
game and leave the game as it progresses. As a good object-oriented programmer, you
probably have a class named Player to represent the individual players in the game. A
list of all players who are currently in the game could be stored in an
array, playerList, of type Player[]. Since the number of players can change,
you will follow the partially full array pattern, and you will need a
variable, playerCt, to record the number of players currently in the game.
Assuming that there will never be more than 10 players in the game, you could
declare the variables as:
Player[] playerList = new Player[10]; // Up to 10 players.
int playerCt = 0; // At the start, there are no players.


Q9 ans:
Objects can work together in three different types of relationships:
Uses: An object can use another to do some work (association).
Composition: A complex object may be composed of several
parts (aggregation, containment).
Simple: Two objects may depend on each other but dont
interact directly (weak association).
The underlying principle behind these categories is found in the idea or
coupling, which measures the mutual dependence of two objects on each
other.
In general, you want to create classes that are as loosely coupled as possible
while still being able to efficiently carry out their responsibilities.
This goal is laudable; however, most of the time the nature of the problem
itself will dictate the type of association you must use.
Objects can work together in three different types of relationships:
Uses: An object can use another to do some work (association).
Composition: A complex object may be composed of several
parts (aggregation, containment).
Simple: Two objects may depend on each other but dont
interact directly (weak association).
The underlying principle behind these categories is found in the idea or
coupling, which measures the mutual dependence of two objects on each
other.
In general, you want to create classes that are as loosely coupled as possible
while still being able to efficiently carry out their responsibilities.
This goal is laudable; however, most of the time the nature of the problem
itself will dictate the type of association you must use.
Class Relationships - Inheritance
Theres not just one, but three forms of inheritance (isA) relationships:
An extension relationship: A subclass may extend an existing class, so that
all the data members and all the methods of the superclass are left intact, and
only new methods or fields are added.
This extension or strict-subtype relationship involves the inheritance of the
interface as well as its implementation.
A specification relationship: A superclass may specify a set of
responsibilities that a subclass must fulfill, but not provide any actual
implementation.
In this case, the only thing inherited is the interface from the superclass.
A combination of extension and specification: The subclass inherits the
interface and a default implementation of, at least some of the methods
(most common).

Vous aimerez peut-être aussi