data structures and object oriented design
play

Data Structures and Object-Oriented Design Spring 2014 Carola Wenk - PowerPoint PPT Presentation

Data Structures and Object-Oriented Design Spring 2014 Carola Wenk Data Types So Far With classes we can define our own abstract data types. The purpose of classes is to package data and functionality. [Booch 98] The goal in


  1. Data Structures and Object-Oriented Design Spring 2014 Carola Wenk

  2. Data Types So Far With classes we can define our own ‘abstract data types’. • The purpose of classes is to package data and functionality. [Booch ’98] The goal in packaging is to hide complexity and only expose data/functionality necessary.

  3. Data Types So Far With classes we can define our own ‘abstract data types’. • The purpose of classes is to package data and functionality. Class/Instance “Programmer” “user” ... The programmer provides all the details, the user just invokes what is necessary and expects correct behavior.

  4. Stacks A stack is a “last-in, first-out” data structure. The functionality • is very simple, but how do we implement it? “Push” “Pop” class Stack { ... public Stack(..) { ... } public int pop() { ... } public void push(int x) { ... }

  5. Stacks A stack is a “last-in, first-out” data structure. The functionality is very simple, but how do we implement it? class Stack { ... public Stack(...) { ... } public int pop() { ... } public void push(...) { ... }

  6. Stacks • Are the methods in this class guaranteed to work? What kind of specifications can we guarantee to ensure the correctness of push and pop ? • How does pop handle empty stacks? public int pop() { return S[top--]; } public int pop() { Does not if (top >= 0) run. return S[top--]; }

  7. Java Exceptions In Python, we were allowed to write functions like this: • def f(a, b): if (a + b > 0): return a+b Note that this kind of function can return nothing at all, even though we can use it in an assignment statement or comparison. This is not allowed in Java, and so we really need a way to avoid returning something of the declared type when we “need” to.

  8. Stacks • Are the methods in this class guaranteed to work? What kind of specifications can we guarantee to ensure the correctness of push and pop ? public int pop() { if (top >= 0) • How does pop handle empty stacks? return S[top--]; else public int pop() { return -999; return S[top--]; } } public int pop() { if (top >= 0) return S[top--]; } public int pop() { if (top >= 0) return S[top--]; else throw new RuntimeException(“Stack is empty”); }

  9. Java Exceptions Java allows the programmer to subvert the type system to • return error messages: ... ... try { f(a, b); } public int f(int a, int b) throws catch(Exception e) { Exception { System.out.println(e.getMessage()); if (a + b > 0) } return a+b; finally { else // “clean up” area throw new Exception(“Error!”); } ... ... Some exceptions must always be handled (either by the caller or higher up), while others are handled by the system. Java exceptions are just classes, and can be treated as such.

  10. Stacks • Are the methods in this class guaranteed to work? What kind of specifications can we guarantee to ensure the correctness of push and pop ? public int pop() { if (top >= 0) • How does pop handle empty stacks? return S[top--]; else public int pop() { return -999; return S[top--]; } } public int pop() { if (top >= 0) return S[top--]; } public int pop() { if (top >= 0) return S[top--]; else throw new RuntimeException(“Stack is empty”); }

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend