Review for the Final Exam

Slides:



Advertisements
Similar presentations
1 Classes, Encapsulation, Methods and Constructors (Continued) Class definitions Instance data Encapsulation and Java modifiers Method declaration and.
Advertisements

Inheritance Inheritance Reserved word protected Reserved word super
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 8.1 – 8.5.
INF 523Q Chapter 7: Inheritance. 2 Inheritance  Another fundamental object-oriented technique is called inheritance, which enhances software design and.
COMP 14 Introduction to Programming Miguel A. Otaduy May 25, 2004.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 8, 2005.
Inheritance. © 2004 Pearson Addison-Wesley. All rights reserved 8-2 Inheritance Inheritance is a fundamental object-oriented design technique used to.
Writing Classes (Chapter 4)
CSE 1301 Lecture 11 Object Oriented Programming Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
© 2004 Pearson Addison-Wesley. All rights reserved November 7, 2007 Interfaces ComS 207: Programming I (in Java) Iowa State University, FALL 2007 Instructor:
© 2004 Pearson Addison-Wesley. All rights reserved April 17, 2006 Polymorphism (part 2) ComS 207: Programming I (in Java) Iowa State University, SPRING.
1 Object Oriented Design and UML Class Relationships –Dependency –Aggregation –Interfaces –Inheritance Interfaces Reading for this Lecture: L&L 6.4 – 6.5.
Chapter 6 Object-Oriented Design. © 2004 Pearson Addison-Wesley. All rights reserved6-2 Object-Oriented Design Now we can extend our discussion of the.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Static class members.
© 2004 Pearson Addison-Wesley. All rights reserved November 30, 2007 Exceptions ComS 207: Programming I (in Java) Iowa State University, FALL 2007 Instructor:
© 2004 Pearson Addison-Wesley. All rights reserved September 14, 2007 Anatomy of a Method ComS 207: Programming I (in Java) Iowa State University, FALL.
Review Class Inheritance, Abstract, Interfaces, Polymorphism, GUI (MVC)
© 2004 Pearson Addison-Wesley. All rights reserved April 14, 2006 Polymorphism ComS 207: Programming I (in Java) Iowa State University, SPRING 2006 Instructor:
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Chapter 5 Methods 1. Motivations Method : groups statements that perform a function.  Level of abstraction (black box)  Code Reuse – no need to reinvent.
Methods.
© 2004 Pearson Addison-Wesley. All rights reserved November 12, 2007 Inheritance ComS 207: Programming I (in Java) Iowa State University, FALL 2007 Instructor:
Outline Software Development Activities Identifying Classes and Objects Static Variables and Methods Class Relationships Interfaces Enumerated Types Revisited.
© 2004 Pearson Addison-Wesley. All rights reserved April 10, 2006 Inheritance (part 2) ComS 207: Programming I (in Java) Iowa State University, SPRING.
© 2004 Pearson Addison-Wesley. All rights reserved January 23, 2006 Creating Objects & String Class ComS 207: Programming I (in Java) Iowa State University,
© 2004 Pearson Addison-Wesley. All rights reserved November 14, 2007 Inheritance (part 2) ComS 207: Programming I (in Java) Iowa State University, FALL.
© 2004 Pearson Addison-Wesley. All rights reserved April 5, 2006 Method Design & Method Overloading ComS 207: Programming I (in Java) Iowa State University,
Object-Oriented Design Chapter 7 1. Objectives You will be able to Use the this reference in a Java program. Use the static modifier for member variables.
© 2004 Pearson Addison-Wesley. All rights reserved November 2, 2007 Class Relationships ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
CprE 185: Intro to Problem Solving (using C)
Polymorphism November 27, 2006 ComS 207: Programming I (in Java)
Interfaces November 6, 2006 ComS 207: Programming I (in Java)
Polymorphism 2nd Lecture
Parameter Lists & Command Line Arguments
Lecture 17: Polymorphism (Part II)
Creating Objects & String Class
Polymorphism.
Inheritance November 10, 2006 ComS 207: Programming I (in Java)
Inheritance April 7, 2006 ComS 207: Programming I (in Java)
CMSC 202 Interfaces.
Java Programming Language
2D Arrays October 12, 2007 ComS 207: Programming I (in Java)
CMSC 202 Polymorphism.
Instructor: Alexander Stoytchev
Classes, Encapsulation, Methods and Constructors (Continued)
Recursion (part 1) October 24, 2007 ComS 207: Programming I (in Java)
CS139 October 11, 2004.
Overriding Methods & Class Hierarchies
Instructor: Alexander Stoytchev
Exceptions April 19, 2006 ComS 207: Programming I (in Java)
Chapter 10: Method Overriding and method Overloading
Object Oriented Programming Review
Packages & Random and Math Classes
Instructor: Alexander Stoytchev
Midterm Review October 23, 2006 ComS 207: Programming I (in Java)
Chapter 9 Polymorphism.
Method Overriding and method Overloading
I/O Exceptions & Working with Files
Comparing Data & the ‘switch’ Statement
Comparing Data & the ‘switch’ Statement
CMSC 202 Interfaces.
Recursion (part 1) October 25, 2006 ComS 207: Programming I (in Java)
Classes and Objects Object Creation
Programming in C# CHAPTER 5 & 6
Inheritance and Polymorphism
Presentation transcript:

