הנכות 1 םוכיס לוגרת 13 1

Similar documents
הנכות 1 םוכיס לוגרת 13 1

הנכות 1 םוכיס לוגרת 13 1

הנכות 1 םוכיס לוגרת 14 1

Software 1 with Java. Initialization. Initialization. Initialization. Pass by Value. Initialization. Recitation No. 11 (Summary)

תוכנה 1. תרגול מספר 11: Static vs. Dynamic Binding מחלקות מקוננות Nested Classes

מערכים שעור מס. 4 כל הזכויות שמורות דר ' דרור טובי המרכז האוניברסיטאי אריאל 1

תוכנה 1. תרגול מספר 11: Static vs. Dynamic Binding מחלקות מקוננות Nested Classes בית הספר למדעי המחשב אוניברסיטת תל אביב

תוכנה 1 סמסטר א' תשע"א

תוכנה 1 תרגול מספר 13

הקלחמ ה תמרב ת ונ וכ ת (static members ) יליזרב דהוא Java תפשב ם דקת מ תונכת ביבא ל ת תטיסרבינוא

תוכנה 1 תרגול מספר 13

תוכנה 1 * לא בהכרח בסדר הזה

הנכות 1 תואיגש םע תודדומתהו תואלול,םי : כרעמ 2 לוגרת

תוכנה 1 * לא בהכרח בסדר הזה

משתנים שעור מס. 2 כל הזכויות שמורות דר ' דרור טובי המרכז האוניברסיטאי אריאל 1

מבוא למדעי המחשב תרגול 8 רשימה משורשרת כללית, Comparator

הנכות 1 תואיגש םע תודדומתהו תואלול,םיכרעמ : לו 2 גרת

תוכנה 1 תרגול 2: מערכים ומבני בקרה

CSCI 201L Written Exam #1 - SOLUTION Fall % of course grade

Tutorial 10. Introduction to C++ שימו

תוכנה 1 ומחלקות פנימיות

תוכנה 1 תרגול 2: מערכים, מבני בקרה ושגיאות

בית הספר למדעי המחשב אוניברסיטת תל אביב תוכנה 1 תרגול מספר 9: הורשה מחלקות אבסטרקטיות חריגים

Objectives. Describe ways to create constants const readonly enum

מבוא לתכנות בשפת C. Tzachi (Isaac) Rosen

מבוא למדעי המחשב השפעת השינוי על סטודנט הרצאה 18: פולימורפיזם ומחלקות אבסטרקטיות אם ברצוננו ששכר הלימוד לא יעלה על 2500.

תוכנה 1 * לא בהכרח בסדר הזה

CSCI 201L Written Exam #1 Fall % of course grade

HAS-A Relationship. Association is a relationship where all objects have their own lifecycle and there is no owner.

Timing for Interfaces and Abstract Classes

Enumerated Types. CSE 114, Computer Science 1 Stony Brook University

תוכנה 1 מערכים. Array Creation and Initialization. Array Declaration. Array Creation and Initialization. Loop through Arrays

תוכנה 1 3 תרגול מס' מערכים ומבני בקרה

תוכנה 1 בשפת Java נושאים שונים בהורשה רובי בוים ומתי שמרת בית הספר למדעי המחשב אוניברסיטת תל אביב

לתיכנות עם MATLAB Lecture 5: Boolean logic and Boolean expressions

תוכנה 1 מערכים. Array Creation and Initialization. Array Declaration. Loop through Arrays. Array Creation and Initialization

Wrapper Classes double pi = new Double(3.14); 3 double pi = new Double("3.14"); 4... Zheng-Liang Lu Java Programming 290 / 321

Algorithms. Intro2CS week 5

CS 113 PRACTICE FINAL

מבוא למדעי המחשב הרצאה 18: פולימורפיזם ומחלקות אבסטרקטיות

תוכנה 1 תרגול מספר 10: תרגיל חברת הייטק בית הספר למדעי המחשב אוניברסיטת תל אביב

