Java and C# in Depth

Similar documents
Java Fundamentals (II)

Java and C# in Depth Carlo A. Furia, Marco Piccioni, Bertrand Meyer

Java: introduction to object-oriented features

Exercise Session Week 7

Class, Variable, Constructor, Object, Method Questions

Exercise Session Week 8

Exercise Session Week 8

Implements vs. Extends When Defining a Class

Principles of Software Construction: Testing: One, Two, Three

Chair of Software Engineering. Languages in Depth Series: Java Programming. Prof. Dr. Bertrand Meyer. Exercise Session 3

Chair of Software Engineering. Java and C# in depth. Carlo A. Furia, Marco Piccioni, Bertrand Meyer. Java: reflection

Exercise Session Week 6

Mid-Term 2 Grades

CMSC 132: Object-Oriented Programming II

Computer Science II (20073) Week 1: Review and Inheritance

Java: advanced object-oriented features

Review Chapter 6 in Bravaco. Short Answers 1. This type of method does not return a value. a. null b. void c. empty d. anonymous

Tarkvaraprojekt seminar XI 27.aprill Ivo Mägi Java Puzzlers

Today. Book-keeping. Inheritance. Subscribe to sipb-iap-java-students. Slides and code at Interfaces.

Practice for Chapter 11

CS260 Intro to Java & Android 03.Java Language Basics

Announcement. Agenda 7/31/2008. Polymorphism, Dynamic Binding and Interface. The class will continue on Tuesday, 12 th August

Computer Science II (20082) Week 1: Review and Inheritance

Name Return type Argument list. Then the new method is said to override the old one. So, what is the objective of subclass?

COMP200 - Object Oriented Programming: Test One Duration - 60 minutes

Contents. I. Classes, Superclasses, and Subclasses. Topic 04 - Inheritance

CSCI-142 Exam 1 Review September 25, 2016 Presented by the RIT Computer Science Community

Java Magistère BFA

Programming overview

1 Shyam sir JAVA Notes

C#: framework overview and in-the-small features

Object Oriented Programming: In this course we began an introduction to programming from an object-oriented approach.

Zhifu Pei CSCI5448 Spring 2011 Prof. Kenneth M. Anderson

Object Oriented Programming. Java-Lecture 11 Polymorphism

Chair of Software Engineering Java and C# in Depth

Object-Oriented Concepts

Introduction to Collections

Chair of Software Engineering. Languages in Depth Series: Java Programming. Prof. Dr. Bertrand Meyer. Exercise Session 10

Rules and syntax for inheritance. The boring stuff

Object Oriented Programming in C#

Chair of Software Engineering. Java and C# in Depth. Prof. Dr. Bertrand Meyer. Exercise Session 7. Nadia Polikarpova

Declarations and Access Control SCJP tips

Marcin Luckner Warsaw University of Technology Faculty of Mathematics and Information Science

JAVA MOCK TEST JAVA MOCK TEST II

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)

Chair of Software Engineering. Java and C# in depth. Carlo A. Furia, Marco Piccioni, Bertrand Meyer. C#: reflection

Binghamton University. CS-140 Fall Problem Solving. Creating a class from scratch

Chair of Software Engineering. Languages in Depth Series: Java Programming. Prof. Dr. Bertrand Meyer. Exercise Session 9.

Principles of Software Construction: Concurrency, Part 2

Lecture 1: Object Oriented Programming. Muhammad Hafeez Javed

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

C#: advanced object-oriented features

Agenda. Objects and classes Encapsulation and information hiding Documentation Packages

C#: advanced object-oriented features

Vendor: Oracle. Exam Code: 1Z Exam Name: Java Certified Programmer. Version: Demo

Lecture Notes CPSC 224 (Spring 2012) Today... Java basics. S. Bowers 1 of 8

Chapter Two Bonus Lesson: JavaDoc

QUIZ 2 Introduction to Computer Science (COMP 250) Mon. March 2, 2009 Professor Michael Langer

EMBEDDED SYSTEMS PROGRAMMING More About Languages

Chair of Software Engineering. Java and C# in depth. Carlo A. Furia, Marco Piccioni, Bertrand Meyer. Java: concurrency

Inheritance and Interfaces