Review for the Final Exam December 8, 2006 ComS 207: Programming I (in Java) Iowa State University, FALL 2006 Instructor: Alexander Stoytchev © 2004 Pearson Addison-Wesley. All rights reserved

Final Exam Time: Tuesday Dec 12 @ 7:00-9:00 p.m. Location: same as midterms, i.e., Hoover 2055. © 2004 Pearson Addison-Wesley. All rights reserved

Final Format True/False (10 x 1p each = 10p) Short answer ( 5 x 3p each = 15p) Code Snippets ( 4 x 5p each = 20p) What is the output ( 2 x 5p each = 10p) Program 1 (20p) Program 2 (25p) Program 3 (30p) TOTAL (130p) © 2004 Pearson Addison-Wesley. All rights reserved

Final Format You don’t need to get all 130 points to get an A 100 is a 100 You must get at least 65 points in order to pass this exam © 2004 Pearson Addison-Wesley. All rights reserved

Ways to get an ‘A’ on the Final Option 1: 3 program (75p) True/False (10p) Short Answers (15p) TOTAL: (100p) Option 2: 2 programs (45p) True/False (10p) Short Answers (15p) Code Snippets (20p) What is the output (10p) TOTAL: (100p) © 2004 Pearson Addison-Wesley. All rights reserved

A Crash Course in Java © 2004 Pearson Addison-Wesley. All rights reserved

Recursive Programming Consider the problem of computing the sum of all the numbers between 1 and any positive integer N This problem can be recursively defined as: © 2004 Pearson Addison-Wesley. All rights reserved

Recursive Programming // This method returns the sum of 1 to num public int sum (int num) { int result; if (num == 1) result = 1; else result = num + sum (n-1); return result; } © 2004 Pearson Addison-Wesley. All rights reserved

Recursive Programming main sum sum(3) sum(1) sum(2) result = 1 result = 3 result = 6 © 2004 Pearson Addison-Wesley. All rights reserved

Think of recursion as a tree … © 2004 Pearson Addison-Wesley. All rights reserved

… an upside down tree © 2004 Pearson Addison-Wesley. All rights reserved

© 2004 Pearson Addison-Wesley. All rights reserved

Mystery Recursion from HW8 public class Recursion { public static void mystery1(int a, int b) if (a <= b) int m = (a + b) / 2; System.out.print(m + " "); mystery1(a, m-1); mystery1(m+1, b); } public static void main(String[] args) mystery1(0, 5); © 2004 Pearson Addison-Wesley. All rights reserved