מחרוזות ב Java ותכנות מונחה בדיקות )Test Driven Development(

Exercise. Zheng-Liang Lu Java Programming 273 / 324

תוכנה 1 תרגול מספר 10: תרגיל חזרה חברת הייטק בית הספר למדעי המחשב אוניברסיטת תל אביב

לתיכנות עם MATLAB Lecture 5: Boolean logic and Boolean expressions

CSCI 201L Written Exam #1 Fall % of course grade

ב ה צ ל ח ה! אוניברסיטת בן גוריון בנגב מספר נבחן : תאריך המבחן: כ"ה תשרי תשע"ח 15/10/17 שמות המורים: ציון סיקסיק מיועד לתלמידי : א'

Lecture 13 Summary. Assignment 3 Discussion. Polymorphism 3/4/2009. Assignment 3 Polymorphism Interfaces

מבוא לתכנות ב- JAVA תרגול 7

קורס גרפיקה ממוחשבת 2008 סמסטר ב' ליאור שפירא 1 חלק מהשקפים מעובדים משקפים של פרדו דוראנד, טומס פנקהאוסר ודניאל כהן-אור

תוכנה 1 מבני נתונים גנריים

מבוא לתכנות ב- JAVA מעבדה 2

- מבחן. - 4 עבודות ב- JAVA. הגשה בזוגות דרך ה- System Submission

שיעור מספר 8: "אמא יש רק אחת" )הורשה I(

תוכנה 1 מבני נתונים גנריים

מצליחה. 1. int fork-bomb() 2. { 3. fork(); 4. fork() && fork() fork(); 5. fork(); printf("bla\n"); 8. return 0; 9. }

מבוא למדעי המחשב תירגול 2: מבוא למדעי המחשב מ' - תירגול 2

תרגול 4 פונקציות. מבנה של פונקציה: public static <return value type> <function name> (<arg1 type> <arg1>, <arg2 type> <arg2>, ) { <function body> }

ספרית התבניות הסטנדרטית (STL) כתיבת אלגוריתמים גנריים מצביעים חכמים. .vector. list iterator נכיר תחילה את האוסף הפשוט ביותר בספריה

שים לב! אין לכתוב בשוליים. השוליים יחתכו לצורך סריקת המבחן.

תרגול 12. Standard Template Library כתיבת אלגוריתמים גנריים מצביעים חכמים

ECE Fall 2014, Final Exam

Programming for Engineers in Python

Programming for Engineers in Python

Agenda: Discussion Week 7. May 11, 2009

Class definition. complete definition. public public class abstract no instance can be created final class cannot be extended

COE318 Lecture Notes Week 10 (Nov 7, 2011)

מבוא למדעי המחשב תרגול 13: עצים בינאריים

במידה ולסעיף ניתנה תשובה ובנוסף נרשם לגבי הסעיף לא יודע/ת אזי הניקוד שיינתן

ECE Fall 20l2, Second Exam

JAVA Programming Language Homework I - OO concept

Fall CS 101: Test 2 Name UVA ID. Grading. Page 1 / 4. Page3 / 20. Page 4 / 13. Page 5 / 10. Page 6 / 26. Page 7 / 17.

<exp> ::= <define> <cexp> <define> ::= ( define <var-decl> <cexp> ) / DefExp(var:VarDecl, val:cexp)

public class B { private int f = 0; public static void main(string[] args) { B b1 = new B(); B b2 = new B(); Object b3 = b1;

CS 251 Intermediate Programming More on classes

CISC-124. Dog.java looks like this. I have added some explanatory comments in the code, and more explanation after the code listing.

תוכנה 1 טיפוסי השפה טיפוסים לא פרימיטיביים הטיפוסים הפרימיטיביים מחרוזות המרה למספרים תרגול 2: טיפוסי שפה, מחרוזות, מערכים ושגיאות

9. טופס הזמנת מוצרים טופס ההזמנה הוא טופס מורכב. מעורבים בו 4 טבלאות נתונים. קשרי הגומלין בין הטבלאות : הטופס :

C# part II Object-oriented Programming Object-oriented Programming 1

שאלה 1, סעיף ב )11 נק'(

Exercise: Singleton 1

CS/B.TECH/CSE(New)/SEM-5/CS-504D/ OBJECT ORIENTED PROGRAMMING. Time Allotted : 3 Hours Full Marks : 70 GROUP A. (Multiple Choice Type Question)

Amortized Analysis, Union-Find,

מבוא לתכנות ב- JAVA מעבדה 3. Ipc161-lab3

Nested Classes in Java. Slides by: Alon Mishne Edited by: Eran Gilad, Eyal Moscovici April 2013

Class, Variable, Constructor, Object, Method Questions

OOPs Concepts. 1. Data Hiding 2. Encapsulation 3. Abstraction 4. Is-A Relationship 5. Method Signature 6. Polymorphism 7. Constructors 8.

תרגול 3 מערכים ופונקציות

enum Types 1 1 The keyword enum is a shorthand for enumeration. Zheng-Liang Lu Java Programming 267 / 287

מבוא למדעי המחשב תירגול 3:

H212 Introduction to Software Systems Honors

Software 1. תרגול 9 Java I/O

Last name:... First name:... Department (if not D-INFK):...

תכנות מתקדם בשפת C משתנים

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

תרגול מספר 3: מערכים

Java Exceptions Java, winter semester

HAS-A Relationship. Association is a relationship where all objects have their own lifecycle and there is no owner.

תוכנה 1 מבני נתונים גנריים

Introduction to Programming Using Java (98-388)

Transcription:

תוכנה 1 סיכום תרגול 13 1

בחינה באופק! הבחינה תכלול את כל הנושאים שכיסינו במהלך הסמסטר: כל ההרצאות כל תרגולים כל תרגילי בית חומר סגור שאלות אמריקאיות 2

קצת על מנשקים מנשק יכול להרחיב יותר ממנשק אחד שירותים במנשק הם תמיד מופשטים וציבוריים public interface MyInterface { public abstract int foo1(int i); int foo2(int i); The modifiers of foo1 and foo2 are the same. 3

מנשקים public interface Foo { public void bar() throws Exception; public class FooImpl implements Foo { public void bar() { System.out.println("No exception is thrown"); public static void main(string args[]) { Foo foo = new FooImpl(); foo.bar(); האם הקוד מתקמפל? אם לא, למה? אם כן, האם יש שגיאת ריצה? אם יש, למה? אחרת, מה הפלט? שגיאת קומפילציה: "Unhandled exception type Exception" 4

מנשקים - המשך public interface Foo { public void bar() throws Exception; public class FooImpl implements Foo { public void bar() { System.out.println("No exception is thrown"); public static void main(string args[]) { FooImpl foo = new FooImpl(); foo.bar(); האם הקוד מתקמפל? אם לא, למה? אם כן, האם יש שגיאת ריצה? אם יש, למה? אחרת, מה הפלט? פלט: "No exception is thrown" 5

מנשקים וירושה Consider the following class hierarchy: Interface Animal { class Dog implements Animal{ class Poodle extends Dog { class Labrador extends Dog { Animal Dog Poodle Labrador Which of the following lines (if any) will not compile? Poodle poodle = new Poodle(); Animal animal = (Animal) poodle; Dog dog = new Labrador(); animal = dog; poodle = dog; poodle = (Poodle) dog; -No compilation error -Runtime Exception - Compilation Error Type mismatch: cannot convert from Dog to Poodle Labrador labrador = (Labrador) dog; -No compilation error -No Runtime Exception 6

מנשקים וירושה class A { public void print() { System.out.println("A"); האם יש שגיאה? class B extends A implements C { interface C { void print(); אין שגיאות קומפילציה public כברירת מחדל 7

מנשקים וירושה class A { void print() { System.out.println("A"); האם יש שגיאה? class B extends A implements C { interface C { void print(); שגיאת קומפילציה: The inherited package method A.print() cannot hide the public abstract method in C 8

The following table shows the access to members permitted by each modifier 9

דריסה של שירותים public class A { public void print() { System.out.println("A"); public class C { public static void main(string[] args){ B b = new B(); A a = b; public class B extends A { public void print() { System.out.println("B"); b.print(); a.print(); אין צורך ב- casting B B האם הקוד מתקמפל? אם לא, למה? אם כן, האם יש שגיאת ריצה? אם יש, למה? הפלט: 10 אחרת, מה הפלט?

דריסה של שירותים וניראות public class A { public void print() { System.out.println("A"); public class B extends A { protected void print() { System.out.println("B"); public class C { public static void main(string[] args) { B b = new B(); b.print(); שגיאת קומפילציה: "Cannot reduce the visibility of the inherited method from A" האם הקוד מתקמפל? אם לא, למה? אם כן, האם יש שגיאת ריצה? אם יש, למה? 11 אחרת, מה הפלט?

דריסה של שירותים )2( וניראות public class A { protected void print() { System.out.println("A"); public class B extends A { public void print() { System.out.println("B"); public class C { public static void main(string[] args) { B b = new B(); b.print(); B האם הקוד מתקמפל? אם לא, למה? אם כן, האם יש שגיאת ריצה? אם יש, אחרת, מה הפלט? למה? הפלט: 12

הורשה public class A { public void foo() { System.out.println("A.foo()"); public class B extends A { public void foo() { System.out.println("B.foo()"); public void bar() { System.out.println("A.bar()"); foo(); public static void main(string[] args) { A a = new B(); a.bar(); הפלט: A.bar() B.foo() האם הקוד מתקמפל? אם לא, למה? אם כן, האם יש שגיאת ריצה? אם יש, אחרת, מה הפלט? למה? 13

הורשה )2( public class A { private void foo() { System.out.println("A.foo()"); public class B extends A { public void foo() { System.out.println("B.foo()"); public void bar() { System.out.println("A.bar()"); foo(); public static void main(string[] args) { A a = new B(); a.bar(); הפלט: A.bar() A.foo() האם הקוד מתקמפל? אם לא, למה? אם כן, האם יש שגיאת ריצה? אם יש, אחרת, מה הפלט? למה? 14

הורשה ובנאים public class A { String bar = "A.bar"; A() { foo(); public void foo() { System.out.println("A.foo(): bar = " + bar); public class C { public static void main(string[] args) { A a = new B(); String s = "a.bar = " + a.bar System.out.println(s); a.foo(); public class B extends A { String bar = "B.bar"; B() { foo(); public void foo() { System.out.println("B.foo(): bar = " + bar); : הפלט? output What is the B.foo(): bar = null B.foo(): bar = B.bar a.bar = A.bar B.foo(): bar = B.bar 15

הורשה ובנאים )2( public class A { protected B b = new B(); public A() { System.out.println("in A: no args."); public A(String s) { System.out.println("in A: s = " + s); public class B { public B() { System.out.println("in B: no args."); public class C extends A { protected B b; public C() { System.out.println("in C: no args."); public C(String s) { System.out.println("in C: s = " + s); : הפלט? output What is the in B: no args. in A: no args. in C: no args. in B: no args. in A: no args. in C: no args. public class D { public static void main(string args[]) { C c = new C(); A a = new C(); 16

הורשה ובנאים )3( public class A { protected B b = new B(); public A() { System.out.println("in A: no args."); public A(String s) { System.out.println("in A: s = " + s); public class B { public B() { System.out.println("in B: no args."); public class C extends A { protected B b; public C() { System.out.println("in C: no args."); public C(String s) { System.out.println("in C: s = " + s); : הפלט? output What is the in B: no args. in A: no args. in C: s = c in B: no args. in A: no args. in C: s = a public class D { public static void main(string args[]) { C c = new C("c"); A a = new C( a"); 17

דריסה והעמסה של שירותים public class A { public float foo(float a, float b) throws IOException { public class B extends A { אילו מהשירותים הבאים ניתן להגדיר ב- B? 1. float foo(float a, float b){ 2. public int foo(int a, int b) throws Exception{ 3. public float foo(float a, float b) throws Exception{ 4. public float foo(float p, float q) { 18

הורשה ודריסת שירותים public class A { public void foo() { public class B extends A { public void foo() { האם אפשר לקרוא ל- foo של?B מתוך A תשובה: דרך super.foo() 19

הורשה ודריסת שירותים )2( public class A { public void foo() { public class B extends A { public void foo() { האם אפשר לקרוא ל- foo של?C מתוך A תשובה: אי אפשר, super.super.foo() - לא חוקי public class C extends B { public void foo() { 20

מחלקות פנימיות public class Test { public int a = 0; private int b = 1; אילו משתנים מ- a-e המסומנת? נגישים מהשורה public void foo(final int c) { int d = 2; class InnerTest { private void bar(int e) { תשובה: כולם חוץ מ- d 21

השימוש במילה השמורה final מקום השימוש שדה\משתנה מתודה מחלקה משמעות יש לאתחל פעם אחת בלבד שדה שהוא final לא מקבל ערך דיפולטי איפה מאתחלים? לא ניתן ניתן לדרוס מתודה זו בירושה. לא ניתן לרשת ממחלקה זו. 22

מחלקות פנימיות - סיכום Type Scope Inner Define an interface inside Fields access Static nested member no yes Only static Inner nonstatic member yes no Static and non-static local Local scope yes no final local variables or parameters that are accessible in the scope of the block anonymous Only the point where it is defined yes no final local variables or parameters that are accessible in the scope of the block 23

enum All enums implicitly extend java.lang.enum An enum cannot extend anything else. The constructor for an enum type must be package-private or private access. You cannot invoke an enum constructor yourself. fixed set of constants 24

enum Output: Mondays are bad. SUNDAY MONDAY TUESDAY WEDNESDAY THURSDAY FRIDAY SATURDAY static values method that returns an array containing all of the values of the enum in the order they are declared 25

אוספים גנריים public static void func(hashset<string> set){ for (String s : set){ System.out.println(s); public static void main(string[] args){ HashSet<String> myset = new HashSet<String>(); myset.add("abc"); myset.add("dce"); func(myset); : לכתוב גם new HashSet<>(); ניתן )ואפילו רצוי( 26

אוספים גנריים public static void func(hashset<string> set){ for (String s : set){ System.out.println(s); public static void main(string[] args){ HashSet<String> myset = new HashSet<String>(); myset.add("abc"); myset.add("dce"); func(myset); האם אנחנו חייבים להצהיר על טיפוס סטטי שהוא?HashSet בד"כ נשתמש בטיפוס הכללי יותר Set אלא אם כן אנחנו נדרשים ספציפית ל.HashSet למשל במקרים הבאים: 1. אנחנו רוצים להפעיל מתודה שיש ל HashSet אך לא ל Set )יש כזו בכלל?( 2. אנחנו משתמשים בשירות שדורש לקבל רק HashSet ולא.Set 27

אוספים גנריים public static void func(hashset<string> set){ for (String s : set){ System.out.println(s); public static void main(string[] args){ HashSet<String> myset = new HashSet<String>(); myset.add("abc"); myset.add("dce"); func(myset); מדוע הפונקציה דורשת לקבל?HashSet בד"כ נשתמש בטיפוס כמה שיותר כללי. האם נוכל לשלוח לפה כל?Set ע"מ המימוש שלה, אין סיבה שלא. למען האמת, נוכל לשלוח אפילו.Collection 28

אוספים גנריים public static void func(collection<string> set){ for (String s : set){ System.out.println(s); public static void main(string[] args){ Set<String> myset = new HashSet<>(); myset.add("abc"); myset.add("dce"); func(myset); האם יש עוד משהו שנוכל לשפר בקוד? נשים לב כי המימוש של func לא מחייב אותנו לקבל אוסף של מחרוזות. הדרישה היחידה היא שאברי האוסף יממשו את.Java מה שמובטח לכל אובייקט ב,toString 29

אוספים גנריים public static void func(collection<?> set){ for (Object s : set){ System.out.println(s); public static void main(string[] args){ Set<String> myset = new HashSet<>(); myset.add("abc"); myset.add("dce"); func(myset); נשתמש ב >?< ע"מ לאפשר שימוש באוספים של כל טיפוס אפשרי. שימו לב, להגדיר את set מטיפוס Collection<Object> לא ישיג את אותה המטרה, כיוון שאז נוכל להפעיל את הפונקציה הזו רק עם אובייקט מטיפוס סטטי Collection<Object> 30

אוספים גנריים public static void func(collection<? extends Rectangle> set){ for (Rectangle r : set){ System.out.println(r.getArea()); public static void main(string[] args){ Set<Rectangle> myset = new HashSet<>(); myset.add(new Rectangle(5,6)); myset.add(new Rectangle(1,2)); func(myset); 31