AP CS Unit 6: Inheritance Notes

The Notion of a Class and Some Other Key Ideas (contd.) Questions:

CS-140 Fall 2017 Test 2 Version A Nov. 29, 2017

Programming Language Concepts: Lecture 2

Questions Answer Key Questions Answer Key Questions Answer Key

Chair of Software Engineering. Java and C# in Depth. Prof. Dr. Bertrand Meyer. Exercise Session 8. Nadia Polikarpova

Chapter 4 Defining Classes I

Full file at Chapter 2 - Inheritance and Exception Handling

Abstract Classes. Abstract Classes a and Interfaces. Class Shape Hierarchy. Problem AND Requirements. Abstract Classes.

Inheritance and Polymorphism

Operators and Expressions

Solutions to Sample JAC444 Midterm Test

Administrivia. Java Review. Objects and Variables. Demo. Example. Example: Assignments

CS 231 Data Structures and Algorithms, Fall 2016

Chapter 1 Getting Started

The class Object. Lecture CS1122 Summer 2008

Lecture 2: Java & Javadoc

COMPUTER SCIENCE DEPARTMENT PICNIC. Operations. Push the power button and hold. Once the light begins blinking, enter the room code

CS/ENGRD 2110 SPRING Lecture 2: Objects and classes in Java

Questions Answer Key Questions Answer Key Questions Answer Key

Inheritance. Inheritance allows the following two changes in derived class: 1. add new members; 2. override existing (in base class) methods.

Inheritance (Part 2) Notes Chapter 6

First IS-A Relationship: Inheritance

Java Class Design. Eugeny Berkunsky, Computer Science dept., National University of Shipbuilding

I pledge by honor that I will not discuss this exam with anyone until my instructor reviews the exam in the class.

CSCE3193: Programming Paradigms

Subclass Gist Example: Chess Super Keyword Shadowing Overriding Why? L10 - Polymorphism and Abstract Classes The Four Principles of Object Oriented

1.00 Introduction to Computers and Engineering Problem Solving. Quiz 1 March 7, 2003

Inheritance (Part 5) Odds and ends

Lecture 10. Overriding & Casting About

AP CS Unit 8: Inheritance Exercises

CMSC 132: Object-Oriented Programming II. Inheritance

*Java has included a feature that simplifies the creation of

CO Java SE 8: Fundamentals

Today s Agenda. Quick Review

JAVA BASICS II. Example: FIFO

AP CS Unit 6: Inheritance Exercises

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

Chapter 11 Inheritance and Polymorphism. Motivations. Suppose you will define classes to model circles,

Transcription:

Chair of Software Engineering Java and C# in Depth Carlo A. Furia, Marco Piccioni, Bertrand Meyer Exercise Session Week 4

Chair of Software Engineering Don t forget to form project groups by tomorrow (March 11 th, 2014)

Chair of Software Engineering Please do not submit assignments that do not compile

Agenda Quizzes More quizzes And even more quizzes 4