(a=0, b=5) (a=0, b=1) (a=3, b=5) (a=0, b=-1) (a=1, b=1) (a=3, b=3) © 2004 Pearson Addison-Wesley. All rights reserved

2 (a=0, b=5) m=(a+b)/2 => print 2 (a=0, b=1) (a=3, b=5) (a=0, b=-1) © 2004 Pearson Addison-Wesley. All rights reserved

2 4 1 3 5 (a=0, b=5) (a=0, b=1) (a=3, b=5) (a=0, b=-1) (a=1, b=1) 4 (a=0, b=-1) (a=1, b=1) (a=3, b=3) (a=5, b=5) 1 3 5 (a=1, b=0) (a=2, b=1) (a=3, b=2) (a=4, b=3) (a=5, b=4) (a=6, b=5) © 2004 Pearson Addison-Wesley. All rights reserved

Traversal Order 2 4 1 3 5 (a=0, b=5) (a=0, b=1) (a=3, b=5) (a=0, b=-1) 4 (a=0, b=-1) (a=1, b=1) (a=3, b=3) (a=5, b=5) 1 3 5 (a=1, b=0) (a=2, b=1) (a=3, b=2) (a=4, b=3) (a=5, b=4) (a=6, b=5) © 2004 Pearson Addison-Wesley. All rights reserved

Parameter Passing (primitive types) The act of passing an argument takes a copy of a value and stores it in a local variable acessible only to the method which is being called. { int num1=38; myMethod(num1); } void myMethod(int num2) { num2 =50; } num1 38 Before: num2 38 Before: num1 38 After: num2 50 After: © 2004 Pearson Addison-Wesley. All rights reserved

Objects and Reference Variables acct1 72354 acctNumber 102.56 balance name “Ted Murphy” acct2 69713 acctNumber 40.00 balance name “Jane Smith” © 2004 Pearson Addison-Wesley. All rights reserved

Parameter Passing (objects) Objects (in this case arrays) are also passed by value. In this case, however, the value is the address of the object pointed to by the reference variable. { int[] a={5, 7}; myMethod(a); } void myMethod(int[] b) { b[0]+=5; } b Before: 5 7 a Before: 5 7 b After: 10 7 a After: 10 7 © 2004 Pearson Addison-Wesley. All rights reserved

In the previous example there is only one array and two references to it. 5 7 b © 2004 Pearson Addison-Wesley. All rights reserved

The array can be modified through either reference. 10 7 b © 2004 Pearson Addison-Wesley. All rights reserved

Method Overloading The compiler determines which method is being invoked by analyzing the parameters [signature 1] tryMe: int float tryMe(int x) { return x + .375; } [signature 2] tryMe: int, float float tryMe(int x, float y) { return x*y; } © 2004 Pearson Addison-Wesley. All rights reserved

Method Overriding Same Signatures Different Method Bodies public class Parent { public float tryMe(int x) return x + .375; } Same Signatures public class Child extends Parent { public float tryMe(int x) return x*x; } Different Method Bodies © 2004 Pearson Addison-Wesley. All rights reserved

Overloading vs. Overriding Overloading deals with multiple methods with the same name in the same class, but with different signatures Overriding deals with two methods, one in a parent class and one in a child class, that have the same signature © 2004 Pearson Addison-Wesley. All rights reserved

Overloading vs. Overriding Overloading lets you define a similar operation in different ways for different parameters Overriding lets you define a similar operation in different ways for different object types © 2004 Pearson Addison-Wesley. All rights reserved

Average Example from HW9 public class Average { public static double average(double a, double b) { return (a+b)/2.0; } public static double average(double a, double b, double c) { return (a+b+c)/3.0; } public static void main (String[] args) { System.out.println (average(1, 2)); System.out.println (average (1, 2, 3)); } } © 2004 Pearson Addison-Wesley. All rights reserved

Aggregation Example: Copmonents of a Student © 2004 Pearson Addison-Wesley. All rights reserved

Student First Name Last Name Home Address School Address © 2004 Pearson Addison-Wesley. All rights reserved

