3 Software Design

3 Software Design - Recap from last time We were talking...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
30/08/2010 1 CS 2110 Software Design Principles I Recap from last time ± We were talking about the class hierarchy and inheritance of methods ± Basic idea was to have a parent class that implements some very general functionality and then a child class that specializes it. ± A parent class can also standardize an “interface” shared by child classes. For example: classes that support the same interface as an Array ² X[i] = 17 + Y[j,k] / X[i]; // Is X really an array? Array vs ArrayList vs HashMap (latter two from java.util) ± Array ± Storage is allocated when array created; cannot change ± Extremely fast lookups ± HashMap (in java.util) ± Save data indexed by keys ± Can look up data by its key ± Can get an iteration of ± ArrayList (in java.util) ± An “extensible” array ± Can append or insert elements, access i’th element, reset to 0 length ± Lookup is slower than an array Can get an iteration of the keys or values ± Storage allocated as needed but works best if you can anticipate need and tell it at creation time. HashMap Example ± Create a HashMap of numbers, using the names of the numbers as keys: Map<String, Integer> numbers = new HashMap<String, Integer>(); numbers.put("one", new Integer(1)); numbers["two"] = new Intege (2)) numbers[ two ] = new Integer(2)); numbers.put("three", new Integer(3)); ± To retrieve a number: Integer n = numbers.get("two"); // Explicit method call Integer n = numbers["two"]; // Array notation ± Returns null if the HashMap doesn’t contain key ± Can use numbers.containsKey(key) to check this Generics and Autoboxing ± Is a number like 71 an Integer (an object) or a base type (an "int" )? ± How do I create an array with an object, not a base type, in the entries? ± Java automatically “autoboxes” and also lets you use types as a kind of parameter Map<String, Integer> numbers = new HashMap<String, Integer>(); numbers.put("one", 1); // Autobox converts 1 to new Integer(1); int s = numbers.get("one"); What do these tell us? ± There is a great deal of power in “abstraction” ± Here we’re seeing examples in which the abstract type is an array, but the values and even the index can be arbitrary objects! ± In Java we often also need special-purpose objects that add functionality, properties etc ± For example, to make an array extensible, or to ensure that lookup will use a very fast method even if the index type isn’t an integer
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
30/08/2010 2 Our challenge? ± We need to look at a computing problem, such as building software for cyclists, and learn to ± See the most general abstractions, where they arise. For example “gosh, these are graphs” ± Build powerful, general purpose solutions, such as a graph class supporting graph operations ± But then also see how to map that general abstraction back to the real world ² For example, creating bike routes that have GPS locations and times and other bike-specific properties Mapping goes two ways ± You look at a problem and say “I see a more basic, general idea here” ± These bike routes look like graphs to me ± So I’ll build a graph class, and then I’ll specialize
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 09/30/2011 for the course CS 2110 taught by Professor Francis during the Fall '07 term at Cornell.

Page1 / 8

3 Software Design - Recap from last time We were talking...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online