rec11 - CS 177 Week 11 Recitation Slides Class...

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

View Full Document Right Arrow Icon
1 CS 177 Week 11 Recitation Slides Class Design/Custom Classes
Background image of page 1

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

View Full DocumentRight Arrow Icon
2 Announcements Project 4 due 9 pm. Sunday April 4
Background image of page 2
3 ANY QUESTIONS?
Background image of page 3

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

View Full DocumentRight Arrow Icon
4 Class Design Techniques Reusability: want to create a class that can be useful to programmers over a period of time without making significant changes. Documentation: class should be thoroughly documented to allow users/programmers to know exactly how to use your class without ever seeing your code “Less is More” : class should be easy to follow and use. Too many methods and variables can be confusing. Try to have as many static and constant members as possible in order to make your class as easy to use as possible Future Proof: designing a class that can be useful in an unpredictable future can be difficult. Design the class to be flexible to allow for easy updates as technology advances Privacy: Try to hide as much data as possible (i.e. private members) to prevent corruption
Background image of page 4
5 Class Variables Remember all classes have properties and methods. The properties and methods that makeup a class are called members of the class By default, members are associated with the object created as an instance of the class, but not the class itself To create members that are directly associated with the class, use the static keyword. These members are also known as class variables .
Background image of page 5

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

View Full DocumentRight Arrow Icon
6 Static Members Remember: static members can be accessed by static and regular methods, but non-static members cannot be accessed by static methods. Static members can be either public or private like non- static members How can you tell a static member by looking at the code? Give some examples of static members you’ve used so far in the course. Math.random(), Math.cos() System.out.print() StdIn.readInt()
Background image of page 6
Static.java Example public class Item { private static int count = 0; private String name; public Item(String s) { name = s; count++; } public String getName() { return name; } public static int getItemsInUnviverse() { return count; } } public class Static { public static void main(String args[]) { Item item1 = new Item("book"); System.out.println("Name of item1: " + item1.getName()); Item item2 = new Item("car"); System.out.println("Name of item2: " + item2.getName()); System.out.println("Total Number of items: " + Item.getItemsInUnviverse()); } } Would it make sense to declare the class member name as static?
Background image of page 7

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

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

This note was uploaded on 02/23/2012 for the course CS 177 taught by Professor Staff during the Spring '08 term at Purdue University-West Lafayette.

Page1 / 20

rec11 - CS 177 Week 11 Recitation Slides Class...

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

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