Java Cvičení 05 http://d3s.mff.cuni.cz CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics
Test 1 Co program vypíše? Hello world Nic Něco jiného package cz.cuni.mff.java.example05.tests; public class Test01 { public static void main(string[] argv) { String greating = Hello world"; for(int i = 0; i < greating.length(); i++) { System.out.write(greating.charAt(i));
Test 1 Správná odpověď: B) Nic Důvod: a) použití metody raw write(int) místo print(char) b) bufferování System.out = PrintStream Interní bufferování flush = konec řádku Ponaučení: používat správné metody pozor na bufferování
Test 1 // java.io.outputstream /** * Flushes this output stream and forces any buffered output bytes * to be written out. The general contract of <code>flush</code> is * that calling it is an indication that, if any bytes previously * written have been buffered by the implementation of the output * stream, such bytes should immediately be written to their * intended destination. * <p> * If the intended destination of this stream is an abstraction provided by * the underlying operating system, for example a file, then flushing the * stream guarantees only that bytes previously written to the stream are * passed to the operating system for writing; it does not guarantee that * they are actually written to a physical device such as a disk drive. * <p> * The <code>flush</code> method of <code>outputstream</code> does nothing. * * @exception IOException if an I/O error occurs. */ public void flush() throws IOException;
Test 2 Co program vypíše? package cz.cuni.mff.java.example05.tests; public class Test02 { public static void main(string[] argv) { String fullclassname = cz.cuni.mff.java. + example05.tests.test02 ; String filename = fullclassname.replaceall(., / ) +.java ; System.out.println( Trida + fullclassname + musi byt v souboru + filename);
Test 2 Správná odpověď: Trida cz.cuni.mff.java.example05.tests.test02 musí byt v souboru ///////////////////////////////////////.java Důvod: použití špatné metody String.replaceAll místo String.replace(char, char) Ponaučení: Vždy číst dokumentaci (javadoc)
Test 2 // java.lang.string /** * Replaces each substring of this string that matches * the given regular expression with the given replacement. * * An invocation of this method of the form str.replaceall(regex, repl) * yields exactly the same result as the expression * Pattern.compile(regex).matcher(str).replaceAll(repl) * * Note that backslashes (\) and dollar signs ($) in the replacement string * may cause the results to be different than if it were being treated as a * literal replacement string; see Matcher.replaceAll. * Use Matcher.quoteReplacement(java.lang.String) to suppress the special * meaning of these characters, if desired. * * @param regex the regular expression to which this string is to be matched * @param replacement the string to be substituted for each match * @return the resulting String * @throws PatternSyntaxException if the regular expression's syntax is invalid * @see java.util.regex.pattern */ public String replaceall(string regex, String replacement);
Test 2 Správná odpověď: Trida cz.cuni.mff.java.example05.tests.test02 musí byt v souboru ///////////////////////////////////////.java Důvod: použití špatné metody String.replaceAll místo String.replace(char, char) Ponaučení: Vždy číst dokumentaci (javadoc) Úkol 0 Nastavte si IDE tak, aby zobrazovalo javadoc
Test 3 Co se stane: A) Nelze přeložit chybná syntax B) Lze přeložit, ale nelze spustit hlavní program nemůže být v enumu C) Lze přeložit i spustit vypíše Hello package cz.cuni.mff.java.example05.tests; public enum Test03 { RED, GREEN, BLUE; public static void main(string[] argv) { System.out.println( Hello );
Úkol 1 File Copy Vytvořte program FileCopy Vstup: parametry programu zdrojový, cílový soubor a metoda Podporované metody: IO - Input/OutputStream NIO Channels a Buffers NIO FileChannel.transferFrom/transferTo NIO Memory mapped files NIO.2 Files.copy() Upravte FileCopy druhý parameter existující adresář zkopíruje soubor do daného adresáře (jméno souboru zůstane stejné)
Úkol 2 - Heapsort Vytvořte program Heapsort metodu public static void heapsort(int[] arr) in place setřídí pole intů metodu main vytvori nahodne pole a overi ze heapsort tridi Heapsort reminder (Reversed) Halda binární strom, maximum v kořeni, pro každý uzel potomci mají menší hodnoty uložená v poli potomek uzlu i 2*i + 1 a 2*i + 2 public static void heapsort(int[] arr) { Heapsort.heapify(arr, arr.length); int end = arr.length 1; while(end > 0) { swap arr[end] arr[0] Heapsort.shiftDown(arr, 0, end - 1); // Repair heap end--;
Úkol 3 - AddUser Vytvořte program AddUser Přidá uživatele na Unixový OS Seznam uživatelů - /etc/passwd vytvořte si kopii a pracuj nad ní Formát viz: man 5 passwd Bude interaktivní ( java.io.console) zeptá se na login (ověří zda neexistuje) zeptá se na uživatelské jméno nabídne UID (umožní změnit a ověří že UID není obsazený) nabídne domovský adresář - /home/username nabídne shell