CS1101X: Programming Methodology Recitation 10 Inheritance and Polymorphism.

Slides:



Advertisements
Similar presentations
Chapter 1 Object-Oriented Concepts. A class consists of variables called fields together with functions called methods that act on those fields.
Advertisements

CSCI S-1 Section 5. Deadlines for Problem Set 3 Part A – Friday, July 10, 17:00 EST Parts B – Tuesday, July 14, 17:00 EST Getting the code examples from.
Polymorphism. Legal assignments Widening is legal Narrowing is illegal (unless you cast) class Test { public static void main(String args[]) { double.
CS102--Object Oriented Programming Review 1: Chapter 1 – Chapter 7 Copyright © 2008 Xiaoyan Li.
Writing algorithms using the for-statement. Programming example 1: find all divisors of a number We have seen a program using a while-statement to solve.
MSc IT Programming Methodology (2). MODULE TEAM Dr Aaron Kans Dr Sin Wee Lee.
Computer Programming Lab(5).
Multiple Choice Solutions True/False a c b e d   T F.
Inheritance and Polymorphism Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
220 FINAL TEST REVIEW SESSION Omar Abdelwahab. INHERITANCE AND POLYMORPHISM Suppose you have a class FunClass with public methods show, tell, and smile.
CS1101X: Programming Methodology Recitation 7 Arrays II.
 The pool rack example could be implemented using a for loop.  It is also possible to write recursive methods that accomplish things that you might.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Java Quiz Bowl A fun review of the Java you should know from CMPT 201 If you don’t know the answers - this week is for you to study up!
Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.
2D-Arrays Quratulain. Learning Objectives Two-dimensional arrays Declaration Initialization Applications.
1 Fencepost loops “How do you build a fence?”. 2 The fencepost problem Problem: Write a class named PrintNumbers that reads in an integer called max and.
CSCI S-1 Section 6. Coming Soon Homework Part A – Friday, July 10, 17:00 EST Homework Part B – Tuesday, July 14, 17:00 EST Mid-Term Quiz Review – Friday,
Boolean expressions, part 2: Logical operators. Previously discussed Recall that there are 2 types of operators that return a boolean result (true or.
Can we talk?. In Hello World we already saw how to do Standard Output. You simply use the command line System.out.println(“text”); There are different.
Introduction to Programming G50PRO University of Nottingham Unit 8 : Methods 2 - Arrays Paul Tennent
CS1101X: Programming Methodology Recitation 10 Inheritance and Polymorphism.
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures for Java William H. Ford William R. Topp Chapter 5 Generic.
Chapter 6. else-if & switch Copyright © 2012 Pearson Education, Inc.
CS1101X: Programming Methodology Recitation 4 Design Issues and Problem Solving.
The assignment expressions. The assignment operator in an assignment statement We have seen the assignment statement: Effect: var = expr; Stores the value.
Output Programs These slides will present a variety of small programs. Each program has a control structure that was introduced in this chapter. Our concern.
1 Inheritance and Polymorphism. 2 This section is not required material!!!!  Since we are ahead in lecture, I’m delving into it  If you feel confused,
Java Review if Online Time For loop Quiz on Thursday.
SELF STUDY. IF STATEMENTS SELECTION STRUCTURE if selection statement if … else selection statement switch selection statement.
Boolean expressions, part 1: Compare operators. Compare operators Compare operators compare 2 numerical values and return a Boolean (logical) value A.
CS1101: Programming Methodology Preparing for Practical Exam (PE)
Classes Revisited Chapter 8.
import java.util.Scanner; class myCode { public static void main(String[] args) { Scanner input= new Scanner(System.in); int num1; System.out.println(“Enter.
Output Programs These slides will present a variety of small programs. Each program has a compound condition which uses the Boolean Logic that was introduced.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Decisions and Iterations.
WAP to find out the number is prime or not Import java.util.*; Class Prime { public static void main(string args[]) { int n,I,res; boolean flag=true;
Exceptions and Error Handling. Exceptions Errors that occur during program execution We should try to ‘gracefully’ deal with the error Not like this.
Last Revision. Question1 Novice Java programmers often write code similar to, class C { public int x;... }... C[] a = new C[10]; for(int i = 0; i < a.length;
CS1101X: Programming Methodology Recitation 6 Arrays I.
(Dreaded) Quiz 2 Next Monday.
Object-Oriented Concepts
Chapter 2 Clarifications
CSC111 Quick Revision.
Chapter 6 More Conditionals and Loops
Inheritance and Polymorphism
Repetition-Counter control Loop
The switch Statement The switch statement provides another way to decide which statement to execute next The switch statement evaluates an expression,
SELECTION STATEMENTS (1)
Something about Java Introduction to Problem Solving and Programming 1.
Building Java Programs
Building Java Programs
Building Java Programs
Overloading and Constructors
Building Java Programs
Chapter 6 More Conditionals and Loops
CSC 113 Tutorial QUIZ I.
TO COMPLETE THE FOLLOWING:
מיונים וחיפושים קרן כליף.
Java so far Week 7.
Building Java Programs
Self study.
class PrintOnetoTen { public static void main(String args[]) {
Object Oriented Programming
CIS 110: Introduction to Computer Programming
Building Java Programs
Repetition Statements
Building Java Programs
Building Java Programs
Building Java Programs
Presentation transcript:

CS1101X: Programming Methodology Recitation 10 Inheritance and Polymorphism

CS1101X Recitation #102 Qn 1: Class F (1/2) Suppose class F has the following definition: public class F { private int value; public F(int x) { value = x; System.out.println("Made F: " + value); }

CS1101X Recitation #103 Qn 1: Class F (2/2) What is the output of the following program? public class G extends F { private F value; public G(int x, int y) { super(x); value = new F(y); } public static void main(String[] args) { G g = new G(11, 28); } + Made F: 11 Made F: 28

CS1101X Recitation #104 Qn 2: Class Y (1/5) Given this class X: public class X { // default constructor public X() { // no body needed } // isX(): class method public static boolean isX(Object v) { return (v instanceof X); } // isObject(): class method public static boolean isObject(X v) { return (v instanceof Object); }

CS1101X Recitation #105 Qn 2: Class Y (2/5) And this class Y: public class Y extends X { // Y(): default constructor public Y() { // no body needed } // isY(): class method public static boolean isY(Object v) { return (v instanceof Y); }

CS1101X Recitation #106 Qn 2: Class Y (3/5) Class Y (continued): public static void main(String[] args) { X x = new X(); Y y = new Y(); X z = y; System.out.println("x is an Object: " X.isObject(x)); System.out.println("x is an X: " + X.isX(x)); System.out.println("x is a Y: " + Y.isY(x)); System.out.println(); System.out.println("y is an Object: " + X.isObject(y)); System.out.println("y is an X: " + X.isX(y)); System.out.println("y is a Y: " + Y.isY(y)); System.out.println(); System.out.println("z is an Object: " + X.isObject(z)); System.out.println("z is an X: " + X.isX(z)); System.out.println("z is a Y: " + Y.isY(z)); }

CS1101X Recitation #107 Qn 2: Class Y (4/5) Which of the following statements could be the fourth statement in method main() ? What would be the output? Explain. public static void main(String[] args) { X x = new X(); Y y = new Y(); X z = y; // fourth statement here... } a)x = y; b)x = (X) y; c)y = x; d)y = (Y) x;

CS1101X Recitation #108 Qn 2: Class Y (5/5) Answers: a) x = y; This state is legal. It assigns a subclass object to a superclass variable. x is an Object: true x is an X: true x is a Y: true b)x = (X) y; This state is legal. It casts a subclass object to an object of its superclass, then assigns that value to a superclass variable. y is an Object: true y is an X: true y is a Y: true c) y = x; This statement is illegal. It attempts to assign a superclass object to a subclass variables, which is not permitted. d) y = (Y) x; This statement is illegal. It attempts to cast a superclass object to an object of its subclass, which is not permitted. +

CS1101X Recitation #109 Qn 3: Colored3DPoint (1/2) Suppose the following method main() was added to class Colored3DPoint. What would the output be? public static void main(String[] args) { Colored3DPoint c = new Colored3DPoint(); Colored3DPoint d = new Colored3DPoint(1, 2, 3, color.BLACK); Colored3DPoint e = (Colored3DPoint) d.clone(); System.out.println(c); System.out.println(d); System.out.println(e); System.out.println(d.equals(c)); System.out.println(d.equals(e)); }

CS1101X Recitation #1010 Qn 3: Colored3DPoint (2/2) Output: class Colored3DPoint[0, 0, 0, java.awt.Color[r=0,g=0,b=255]] class Colored3DPoint[1, 2, 3, java.awt.Color[r=0,g=0,b=0]] false true +

CS1101X Recitation #1011 Qn 4: Sum of two elements (1/5) Given this problem: A sorted list of integers list and a value is given. Write an algorithm to find the subscripts of (any) two distinct elements in the list whose sum is equal to the given value. Example: list: 2, 3, 8, 12, 15, 19, 22, 24 value: 23 answer: elements 8 (at subscript 2) and 15 (at subscript 4) Write an efficient code for this problem. What is the running-time of your algorithm?

CS1101X Recitation #1012 Qn 4: Sum of two elements (2/5) Sample run: Enter number of elements: 8 Enter elements (in non-decreasing order): Enter sum: 23 Answer: 2, 4

CS1101X Recitation #1013 Qn 4: Sum of two elements (3/5) import java.util.*; class SumTwoElements { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int[] intArray = createArray(scanner); // printArray(intArray); // for checking System.out.print("Enter sum: "); int sum = scanner.nextInt(); search(intArray, sum); }

CS1101X Recitation #1014 Qn 4: Sum of two elements (4/5) public static int[] createArray(Scanner scan) { System.out.print("Enter number of elements: "); int n = scan.nextInt(); System.out.println( "Enter elements (in non-decreasing order): "); int arr[] = new int[n]; for (int i = 0; i < n; ++i) { arr[i] = scan.nextInt(); } return arr; } public static void printArray(int[] arr) { for (int i = 0; i < arr.length; ++i) System.out.print(arr[i] + " " ); System.out.println(); }

CS1101X Recitation #1015 Qn 4: Sum of two elements (5/5) + public static void search(int[] arr, int sum) { if (arr.length >= 2) { int left = 0; int right = arr.length - 1; while (left < right) { if (arr[left] + arr[right] == sum) { System.out.println("Answer: " + left + ", " + right); return; } else if (arr[left] + arr[right] < sum) left++; else // arr[left] + arr[right] > sum right--; } System.out.println("No solution."); }

CS1101X Recitation #1016 End of Recitation #10