DOC PREVIEW
UMD CMSC 131 - Lecture Set 5: Design and Classes

This preview shows page 1-2-3-4-5 out of 16 pages.

Save
View full document
Premium Document
Do you want full access? Go Premium and unlock all 16 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Lecture Set 5 Design and Classes This Set Basics of program design Pseudo code Objects and classes Heaps Garbage Collection More about Creating Objects and classes in Java Methods Constructors Accessors Mutators Equality Printing an object CMSC 131 Spring 2010 Jan Plane and Ben Bederson adapted from Bonnie Unit testing 0 Dorr The Software Lifecycle waterfall What customers want Requirements Bug fixes Design What you plan to do New versions Coding Maintenance Deployment Testing Your program Evolution Delivery documentation etc Did you meet requirements CMSC 131 Spring 2010 Jan Plane and Ben Bederson adapted from Bonnie Dorr 1 The Software Lifecycle actual Requirements Evolution Design Maintenance Coding Deployment Testing CMSC 131 Spring 2010 Jan Plane and Ben Bederson adapted from Bonnie Dorr 2 1 In the Real World Requirements and Design Rule Getting requirements right is essential for successful projects FBI electronic case file junked after 180m IRS system upgrade in late 90s junked after 2bn FAA air traffic control false starts 10bn spent Good design makes other parts of lifecycle easier In the real world coding typically 30 of total project costs A good design improves efficiency speed efficiency memory ease of coding ease of debugging ease of expansion CMSC 131 Spring 2010 Jan Plane and Ben Bederson adapted from Bonnie Dorr 3 Usability Matters CMSC 131 Spring 2010 Jan Plane and Ben Bederson adapted from Bonnie Dorr 4 Program Design There are many aspects to good design Architecture Modeling Requirements decomposition Pseudo code In this class we will focus on latter CMSC 131 Spring 2010 Jan Plane and Ben Bederson adapted from Bonnie Dorr 5 2 What Is Pseudo code When developing a complex part of a program an algorithm one of the tools often useful is pseudo code It s not English not programming language somewhere between Captures the flow of the program without worrying about language specific details CMSC 131 Spring 2010 Jan Plane and Ben Bederson adapted from Bonnie Dorr 6 Objects Bundles of related data state operations behavior Data often referred to as instance variables Operations usually called methods Invoking operations can change state values stored in instance variables CMSC 131 Spring 2010 Jan Plane and Ben Bederson adapted from Bonnie Dorr 7 Sample Student Class State Methods Name getAge ID getGrades DOB date age sem class grades etc Major etc CMSC 131 Spring 2010 Jan Plane and Ben Bederson adapted from Bonnie Dorr 8 3 Sample Student Object State Name ID Methods Kerry Keenan 444230695 DOB 06 22 1987 Major CMSC getAge getGrades date age sem class grades etc etc CMSC 131 Spring 2010 Jan Plane and Ben Bederson adapted from Bonnie Dorr 9 Accessing State Methods If o is an object v is an instance variable of the object m is a method of the object Then So Then str is a String o v is how to access the data v in o o m is how to invoke m in o If you have already done String str Jan str is an instance of an object Methods of this object equals compareTo etc str equals str compareTo etc invokes these methods on that object CMSC 131 Spring 2010 Jan Plane and Ben Bederson adapted from Bonnie Dorr 10 Object Oriented Programming Programs are collections of interacting objects Writing programs involves identifying what the objects should be and programming them Object oriented languages provide features to ease object oriented programming Defining objects involves indentifying state methods CMSC 131 Spring 2010 Jan Plane and Ben Bederson adapted from Bonnie Dorr 11 4 Classes Blueprints templates for objects Classes include specifications of Instance variables including types etc to include in objects Implementations of methods to include in objects Classes can include other information also as will be seen later static methods instance variables public private methods instance variables And so on CMSC 131 Spring 2010 Jan Plane and Ben Bederson adapted from Bonnie Dorr 12 StudentClassExample1 Student java Student Class Example Conceptually Instance variables String name int ID int dateOfBirth String major Methods getAge getGrades etc The actual class implementation will include code for the methods This describes a blueprint for student objects CMSC 131 Spring 2010 Jan Plane and Ben Bederson adapted from Bonnie Dorr 13 Driver java How Are Objects Created In Java using new Recall Scanner sc new Scanner System in Invoking new creates fresh copies of instance variables in the heap returns the address where the fresh variables are stored Heap Address CMSC 131 Spring 2010 Jan Plane and Ben Bederson adapted from Bonnie Dorr 14 5 Heap Fresh Memory While a program is running some memory is used to store variables Terminology stack We have been representing the stack as a table e g Variable x Value 3 y 4 5 Rest of memory is called heap and can be used for other purposes including storing new objects CMSC 131 Spring 2010 Jan Plane and Ben Bederson adapted from Bonnie Dorr 15 Main Memory Stack grows shrinks during program execution why So does allocated heap part of heap in use Unallocated part of heap is called free Allocated Heap Free Heap Stack CMSC 131 Spring 2010 Jan Plane and Ben Bederson adapted from Bonnie Dorr 16 Object Creation New space allocated in heap to store instance variables Reference address to this space is returned Scanner sc new Allocated Heap Free Heap Stack sc CMSC 131 Spring 2010 Jan Plane and Ben Bederson adapted from Bonnie Dorr 17 6 Strings Are Objects Where is new in String name Narita Java provides it String is special because it is used so often Java automatically fills in new You can too String name new String Narita CMSC 131 Spring 2010 Jan Plane and Ben Bederson adapted from Bonnie Dorr 18 In Java 9 Sorts of Variables 8 primitive types Reference type Objects always stored in heap including all data Reference to objects are another type and hold one memory address typically one word Stack holds local variables Types are the 8 built ins int byte double etc e g int x e g String str str is reference variable Heap holds allocated memory i e with new e g e g Scanner sc new Scanner System in str Jan Plane str is reference created above CMSC 131 Spring 2010 Jan Plane and Ben Bederson adapted from Bonnie Dorr 19 Example int x 7 float y 3 3 String f cat Stack x Heap 7 y 3 3 f cat CMSC 131 Spring 2010 Jan Plane and Ben Bederson adapted from Bonnie Dorr 20 7 Heap Issues What happens if new is called and there is no free heap Crash Stack Heap s What happens if


View Full Document

UMD CMSC 131 - Lecture Set 5: Design and Classes

Documents in this Course
Set #3

Set #3

7 pages

Exam #1

Exam #1

6 pages

Exam #1

Exam #1

6 pages

Notes

Notes

124 pages

Notes

Notes

124 pages

Load more
Download Lecture Set 5: Design and Classes
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture Set 5: Design and Classes and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture Set 5: Design and Classes and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?