Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package de.dhbw.klausurVorbereitung;
- import java.util.ArrayList;
- import java.util.Collection;
- import java.util.Collections;
- import java.util.Comparator;
- import java.util.List;
- public class Klausurvorbereitung {
- //Schreibe ein JavaDoc
- /**
- * Diese Methode berechnet die Fakultaet mit Hilfe von Rekursion.
- * Abbruchbedingungen sind x < 0, da es hiervon keine Fakultaet gibt und
- * x < 2, da die Fakultaet von diesen Zahlen per Definition gleich 1 ist.
- * @param x eine natuerliche Zahl, von der die Fakultaet gebildet wird.
- * @return Fakultaet von x
- * @throws Illegal-Argument-Exception (unchecked), falls x < 0
- */
- public static int getFoo(int x) {
- if (x < 0) {
- throw new IllegalArgumentException();
- }
- if (x < 2) {
- return 1;
- }
- int foo = x * getFoo(x - 1);
- return foo;
- }
- // Was steht nach Ablauf des Programms in der Konsole
- public static void main(String[] args) {
- // set up data
- String[] namesArray = {"Martin", "Hans", "Christian", "Max"};
- List<String> namesList = new ArrayList<String>();
- for (String s : namesArray) {
- namesList.add(s);
- }
- Collections.sort(namesList, new Comparator<String>() {
- public int compare(String o1, String o2) {
- return o1.length() - o2.length();
- }
- });
- // print data
- for (String s : namesList) {
- System.out.println(s);
- }
- System.out.println();
- // do some work on it and print it again
- List<String> namesList2 = namesList;
- namesList2.remove("Martin");
- namesList.remove(1);
- for (String s : namesList) {
- System.out.println(s);
- }
- // some comparisons
- System.out.println();
- System.out.println(namesList2 == namesList);
- namesList2 = new ArrayList<String>(namesList);
- System.out.println(namesList2.equals(namesList));
- }
- // The following code excerpt contains five compile-time errors. locate and describe them
- public String getText(Collection<String> strings) { // nicht in jedem Fall ein Return-wert
- int result = Double.MAX_VALUE; // Wir versuchen einen Double einem int zuzuweisen
- if (strings = null) { // Zuweisung anstatt Vergleich. null ist kein boolean
- return "";
- }
- for (String s : strings) {
- result -= s.length() // Kein Semikolon
- }
- if (result > 0) {
- return result; // result ist kein String
- }
- }
- /*
- * Given is the following code snippet. Comment on the code:
- * - what is given in the snippet? What are the snippet`s consequences?
- * - where might this piece of code be used? What is missing to make it functional in
- * those situations? Give examples for those situations.
- */
- public class MyClass {
- private MyClass() {
- }
- }
- // public default constructor wurde private gemacht
- // notwendig für einen singleton
- // von außen kann niemand eine Instanz erzeugen
- // singleton wäre in der Klausur ein Bonus
- //Praktischer Teil
- /*
- * TODO Task 1: Exit the program in a controlled manner (15 points)
- * Currently the program cannot be exited, it can just be terminated from outside.
- * It should be possible to exit the program in two
- * ways:
- * - By clicking to close button in the windows icon bar
- * (upper right corner)
- * By clicking an 'Exit' menu item from the menu bar
- * Before actually exiting the program the user must be challenged
- * ("Do you really..."). Only in case of a positive answer the program should be terminated.
- * The text typed in by the user does not have to be persisted.
- * The classes JOptionPane and System can be used here.
- */
- /*
- * TODO Task 2: Read input text over a network (20 points)
- * Text to be edited can either be entered by a user manually or read
- * via a network connection from another process using socket
- * communication. Write code to read the text from the given server
- * program and display this text instead of the currently edited one
- * in the editor's text area.
- * The users should be informed appropriately in case of errors.
- * All resource must be cleaned up after using them.
- *
- * You can start the server by typing: java -jar server.jar
- * on the command line. The server should write
- * Text server up and running.
- * Port: 28060
- * after startup.
- *
- * The following protocol is used:
- * - Client sends: _reuestForText_
- * - Server sends: <some text (as string) >
- * - Client sends: _ack_
- * - Server sends: _ack_
- */
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement