week11

week11 - Week 11: Class Design CS 177 1 Designing reusable...

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

View Full Document Right Arrow Icon
CS 177 Week 11: Class Design 1
Background image of page 1

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

View Full DocumentRight Arrow Icon
Designing reusable classes Most classes are meant to be used more than once This means that you have to think about what will be helpful for future programmers There are a number of trade-offs to consider More methods means more flexibility Too many methods can be confusing 2
Background image of page 2
Documentation Document thoroughly Write documentation clearly in a way that describes what every method does Input Output Expectations The user probably will not be able to see your code String API example: 3
Background image of page 3

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

View Full DocumentRight Arrow Icon
Limited interfaces Try to provide only the methods that are absolutely essential Example: The Java Math library provides sin() and cos() methods but no sec() or cosec() methods String has lots of methods, and they are great Usually, less is more 4
Background image of page 4
Planning for the future How do you make sure that your class does everything it will need to? If you design a class to keep records about human beings, should you check to make sure that the age is in a certain range? What range? Make sure it’s less than 130 years? Make sure it’s not negative? 5
Background image of page 5

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

View Full DocumentRight Arrow Icon
Classic failures in design Y2K Bug 2 digits for the date was not enough, now 4 It’s all just going to get messed up in Y10K Y2038 Bug Unix machines often use a 32-bit integer to represent seconds since January 1, 1970 Zip Codes (5+4) Vehicle Identification Numbers 6
Background image of page 6
Designs with staying power? IPv4 uses n1:n2:n3:n4 where each n is 0- 255 (like 128.210.56.104) That allows 232 IP addresses (8.6 billion) There are approximately 6.8 billion people on earth IPv6 uses n1:n2:n3:n4:n5:n6:n7:n8 8 groups of 4 hexadecimal digits each f7c3:0db8:85a3:0000:0000:8a2e:0370:7334 How many different addresses is this? 7
Background image of page 7

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

View Full DocumentRight Arrow Icon
Objects have members Members are the data inside objects But, sometimes, wouldn’t it be nice if some data were linked to the class as a whole, rather than just one object? What if you wanted to keep track of the total number of a particular kind of object you create? 8
Background image of page 8
Static members Static members are stored with the class, not with any particular object public class Item { private static int count = 0; // only one copy (in class) private String name; // one copy per object public Item( String s ) { name = s; 9
Background image of page 9

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

View Full DocumentRight Arrow Icon
Image of page 10
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 33

week11 - Week 11: Class Design CS 177 1 Designing reusable...

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

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