Copyright 2008 by Pearson Education Building Java Programs Subtype Polymorphism; Sorting (an extended programming example)

Slides:



Advertisements
Similar presentations
College of Information Technology & Design
Advertisements

CS 206 Introduction to Computer Science II 02 / 27 / 2009 Instructor: Michael Eckmann.
Copyright 2010 by Pearson Education Building Java Programs Chapter 7 Lecture 7-2: Arrays as Parameters reading: , 3.3 self-checks: Ch. 7 #5, 8,
Analysis of Algorithms
Radix Sorting CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1.
Sorting Sorting is the process of arranging a list of items in a particular order The sorting process is based on specific value(s) Sorting a list of test.
Sorting I Chapter 8 Kruse and Ryba. Introduction Common problem: sort a list of values, starting from lowest to highest. –List of exam scores –Words of.
Fundamentals of Computer Science Lecture 14: Recursion Instructor: Evan Korth New York University.
Fundamentals of Computer Science Lecture 14: Recursion Instructor: Evan Korth New York University.
Introduction to Computers and Programming Lecture 5 New York University.
Recursion. Objectives At the conclusion of this lesson, students should be able to Explain what recursion is Design and write functions that use recursion.
CS 206 Introduction to Computer Science II 10 / 14 / 2009 Instructor: Michael Eckmann.
1 Inheritance Readings: Writing classes Write an Employee class with methods that return values for the following properties of employees at a.
Copyright 2008 by Pearson Education Building Java Programs Chapter 9 Critters; Subtype Polymorphism Reading: HW9 Handout, Chapter 9.2.
CS 106 Introduction to Computer Science I 10 / 15 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 10 / 16 / 2006 Instructor: Michael Eckmann.
Do Now Take out ch6 test answers – be ready to hand it in Pick a leader in each group of up to 3 students; Leader will retrieve a whiteboard, marker, and.
Invitation to Computer Science, Java Version, Second Edition.
Building Java Programs Chapter 9 Inheritance and Interfaces Copyright (c) Pearson All rights reserved.
CS 106 Introduction to Computer Science I 03 / 19 / 2007 Instructor: Michael Eckmann.
Lecturer: Dr. AJ Bieszczad Chapter 11 COMP 150: Introduction to Object-Oriented Programming 11-1 l Basics of Recursion l Programming with Recursion Recursion.
10/14/ Algorithms1 Algorithms - Ch2 - Sorting.
Objectives - 11  We will work with processing Arrays.  Objectives:  Describe the concept of an array and its benefits.  Define the terms index, traverse,
BUILDING JAVA PROGRAMS CHAPTER 7 Arrays. Exam #2: Chapters 1-6 Thursday Dec. 4th.
Copyright 2008 by Pearson Education Building Java Programs Chapter 9 Lecture 9-1: Inheritance reading:
IB Computer Science Unit 5 – Advanced Topics Recursion.
Copyright 2008 by Pearson Education Building Java Programs ArrayList Reading: 10.1.
Aug 9, CMSC 202 ArrayList. Aug 9, What’s an Array List ArrayList is  a class in the standard Java libraries that can hold any type of object.
CPS 100, Spring Tools: Solve Computational Problems l Algorithmic techniques  Brute-force/exhaustive, greedy algorithms, dynamic programming,
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 9 Searching & Sorting.
Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.
ALGORITHMS.
Composition When one class contains an instance variable whose type is another class, this is called composition. Instead of inheritance, which is based.
Data Structures - CSCI 102 Selection Sort Keep the list separated into sorted and unsorted sections Start by finding the minimum & put it at the front.
CSE 1201 Object Oriented Programming ArrayList 1.
Textbook C for Scientists and Engineers © Prentice Hall 1997 Available at NUS CO-OP at S$35.10.
Fall 2015, Kevin Quinn CSE373: Data Structures & Algorithms Lecture 25: Problem Solving CSE373: Data Structures and algorithms1.
Copyright 2009 by Pearson Education Building Java Programs Chapter 9: Inheritance and Interfaces Lecture 9-2: Polymorphism reading: 9.2 self-check: #5-9.
CS 162 Intro to Programming II Insertion Sort 1. Assume the initial sequence a[0] a[1] … a[k] is already sorted k = 0 when the algorithm starts Insert.
1 CSE 142 Final Exam Review Problems. 2 Question Types expressions array mystery inheritance mystery file processing array programming Critters classes.
Mid-Year Review. Coding Problems In general, solve the coding problems by doing it piece by piece. Makes it easier to think about Break parts of code.
Recursion. Objectives At the conclusion of this lesson, students should be able to Explain what recursion is Design and write functions that use recursion.
CSE 143 Lecture 3 Implementing ArrayIntList reading: slides created by Marty Stepp and Hélène Martin
Copyright 2008 by Pearson Education Building Java Programs Chapter 9 Lecture 9-3: Polymorphism reading: 9.3.
1 Interacting with the superclass (continued) suggested reading:9.4.
Building Java Programs Chapter 9 Interfaces Copyright (c) Pearson All rights reserved.
Building Java Programs
Lecture 16: Polymorphism (Part I)
UNIT - IV SORTING By B.Venkateswarlu Dept of CSE.
Building Java Programs
Topic 32 - Polymorphism.
Building Java Programs Chapter 14
Advanced Sorting Methods: Shellsort
Topic 32 - Polymorphism.
Data Structures and Algorithms
Building Java Programs
Building Java Programs
Building Java Programs
Inheritance Readings: 9.1.
Lecture 17: Polymorphism (Part I)
The software crisis software engineering: The practice of developing, designing, documenting, testing large computer programs. Large-scale projects face.
Building Java Programs
Chapter 9 9-3: Polymorphism reading: 9.3
Sorting and Searching -- Introduction
Building Java Programs
CS203 Lecture 15.
Sorting.
Advanced Sorting Methods: Shellsort
Presentation transcript:

