Class Notes (835,705)
Canada (509,334)
CSC207H1 (40)
Lecture

oct10c.docx

5 Pages
129 Views
Unlock Document

Department
Computer Science
Course
CSC207H1
Professor
Diane Horton
Semester
Fall

Description
OCTOBER 10  Access Modifiers o Private parent instance variables/methods can’t be accessed  Need setter and getter Have Access? Accessibility Modifier Class (thisPackage Subclass Anyone Public Y Y Y Y Protected Y Y Y - Default (package private) Y Y - - Private Y - - - o Ex. A Racy Slide 1. public class Person { 2. private BodyPart arm; 3. private BodyPart leg; 4. ... 5. public void transplant(Person donor) { 6. this.arm = donor.arm; // donar arm same class, can access private 7. donor.arm = null; // else arm may become confused 8. this.leg = donor.leg; 9. donor.leg = null; 10. } 11.}  A few quick points about abstract classes and interfaces o A class may define methods w/o giving a body. In that case,  Each of those methods must be declared abstract  The class must be declared abstract too  The class can’t be instantiated o A child class may implement some or all of the interited abstract methods  If all, it’s not abstract and can be instantiated o If a class is completely abstract, we can choose instead to declare it to be an interface  Generics o Some generics we’ve seen: ArrayList 1. package generics; 2. import java.util.ArrayList; 3. import java.util.List; 4. 5. public class Demo { 6. 7. public static void main(String[] args) { 8. ArrayList numbers = new ArrayList(); 9. 10. numbers.add(5); // Java autoboxes the int to make an Integer. 11. numbers.add(12); 12. numbers.add(-1); 13. System.out.println(numbers); 14. 15. int sum = 0; 16. int i = numbers.get(0); // unbox Interger at index 0 into an int 17. for (Integer item : numbers) { 18. sum += item; // Java unboxes the Integer to make an int. 19. } 20. System.out.println(sum); 21. } 22. } o Some generics we’ve seen: HashMap 1. Public class Encoder { 2. ... 3. Private HashMap lookup = 4. New HashMap(); 5. ... 6. } OCTOBER 10 o Some generics we’ve seen: Comparable 1. public interface Comparable { 2. /** 3. * Compare this object with o for order. Return a negative integer, zero, or a 4. * positive integer as this object is less than, equal to, or greater thatn o. 5. */ 6. int compareTo(T o); // No body at all, just the method head. 7. } 8. public class Student implements Comparable { 9. ... 10. public int compareTo(Student other) { 11. //Here we need to provide a body for the method. 12. } 13. } o We can make our own code generic too  Suppose you wantd stacks of Strings (and that Java didn’t already implement them for you)  Very non-generic stacks, can only deal with String objects:  Stack.java 1. package nongeneric; 2. 3. public class Stack 4. String[] contents; // The contents of this Stack. The top item is in contents[size - 1]. 5. int size; // The number of items in this Stack. 6. private static final int MAX_SIZE = 20; // The maximum number of items I can store. 7. 8. public Stack() { 9. contents = new String[MAX_SIZE]; 10. size = 0; 11. } 12. 13. /** Make value my new top value. 14. * @param value the new top value. 15. */ 16. public void push(String value) { 17. contents[size++] = value; 18. // contents[size] = value; Also valid 19. // size++; 20. } 21. 22. /** Return the top value. 23. * @return the top value. 24. */ 25. public String top() { 26. return contents[size - 1]; 27. } 28. 29. /** Remove and return the top value. 30. * @return the top value. 31. */ 32. public String pop() { 33. return contents[--size]; 34. } 35. 36. /** Return whether there are any items in this stack. 37. * @return whether there are any items in this stack. 38. */ 39. public boolean isEmpty() { 40. return size == 0; 41. } 42. 43. /** Return the number of items in this Stack. 44. * @return the number of items in this Stack. 45. */ 46. public int size() { 47. return size; 48. } 49.} OCTOBER 10  DemoNongeneric.java 1. package nongeneric; 2. 3. public class Demo { 4. 5. public static void main(String[] args) { 6. Stack s = new Stack();
More Less

Related notes for CSC207H1

Log In


OR

Join OneClass

Access over 10 million pages of study
documents for 1.3 million courses.

Sign up

Join to view


OR

By registering, I agree to the Terms and Privacy Policies
Already have an account?
Just a few more details

So we can recommend you notes for your school.

Reset Password

Please enter below the email address you registered with and we will send you a link to reset your password.

Add your courses

Get notes from the top students in your class.


Submit