Arrays
final String
karen = "Karen Smith",
john = "John Duncan",
paul = "Paul Jacobs",
suzanne = "Suzanne Enders",
peter = "Peter Phillips"; // 10 more to come ...
System.out.println(karen);
System.out.println(john);
...
-
Generate Comma separated list:
Karen Smith, John Duncan, Paul Jacobs, Suzanne Enders, Peter Phillips
-
Generate HTML list emphasizing family names:
<ul> <li>Karen <emph>Smith</emph></li> <li>John <emph>Duncan</emph></li> <li>Paul <emph>Jacobs</emph></li> <li>Suzanne <emph>Enders</emph></li> <li>Peter <emph>Phillips</emph></li> </ul>
No. 137
Assignment to final variable?
Q: |
In Figure 422, “Example:
|
A: |
The variable
|
for (int i = 0; i < 5; i++) {
System.out.println("At index " + i + ": value == " + primes[i]);
}
Result:
At index 0: value == 2 At index 1: value == 3 At index 2: value == 5 At index 3: value == 7 At index 4: value == 11
for (int i = 0; i < 6; i++) {
System.out.println("At index " + i + ": value == " + primes[i]);
} |
Result: |
At index 0: value == 2
At index 1: value == 3
At index 2: value == 5
At index 3: value == 7
At index 4: value == 11
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5
at qq.arrayex.Motivate.main(Motivate.java:27) |
System.out.println("primes.length == " + primes.length); for (int i = 0; i < primes.length; i++) { System.out.println("At index " + i + ": value == " + primes[i]); } |
|
Result: | primes.length == 5 At index 0: value == 2 At index 1: value == 3 At index 2: value == 5 At index 3: value == 7 At index 4: value == 11 |
|
|
Result: | value == 2 value == 3 value == 5 value == 7 value == 11 |
final int[] primes = new int[5]; // Last index is 4 rather than 5! primes[0] = 2; primes[1] = 3; primes[2] = 5; primes[3] = 7; primes[4] = 11; primes[5] = 13; // Excessing array limit |
Result: |
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5
at qq.arrayex.Motivate.main(Motivate.java:25) |
Combining array allocation and value assignment:
|
|
Combining array allocation and value assignment:
|
|
No. 138
Converting string arrays to HTML.
Q: |
Consider an array of strings. As an example we provide a list of country names:
An application is supposed to generate the following output:
Implement A class method
Do not forget to provide appropriate Javadoc™ method and class documentation. You may want
to reconsider Figure 289, “Maven
Generating Javadoc™ HTML documentation from your project should yield a result like: Provide appropriate unit tests covering at least:
|
A: |
|
No. 140
Examinations and mark frequencies
Q: |
Consider an examination resulting in the following list of marks:
You may represent this examination result by:
Lecturers and participants may be interested in the distribution of marks i.e. their frequencies. Thus having marks in a range from 1 to 5 in the given example we'd like to create the following overview of marks:
Write a Java™ application turning an array of participants' marks into the above table of marks among with their respective frequencies. For the given example the intended terminal output reads: Mark|frequency ----+--------- 1|3 2|4 3|6 4|3 5|2 Your application shall work for other marking schemes (i.e. marks ranging from -2 to 10) as well. Define a suitable class like:
The constructor assigning values to both
Provide appropriate unit tests beforehand. Tip
After implementing your method extend your application to allow for being called from the command line like: marking> mvn package
....
marking> java -jar target/marking-0.9.jar 2 1 3 3 5 4 1 2 2 4 3 2 3 3 1 5 3 4
Mark|frequency
----+---------
1|3
2|4
3|6
4|3
5|2 This requires evaluating the args parameter from your
Tip
|
||||||||||||||||||||||||||||||||||||||||||||||||||
A: |
The method
|
No. 141
Pangram checker
Q: |
A pangram is a string containing all
letters of a given alphabet at least once. As an example we consider
the alphabet of ASCII letters
Tip
|
A: |
In addition to the above “regular” solution we also provide an approach featuring Java™ streams with respect to the upcoming “Software development 2” lecture:
|
-
Series of objects having identical type.
-
Array consists of array elements.
-
Element access by index value.
-
Holding either primitive types or object references (Class instance or array).
-
Contiguous storage in memory.
-
Arbitrary array dimensions by virtue of nesting: One-dimensional, two-dimensional etc.
...println(" String: " + "".getClass().getTypeName());
...println(" int[]: " + new int[]{}.getClass().getTypeName());
...println(" double[]: " + new double[]{}.getClass().getTypeName());
...println(" boolean[]: " + new boolean[]{}.getClass().getTypeName());
...println("StringBuffer[]: " + new StringBuffer[]{}.getClass().getTypeName());
String: java.lang.String int[]: int[] double[]: double[] boolean[]: boolean[] String[]: java.lang.String[] StringBuffer[]: java.lang.StringBuffer[]
public static void main(String[] args) {
final int [] lectures = new int[3]; // Three lectures
fill(lectures, 25); // Default lecture having 25 participants
System.out.println("Second lecture has got " + lectures[1] +
" participants");
}
/**
* Initialize array with default value.
*
* @param values Array to be initialized.
* @param common New value for all array elements.
*/
static void fill(final int[] values, final int common) {
for (int i = 0; i < values.length; i++) {
values[i] = common;
}
}
Second lecture has got 25 participants
// Value type
final boolean values[] = new boolean[]{true, true, false, true};
// Reference type
final String shapes[] = new String[]{"Triangle", "Circle"};
Same result:
final boolean values[] = {true, true, false, true};
final String shapes[] = {"Triangle", "Circle"};