Quiz 1. Diffences between Struct and Class (C#) Structs define value types, while classes define reference types. A struct cannot inherit from another struct or from classes. A struct can only be used as the base for a struct, but not for a class. o A struct cannot be used as the base. o A struct can implement interfaces. A default constructor will be provided for a struct, only if it does not have any user defined constructors. o A struct always has a default constructor, which clears the memory to zeroes. o Thus, although a struct may declare constructors, those constructors must take at least one argument. The struct members cannot have initializers. T T F F T 5

Quiz 2. Abstract Classes (Java Vs. C#) Can an abstract class have no abstract methods? o (Java) Yes. o (C#) Yes. Can an abstract class have more than one superclass? o (Java) No, single inheritance only. o (C#) No, single inheritance only. Can an abstract class be a subclass of a concrete class? o (Java) Yes, e.g. class Object. o (C#) Yes, e.g. class Object. 6

Quiz 3. Code Organization (Java Vs. C#) How many package or namespace declarations may be contained in one source file? o (Java) One at most. o (C#) No restriction. How is a package/namespace name related with the physical storage structure of code? o (Java) Package names correspond to the directory names. o (C#) No relation. How many classes can be contained in one source file? o (Java) At most one public class, but no restrictions otherwise. o (C#) No restriction. 7

Quiz 4. What does the program do? Static method public class Null { public static void greet() { System.out.println("Hello world!"); public static void main(string[] args) { ((Null) null).greet(); Hello world! A qualifying expression for a static method invocation is evaluated, but its value is ignored. class Null{ static void greet() { Console.WriteLine("Hello world!"); static void Main(string[] args){ ((Null) null).greet(); Compilation error! Member 'Null.greet()' cannot be accessed with an instance reference; qualify it with a type name instead 8

Quiz 5: Overloading Is it ok to have the following method declarations in a class A? Why? void print(int i){... // 1 void print(float f){... // 2 int print(float f){... // 3 1 and 2: Fine. 1 and 3: Fine. 2 and 3: Error. If class A has the following two declarations, void print(int i){... // 1 void print(float f){... // 2 and in class B, a subclass of A, we define two methods as follows, will it be ok? Why? void print(long i){... // 3 void print(int f){... // 4 1, 2, and 3: Fine (overloading) 1, 2, and 4: Java: also fine (overriding); C#: warning (declare as new) 9

Quiz 6. What does the program do? Method overloading public class Base{ public virtual void M1(double val){ Console.WriteLine("Base.M1(double)"); public class Derived : Base{ public virtual void M1(int val){ Console.WriteLine("Derived.M1(int)"); class Test{ static void Main(string[] args){ Derived d = new Derived(); Base b = d; b.m1(3); d.m1(3); Base.M1(double) Derived.M1(int) class Base { public void M1(double val) { System.out.println("Base.M1(double)"); class Derived extends Base { public void M1(int val) { System.out.println("Derived.M1(int)"); public class Test { public static void main(string[] args) { Derived d = new Derived(); Base b = d; b.m1(3); d.m1(3); Base.M1(double) Derived.M1(int) 10

Quiz 7. What does the program do? Method overriding (Java) import java.util.*; public class Name { private final String first, last; public Name(String first, String last) { this.first = first; this.last = last; public boolean equals(object o) { if (!(o instanceof Name)) return false; Name n = (Name) o; return n.first.equals(first) && n.last.equals(last); public static void main(string[] args) { Set<Name> s = new HashSet<Name>(); s.add(new Name("Mickey", "Mouse")); System.out.println( s.contains(new Name("Mickey", "Mouse"))); false 11

Anonymous function expressions: C# (1) Anonymous method expressions delegate void Printer(string s); class TestClass{ static void DoWork(string k){ System.Console.WriteLine(k); static void Main(){ Printer p = TestClass.DoWork; // p = new Printer(TestClass.DoWork); p("delegate with named method."); p = delegate (string j){ System.Console.WriteLine(j); ; p("delegate with anonymous method."); 12

Anonymous function expressions: C# (2) Lambda expressions Statement lambda Arguments => {Statements (int i) => { bool iseven = (i%2 == 0); return iseven; Expression lambda Arguments => Expression (int i) => (i % 2) == 0 o Could also be used to construct expression tree objects Expression<Func<int, int>> exp = (n) => (n * 2 + 1) * 4; Arguments could be implicitly typed Parentheses are optional for single argument but not in the case of no argument Func<int,int> Double = (n) => n*2; i => (i % 2) == 0 () => {Console.Write ( ); 13

Variables in anonymous functions: C# An anonymous function can access the local variables and (some of) the parameters of the enclosing method (called outer variables) Value parameters, and parameter array o In an instance function member of a class, the this value is considered a value parameter Not ref or out parameters of the enclosing method Defining local variables Can declare local variables with the same name as outer class member variables. Cannot have a local variable with the same name as a local variable in the enclosing method. 14

Quiz 8. What will be printed? Anonymous method expressions delegate void D(); static D[] F() { D[] result = new D[3]; int i; for (i = 0; i < 3; i++) { result[i] = () => { Console.WriteLine(i); ; return result; static void Main() { foreach (D d in F()) d(); 3 3 3 delegate void D(); static D[] F() { D[] result = new D[3]; int i; for (i = 0; i < 3; i++) { int j = i; result[i] = () => { Console.WriteLine(j); ; return result; static void Main() { foreach (D d in F()) d(); 0 1 2 15

Questions? 16