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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Objectives. Describe ways to create constants const readonly enum

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

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

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

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

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

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

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

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

Algorithms. Intro2CS week 5

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

Timing for Interfaces and Abstract Classes

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

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

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

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

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

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

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

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

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

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

Exercise. Zheng-Liang Lu Java Programming 273 / 324

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

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

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

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

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

CS 113 PRACTICE FINAL

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

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

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

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

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

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

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

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

COE318 Lecture Notes Week 10 (Nov 7, 2011)

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

CS 251 Intermediate Programming More on classes

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

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

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

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

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

Exercise: Singleton 1

היצביט ומ - ןוכית ת וי נבת

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

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

ECE Fall 2014, Final Exam

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

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

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

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

H212 Introduction to Software Systems Honors

Advanced Programming & C++ Language

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

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

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

ECE Fall 20l2, Second Exam

Java Exceptions Java, winter semester

Software 1. The java.io package. Streams. Streams. Streams. InputStreams

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

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

Practical Session - Heap

Introduction to Programming Using Java (98-388)

Agenda: Discussion Week 7. May 11, 2009

Another IS-A Relationship

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

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

What will this print?

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

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

public class Foo { private int var; public int Method1() { // var accessible anywhere here } public int MethodN() {

JAVA Programming Language Homework I - OO concept

public class Test { static int age; public static void main (String args []) { age = age + 1; System.out.println("The age is " + age); }

Cloning Enums. Cloning and Enums BIU OOP

מבוא למדעי המחשב תרגול 12 מחסנית )Stack( memoization

CMSC 132: Object-Oriented Programming II

קורס תכנות שיעור שישי: מחרוזות, מצביעים

מדעי המחשב 2 יחידות לימוד פתרון בחינת הבגרות פרק א. I x > a. פתרון 2: משפט switch

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

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

Transcription:

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

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

מנשקים 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" 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[]) { FooImpl foo = new FooImpl(); foo.bar(); האם הקוד מתקמפל? אם לא, למה? אם כן, האם יש שגיאת ריצה? אם יש, למה? אחרת, מה הפלט? פלט: "No exception is thrown" 4

מנשקים וירושה 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 5

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

מנשקים וירושה 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 7

דריסה של שירותים 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 האם הקוד מתקמפל? אם לא, למה? אם כן, האם יש שגיאת ריצה? אם יש, למה? הפלט: 8 אחרת, מה הפלט?

דריסה של שירותים וניראות 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" האם הקוד מתקמפל? אם לא, למה? אם כן, האם יש שגיאת ריצה? אם יש, למה? 9 אחרת, מה הפלט?

דריסה של שירותים )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 האם הקוד מתקמפל? אם לא, למה? אם כן, האם יש שגיאת ריצה? אם יש, אחרת, מה הפלט? למה? הפלט: 10

הורשה 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() האם הקוד מתקמפל? אם לא, למה? אם כן, האם יש שגיאת ריצה? אם יש, אחרת, מה הפלט? למה? 11

הורשה )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() האם הקוד מתקמפל? אם לא, למה? אם כן, האם יש שגיאת ריצה? אם יש, אחרת, מה הפלט? למה? 12

הורשה ובנאים public class A { String bar = "A.bar"; A() { foo(); public void foo() { System.out.println("A.foo(): bar = " + bar); public class B extends A { String bar = "B.bar"; public class C { public static void main(string[] args) { A a = new B(); System.out.println("a.bar = " + a.bar); a.foo(); 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 13

הורשה ובנאים )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(); 14

הורשה ובנאים )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"); 15

דריסה והעמסה של שירותים 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) { 16

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

הורשה ודריסת שירותים )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() { 18

מחלקות פנימיות 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 19

מחלקות פנימיות - סיכום Type Scope Inner Interface 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 20

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 21

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 22

אוספים גנריים new HashSet<>(); ניתן לכתוב גם : 23

אוספים גנריים 24 האם אנחנו חייבים להצהיר על טיפוס סטטי שהוא?HashSet בד"כ נשתמש בטיפוס הכללי יותר Set אלא אם כן אנחנו נדרשים ספציפית ל.HashSet למשל במקרים הבאים: 1. אנחנו רוצים להפעיל מתודה שיש ל HashSet אך לא ל Set )יש כזו בכלל?( 2. אנחנו משתמשים בשירות שדורש לקבל רק HashSet ולא.Set

אוספים גנריים מדוע הפונקציה דורשת לקבל?HashSet בד"כ נשתמש בטיפוס כמה שיותר כללי. האם נוכל לשלוח לפה כל?Set ע"מ המימוש שלה, אין סיבה שלא. למען האמת, נוכל לשלוח אפילו.Collection 25

אוספים גנריים האם יש עוד משהו שנוכל לשפר בקוד? נשים לב כי המימוש של func לא מחייב אותנו לקבל אוסף של מחרוזות. הדרישה היחידה היא שאברי האוסף יממשו את.Java מה שמובטח לכל אובייקט ב,toString 26

אוספים גנריים נשתמש ב >?< ע"מ לאפשר שימוש באוספים של כל טיפוס אפשרי. שימו לב, להגדיר את set מטיפוס Collection<Object> לא ישיג את אותה המטרה, כיוון שאז נוכל להפעיל את הפונקציה הזו רק עם אובייקט מטיפוס סטטי Collection<Object> 27

אוספים גנריים נשתמש ב >?< ע"מ לאפשר שימוש באוספים של כל טיפוס אפשרי. שימו לב, להגדיר את set מטיפוס Collection<Object> לא ישיג את אותה המטרה, כיוון שאז נוכל להפעיל את הפונקציה הזו רק עם אובייקט מטיפוס סטטי Collection<Object> 28

אוספים גנריים 29

בחינה באופק!... הבחינה תכלול את כל הנושאים שכיסינו במהלך הסמסטר )שיעורים, תרגולים ועבודות בית(,IO,iterator ירושה ופולימורפיזם,,DBC,Java,Collection Framework מחלקות פנימיות,,Generics חומר סגור לפתור כמה שיותר מבחנים משנים שעברו לא כל הסמסטרים זהים מבחינת חומר לתרגל כתיבת קוד על נייר 30