Copyright 2008 by Pearson Education Building Java Programs Subtype Polymorphism; Sorting (an extended programming example)

Copyright 2008 by Pearson Education 2 A tricky problem The order of the classes is jumbled up (easy). The methods sometimes call other methods (tricky!!) public class Lamb extends Ham { public void b() { System.out.print("Lamb b "); } public class Ham { public void a() { System.out.print("Ham a "); b(); } public void b() { System.out.print("Ham b "); } public String toString() { return "Ham"; }

Copyright 2008 by Pearson Education 3 Another problem 2 public class Spam extends Yam { public void b() { System.out.print("Spam b "); } public class Yam extends Lamb { public void a() { System.out.print("Yam a "); } public String toString() { return "Yam"; } What would be the output of the following client code? Ham[] food = {new Lamb(), new Ham(), new Spam(), new Yam()}; for (int i = 0; i < food.length; i++) { System.out.println(food[i]); food[i].a(); System.out.println(); // to end the line of output food[i].b(); System.out.println(); // to end the line of output System.out.println(); }

Copyright 2008 by Pearson Education 4 Class diagram

Copyright 2008 by Pearson Education 5 Polymorphism at work Lamb inherits Ham 's a. a calls b. But Lamb overrides b... public class Ham { public void a() { System.out.print("Ham a "); b(); } public void b() { System.out.print("Ham b "); } public String toString() { return "Ham"; } public class Lamb extends Ham { public void b() { System.out.print("Lamb b "); } Lamb 's output from a : Ham a Lamb b

Copyright 2008 by Pearson Education 6 The table methodHamLambYamSpam a b toString methodHamLambYamSpam aHam a b() Yam a Ham a b() bHam bLamb bSpam b toString HamYam methodHamLambYamSpam aHam a b() Ham a b() Yam a Ham a b() bHam bLamb b Spam b toString Ham Yam

Copyright 2008 by Pearson Education 7 The answer Ham[] food = {new Lamb(), new Ham(), new Spam(), new Yam()}; for (int i = 0; i < food.length; i++) { System.out.println(food[i]); food[i].a(); food[i].b(); System.out.println(); } Output: Ham Ham a Lamb b Lamb b Ham Ham a Ham b Ham b Yam Yam a Spam b Yam Yam a Lamb b

Copyright 2008 by Pearson Education 8 Now what? Done: Almost all the Java language features we’ll learn There are more But methods, loops, arrays, objects, files, if/else, strings, etc. are a powerful toolset (not bad for 9 weeks!) Remaining lectures: 1. ArrayList library (next week) 2. Review of references (next week; for the final) 3. More practice with arrays 4. More about writing larger/trickier programs Sorting: A great example for 3-4

Copyright 2008 by Pearson Education 9 Sorting Computers are used to sort a lot: Take a list of data and return all the same data “in order” Example: Students by id number Example: Students by exam score Example: High temperatures for the last month “In what order” depends on what you want to do For now, assume integers and lesser before greater Example: sorted is So far we have described the external behavior Now we need an algorithm Then (and only then) we can “code it up”

Copyright 2008 by Pearson Education 10 An algorithm Recall: An algorithm is a sequence of steps to compute an answer There can be more than one algorithm for a problem! This is a concept bigger than programming People sorted long before computers existed (of course) But computer scientists invented better ways! Today we’ll stick to simpler not-as-good ways

Copyright 2008 by Pearson Education 11 Insertion sort Pseudocode / English: Copy each input element to the output one at a time. Keep the output sorted by: (1) finding the right place for the next element (2) shift over all the larger numbers to make room (3) insert the number in the space you just made

Copyright 2008 by Pearson Education 12 Selection sort Pseudocode / English: Initially no elements have been copied to the output. Select the least element that has not already been copied and copy it to end of the output. Repeat until all elements have been copied. Note: Have to keep track of what has already been copied.

Copyright 2008 by Pearson Education 13 Coding it up Translating an algorithm to Java is not easy Defining the right arrays Avoiding off-by-one bugs Implementing things like “find the least” Want to test our implementations Think about corner cases! 0-element array 1-element array Negative numbers Array with all the same number …

Copyright 2008 by Pearson Education 14 More generally What part of our code was specific to sorting numbers? Almost nothing – the code for something else would look almost identical Example: Sort Point objects need an order, such as distance to the origin Good news / bad news: We ended up with offensive redundancy In 142, we don’t have the knowledge to avoid this Okay, so there is more to learn about programming At least we can recognize the algorithm is the same And there “oughtta be a way” to implement the algorithm only once, even if we don’t know it yet