d04 assignment 1 comments
TRANSCRIPT
![Page 1: D04 Assignment 1 Comments](https://reader033.vdocument.in/reader033/viewer/2022060718/6297f8099f06974c0868c9ff/html5/thumbnails/1.jpg)
CS 635 Advanced Object-Oriented Design & Programming Fall Semester, 2018
Doc 4 Assignment 1 Comments Sep 11, 2018
![Page 2: D04 Assignment 1 Comments](https://reader033.vdocument.in/reader033/viewer/2022060718/6297f8099f06974c0868c9ff/html5/thumbnails/2.jpg)
2
Don't Panic This assignment is 1% of your grade
![Page 3: D04 Assignment 1 Comments](https://reader033.vdocument.in/reader033/viewer/2022060718/6297f8099f06974c0868c9ff/html5/thumbnails/3.jpg)
3
How many people have trained in one of? Sports Musical instruments Dance Debate Chess Art Public speaking Performance art
How important is practice?
![Page 4: D04 Assignment 1 Comments](https://reader033.vdocument.in/reader033/viewer/2022060718/6297f8099f06974c0868c9ff/html5/thumbnails/4.jpg)
Comments
4
What is the purpose of comments?
x = x + 1 // Add one to x
Duh Comments Repeating code in English Commenting the obvious
Why are we adding one to x?
![Page 5: D04 Assignment 1 Comments](https://reader033.vdocument.in/reader033/viewer/2022060718/6297f8099f06974c0868c9ff/html5/thumbnails/5.jpg)
Comments
5
// Class to represent Priority Queue class PriorityQueue {
// fields private Student[ ] studentHeap; private int heapSize;
// Constructor to create the priority queue public PriorityQueue(int capacity) { ... }
// get size of the heap public int size() {
return heapSize; }
![Page 6: D04 Assignment 1 Comments](https://reader033.vdocument.in/reader033/viewer/2022060718/6297f8099f06974c0868c9ff/html5/thumbnails/6.jpg)
Comments
6
// get size of the heap public int size() {
return heapSize; }
/*** * return size of the queue */ public int size() {
return heapSize; }
![Page 7: D04 Assignment 1 Comments](https://reader033.vdocument.in/reader033/viewer/2022060718/6297f8099f06974c0868c9ff/html5/thumbnails/7.jpg)
7
class Student {
public double priority() { return this.units * 0.7/150 + this.gpa * 0.3/4.0;
}
What are 0.7 150 0.3 4.0
![Page 8: D04 Assignment 1 Comments](https://reader033.vdocument.in/reader033/viewer/2022060718/6297f8099f06974c0868c9ff/html5/thumbnails/8.jpg)
8
class Student {
public double priority() { return this.units * UNITS_WEIGHT/MAX_UNITS + this.gpa * GPA_WEIGHT/MAX_GPA;
}
![Page 9: D04 Assignment 1 Comments](https://reader033.vdocument.in/reader033/viewer/2022060718/6297f8099f06974c0868c9ff/html5/thumbnails/9.jpg)
Comments
9
What needs commenting in this assignment?
Why did most people leave the first element of array/array list empty?
![Page 10: D04 Assignment 1 Comments](https://reader033.vdocument.in/reader033/viewer/2022060718/6297f8099f06974c0868c9ff/html5/thumbnails/10.jpg)
Struct
10
class Element { private Student student; private double priority;
public Element(Student student) { this.student = student; this.priority = student.units() * 0.7 + student.gpa() * 0.3;
}
public Student getStudent() { return student;}
public double getPriority() { return priority;}
Only works this assignment Hides priority computation
How to find it?
![Page 11: D04 Assignment 1 Comments](https://reader033.vdocument.in/reader033/viewer/2022060718/6297f8099f06974c0868c9ff/html5/thumbnails/11.jpg)
11
class Association<K,V> { private K key; private V value;
public Association(K key, V value) { this.key
}
public key() { return key; }
public value() { return value; }
Independent of student & assignment
Usable in hashtable/dictionary priority queue binary tree
![Page 12: D04 Assignment 1 Comments](https://reader033.vdocument.in/reader033/viewer/2022060718/6297f8099f06974c0868c9ff/html5/thumbnails/12.jpg)
Code Reuse
12
class PriorityQueue { private Student[ ] studentHeap;
Where can this be used?
class PriorityQueue<E> { private Association<Double, E>[ ] heap;
Where can this be used?
class PriorityQueue { private Student[ ] studentHeap;
private double studentPriority(Student aStudent) { return 0.3 * aStudent.gpa()/4.0 + 0.7 * aStudent.units()/150;
}
Where can this be used?
![Page 13: D04 Assignment 1 Comments](https://reader033.vdocument.in/reader033/viewer/2022060718/6297f8099f06974c0868c9ff/html5/thumbnails/13.jpg)
13
class PriorityQueue:
def add_node(self, input): if type(input) is not Student:
raise ValueError("Must be a Student")
etc
How do Python collection classes handle this?
![Page 14: D04 Assignment 1 Comments](https://reader033.vdocument.in/reader033/viewer/2022060718/6297f8099f06974c0868c9ff/html5/thumbnails/14.jpg)
14
class PriorityQueue:
def addNode(self, input): if type(input) is not Student:
raise ValueError("Must be a Student")
etc
What does Pep 8 say about naming methods?
![Page 15: D04 Assignment 1 Comments](https://reader033.vdocument.in/reader033/viewer/2022060718/6297f8099f06974c0868c9ff/html5/thumbnails/15.jpg)
Responsibility
15
class PriorityQueue { private Student[ ] studentHeap;
private double studentPriority(Student aStudent) { return 0.3 * aStudent.gpa()/4.0 + 0.7 * aStudent.units()/150;
}
Why is PriorityQueue computing priority of a student?
studentPriority does not use any field of PriorityQueue
![Page 16: D04 Assignment 1 Comments](https://reader033.vdocument.in/reader033/viewer/2022060718/6297f8099f06974c0868c9ff/html5/thumbnails/16.jpg)
16
class Student { private int units; private double gpa;
public double priority() { return return 0.3 * gpa()/4.0 + 0.7 * units()/150;
}
Why this priority?
Why only one priority?
Priority depends on the context of the object
![Page 17: D04 Assignment 1 Comments](https://reader033.vdocument.in/reader033/viewer/2022060718/6297f8099f06974c0868c9ff/html5/thumbnails/17.jpg)
17
class PriorityQueue { private Student[ ] studentHeap;
private int checkGpaRange(double g) { int checkgpa = 1; if (!(g>= 0.0 && g<= 4.0)) {
System.out.println("\nInvalid Input"); checkgpa = 0; return checkgpa;
} else { checkgpa = 1; return checkgpa;
}
private int checkGpaRange(double g) { if (!(g>= 0.0 && g<= 4.0)) {
return 0; } else {
return 1; }
private int checkGpaRange(double gpa) { return gpa>= 0.0 && gpa<= 4.0;
}
Why is PriorityQueue validating student's data?
![Page 18: D04 Assignment 1 Comments](https://reader033.vdocument.in/reader033/viewer/2022060718/6297f8099f06974c0868c9ff/html5/thumbnails/18.jpg)
18
class PriorityQueue { private static Student[ ] studentHeap;
PriorityQueue a = new PriorityQueue(); a.add(student1); a.add(student2);
PriorityQueue b = new PriorityQueue(); b.size(); // 2
![Page 19: D04 Assignment 1 Comments](https://reader033.vdocument.in/reader033/viewer/2022060718/6297f8099f06974c0868c9ff/html5/thumbnails/19.jpg)
19
class PriorityQueue { static int size = 0; private ArrayList<Student> studentList = new ArrayList<Student>();
size will not be consistent with size in studentList
studentList knows its size
![Page 20: D04 Assignment 1 Comments](https://reader033.vdocument.in/reader033/viewer/2022060718/6297f8099f06974c0868c9ff/html5/thumbnails/20.jpg)
20
class PriorityQueue { private Student[ ] studentHeap;
public void askForInput() { promptUserForStudentInfo(): Student newStudent = readStudentInfoFromComandLine(); add(newStudent); print();
}
![Page 21: D04 Assignment 1 Comments](https://reader033.vdocument.in/reader033/viewer/2022060718/6297f8099f06974c0868c9ff/html5/thumbnails/21.jpg)
21
class Student { public Student(String name, int redID, String email, float GPA, int unitsTaken) {
Scanner in = new Scanner(System.in); this.name = name; this.redID = redID; this.email = email;
while (GPA < 0.0f || GPA > 4.0f) { System.out.print("Please enter a GPA between 0.0 and 4.0: "); GPA = in.nextFloat();
} }
}
![Page 22: D04 Assignment 1 Comments](https://reader033.vdocument.in/reader033/viewer/2022060718/6297f8099f06974c0868c9ff/html5/thumbnails/22.jpg)
22
class PriorityQueue { private Student[ ] studentHeap;
public void print() { for (student in list) {
System.out.println(student.toString()); }
Can not test the result
Can not use the print method in mobile web back end enterprise GUI app Embedded
How many Java JDK classes have methods that print to standard out?
![Page 23: D04 Assignment 1 Comments](https://reader033.vdocument.in/reader033/viewer/2022060718/6297f8099f06974c0868c9ff/html5/thumbnails/23.jpg)
23
class PriorityQueue { private Student[ ] studentHeap;
public String toString() { blah return result;
}
Can test the result
Can use the toString method in mobile web back end enterprise GUI app Embedded
![Page 24: D04 Assignment 1 Comments](https://reader033.vdocument.in/reader033/viewer/2022060718/6297f8099f06974c0868c9ff/html5/thumbnails/24.jpg)
24
class PriorityQueue { private Student[ ] studentHeap;
public Iterator iterator() { blah return iterator;
}
Can test the result
Can use anywhere Can do more than display string Have access to student objects
![Page 25: D04 Assignment 1 Comments](https://reader033.vdocument.in/reader033/viewer/2022060718/6297f8099f06974c0868c9ff/html5/thumbnails/25.jpg)
25
class PriorityQueue { private Student[ ] studentHeap;
public Student removeStudent() { if (studentHeap.isEmpty())
System.out.println("Can not remove student from empty queue");
}
![Page 26: D04 Assignment 1 Comments](https://reader033.vdocument.in/reader033/viewer/2022060718/6297f8099f06974c0868c9ff/html5/thumbnails/26.jpg)
26
class PriorityQueue { private Student[ ] studentHeap;
public Student removeStudent() { if (studentHeap.isEmpty())
What to do here?
}
What does your language's collection classes do in this case?
Why should you do the same?
![Page 27: D04 Assignment 1 Comments](https://reader033.vdocument.in/reader033/viewer/2022060718/6297f8099f06974c0868c9ff/html5/thumbnails/27.jpg)
Standard In/Out
27
Useful in Debugging Unix commands Command line installers Command line programs
Not useful in GUI applications
Desktop & Mobile Front end web Server side applications Enterprise Embedded software
Java only recently has reasonable ways to read standard in
![Page 28: D04 Assignment 1 Comments](https://reader033.vdocument.in/reader033/viewer/2022060718/6297f8099f06974c0868c9ff/html5/thumbnails/28.jpg)
28
class PriorityQueue { private Student[ ] studentHeap;
public static void main([String[] args) { Scanner in = new Scanner(System.in); System.out.println("Enter size of priority queue"); PriorityQueue waitlist = new PriorityQueue(in.nextInt());
char ch = 0;
do { System.out.println("\nPriority Queue Operations\n"); System.out.println("1. Insert student in queue"); System.out.println("2. Remove student from queue"); System.out.println("3. Print queue"); etc.
}
![Page 29: D04 Assignment 1 Comments](https://reader033.vdocument.in/reader033/viewer/2022060718/6297f8099f06974c0868c9ff/html5/thumbnails/29.jpg)
29
class PriorityQueue { private Student[ ] studentHeap;
public Student removeStudent() {
}
What does your language's collection call this method?
Why does it matter?
![Page 30: D04 Assignment 1 Comments](https://reader033.vdocument.in/reader033/viewer/2022060718/6297f8099f06974c0868c9ff/html5/thumbnails/30.jpg)
Naming Methods
30
Look to see if your class library has a similar method
Consider using the same name Fewer names to remember Programmers already know the meaning Start to get replaceable components
![Page 31: D04 Assignment 1 Comments](https://reader033.vdocument.in/reader033/viewer/2022060718/6297f8099f06974c0868c9ff/html5/thumbnails/31.jpg)
31
class PriorityQueue { private Student[ ] studentHeap;
public Student removeStudent() {
}
What interface/class does PriorityQueue implement in your language?
Why does it matter?
![Page 32: D04 Assignment 1 Comments](https://reader033.vdocument.in/reader033/viewer/2022060718/6297f8099f06974c0868c9ff/html5/thumbnails/32.jpg)
Java Queue Interface
32
AbstractQueue, ArrayBlockingQueue, ArrayDeque, ConcurrentLinkedDeque, ConcurrentLinkedQueue, DelayQueue, LinkedBlockingDeque, LinkedBlockingQueue, LinkedList, LinkedTransferQueue, PriorityBlockingQueue, PriorityQueue, SynchronousQueue
Implementing classes
public foo(Queue x) { blah
}
foo(Z); // Z can be any of the above classes
![Page 33: D04 Assignment 1 Comments](https://reader033.vdocument.in/reader033/viewer/2022060718/6297f8099f06974c0868c9ff/html5/thumbnails/33.jpg)
Program to an Interface, not an Implementation
33
public foo(ArrayList x) { blah
}
public foo(List x) { blah
}
ArrayList, AttributeList, CopyOnWriteArrayList, LinkedList, RoleList, RoleUnresolvedList, Stack, Vector
![Page 34: D04 Assignment 1 Comments](https://reader033.vdocument.in/reader033/viewer/2022060718/6297f8099f06974c0868c9ff/html5/thumbnails/34.jpg)
34
public interface PriorityQueue { void add(Student s);
Student remove();
Student peek();
boolean isEmpty();
int size();
}
How many types of PriorityQueues that hold students are you going to implement?
So why bother with interface?
![Page 35: D04 Assignment 1 Comments](https://reader033.vdocument.in/reader033/viewer/2022060718/6297f8099f06974c0868c9ff/html5/thumbnails/35.jpg)
35
class PriorityQueue { public void add(String name, String redid, String email, String address, int units, double gpa) { }
class PriorityQueue { public void add(Student item) { }
What does the add method return in your language's add method?
Why does it matter?
Why should a PriorityQueue know how to create a Student object?
![Page 36: D04 Assignment 1 Comments](https://reader033.vdocument.in/reader033/viewer/2022060718/6297f8099f06974c0868c9ff/html5/thumbnails/36.jpg)
36
class Student { private String name; private String redid; private String email; private int units; private double gpa; private double priority;
public String getName() { return name;} public String getRedid() { return redid;} public String getEmail() { return email;} public int getUnits() { return units;} public double getGpa() { return gpa;} public double getPriority() {return priority;}
public void setName(String name) { this.name = name:} etc
![Page 37: D04 Assignment 1 Comments](https://reader033.vdocument.in/reader033/viewer/2022060718/6297f8099f06974c0868c9ff/html5/thumbnails/37.jpg)
Improper Inheritance
37
class StudentNode extends Student { private StudentNode parent; private StudentNode leftChild; private StudentNode rightChild;
class StudentNode { private Student data private StudentNode parent; private StudentNode leftChild; private StudentNode rightChild;
The node in the tree now has a name address email address etc. They are not used
A StudentNode is not a type of student
A StudentNode has a student
![Page 38: D04 Assignment 1 Comments](https://reader033.vdocument.in/reader033/viewer/2022060718/6297f8099f06974c0868c9ff/html5/thumbnails/38.jpg)
Is-a Has-a Test
38
If A has a B B is a field in A
If A is a type of B A is a subclass of B
![Page 39: D04 Assignment 1 Comments](https://reader033.vdocument.in/reader033/viewer/2022060718/6297f8099f06974c0868c9ff/html5/thumbnails/39.jpg)
39
class Priority { static let instance = Priority()
func getPriority(student: Student) -> Double { return 0.7 * Double(student.units) + 0.3 * student.gpa
} }
let student = Student(name: "Roger", redId: "1234566789", units: 25, gpa: 3.7, email: "[email protected])
Priority.instance.getPriority(student: student)
Not a class, just a function
![Page 40: D04 Assignment 1 Comments](https://reader033.vdocument.in/reader033/viewer/2022060718/6297f8099f06974c0868c9ff/html5/thumbnails/40.jpg)
40
func getPriority(student: Student) -> Double { return 0.7 * Double(student.units) + 0.3 * student.gpa
}
let student = Student(name: "Roger", redId: "1234566789", units: 25, gpa: 3.7, email: "[email protected])
getPriority(student: student)
Simpler to just use function!
But function just used data from student!
![Page 41: D04 Assignment 1 Comments](https://reader033.vdocument.in/reader033/viewer/2022060718/6297f8099f06974c0868c9ff/html5/thumbnails/41.jpg)
Heuristics
41
Keep related data and behavior in one place
A class should capture one and only one key abstraction
![Page 42: D04 Assignment 1 Comments](https://reader033.vdocument.in/reader033/viewer/2022060718/6297f8099f06974c0868c9ff/html5/thumbnails/42.jpg)
Class
42
Represents an abstraction
Encapsulates data and operations of the abstraction
Hide design decisions/details
Data
Operations
![Page 43: D04 Assignment 1 Comments](https://reader033.vdocument.in/reader033/viewer/2022060718/6297f8099f06974c0868c9ff/html5/thumbnails/43.jpg)
Utility method
43
Method in class that Does not access any field (data member, instance variables) Just uses parameters
![Page 44: D04 Assignment 1 Comments](https://reader033.vdocument.in/reader033/viewer/2022060718/6297f8099f06974c0868c9ff/html5/thumbnails/44.jpg)
44
class Student {
func priority() -> Double { return 0.7 * Double(this.units) + 0.3 * this.gpa
} }
let student = Student(name: "Roger", redId: "1234566789", units: 25, gpa: 3.7, email: "[email protected])
student.priority()
Keeping operations with data
Swift does not prepend get in front of accessors
![Page 45: D04 Assignment 1 Comments](https://reader033.vdocument.in/reader033/viewer/2022060718/6297f8099f06974c0868c9ff/html5/thumbnails/45.jpg)
More Swift Like
45
struct Student {
var priority: Double { get {
return 0.7 * Double(this.units) + 0.3 * this.gpa }
} }
let student = Student(name: "Roger", redId: "1234566789", units: 25, gpa: 3.7, email: "[email protected])
student.priority
![Page 46: D04 Assignment 1 Comments](https://reader033.vdocument.in/reader033/viewer/2022060718/6297f8099f06974c0868c9ff/html5/thumbnails/46.jpg)
46
class PriorityQueue {
public String toString() { printQueue(); return "End of current queue";
}
![Page 47: D04 Assignment 1 Comments](https://reader033.vdocument.in/reader033/viewer/2022060718/6297f8099f06974c0868c9ff/html5/thumbnails/47.jpg)
Information Hiding
47
class PriorityQueue { private ArrayList<Student> studentList = new ArrayList<Student>();
public ArrayList<Student> print() { for (Student student : studentList) {
System.out.println(student.toString()); return studentList;
}
public ArrayList<Student> print() { for (Student student : studentList) {
System.out.println(student.toString()); return studentList.copy();
}
Anyone can now edit the student list
![Page 48: D04 Assignment 1 Comments](https://reader033.vdocument.in/reader033/viewer/2022060718/6297f8099f06974c0868c9ff/html5/thumbnails/48.jpg)
48
class PriorityQueue { ArrayList<Student> studentList = new ArrayList<Student>();
public static Student addStudent() { code not shown
}
How to add students?
PriorityQueue studentQueue = new PriorityQueue(); studentQueue.studentList.add(new Student());