Advertisement
Guest User

Untitled

a guest
Sep 23rd, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.36 KB | None | 0 0
  1. package de.dhbw.klausurVorbereitung;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Collection;
  5. import java.util.Collections;
  6. import java.util.Comparator;
  7. import java.util.List;
  8.  
  9. public class Klausurvorbereitung {
  10.  
  11.     //Schreibe ein JavaDoc
  12.     /**
  13.      * Diese Methode berechnet die Fakultaet mit Hilfe von Rekursion.
  14.      * Abbruchbedingungen sind x < 0, da es hiervon keine Fakultaet gibt und
  15.      * x < 2, da die Fakultaet von diesen Zahlen per Definition gleich 1 ist.
  16.      * @param x eine natuerliche Zahl, von der die Fakultaet gebildet wird.
  17.      * @return Fakultaet von x
  18.      * @throws Illegal-Argument-Exception (unchecked), falls x < 0
  19.      */
  20.     public static int getFoo(int x) {
  21.         if (x < 0) {
  22.             throw new IllegalArgumentException();
  23.         }
  24.         if (x < 2) {
  25.             return 1;
  26.         }
  27.         int foo = x * getFoo(x - 1);
  28.         return foo;
  29.     }
  30.    
  31.    
  32.     // Was steht nach Ablauf des Programms in der Konsole
  33.     public static void main(String[] args) {
  34.         // set up data
  35.         String[] namesArray = {"Martin", "Hans", "Christian", "Max"};
  36.         List<String> namesList = new ArrayList<String>();
  37.         for (String s : namesArray) {
  38.             namesList.add(s);
  39.         }
  40.         Collections.sort(namesList, new Comparator<String>() {
  41.             public int compare(String o1, String o2) {
  42.                 return o1.length() - o2.length();
  43.             }
  44.         });
  45.        
  46.         // print data
  47.         for (String s : namesList) {
  48.             System.out.println(s);
  49.         }
  50.         System.out.println();
  51.        
  52.         // do some work on it and print it again
  53.         List<String> namesList2 = namesList;
  54.         namesList2.remove("Martin");
  55.         namesList.remove(1);
  56.         for (String s : namesList) {
  57.             System.out.println(s);
  58.         }
  59.        
  60.         // some comparisons
  61.         System.out.println();
  62.         System.out.println(namesList2 == namesList);
  63.         namesList2 = new ArrayList<String>(namesList);
  64.         System.out.println(namesList2.equals(namesList));
  65.     }
  66.    
  67.    
  68.     // The following code excerpt contains five compile-time errors. locate and describe them
  69.     public String getText(Collection<String> strings) { // nicht in jedem Fall ein Return-wert
  70.         int result = Double.MAX_VALUE; // Wir versuchen einen Double einem int zuzuweisen
  71.         if (strings = null) { // Zuweisung anstatt Vergleich. null ist kein boolean
  72.             return "";
  73.         }
  74.         for (String s : strings) {
  75.             result -= s.length() // Kein Semikolon
  76.         }
  77.         if (result > 0) {
  78.             return result; // result ist kein String
  79.         }
  80.     }
  81.    
  82.    
  83.     /*
  84.     * Given is the following code snippet. Comment on the code:
  85.     *   - what is given in the snippet? What are the snippet`s consequences?
  86.     *   - where might this piece of code be used? What is missing to make it functional in
  87.     *       those situations? Give examples for those situations.
  88.     */
  89.     public class MyClass {
  90.         private MyClass() {
  91.         }
  92.     }
  93.     // public default constructor wurde private gemacht
  94.     // notwendig für einen singleton
  95.     // von außen kann niemand eine Instanz erzeugen
  96.     // singleton wäre in der Klausur ein Bonus
  97.    
  98.    
  99.    
  100.     //Praktischer Teil
  101.    
  102.     /*
  103.      * TODO Task 1: Exit the program in a controlled manner (15 points)
  104.      * Currently the program cannot be exited, it can just be terminated from outside.
  105.      * It should be possible to exit the program in two
  106.      * ways:
  107.      *  - By clicking to close button in the windows icon bar
  108.      * (upper right corner)
  109.      * By clicking an 'Exit' menu item from the menu bar
  110.      * Before actually exiting the program the user must be challenged
  111.      * ("Do you really..."). Only in case of a positive answer the program should be terminated.
  112.      * The text typed in by the user does not have to be persisted.
  113.      * The classes JOptionPane and System can be used here.
  114.      */
  115.    
  116.     /*
  117.      * TODO Task 2: Read input text over a network (20 points)
  118.      * Text to be edited can either be entered by a user manually or read
  119.      * via a network connection from another process using socket
  120.      * communication. Write code to read the text from the given server
  121.      * program and display this text instead of the currently edited one
  122.      * in the editor's text area.
  123.      * The users should be informed appropriately in case of errors.
  124.      * All resource must be cleaned up after using them.
  125.      *
  126.      * You can start the server by typing: java -jar server.jar
  127.      * on the command line. The server should write
  128.      *      Text server up and running.
  129.      *      Port: 28060
  130.      * after startup.
  131.      *
  132.      * The following protocol is used:
  133.      * - Client sends: _reuestForText_
  134.      * - Server sends: <some text (as string) >
  135.      * - Client sends: _ack_
  136.      * - Server sends: _ack_
  137.      */
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement