260 likes | 336 Views
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
E N D
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.
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;}
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)
public class Group implements Iterable<Person> { private HashSet<Person> people; public Group() { this.people = new HashSet<Person>(); }
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); }}
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(); }
@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; }
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
public class Group implements Iterable<Person> { private Person[] people; public Group() { this.people = new Person[0]; }
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; }}
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
private boolean contains(Person person) { boolean result = false; for(Person p : this.people) { if(p.equals(person)) { result = true; break; } } return result; }
private boolean containsAll(Person[] people) { boolean result = true; for(Person p : people) { if(!this.contains(p)) { result = false; break; } } return result; }
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?
@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; }
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; }
@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; }
@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"); } }
@Override public void remove() throws throws UnsupportedOperationException { throw new UnsupportedOperationException("remove no supported"); }
Graphical User Interfaces [notes Chap 7] and [AJ Chap 17, Sec 13.2]
Model—View—Controller Model View Controller
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