Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Tirgul no. 14 Topics covered: H Test Questions H Review of important concepts.

Similar presentations


Presentation on theme: "1 Tirgul no. 14 Topics covered: H Test Questions H Review of important concepts."— Presentation transcript:

1 1 Tirgul no. 14 Topics covered: H Test Questions H Review of important concepts

2 2 Test Format H The test will consist of 4 questions of which you’ll have to choose 3. H Each question may require writing code, explaining written code, and answering general knowledge question about java concepts and computer science concepts.

3 3 Important Concepts H OOP in java: Encapsulation (access modifiers) Inheritance Polymorphism Overloading Modifiers: static, final etc. Exceptions

4 4 Important Concepts H Computer Science Concepts: Data Structures: Array, List, Stack, Queue, BinaryTree Complexity analysis: running time, space Memory segments: Stack, Heap. References and aliasing Recursion Sorting algorithms

5 5 Example Questions H The examples provided here are from former tests. Note that the test format has changed, but the topics, concepts and important issues are the same.. מה זמן הריצה של המתודה הבאה: static void f(int n) { for ( int j=0 ; j < n ; j++ ) for ( int k = n ; k > 1 ; k = k/2 ) System.out.println(“*”); } א. O(1) ב. O(logn) ג. O(n) ד. O(nlogn)

6 6 Example Questions H The examples provided here are from former tests. Note that the test format has changed, but the topics, concepts and important issues are the same.. מה זמן הריצה של המתודה הבאה: static void f(int n) { for ( int j=0 ; j < n ; j++ ) for ( int k = n ; k > 1 ; k = k/2 ) System.out.println(“*”); } א. O(1) ב. O(logn) ג. O(n) ד. O(nlogn)

7 7 Exception Handling. נתון עץ ההורשה הבא : Exception AException BException בהנתן פונקציה m1 שזורקת (throws) חריגות (Exceptions) מסוג AException וגם – BException שנרצה לטפל בהן בנפרד, הדרך הנכונה לעשות זאת היא : א. try { m1(); } catch(BException be) {System.out.println("got BException"); }catch(AException ae) {System.out.println("got AException"); } ב. try { m1(); } catch(AException ae) {System.out.println("got AException"); }catch(BException be) {System.out.println("got BException"); }

8 8 ג. try { m1(); } catch(Exception e) {System.out.println("got Exception"); }catch(AException ae) {System.out.println("got AException"); }catch(BException be) {System.out.println("got BException"); } ד. אין דרך להבדיל בין שתי החריגות AException ו – BException שנזרקו מהפונקציה m1

9 9. נתון עץ ההורשה הבא : Exception AException BException בהנתן פונקציה m1 שזורקת (throws) חריגות (Exceptions) מסוג AException וגם – BException שנרצה לטפל בהן בנפרד, הדרך הנכונה לעשות זאת היא : א. try { m1(); } catch(BException be) {System.out.println("got BException"); }catch(AException ae) {System.out.println("got AException"); } ב. try { m1(); } catch(AException ae) {System.out.println("got AException"); }catch(BException be) {System.out.println("got BException"); }

10 10 Recursion questions. נתונה הפונקציה הבאה : public int what(int data[], int i) { if(i==0) return(data[0]); else return 2*what(data,i-1) + data[i]; } מה יהיה ערך ההחזרה של הפונקציה מההפעלה הבאה : int data[] = {1,1,0,1}; what(data,data.length-1); א. 11 ב. 13 ג. 15 ד. 17

11 11 Recursion - BinaryTree כתוב פונקציה סטטית אשר מקבלת כפרמטר עץ בינארי ומחזירה את גובהו של העץ. גובה העץ מוגדר כמסלול הארוך ביותר בין שורש העץ לעלים. public static int treeHeight(BinaryTree tree) הנח כי נתון הממשק הבא של עץ בינארי ( כאן לא מופיע הממשק מטעמי מקום, ולכן נשתמש בממשק שהוצג בתרגולים קודמים ).

12 12 treeHeight - Solution public static int treeHeight(BinaryTree tree){ if(tree == null) return(0); return( max(treeHeight(tree.getLeft()), treeHeight(tree.getRight()) ) + 1 ); }

