1 / 26

PEx03

PEx03. PEx03. Modify your Person class such that the class is part of the package pex03 and such that it also supports the following method. getNumberOfPersons public static int getNumberOfPersons () Returns the number of persons that have been created so far. public class Person

lexine
Download Presentation

PEx03

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. PEx03

  2. PEx03 • Modify your Person class such that the class is part of the package pex03 and such that it also supports the following method. getNumberOfPersons public static intgetNumberOfPersons() Returns the number of persons that have been created so far.

  3. public class Person {// name and age attributesprivate static intnumCreated = 0;public Person(){ // set non-static attributesPerson.numCreated++;}Person(String name, int age){ // set non-static attributesPerson.numCreated++;} public static intgetNumberOfPersons(){ return Person.numCreated;}

  4. PEx04

  5. PEx04 • implement a class that represents a group of Person instances • the group contains unique Persons • the group supports iteration • the group should delegate to a Collection of some sort • 1, 2, and 3 imply that you should use a Set (or possibly a Map, but this is harder)

  6. public class Group implements Iterable<Person> { private HashSet<Person> people; public Group() { this.people = new HashSet<Person>(); }

  7. public Group(Group other) { this(); for(Person p : other) { // shallow copy // this.people.add(p); // deep copy Person q = new Person(p.getName(), p.getAge()); this.people.add(q); }}

  8. public boolean add(Person person) { Person p = new Person(person.getName(), person.getAge()); return this.people.add(p); } public Iterator<Person> iterator() { return this.people.iterator(); }

  9. @Override public boolean equals(Object obj) { boolean eq = false; if(obj != null && this.getClass() == obj.getClass()) { Group other = (Group) obj; eq = this.size() == other.size() && this.people.containsAll(other.people); } return eq; }

  10. PEx05

  11. PEx05 • PEx04 except that you need to replace the Set with an array • because Group implements Iterable<Person> you need to implement a class that represents an iterator

  12. public class Group implements Iterable<Person> { private Person[] people; public Group() { this.people = new Person[0]; }

  13. public Group(Group other) { this.people = new Person[other.size()]; int i = 0; for(Person p : other) { // shallow copy // this.people[i++] = p; // deep copy Person q = new Person(p.getName(), p.getAge()); this.people[i++] = q; }}

  14. public int size() { return this.people.length; } public Iterator<Person> iterator() { return new GroupIterator(this); } every array has a public attribute named length that contains the number of elements in the array this is a reference to the Group instance that was used to call iterator

  15. private boolean contains(Person person) { boolean result = false; for(Person p : this.people) { if(p.equals(person)) { result = true; break; } } return result; }

  16. private boolean containsAll(Person[] people) { boolean result = true; for(Person p : people) { if(!this.contains(p)) { result = false; break; } } return result; }

  17. public boolean add(Person person) { boolean result = false; Person p = new Person(person.getName(), person.getAge()); if(!this.contains(p)) { int newSize = this.size() + 1; this.people = Arrays.copyOf(this.people, newSize); this.people[newSize – 1] = p; result = true; } return result; } shallow copy; why is this ok here?

  18. @Override public boolean equals(Object obj) { boolean eq = false; if(obj != null && this.getClass() == obj.getClass()) { Group other = (Group) obj; eq = this.size() == other.size() && this.containsAll(other.people); } return eq; }

  19. public class GroupIterator implements Iterator<Person> { private Person[] people; private int next; public GroupIterator(Group group) { // copy all the Persons in group into this.people this.people = new Person[group.size()]; for(int i = 0; i < this.people.length; i++) { this.people[i] = group.get(i); } this.next = 0; }

  20. @Override public boolean hasNext() { boolean result = true; if(this.next >= this.people.length) { result = false; } else if(this.people[next] == null) { result = false; } return true; }

  21. @Override public Person next() throws NoSuchElementException { if(this.hasNext()) { Person p = this.people[this.next]; this.next++; return p; } else { throw new NoSuchElementException("no more elements"); } }

  22. @Override public void remove() throws throws UnsupportedOperationException { throw new UnsupportedOperationException("remove no supported"); }

  23. Graphical User Interfaces [notes Chap 7] and [AJ Chap 17, Sec 13.2]

  24. Model—View—Controller Model View Controller

  25. http://java.sun.com/developer/technicalArticles/javase/mvc/

  26. model • represents state of the application and the rules that govern access to and updates of state • view • presents the user with a sensory (visual, audio, haptic) representation of the model state • a user interface element (the user interface for simple applications) • controller • processes and responds to events (such as user actions) from the view and translates them to model method calls

More Related