1 tirgul no. 14 topics covered: h test questions h review of important concepts

Post on 20-Dec-2015

218 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

1

Tirgul no. 14Tirgul no. 14

Topics covered:

Test Questions Review of important concepts

2

Test FormatTest Format

The test will consist of 4 questions of which you’ll have to choose 3.

Each question may require writing code, explaining written code, and answering general knowledge question about java concepts and computer science concepts.

3

Important ConceptsImportant Concepts

OOP in java: • Encapsulation (access modifiers)

• Inheritance

• Polymorphism

• Overloading

• Modifiers: static, final etc.

• Exceptions

4

Important ConceptsImportant Concepts

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

Example QuestionsExample Questions 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

Example QuestionsExample Questions 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

Exception HandlingException Handling . נתון עץ ההורשה הבא:Exception

   AException 

BException – וגם AException) מסוג Exceptions)חריגות (throws שזורקת (m1 בהנתן פונקציה

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

ג. 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");

}

 

שנזרקו BException– ו AExceptionד. אין דרך להבדיל בין שתי החריגות m1מהפונקציה

9

. נתון עץ ההורשה הבא:Exception

   AException 

BException – וגם AException) מסוג Exceptions)חריגות (throws שזורקת (m1 בהנתן פונקציה

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

Recursion questionsRecursion 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

Recursion - BinaryTreeRecursion - BinaryTree

כתוב פונקציה סטטית אשר מקבלת כפרמטר עץ בינארי ומחזירה את גובהו של העץ. גובה העץ מוגדר כמסלול

הארוך ביותר בין שורש העץ לעלים.

public static int treeHeight(BinaryTree tree)

הנח כי נתון הממשק הבא של עץ בינארי (כאן לא מופיע הממשק מטעמי מקום, ולכן נשתמש בממשק שהוצג

בתרגולים קודמים).

12

treeHeight - SolutiontreeHeight - Solution

public static int treeHeight(BinaryTree tree){

if(tree == null)

return(0);

return( max(treeHeight(tree.getLeft()),

treeHeight(tree.getRight()) ) + 1 );

}

13

Concepts in OOPConcepts 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

Concepts contConcepts 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

1 2 3 4 5 6א. 5 6 3 4 5 6ב. 5 6 5 6 5 6ג. 1 2 3 4 1 2ד.

15

Concepts contConcepts 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

1 2 3 4 5 6א. 5 6 3 4 5 6ב. 5 6 5 6 5 6ג. 1 2 3 4 1 2ד.

16

Concepts contConcepts cont..

להגדרה הבאה:center נחליף את הגדרת השדה Circleבמחלקה  

class Circle { public staticPoint2D center; public int radius; … //same class methods } 

מה ידפיס קטע הקוד המופיע לעיל כעת? 5 6 3 4 5 6 א. 1 2 3 4 5 6ב. 5 6 5 6 5 6ג. 1 2 3 4 1 2ד.

17

Concepts contConcepts cont..

להגדרה הבאה:center נחליף את הגדרת השדה Circleבמחלקה  

class Circle { public staticPoint2D center; public int radius; … //same class methods } 

מה ידפיס קטע הקוד המופיע לעיל כעת? 5 6 3 4 5 6 א. 1 2 3 4 5 6ב. 5 6 5 6 5 6ג. 1 2 3 4 1 2ד.

18

InheritanceInheritanceנתונת המחלקות הבאות:

 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

Inheritance contInheritance cont..מה ידפיס קטע הקוד הבא:

//in mainB b1= new B(); 

א.             I’m A’s constructor

I’m B’s constructor

ב. I’m B’s constructor

 כלום, יש בעיה עם הקוד. ג.      

       ד. I’m B’s constructorI’m A’s constructor 

20

Inheritance contInheritance cont..מה ידפיס קטע הקוד הבא:

//in mainB b1= new B(); 

א.             I’m A’s constructor

I’m B’s constructor

ב. I’m B’s constructor

 כלום, יש בעיה עם הקוד. ג.      

       ד. I’m B’s constructorI’m A’s constructor 

21

Implementation QuestionsImplementation Questionsעליכם לממש את אשר מממשת היסטוגרמה. Histogram הגדירו את המחלקה

. javadoc בסגנון APIהמחלקה ולכלול במימוש תיעוד  

היסטוגרמה הינה מבנה אשר מונה את מספר המופעים של נתונים על פני טווח , היסטוגרמה אפשרית אחת 1-5– מסוים. לדוגמא: אם טווח המספרים הוא

הינה:התרשים המופיע מטה הינו לצורכי המחשה בלבד!

  

* * *

* * * ** * * * *

1 2 3 4 5  

22

Implementation contImplementation cont..המחלקה תכיל את הממשק הבא:

 

public Histogram(int minRange, int maxRange)

אשר מייצגים את טווח הערכים intקונסטרקטור שמקבל שני פרמטרים מסוג . max קטן מ- minניתן להניח כי . אשר תכיל ההיסטוגרמה

public void addEntry(int binValue);

פונקציה שמוסיפה איבר לאחד מתאי ההיסטוגרמה. ניתן להניח כי הערך הינו כלומר בטווח של ההיסטוגרמה הנתונה.– חוקי

public int mostFrequent();

פונקציה שמחזירה את הערך שהוא בעל מספר המופעים המקסימלי בהיסטוגרמה (הערך השכיח ביותר).

אם קיים יותר מערך אחד אשר מופיעים בשכיחות המקסימלית ניתן להחזיר כל אחד מהם.

יתכן בהחלט כי ההיסטוגרמה ריקה ואז ניתן להחזיר כל ערך בטווח.

23

Implementation Question no.2Implementation Question no.2 של איברים מסוג סופית שמהווה סדרה Sequenceא. הגדירו מחלקה אבסטרקטית בשם

מספרים ממשיים. . i<=n<=1 איברים אז n, כלומר אם בסדרה 1האינקדס הראשון בסידרה הינו

 המחלקה תכיל את המתודות הבאות:

 

public abstract double elementAt(int i);

 

  

בסדרה כמספר ממשי. לא ניתן להניח כי i– פונקציה אבסטרקטית שמחזירה את האלמנט ה iאינדקס חוקי בסדרה .

public abstract int length();

 

פונקציה אבסטרקטית שמחזירה את אורך הסדרה הנוכחית.public double sumTo(int n);

 

י . לא ניתן להניח כי n– פונקציה ממשית, אשר מחזירה את סכום איברי הסדרה עד לאיבר ה n1 : אינדקס חוקי כלומר כי=>n<=length().

24

Question no. 2 contQuestion no. 2 cont..בעלת מספר סופי של איברים GeometricSequenceב. ממשו מחלקה בשם

. המחלקה יורשת את n ואת q את a1שמקבלת בקונסטרקטור את .Sequenceהמחלקה האבסטרקטית

 המחלקה תכיל את המתודות הבאות:

 public GeometricSequence(double a1, double q, int n);

קונסטרקטור שמקבל שלושה פרמרטים שמגדירים סדרה הנדסית:a1 – .האיבר הראשון בסדרהQ.המנה של הסדרה - N.אורך הסדרה -  

כמובן שהמחלקה תממש את כל הפונקציות האבסטרקטיות של Sequence.

 

25

Question no. 2 contQuestion no. 2 cont.. אשר יורשת גם היא את המחלקה האבסטרקטית InterleavedSequenceג. נגדיר מחלקה בשם

Sequence שמקבלת בקונסטרקטור שתי סדרות כלשהן, ומרכיבה סדרה שהיא שזירה של, שתי הסדרות הנתונות. אם אחת הסדרות ארוכה יותר מן השניה, אז נתעלם מן הסוף של

אורך הסדרה השזורה יחושב תוך התעלמות מן הסוף של הסדרה הארוכה יותר. :הסדרה הארוכה יותר

 )4. (אורך הסדרה= a1, a2, a3, a4קלט: סדרה א':

)6 (אורך הסדרה = b1, b2, b3, b4, b5, b6 סדרה ב':   

השזירה של הסדרות תוגדר כך:)8 (אורך הסדרה = a1, b1, a2, b2, a3, b3, a4, b4סדרה ג':

 שימו לב כי שני האיברים האחרונים של הסדרה ב' הושמטו!

  המחלקה תכיל את המתודות הבאות:

public InterleavedSequence(Sequence s1, Sequence s2);, ומרכיב סדרה שהיא שזירה של שתי Sequenceקונסטרקטור שמקבל שני פרמטרים מסוג

הסדרות הנתונות.  

.Sequenceהמחלקה תממש כמובן את כל הפונקציות האבסטרקטיות של המחלקה

26

Tips and SuggestionsTips and Suggestions

Review all relevant material. Work on writing code on paper – this is

substantially different than writing on the computer – (no backspace!, no compiler!)

Try to read code other people wrote and understand what it does.

Solve questions from previous test (which can be found in the library)

Tirgul Chazara will be given on Monday 20th of January 16:00-18:00 in Papick Elion.

top related