Path: csiph.com!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Martin Gregorie Newsgroups: comp.lang.java.programmer Subject: Re: A small challenge at beginner's level Date: Thu, 3 Aug 2017 22:47:33 -0000 (UTC) Organization: A noiseless patient Spider Lines: 65 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Injection-Date: Thu, 3 Aug 2017 22:47:33 -0000 (UTC) Injection-Info: mx02.eternal-september.org; posting-host="c814652e0e2530604af510c44ffd91c9"; logging-data="7101"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/EfsqSzvGbLfid47q3qcFsBTk3kYaDQK0=" User-Agent: Pan/0.141 (Tarzan's Death; 168b179 git.gnome.org/pan2) Cancel-Lock: sha1:JP6wLEOPiczqnvJJ2sGFEfX+N8M= Xref: csiph.com comp.lang.java.programmer:37062 On Thu, 03 Aug 2017 13:42:46 +0000, Andreas Leitgeb wrote: > Stefan Ram wrote: >> public final class Main { public static void main( final >> java.lang.String[] args ) >> { java.lang.System.out.println( ... ); }} >> >> where an expression can int be inserted replacing the »...«. > > Is it also allowed to fix the atrocious formatting? > >> and in between you may use literals of the types double, int and >> String, the operators »+«, »-«, »*« and »/« and parentheses »(...)« >> and method calls of static methods of the Java SE standard library. > > String.format("you > have%5.5s.",String.format("%"+(1+4*Math.round(Math.random()))+".1s"," > lost")+"won") > > certainly newbie level ;-) At the bare minimum you should be teaching: - proper use of insertion symbols in format strings - even showing them "%5.5s" as a bad example is abominable - at least one conditional form: either if (condition) ... else ....; or ( condition ? trueval: falseval ) - assignments If only because showing them both condition forms and assignments would let them write: double selector = Math.round(Math.random()); System.out.println("You have " + (selector > 0.5 ? "won" : "lost")); or double selector = Math.round(Math.random()); String result = "lost"; if (selector > 0.5) result = "won"; System.out.println("You have " + result); Both of which are, in my opinion anyway, both simple to write and simple to understand. If you're not going to teach them anything about conditionals at the point where you give them the exercise, then don't set them problems that are expected to produce conditional results: just stick to calculations, e.g. ask them to calculate the difference Math.PI and 22 divided by 7: double diff = Math.abs(Math.PI - (22.0/7.0)); System.out.println("22/7 differs from Pi by " + diff); -- martin@ | Martin Gregorie gregorie. | Essex, UK org |