13 13 Concepts in OOP נתונות המחלקות הבאות : class Point2D { public int x; public int y; public Point2D(int x, int y) { this.x = x; this.y = y; } class Circle { public Point2D center; public int radius; public Circle(int radius, int centerX, int centerY) { this.radius = radius; center = new Point2D(centerX,centerY); } public Circle(int radius, Point2D center) { this.center = center; this.radius = radius; }

14 14 Concepts cont. נתון קטע הקוד הבא : Point2D p = new Point2D(1,2); Circle c1 = new Circle(1,p); Circle c2 = new Circle(1,3,4); p.x = 5; p.y = 6; Circle c3 = new Circle(1,p); System.out.println(c1.center.x + " " + c1.center.y + " " + c2.center.x + " " + c2.center.y + " " + c3.center.x + " " + c3.center.y); 7. מה ידפיס קטע הקוד המופיע לעיל ? א. 6 5 4 3 2 1 ב. 6 5 4 3 6 5 ג. 6 5 6 5 6 5 ד. 2 1 4 3 2 1

15 15 Concepts cont. נתון קטע הקוד הבא : Point2D p = new Point2D(1,2); Circle c1 = new Circle(1,p); Circle c2 = new Circle(1,3,4); p.x = 5; p.y = 6; Circle c3 = new Circle(1,p); System.out.println(c1.center.x + " " + c1.center.y + " " + c2.center.x + " " + c2.center.y + " " + c3.center.x + " " + c3.center.y); 7. מה ידפיס קטע הקוד המופיע לעיל ? א. 6 5 4 3 2 1 ב. 6 5 4 3 6 5 ג. 6 5 6 5 6 5 ד. 2 1 4 3 2 1

16 16 Concepts cont. במחלקה Circle נחליף את הגדרת השדה center להגדרה הבאה : class Circle { public staticPoint2D center; public int radius; … //same class methods } מה ידפיס קטע הקוד המופיע לעיל כעת ? א. 6 5 4 3 6 5 ב. 6 5 4 3 2 1 ג. 6 5 6 5 6 5 ד. 2 1 4 3 2 1

17 17 Concepts cont. במחלקה Circle נחליף את הגדרת השדה center להגדרה הבאה : class Circle { public staticPoint2D center; public int radius; … //same class methods } מה ידפיס קטע הקוד המופיע לעיל כעת ? א. 6 5 4 3 6 5 ב. 6 5 4 3 2 1 ג. 6 5 6 5 6 5 ד. 2 1 4 3 2 1

18 18Inheritance נתונת המחלקות הבאות : public class A { private int n; public A(){ System.out.println(“I’m A’s constructor”); } public A(int n){ this.n= n; } public class B extends A { private String str; public B() { System.out.println(“I’m B’s constructor”); } }

19 19 Inheritance cont. מה ידפיס קטע הקוד הבא : //in main B b1= new B(); א. I’m A’s constructor I’m B’s constructor ב. I’m B’s constructor ג. כלום, יש בעיה עם הקוד. ד. I’m B’s constructor I’m A’s constructor

20 20 Inheritance cont. מה ידפיס קטע הקוד הבא : //in main B b1= new B(); א. I’m A’s constructor I’m B’s constructor ב. I’m B’s constructor ג. כלום, יש בעיה עם הקוד. ד. I’m B’s constructor I’m A’s constructor

21 21 Implementation Questions הגדירו את המחלקה Histogram אשר מממשת היסטוגרמה. עליכם לממש את המחלקה ולכלול במימוש תיעוד API בסגנון javadoc. היסטוגרמה הינה מבנה אשר מונה את מספר המופעים של נתונים על פני טווח מסוים. לדוגמא : אם טווח המספרים הוא – 1-5, היסטוגרמה אפשרית אחת הינה : התרשים המופיע מטה הינו לצורכי המחשה בלבד ! * * * * * * * * 1 2 3 4 5

22 22 Implementation cont. המחלקה תכיל את הממשק הבא : public Histogram(int minRange, int maxRange) קונסטרקטור שמקבל שני פרמטרים מסוג int אשר מייצגים את טווח הערכים אשר תכיל ההיסטוגרמה. ניתן להניח כי min קטן מ - max. public void addEntry(int binValue); פונקציה שמוסיפה איבר לאחד מתאי ההיסטוגרמה. ניתן להניח כי הערך הינו חוקי – כלומר בטווח של ההיסטוגרמה הנתונה. public int mostFrequent(); פונקציה שמחזירה את הערך שהוא בעל מספר המופעים המקסימלי בהיסטוגרמה ( הערך השכיח ביותר ). אם קיים יותר מערך אחד אשר מופיעים בשכיחות המקסימלית ניתן להחזיר כל אחד מהם. יתכן בהחלט כי ההיסטוגרמה ריקה ואז ניתן להחזיר כל ערך בטווח.

23 23 Implementation Question no.2 א. הגדירו מחלקה אבסטרקטית בשם Sequence שמהווה סדרה סופית של איברים מסוג מספרים ממשיים. האינקדס הראשון בסידרה הינו 1, כלומר אם בסדרה n איברים אז 1<=i<=n. המחלקה תכיל את המתודות הבאות : public abstract double elementAt(int i); פונקציה אבסטרקטית שמחזירה את האלמנט ה – i בסדרה כמספר ממשי. לא ניתן להניח כי i אינדקס חוקי בסדרה. public abstract int length(); פונקציה אבסטרקטית שמחזירה את אורך הסדרה הנוכחית. public double sumTo(int n); פונקציה ממשית, אשר מחזירה את סכום איברי הסדרה עד לאיבר ה – n י. לא ניתן להניח כי n אינדקס חוקי כלומר כי: 1<=n<=length().

24 24 Question no. 2 cont. ב. ממשו מחלקה בשם GeometricSequence בעלת מספר סופי של איברים שמקבלת בקונסטרקטור את a1 את q ואת n. המחלקה יורשת את המחלקה האבסטרקטית Sequence. המחלקה תכיל את המתודות הבאות : public GeometricSequence(double a1, double q, int n); קונסטרקטור שמקבל שלושה פרמרטים שמגדירים סדרה הנדסית : a1 – האיבר הראשון בסדרה. Q - המנה של הסדרה. N - אורך הסדרה. כמובן שהמחלקה תממש את כל הפונקציות האבסטרקטיות של Sequence.

25 25 Question no. 2 cont. ג. נגדיר מחלקה בשם InterleavedSequence אשר יורשת גם היא את המחלקה האבסטרקטית Sequence, שמקבלת בקונסטרקטור שתי סדרות כלשהן, ומרכיבה סדרה שהיא שזירה של שתי הסדרות הנתונות. אם אחת הסדרות ארוכה יותר מן השניה, אז נתעלם מן הסוף של הסדרה הארוכה יותר. אורך הסדרה השזורה יחושב תוך התעלמות מן הסוף של הסדרה הארוכה יותר : קלט : סדרה א ': a1, a2, a3, a4. ( אורך הסדרה = 4) סדרה ב ': b1, b2, b3, b4, b5, b6 ( אורך הסדרה = 6) השזירה של הסדרות תוגדר כך : סדרה ג ': a1, b1, a2, b2, a3, b3, a4, b4 ( אורך הסדרה = 8) שימו לב כי שני האיברים האחרונים של הסדרה ב ' הושמטו ! המחלקה תכיל את המתודות הבאות : public InterleavedSequence(Sequence s1, Sequence s2); קונסטרקטור שמקבל שני פרמטרים מסוג Sequence, ומרכיב סדרה שהיא שזירה של שתי הסדרות הנתונות. המחלקה תממש כמובן את כל הפונקציות האבסטרקטיות של המחלקה Sequence.

26 26 Tips and Suggestions H Review all relevant material. H Work on writing code on paper – this is substantially different than writing on the computer – (no backspace!, no compiler!) H Try to read code other people wrote and understand what it does. H Solve questions from previous test (which can be found in the library) H Tirgul Chazara will be given on Monday 20 th of January 16:00-18:00 in Papick Elion.


Download ppt "1 Tirgul no. 14 Topics covered: H Test Questions H Review of important concepts."

Similar presentations


Ads by Google