Eyvind W. Axelsen eyvinda@ifi.uio.no INF3110: Exercises Part 7 Types and Object Orientation II Comments and solutions Eyvind W. Axelsen eyvinda@ifi.uio.no.

Slides:



Advertisements
Similar presentations
Object-Oriented Programming. 2 An object, similar to a real-world object, is an entity with certain properties, and with the ability to react in certain.
Advertisements

Object Oriented Programming
Lecture 10: Part 1: OO Issues CS 540 George Mason University.
Portability and Safety Mahdi Milani Fard Dec, 2006 Java.
Elementary Data Structures CS 110: Data Structures and Algorithms First Semester,
Object Oriented Programming Chapter 7 Programming Languages by Ravi Sethi.
CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 9 GEORGE KOUTSOGIANNAKIS Copyright: 2015 Illinois Institute of Technology/George Koutsogiannakis 1.
Road Map Introduction to object oriented programming. Classes
1 Lecture 24 Abstract Data Types (ADT) –I Overview  What is an Abstract Data type?  What is Stack ADT?  Stack ADT Specifications  Array Implementation.
Data Abstraction and Object- Oriented Programming CS351 – Programming Paradigms.
Cmp Sci 187: Midterm Review Based on Lecture Notes.
30-Jun-15 Stacks. What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on the “top” of the stack Anything.
1 Further OO Concepts II – Java Program at run-time Overview l Steps in Executing a Java Program. l Loading l Linking l Initialization l Creation of Objects.
Interfaces and Inner Classes. What is an Interface?  What is “presented to the user”?  The public part of a class?  What is the substance of an interface?
Subclasses and Subtypes CMPS Subclasses and Subtypes A class is a subclass if it has been built using inheritance. ▫ It says nothing about the meaning.
INF5110: Mandatory Exercise 2 Eyvind W. Axelsen @eyvindwa Slides are partly based on.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
Implementing Stacks Ellen Walker CPSC 201 Data Structures Hiram College.
Stack and Queue.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Generalized Containers CSIS 3701: Advanced Object Oriented Programming.
Programming Languages and Paradigms Object-Oriented Programming (Part II)
The Procedure Abstraction, Part VI: Inheritance in OOLs Comp 412 Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled.
The Procedure Abstraction, Part V: Support for OOLs Comp 412 Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in.
Object Oriented Software Development
Lecture 10 Concepts of Programming Languages Arne Kutzner Hanyang University / Seoul Korea.
Object-Oriented Programming Chapter Chapter
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
Lecture 8: Advanced OOP Part 2. Overview Review of Subtypes Interfaces Packages Sorting.
Access Specifier. Anything declared public can be accessed from anywhere. Anything declared private cannot be seen outside of its class. When a member.
(c) University of Washington06-1 CSC 143 Java Inheritance Tidbits.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Class Relationships Lecture Oo08 Polymorphism. References n Booch, et al, The Unified Modeling Language User Guide, Chapt 10 p.125 n Fowler & Scott, UML.
CS 116 Object Oriented Programming II Lecture 9 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
Inheritance a subclass extends the functionality of a superclass a subclass inherits all the functionality of a superclass don't reinvent the wheel – "stand.
Java Interfaces CS 21a: Introduction to Computing I Department of Information Systems and Computer Science Ateneo de Manila University (see Chapter 9 of.
Class Inheritance Part II: Overriding and Polymorphism Corresponds with Chapter 10.
Modern Programming Tools And Techniques-I
Elementary Data Structures
Review Array Array Elements Accessing array elements
Sixth Lecture ArrayList Abstract Class and Interface
Name Spaces: ALL versus OOL
Object-oriented Programming in Java
7.1 What Is An Object Object-oriented program - Description or simulation of application Object-oriented programming is done by adopting or extending an.
7. Inheritance and Polymorphism
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Interface, Subclass, and Abstract Class Review
Inheritance and Polymorphism
Lecture 2: Data Types, Variables, Operators, and Expressions
INF3110: Exercises Part 6 Object Orientation Comments and solutions
Programming Language Concepts (CIS 635)
INF3110: Exercises Part 3 Comments and Solutions
Interfaces and Inheritance
Stack Data Structure, Reverse Polish Notation, Homework 7
Inheritance "Question: What is the object oriented way of getting rich? Answer: Inheritance.“ “Inheritance is new code that reuses old code. Polymorphism.
Map interface Empty() - return true if the map is empty; else return false Size() - return the number of elements in the map Find(key) - if there is an.
Object Oriented Programming (OOP) LAB # 8
Interface.
Extending Classes.
Java Programming Language
The Procedure Abstraction Part V: Run-time Structures for OOLs
Lecture 26: Advanced List Implementation
OBJECT ORIENTED PROGRAMMING II LECTURE 9 GEORGE KOUTSOGIANNAKIS
Advanced Java Programming
CS18000: Problem Solving and Object-Oriented Programming
Eyvind W. Axelsen INF3110: Solutions to Problem Set 2 Scoping, Runtime Organisation, Parameter Passing Methods Eyvind W. Axelsen.
INF3110: Exercises Part 6 Object Orientation Comments and solutions
Lecture 10 Concepts of Programming Languages
Stacks.
Classes and Objects Object Creation
Presentation transcript:

Eyvind W. Axelsen eyvinda@ifi.uio.no INF3110: Exercises Part 7 Types and Object Orientation II Comments and solutions Eyvind W. Axelsen eyvinda@ifi.uio.no @eyvindwa http://eyvinda.at.ifi.uio.no Slides are based on material from previous years, made by Weiqing Zhang and Henning Berg.

Problem 1

Problem 1

Problem 1 a) Extend the class hierarchy to include a class for product expressions e := … | e * e class prod(e1,e2)=extend expression() with private fields: val left = e1 val right = e2 public methods: atomic?() = false lsub()= left rsub()= right value()=(left.value()*right.value()) end

