Die Präsentation wird geladen. Bitte warten

Die Präsentation wird geladen. Bitte warten

Abteilung für Telekooperation Übung Softwareentwicklung 2 für Wirtschaftsinformatik Dr. Wieland Schwinger

Ähnliche Präsentationen


Präsentation zum Thema: "Abteilung für Telekooperation Übung Softwareentwicklung 2 für Wirtschaftsinformatik Dr. Wieland Schwinger"—  Präsentation transkript:

1 Abteilung für Telekooperation Übung Softwareentwicklung 2 für Wirtschaftsinformatik Dr. Wieland Schwinger se2ue_ws@tk.uni-linz.ac.at http://www.tk.uni-linz.ac.at/teaching/ http://www.tk.uni-linz.ac.at/blackboard/

2 Abteilung für Telekooperation Softwareentwicklung 2 UE WS 2008/09 SE2UE_02 - 2 Motivation - Konstanten // Konstanten Bsp1 public static final int SEASON_WINTER = 0; public static final int SEASON_SPRING = 1; public static final int SEASON_SUMMER = 2; public static final int SEASON_FALL = 3; // Konstanten Bsp2 public static final int COLOR_RED = 0; public static final int COLOR_BLUE = 1; public static final int COLOR_YELLOW = 2; public static final int COLOR_GREEN = 3; … int giveNextColor(int color) { if (color==COLOR_RED) return COLOR_BLUE; if (color==COLOR_BLUE) return COLOR_YELLOW; if (color==COLOR_YELLOW) return COLOR_GREEN; if (color==COLOR_GREEN) return COLOR_RED; }

3 Abteilung für Telekooperation Softwareentwicklung 2 UE WS 2008/09 SE2UE_02 - 3 Motivation - Probleme Nicht Typsicher: Konstanten sind "nur" Integer, dh. die Methode getNextColor(int color) akzeptiert jeden Integerwert auch jene, die nicht dazu gedacht sind eine Farbe zu repräsentieren. Kein Namensraum: Der Programmierer muss durch entsprechende Prefixes sicherstellen, dass sich die Konstenanten-Namen nicht überschneiden. Z.B. SEASON_ oder COLOR_ Wissen über Konstanten liegt auch bei der Verwendung: Werden die Konstanten z.B. in Form einer Reihenfolge verwendet, dann führt ein hinzufügen einer neuen Konstante dazu, dass z.B. Methoden, welche die Konstanten-Reihenfolge verwenden, diese ebenfalls neu berücksichtigen müssen. Z.B.: wenn die Farbe COLOR_BLACK = -1 vor COLOR_RED eingefügt werden soll, dann muss die Methode getNextColor dies richtig berücksichtigen. Zahlen haben per se keine Information: Die Zahl 1 sagt nichts über die Farbe aus, die sie repräsentiert. Es ist nicht gegenben, dass COLOR_ ein Typ ist.

4 Abteilung für Telekooperation Softwareentwicklung 2 UE WS 2008/09 SE2UE_02 - 4 ENUMS // Enum Bsp1 public enum Season {WINTER, SPRING, SUMMER, FALL}; ------------------------------------------------------------------------------------ // Enum Bsp2 public enum Color2 {RED, BLUE, YELLOW, GREEN static void printAll() { for (Color2 c : Color2.values()) IO.writeln("Name: "+ c.name()); } static Color2 getNextColor(Color2 color) { boolean takeNext = false; for (Color2 c : Color2.values() ) { if (takeNext) { System.out.println("Next: " + c.name()); return c; } if (c == color) takeNext = true; } return null; }

5 Abteilung für Telekooperation Softwareentwicklung 2 UE WS 2008/09 SE2UE_02 - 5 ENUMS // Enum Bsp2 (alternative) public enum Color2 { RED, BLUE, YELLOW, GREEN; static void printAll() { for (Color2 c : Color2.values()) IO.writeLn("Name: "+ c.name()); } static Color2 getNextColor2(Color2 color) { if (Color2.values().length <= color.ordinal()) return null; return Color2.values()[color2.ordinal()%color2.values().length]; } public static void main(String[] args) { printAll(); Color2 c = getNextColor2(BLUE); if (c!=null) System.out.println("NEXT: " + c.name()); }

6 Abteilung für Telekooperation Softwareentwicklung 2 UE WS 2008/09 SE2UE_02 - 6 ENUMS public enum Planet { MERCURY (3.303e+23, 2.4397e6), VENUS (4.869e+24, 6.0518e6), EARTH (5.976e+24, 6.37814e6), MARS (6.421e+23, 3.3972e6), JUPITER (1.9e+27, 7.1492e7), SATURN (5.688e+26, 6.0268e7), URANUS (8.686e+25, 2.5559e7), NEPTUNE (1.024e+26, 2.4746e7), PLUTO (1.27e+22, 1.137e6); private final double mass; // in kilograms private final double radius; // in meters Planet(double mass, double radius) { this.mass = mass; this.radius = radius; } public double mass() { return mass; } public double radius() { return radius; } // universal gravitational constant (m3 kg-1 s-2) public static final double G = 6.67300E-11; public double surfaceGravity() { return G * mass / (radius * radius); } public double surfaceWeight(double otherMass) { return otherMass * surfaceGravity(); } }

7 Abteilung für Telekooperation Softwareentwicklung 2 UE WS 2008/09 SE2UE_02 - 7 MenuOptions public enum MenuOption { NO_OPTION, QUIT, INSERT, FIRST, NEXT, SEARCH, CHAR_STARTING; private static final char[] MENU_OPTIONS = {' ', 'q', 'i', 'f', 'n', 's', 'c'}; public MenuOption nextOption() { return MenuOption.values()[(this.ordinal()+1)%MenuOption.value s().length]; } public static MenuOption getOption(char op) { if (!validOption(op)) return NO_OPTION; return MenuOption.values()[getIndexOfOption(op)]; } private static int getIndexOfOption(char op) { int i=0; for (i = 1; i < MenuOption.values().length; i++) { if (op == MENU_OPTIONS[i]) return i; I++; } return -1; } public char getCharOption() { return MENU_OPTIONS[this.ordinal()]; } public MenuOption quitOption() { return QUIT; } public static boolean validOption(char op) { for (int i = 1; i < MENU_OPTIONS.length; i++) { if (op == MENU_OPTIONS[i]) return true; } return false; } public static String getOptions() { if (MENU_OPTIONS.length < 0) return ""; StringBuffer options = new StringBuffer("["); String seperator = ""; for (int i = 1; i < MENU_OPTIONS.length; i++) { options.append(seperator + MENU_OPTIONS[i]); seperator = "|"; } options.append("]"); return options.toString(); } public static boolean proceed(char op) { return (getOption(op) != QUIT); }


Herunterladen ppt "Abteilung für Telekooperation Übung Softwareentwicklung 2 für Wirtschaftsinformatik Dr. Wieland Schwinger"

Ähnliche Präsentationen


Google-Anzeigen