CS1101X: Programming Methodology Recitation 10 Inheritance and Polymorphism.

Slides:



Advertisements
Similar presentations
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.
Advertisements

15 Sorting1June Sorting CE : Fundamental Programming Techniques.
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.
LAB 10.
Computer Programming Lab(4).
MSc IT Programming Methodology (2). MODULE TEAM Dr Aaron Kans Dr Sin Wee Lee.
Computer Programming Lab(5).
The for-statement. Different loop-statements in Java Java provides 3 types of loop-statements: 1. The for-statement 2. The while-statement 3. The do-while-statement.
Inheritance and Polymorphism Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
CS1101X: Programming Methodology Recitation 7 Arrays II.
Programming Methodology (1). import java.util.*; public class FindCost3 { public static void main(String[] args ) { Scanner sc = new Scanner(System.in);
 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!
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,
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
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.
FOR LOOP WALK THROUGH public class NestedFor { public static void main(String [] args) { for (int i = 1; i
CS1101: Programming Methodology Preparing for Practical Exam (PE)
Cumulative algorithms. 2 Adding many numbers How would you find the sum of all integers from ? // This may require a lot of typing int sum = 1 +
Introduction to array: why use arrays ?. Motivational example Problem: Write a program that reads in and stores away 5 double numbers After reading in.
SELF STUDY. IF STATEMENTS SELECTION STRUCTURE if selection statement if … else selection statement switch selection statement.
CS1101: Programming Methodology Preparing for Practical Exam (PE)
College Board Topics – A.P. Computer Science A Program Design - Read and understand a problem's description, purpose, and goals; Apply data abstraction.
import java.util.Scanner; class myCode { public static void main(String[] args) { Scanner input= new Scanner(System.in); int num1; System.out.println(“Enter.
CS1101X: Programming Methodology Recitation 10 Inheritance and Polymorphism.
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.
1 BUILDING JAVA PROGRAMS CHAPTER 5 PROGRAM LOGIC AND INDEFINITE LOOPS.
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.
Introduction to programming in java
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
CS1101X: Programming Methodology Recitation 6 Arrays I.
(Dreaded) Quiz 2 Next Monday.
Slides by Evan Gallagher
Slides by Evan Gallagher
Chapter 2 Clarifications
Inheritance and Polymorphism
Repetition-Counter control Loop
Something about Java Introduction to Problem Solving and Programming 1.
Building Java Programs
Building Java Programs
Building Java Programs
Computing Adjusted Quiz Total Score
TO COMPLETE THE FOLLOWING:
מיונים וחיפושים קרן כליף.
Java so far Week 7.
Building Java Programs
Self study.
Suppose I want to add all the even integers from 1 to 100 (inclusive)
class PrintOnetoTen { public static void main(String args[]) {
Factoring if/else code
Sieve of Eratosthenes The Sieve of Eratosthenes uses a bag to find all primes less than or equal to an integer value n. Begin by creating a bag an inserting.
Scope of variables class scopeofvars {
Object Oriented Programming
CIS 110: Introduction to Computer Programming
Building Java Programs
Repetition Statements
Java 1/31/2017 Back to Objects.
Building Java Programs
Building Java Programs
Building Java Programs
Consider the following code:
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); } +

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: +

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: +

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) { }

CS1101X Recitation #1016 End of Recitation #10