-
Okay, im Prinzip hast du vollkommen Recht dass es ausschließlich Syntaxfehler sind

Vielleicht ist es als Rätsel etwas unsinnig, aber dennoch eine gute Frage, warum der Compiler reagiert, wie er reagiert
Gerade beim ersten und fünften Test reagiert der Compiler meiner Meinung nach einfach falsch. Und wenn es richtig wäre, mit welcher genauen Erklärung?
Viele Grüße,
MANBLT - Die Bundesliga Tabelle der Saison 2011 / 2012 - http://www.spacehoster.de/blt/ NEU: mit Tippspiel!
EMail: mstangel@gmx.de
-
29.03.11 06:01 #77
Bei 5 vielleicht aus den selben Grund, warum byte a = 5; byte b = 3; byte c = (byte)(a + b); gecastet werden muss: Weil der Wert zur Kompilerzeit noch nicht eindeutig bekannt ist. Vielleicht will er deshalb auch final's, und kein Array, da dessen Felder ja auch zwischen Intialisation und Verwendung auch verändert werden können.
:/Geändert von Kai008 (29.03.11 um 06:04 Uhr)
Mein kleiner webstart Projektplaner:
http://178.77.101.236/ppws/
Ideen, Verbesserungsvorschläge, Bugsmeldungen und allg. Kritik erwünscht und erbeten.
Danke. :)
-
03.05.11 11:55 #78
- Registriert seit
- Jun 2002
- Ort
- Saarbrücken (Saarland)
- Beiträge
- 9.886
- Blog-Einträge
- 29
Hallo,
hier noch ein neues Rätsel für euch:
Wie erstellt man eine Instanz der Klasse X ohne den Code der Klasse X zu ändern (weder auf Sourcecode noch auf Bytecode Ebene)?
Code java:1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
package de.tutorials.training; import java.lang.reflect.Constructor; public class SneakyObjectConstruction { public static void main(String[] args) throws Exception { try { System.out.println(X.class.newInstance()); } catch (Throwable t) { System.out.println(t); } try { Constructor<?> ctor = X.class.getDeclaredConstructor(); ctor.setAccessible(true); System.out.println(ctor.newInstance()); } catch (Throwable t) { System.out.println(t); } //TODO insert your approach here :) } static class X { private int i = 1; private X() { if(i == 1){ throw new IllegalStateException("i is 1"); } } } }
Viel Spaß,
Gruß TomJava rocks!
How to become a good Java Programmer?
Does IT in Java and .Net
The only valid measurement of code quality: WTFs / minute
Blog
Xing
Twitter
-
09.05.11 14:31 #79
- Registriert seit
- Jun 2002
- Ort
- Saarbrücken (Saarland)
- Beiträge
- 9.886
- Blog-Einträge
- 29
Hallo zusammen,
hier die Äuflösung des Java Rätsels:
Code java:1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
package de.tutorials.training; import java.lang.reflect.Constructor; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; // Our test driver public class SneakyObjectConstruction { public static void main(String[] args) throws Exception { try { System.out.println(X.class.newInstance()); } catch (Throwable t) { System.out.println(t); } try { Constructor<?> ctor = X.class.getDeclaredConstructor(); ctor.setAccessible(true); System.out.println(ctor.newInstance()); } catch (Throwable t) { System.out.println(((InvocationTargetException)t).getTargetException()); } // Some working approaches :) //ReflectionFactory try { sun.reflect.ReflectionFactory reflectionFactory = sun.reflect.ReflectionFactory.getReflectionFactory(); Constructor<X> ctor = reflectionFactory.newConstructorForSerialization(X.class, Object.class.getDeclaredConstructor()); System.out.println("ReflectionFactory: " + ctor.newInstance()); } catch (Throwable t) { System.out.println(t); } //Unsafe Field f = sun.misc.Unsafe.class.getDeclaredField("theUnsafe"); f.setAccessible(true); sun.misc.Unsafe u = (sun.misc.Unsafe)f.get(null); System.out.println("Unsafe: " + u.allocateInstance(X.class)); //Method by Dr. Heinz Kabuts :) //See [url]http://www.javaspecialists.eu/archive/Issue032.html[/url] System.out.println("Heinz Kabutz: " + Y.make()); } static class X { private int i = 1; private X() { if(i == 1){ throw new IllegalStateException("i is 1"); } } } static class Y extends X { private static Y y; static Y make() { try { new Y(); } catch (Exception ex) { try { synchronized (Y.class) { while (y == null) { System.gc(); Y.class.wait(100); } } } catch (InterruptedException ie) { return null; } } return y; } @Override protected void finalize() throws Throwable { synchronized (Y.class) { y = this; Y.class.notify(); } } } }
Ausgabe:
Code :1 2 3 4 5
java.lang.IllegalAccessException: Class de.tutorials.training.SneakyObjectConstruction can not access a member of class de.tutorials.training.SneakyObjectConstruction$X with modifiers "private" java.lang.IllegalStateException: i is 1 ReflectionFactory: de.tutorials.training.SneakyObjectConstruction$X@459189e1 Unsafe: de.tutorials.training.SneakyObjectConstruction$X@2f9ee1ac Heinz Kabutz: de.tutorials.training.SneakyObjectConstruction$Y@24c21495
Gruß TomJava rocks!
How to become a good Java Programmer?
Does IT in Java and .Net
The only valid measurement of code quality: WTFs / minute
Blog
Xing
Twitter
-
"10, 20", da int ein atomarer Datentyp ist und byValue und nicht byReference der Funktion swap übergeben wird!
BLT - Die Bundesliga Tabelle der Saison 2011 / 2012 - http://www.spacehoster.de/blt/ NEU: mit Tippspiel!
EMail: mstangel@gmx.de
-
Da es hier schon lange kein Rätse mehr gab; hier ein kleines von meiner Seite,...
Es geht um die Berechnung einer Kreditkarten-Quersumme nach dem Module-10-Algorithmus. Wir hatten mal die Anforderung, eine Karte unseres Loyaltysystems zu validieren. Das war ja eher ein ge-copy-paste aus dem Internet. Die nächste Anforderung war, die nächste gültige Nummer zu bekommen.
Die Aufgabe darin besteht eine möglichst elegante/einfache Möglichkeit zur Berechnung der nächsten gültigen Nummer zu formulieren
Hier die Source:
Code :1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
package test; public class CheckSum { public static boolean validateCheckSum(String inData) { char[] c = null; char[] d = null; int sum = 0; int checksum = 0; StringBuffer digits = new StringBuffer(); // isolate digits c = inData.toCharArray(); for (int i = 0; i < c.length; i++) { if (Character.isDigit(c[i])) { digits.append(c[i]); } } // calculate sum int nr = 0; d = digits.toString().toCharArray(); for (int i = 0; i < d.length - 1; i++) { nr = i + 1; if (nr % 2 == 0) { sum += Character.digit(d[i], 10) * 3; } else { sum += Character.digit(d[i], 10); } } // validate check sum if ((10 - (sum % 10)) == 10) { checksum = 0; } else { checksum = 10 - (sum % 10); } if (checksum == Character.digit(c[(d.length - 1)], 10)) { return true; } return false; } public static String getNextNumber(String inLatest) { // your approach here return null; } public static void main(String[] args) { String latestNumber = "1009"; System.out.println("Is " + latestNumber + " valid? " + validateCheckSum(latestNumber)); System.out.println("Is 1018 the next number? " + "1018".equals(getNextNumber(latestNumber))); } }
Ausgabe:
Is 1009 valid? true
Is 1018 the next number? false
Und wehe, es macht jemand einfach return "1018" :P
hf
slowyGeändert von slowfly (18.01.12 um 16:17 Uhr)
-
18.01.12 16:16 #82
- Registriert seit
- Jun 2002
- Ort
- Saarbrücken (Saarland)
- Beiträge
- 9.886
- Blog-Einträge
- 29
Hallo,
danke für das Rätsel
Ich nehme mal an, dass die Aufgabe darin besteht eine möglichst elegante/einfache Möglichkeit zur Berechnung der nächsten gültigen Nummer zu formulieren, oder? Das steht leider nicht soooo explizit drin
Gruß TomJava rocks!
How to become a good Java Programmer?
Does IT in Java and .Net
The only valid measurement of code quality: WTFs / minute
Blog
Xing
Twitter
-
kopf->tisch
ty, ge-edited.
-
Angenommen es gibt die Klassen Othello, welche das Interface Comparator wie folgt implementiert:
Code :1 2 3 4 5
public class Othello implements Comparator<String> { public int compare(String s1, String s2) { return s2.compareTo(s1); } }
Was ergibt demnach folgender System.out.println und weshalb:
Code :1 2 3
String[] s = {"map", "pen", "marble", "key"}; Arrays.sort(s, new Othello()); System.out.println(Arrays.binarySearch(s, "key"));
-
08.03.12 17:54 #85
- Registriert seit
- Jun 2009
- Beiträge
- 870
Die Ausgabe lautet
weil das Array falsch herum sortiert wurde.Code :1
-1
Code bitte so einfügen: [java]System.out.println("Hallo");[/java] (Analog für andere Programmiersprachen)
hilfreich zu Java: Really Big Index, Java ist auch eine Insel Band 1 und Band 2.Code java:1
System.out.println("Hallo");
___________
Ubuntu Bug #1: Microsoft has a majority market share
Casecon: Projekt leiser Käse
-
Ähnliche Themen
-
Rätsel
Von Layna im Forum SmalltalkAntworten: 5Letzter Beitrag: 18.11.04, 19:11 -
Rätsel
Von Olli-Web im Forum Fun-ForumAntworten: 6Letzter Beitrag: 01.05.04, 14:27 -
Rätsel
Von reBourne im Forum SmalltalkAntworten: 6Letzter Beitrag: 30.09.03, 13:23 -
3 Rätsel
Von mslap im Forum Fun-ForumAntworten: 5Letzter Beitrag: 07.08.03, 11:35 -
Ist mir ein Rätsel
Von Eric25 im Forum HTML-EditorenAntworten: 3Letzter Beitrag: 25.04.02, 09:03



5Danke

Zitieren


Login