Problem 1b) e.value() = e.left.value() * e.right.value() = (d.left.value + d.right.value) * 7 = (3 + 5) * 7 = 56

Problem 1 c) class square(e)= extend expression() with private fields: A minimal extension of the expression abstract class uses the lsub() to represent the unary operand:   class expression()= private fields: (* none *) public methods: atomic?() (* returns true if an atomic expression *) unary?() (* returns true if a unary expression *) lsub() (* returns the left subexpression if not atomic, or the operand of a unary expression *) rsub() (* returns the left subexpression if not atomic, but none in case of a unary expression *) value() end class square(e)= extend expression() with private fields: val sub = e public methods: atomic?() = false unary?() = true lsub()= sub rsub()= none value()=(sub.value()*sub.value()) end

Problem 1c, alternative solution Alternatively a separate field sub is introduced to keep this operand:   class expression()= private fields: (* none *) public methods: atomic?() (* returns true if a atomic expression *) unary?() (* returns true if a unary expression *) sub() (* returns the operand of a unary expression *) lsub() (* returns the left subexpression if not atomic, and not unary *) rsub() (* returns the right subexpression if not atomic, value() end class square(e)=extend expression() with private fields: val sub = e public methods: atomic?() = false unary?() = true sub()= sub lsub()= none rsub()= none value()=(sub.value()*sub.value()) end

Problem 2 a) We now have two variables with name ul, and two with named lr What if Java instead would allow the original ul to be redefined to have type ColorPoint in ColoreRect, and correspondingly for lr. What type errors could occur? ColorRect exteands Rect ? Assign a Point to ColorPoint is not allowed in Java, What about assign a ColorPoint to Point? The variables could be assigned values of the superclass Point, even though they were typed with ColorPoint  type error