john John Smith 21 Jump Street 800 Lancaster Ave. © 2004 Pearson Addison-Wesley. All rights reserved

marsha Marsha Jones 123 Main Street 800 Lancaster Ave. © 2004 Pearson Addison-Wesley. All rights reserved

Aggregation In the following example, a Student object is composed, in part, of Address objects A student has an address (in fact each student has two addresses) See StudentBody.java (page 304) See Student.java (page 306) See Address.java (page 307) An aggregation association is shown in a UML class diagram using an open diamond at the aggregate end © 2004 Pearson Addison-Wesley. All rights reserved

super super super this this this © 2004 Pearson Addison-Wesley. All rights reserved

Polymorphism in Nature © 2004 Pearson Addison-Wesley. All rights reserved [http://www.blackwellpublishing.com/ridley/images/h_erato.jpg]

Class Hierarchy © 2004 Pearson Addison-Wesley. All rights reserved

public abstract class Animal { abstract void makeSound(); } public class Cow extends Animal { public void makeSound() { System.out.println("Moo-Moo"); } } public class Dog extends Animal { public void makeSound() { System.out.println(“Wuf-Wuf"); } } public class Duck extends Animal { public void makeSound() { System.out.println(“Quack-Quack"); } } © 2004 Pearson Addison-Wesley. All rights reserved

public class Farm {. public static void main(String[] args). { public class Farm { public static void main(String[] args) { Cow c=new Cow(); Dog d=new Dog(); Duck k= new Duck(); c.makeSound(); d.makeSound(); k.makeSound(); } Result: Moo-Moo Wuf-Wuf Quack-Quack © 2004 Pearson Addison-Wesley. All rights reserved

public class Farm2 {. public static void main(String[] args). { public class Farm2 { public static void main(String[] args) { Animal[] a = new Animal[3]; a[0] = new Cow(); a[1] = new Dog(); a[2] = new Duck(); for(int i=0; i< a.length; i++) a[i].makeSound(); } } Result: Moo-Moo Wuf-Wuf Quack-Quack © 2004 Pearson Addison-Wesley. All rights reserved

a © 2004 Pearson Addison-Wesley. All rights reserved

a Not possible since Animal is abstract © 2004 Pearson Addison-Wesley. All rights reserved

public abstract class Animal { abstract void makeSound(); public void move() { System.out.println(“walk”); } } Define a new method called move(). It is not abstract and will be inherited by all children of Animal. public class Cow extends Animal { public void makeSound() { System.out.println("Moo-Moo"); } } public class Dog extends Animal { public void makeSound() { System.out.println(“Wuf-Wuf"); } } public class Duck extends Animal { public void makeSound() { System.out.println(“Quack-Quack"); } } © 2004 Pearson Addison-Wesley. All rights reserved

public class Farm2b {. public static void main(String[] args). { public class Farm2b { public static void main(String[] args) { Animal[] a = new Animal[3]; a[0] = new Cow(); a[1] = new Dog(); a[2] = new Duck(); for(int i=0; i< a.length; i++) a[i].move(); } } Result: walk © 2004 Pearson Addison-Wesley. All rights reserved

public abstract class Animal { abstract void makeSound(); public void move() { System.out.println(“walk”); } } public class Cow extends Animal { public void makeSound() { System.out.println("Moo-Moo"); } } public class Dog extends Animal { public void makeSound() { System.out.println(“Wuf-Wuf"); } } public class Duck extends Animal { public void makeSound() { System.out.println(“Quack-Quack"); } public void move() { System.out.println(“fly”); } } Override the move method defined in the Animal class. © 2004 Pearson Addison-Wesley. All rights reserved

public class Farm2c {. public static void main(String[] args). { public class Farm2c { public static void main(String[] args) { Animal[] a = new Animal[3]; a[0] = new Cow(); a[1] = new Dog(); a[2] = new Duck(); for(int i=0; i< a.length; i++) a[i].move(); } } Result: Walk Fly © 2004 Pearson Addison-Wesley. All rights reserved

Polymorphism via Inheritance Now let's look at an example that pays a set of diverse employees using a polymorphic method See Firm.java (page 486) See Staff.java (page 487) See StaffMember.java (page 489) See Volunteer.java (page 491) See Employee.java (page 492) See Executive.java (page 493) See Hourly.java (page 494) © 2004 Pearson Addison-Wesley. All rights reserved

The Animals example with interfaces In this case Animal is an interface. The Animals example with interfaces implements implements implements © 2004 Pearson Addison-Wesley. All rights reserved

public interface Animal { public void makeSound(); } public class Cow implements Animal { public void makeSound() { System.out.println("Moo-Moo"); } } public class Dog implements Animal { public void makeSound() { System.out.println(“Wuf-Wuf"); } } public class Duck implements Animal { public void makeSound() { System.out.println(“Quack-Quack"); } } © 2004 Pearson Addison-Wesley. All rights reserved

public class iFarm {. public static void main(String[] args). { public class iFarm { public static void main(String[] args) { Animal domestic; domestic = new Cow(); domestic.makeSound(); domestic = new Dog(); domestic.makeSound(); domestic = new Duck(); domestic.makeSound(); } Result: Moo-Moo Wuf-Wuf Quack-Quack © 2004 Pearson Addison-Wesley. All rights reserved

Replicated code!!! The only move() method that is different from public interface Animal { public void makeSound(); public void move(); } Define a new method called move(). Because Animal is an interface this method cannot be defined as in the previous example in which Animal was an abstract class. public class Cow implements Animal { public void makeSound() { System.out.println("Moo-Moo"); } public void move() { System.out.println(“walk"); } } public class Dog implements Animal { public void makeSound() { System.out.println(“Wuf-Wuf"); } public void move() { System.out.println(“walk"); } } Replicated code!!! public class Duck implements Animal { public void makeSound() { System.out.println(“Quack-Quack"); } public void move() { System.out.println(“fly"); } } The only move() method that is different from the rest. © 2004 Pearson Addison-Wesley. All rights reserved

public class iFarm2 {. public static void main(String[] args). { public class iFarm2 { public static void main(String[] args) { Animal domestic; domestic = new Cow(); domestic.move(); domestic = new Dog(); domestic.move(); domestic = new Duck(); domestic.move(); } Result: walk fly © 2004 Pearson Addison-Wesley. All rights reserved

public interface Animal { public void makeSound(); public void move(); } public class Cow implements Animal { public void makeSound() { System.out.println("Moo-Moo"); } public void move() { System.out.println(“walk"); } } public class Dog implements Animal { public void makeSound() { System.out.println(“Wuf-Wuf"); } public void move() { System.out.println(“walk"); } } public class Duck implements Animal { public void makeSound() { System.out.println(“Quack-Quack"); } public void move() { System.out.println(“fly"); } public void dive() { System.out.println(“Diving…"); } } Only Ducks can dive(). © 2004 Pearson Addison-Wesley. All rights reserved

public class iFarm3 {. public static void main(String[] args). { public class iFarm3 { public static void main(String[] args) { Animal domestic; domestic = new Cow(); //domestic.dive(); // error domestic = new Dog(); //domestic.dive(); // error domestic = new Duck(); // domestic.dive(); // error ((Duck)domestic).dive(); // OK, but uses a cast } } Result: Ducks can dive. © 2004 Pearson Addison-Wesley. All rights reserved

Exceptions Class Hierarchy © 2004 Pearson Addison-Wesley. All rights reserved

The throw Statement Exceptions are thrown using the throw statement Usually a throw statement is executed inside an if statement that evaluates a condition to see if the exception should be thrown See CreatingExceptions.java (page 543) See OutOfRangeException.java (page 544) © 2004 Pearson Addison-Wesley. All rights reserved

Questions? © 2004 Pearson Addison-Wesley. All rights reserved

THE END © 2004 Pearson Addison-Wesley. All rights reserved