Fehler beim Anwenden des Testfalls


Nachdem der Code eh schon gepostet wurde hier mal eine Lösung die

  • nicht die ASCII-Werte von ‘a’ und ‘A’ fest eincodiert
  • die Länge der Arrays aus der Distanz zwischen ‘A’ und ‘Z’ berechnet
  • die Länge der Arrays zum Iterieren verwendet (was IMO guter Stil ist, weil es Out-of-Bounds-Exceptions vermeidet)
  • die Zeile mit den ASCII-Zahlen durch die Verwendung von Modulo geschickter generiert und
  • den StringBuilder und das Generieren der griechischen Buchstaben vermeidet (am besten wäre es natürlich die griechischen Buchstaben einfach in den Code zu schreiben, aber das geht in AuD aus bekannten Gründen ja nicht)

Disclaimer: Ich habe die Aufgabenstellung nicht gelesen.

import java.util.Arrays;

public class Datentypen {
    // DONE: declare a constant named "FOO_BAR" with a value of -123.456 * 10^(-89)
    public static final double FOO_BAR = -123.456e-89;

    // DONE: declare an enumeration named "Months" containing the
    // english names in CAPITAL_LETTERS (!) of all the 12 months of the year
    public static enum Months {
        JANUARY,
        FEBRUARY,
        MARCH,
        APRIL,
        MAY,
        JUNE,
        JULY,
        AUGUST,
        SEPTEMBER,
        OCTOBER,
        NOVEMBER,
        DECEMBER
    }

    public static char[] someCharacters() {
        // DONE: declare, fill and return an 1-dimensional
        // array containing the ASCII-letters A to Z (capital letters)
        char[] upperCaseLetters = new char['Z' - 'A' + 1];
		for (int i = 0; i < upperCaseLetters.length; ++i) {
			upperCaseLetters[i] = (char) ('A' + i);
		}

		// or the version where you have to do a lot more manual labor:
		char[] manualUpperCaseLetters = new char[]{
			'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'
		};
        return upperCaseLetters;
    }

    public static char[][] someMoreCharacters() {

        // DONE: declare, fill and return a 2-dimensional array containing
        char[][] moreLetters = new char[4]['Z' - 'A' + 1];

        // 1) the values (!) 0 to 25 in the first row,
        for (char i = 0; i < moreLetters[0].length; i++) {
            moreLetters[0][i] = i;
        }

        // 2) the ASCII-letters A to Z (capital letters) in the second row,
        moreLetters[1] = someCharacters();

        // 3) the ASCII-characters (!) 0 to 9, than 0 to 9 again and finally 0 to 5 in the third row
		for (int i = 0; i < moreLetters[2].length; ++i) {
			moreLetters[2][i] = (char) ('0' + (i % 10));
		}

        // 4) the ASCII-letters a to z (non-capital letters) in the fourth row,
        for (int i = 0; i < moreLetters[3].length; i++) {
            moreLetters[3][i] = (char) ('a' + i);
        }

        return moreLetters;
    }

    // DONE: declare a constant String named "UNICODE" containing the
    // greek non-capital alphabet from alpha [03B1] to omega [03C9]
    // with a horizontal tabulator between groups of five letters each
    // (e.g. after epsilon, after kappa, after omicron and after tau)
    // and finally followed by a line feed (ASCII: 0x0A)
    // (no spaces or other symbols than those above!!)
    public static final String UNICODE =
		"\u03b1\u03b2\u03b3\u03b4\u03b5\t" +
		"\u03b6\u03b7\u03b8\u03b9\u03ba\t" +
		"\u03bb\u03bc\u03bd\u03be\u03bf\t" +
		"\u03c0\u03c1\u03c2\u03c3\u03c4\t" +
		"\u03c5\u03c6\u03c7\u03c8\u03c9\n";
	// or omit the + signs (and thus the automatically generated string builder
	// object) and specify the string constant as a single string

    // DONE: declare, fill and return a 3-dimensional array
    // of size 3x3x3 (try to imagine a http://en.wikipedia.org/wiki/Rubik's_Cube)
    // containing only integer numbers such that
    // the value at [x][y][z] == (x+1)*100+(y+1)*10+(z+1)
    // (e.g. cube[2][1][0] == 321)
    private static final int[][][] cube = new int[3][3][3];
    static {
        for (int x = 0; x < cube.length; x++) {
            for (int y = 0; y < cube[x].length; y++) {
                for (int z = 0; z < cube[x][y].length; z++) {
                    cube[x][y][z] = (x + 1) * 100 + (y + 1) * 10 + z + 1;
                }
            }
        }
    }
    public static int[][][] theCube() {
        return cube;
    }

    // nothing to do ;) - please do nothing here:
    public static void main(String[] args) {
        System.out.println("-----");
        System.out.println(Datentypen.FOO_BAR);
        System.out.println("-----");
        System.out.println(Months.OCTOBER);
        System.out.println("-----");
        System.out.println(Arrays.toString(someCharacters()));
        System.out.println("-----");
        for (char[] r : someMoreCharacters()) {
            System.out.println(Arrays.toString(r));
        }
        System.out.println("-----");
        System.out.println("[" + Datentypen.UNICODE + "]");
        System.out.println("-----"); //$keine Enterprise Tool NLS-Support-Warnungen$
        for (int[][] plane : theCube()) {
            for (int[] row : plane) {
                System.out.print(Arrays.toString(row));
            }
            System.out.println();
        }
    }
}

P.S.: Mit [code=java]…[/code] bekommt man hier im Forum Syntax-Highlighting.


Schöner wäre es allerdings, die Arrays mit der Initializer-Syntax zu initialisieren und das Ganze nicht mit Schleifen manuell zu machen. Hat den Vorteil, dass das Füllen des Arrays nicht zur Laufzeit passieren muss :wink:


Und es hat den Vorteil, dass es der Otto-Normal-AuD-Studi können muss - if[m]for[/m]-Schleifen hat er beim Blatt-02 noch nicht gekannt… :cry:


Du möchtest dir mal den generierten Java-Bytecode für solchen Code ankucken und dann feststellen, dass auch mit deiner Methode erst zur Laufzeit initialisiert wird. Natürlich nur einmal in einem statischen Initialisierer, was man aber mit eigenem Code auch hinbekommt (ich aber nicht gemacht habe, weil faul).

Dazu: https://www4.cs.fau.de/Publications/2014/erhardt_14_jtres.pdf