Problem 2 b) class Point { int x, y; b) How would this be done if Java had virtual classes? A virtual class is an inner class that can be redefined in a subclass, much like a virtual method class Point { int x, y; virtual class ThePointClass <: Point; bool equal(ThePointClass p) { return x == p.x && y == p.y; } class ColorPoint extends Point { Color c; ThePointClass := ColorPoint; bool equal(ThisPointClass p) { return super.equal(p) && c == p.c; } } Syntax made-up!

Problem 2 c) Virtual classes are not part of Java. Would a cast help, like in this redefinition of setUL in ColorRect: class ColorRect extends Rect { void setUL(Point newUL){ this.ul = (ColorPoint)newUL; } No, this would not help much. Casting would make a runtime type check explicit, but would not guarantee type safety. Variables ul and lr would still be typed as Point and not ColorPoint. Have to do

Problem 3 a) class Dequeue { Stack s = new Stack(); Is there an alternative to multiple inheritance if we want to reuse only implementation, and do not care about subtyping? Use the Stack and Queue, and try to implement Dequeue. class Dequeue { Stack s = new Stack(); Queue q = new Queue(); Object[] r; Dequeue() { s.r = r; q.r = r; } void insert_front(Object o) { s.push(o); } void insert_rear(Object o) { q.insert(o); } void delete_front() { s.pop(); } void delete_rear() { q.delete(); } } Shared between Queue and Stack dequeue/enqueue是对应于队列的,队列是先入先出的线性表。 Queue and Stack will have more methods as they need.

Problem 3 a2) class Stack { Dequeue d = new Dequeue(); Define Stack and Queue by means of an implementation by a Dequeue object d, and use this in the implementation of methods. class Stack { Dequeue d = new Dequeue(); void push(Object o){ d.insert_front(o); } void pop(){ d.delete_front(); } } class Queue { void insert_rear(){ d.insert_rear(o); } void delete_front(){ d.delete_front(); } With a queue, you insert items at one and and extract them from the other end -- i.e. you can ONLY get things out in the order in which they were inserted. With a dequeue, you can insert and/or remove from either end, so you get a combination of stack behavior (remove the most recently inserted item) or queue behavior (as above).

Problem 3 b) What if we want to have a subtyping relationship, so that objects typed as Stack and Queue can hold objects of type Dequeue? interface Stack { void push(Object o); void pop(); } interface Queue { void insert_rear(); void delete_front(); } <<implements>> <<implements>> class Dequeue implements Stack, Queue { Stack s = new StackImpl(); Queue q = new QueueImpl(); Object[] r; Dequeue(){s.r = r; q.r = r;} void insert_front(Object o){s.push(o);}; void insert_rear(Object o){q.insert_rear(o)}; void delete_front(){s.pop()}; void delete_rear(){q.delete_rear()}; }

Problem 4 With this program, describe what happens on lines marked 1, 2 and 3 class TypeTest { C v = new C(); void arrayProb(C[] anArray) { if (anArray.length >0) anArray[0] = v; // (2) } static void main(string[] args) { TypeTest tt = new TypeTest(); CSub[] paramArray = new CSub[10]; tt.arrayProb(paramArray); // (1) paramArray[0].methodOfCSubOnly(); // (3) Runtime type error Ok! Will never be reached!

Problem 5 Suppose that we have class Reservation with subclasses FlightReservation and TrainReservation as described in the foil set. It is desirable to have specific collections of reservations that cater for subclasses for new kinds of reservations (e.g. for space travels). How would you make a print method that prints all elements of such a collection, using the generics mechanisms of Java? void print(Collection<? extends Reservation > reservations) { ... }

Problem 6 Consider the following Java sketch: interface cowboy {void draw(); ...} interface shape {void draw(); ...} class LuckyLuke implements cowboy, shape {...} Is this an example of structural (sub)typing, given the fact that Java may very well get the same method from different interfaces, but still only provide one implementation? No, this is not an example of structural subtyping. Type checking is still done using the names cowboy and shape. 是根据name来确定interface,而不是structure来确定interface

Problem 7 The FlightReservation class we have seen a couple of times has a Flight attribute. We assume that this is a reference to an object of class Flight. The Flight object represents the actual flight reserved. In the flight table of SAS we have entries for e.g. SK451 (Oslo to Copenhagen). Suppose that we would like to represent such an entry by means of a FlightType object. Class FlightType would therefore have attributes that are common to all SK451 Flights, like source, destination, scheduled departure time (8.20), scheduled flying time (1.10), scheduled arrival time, etc. SK451 takes place every day (or almost), so a reservation system would need to have one Flight object for each actual flight. These Flight objects will have a representation of seats (free, occupied), and for other reasons one may imagine that they will also have actual departure time, actual flight time and delay (departure and arrival delay). It is perfectly possible to do this without inner classes, but if you should exploit inner classes, how would this be done. Of special interest are of course the functions computing the departure and arrival delays. Feel free to be inspired by the slide on inner classes exemplified by class Apartment, specially the fact the attribute height of Apartment is visible in the inner classes. Attributes like scheduled departureTime and arrivalTime should be attributes of the outer class FlightType.

Problem 7 – slide from lecture

Problem 7 class FlightType { City source, destination; TimeOfDay departureTime, arrivalTime; Duration flyingTime; class Flight { Seat[] seats; TimeOfDay actualDepartureTime, actualArrivalTime; Duration ActualFlyingTime; Duration departureDelay(){ return actualDepartureTime – departureTime; }; Duration arrivalDelay(){ return actualArrivalTime – arrivalTime; }; } }; class TimeTable { FlightType SK451 = new FlightType(Oslo, Copenhagen, 8.20); // provided a corresponding constructor TimeTableWithReservations ttwr = new TimeTable { SK451.Flight[365] SK451flights; // note: an array of an inner class Flight in an object SK451 of class FlightType ... }