30
Reviewing for the Midterm Covers chapters 1 to 5, 7 to 9 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Reviewing for the Midterm Covers chapters 1 to 5, 7 to 9 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Embed Size (px)

Citation preview

Page 1: Reviewing for the Midterm Covers chapters 1 to 5, 7 to 9 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Reviewing for the MidtermCovers chapters 1 to 5, 7 to 9

Instructor: Scott Kristjanson

CMPT 125/125

SFU Burnaby, Fall 2013

Page 2: Reviewing for the Midterm Covers chapters 1 to 5, 7 to 9 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Wk07.5 Slide 2Slides based on Java Foundations 3rd Edition, Lewis/DePasquale/Chase

2

Scott Kristjanson – CMPT 125/126 – SFU

Things to Review

Review the Class Slides:• Key Things to Take Away – Do you understand these the points?• Review Slides, especially if some Key Points are unclear

Make use of the Textbook• Answer Self-Review Questions at back of each chapter• Select Exercises and Programming Projects to practice

Review Quiz and Assignments• Do you understand how to answer Assignment 3 Written Questions?• Review Quiz #1 and Assignment #2 questions, then see the solutions

Get some Coding Practice!• Sign up on CodingBat.com and work through the problems there

Page 3: Reviewing for the Midterm Covers chapters 1 to 5, 7 to 9 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Wk07.5 Slide 3Slides based on Java Foundations 3rd Edition, Lewis/DePasquale/Chase

3

Scott Kristjanson – CMPT 125/126 – SFU

Review the Class Slides

Week 01• wk01.3 – Background Info, Introduction

Week 02• wk02.1 – Introduction to Java • wk02.3 – Intro to Objects and String and Scanner Objects • wk02.5 – Packages Enums and Wrappers

Week 03• wk03.1 – Boolean Expressions, Conditionals, Definite Loops

Week 04• wk04.1 – Structured Analysis and Data Flow Diagrams • wk04.3 – Review

Week 05• wk05.1 – Writing Classes • wk05.5 – Quiz 1 Solutions

Week 06• wk06.1 – Testing • wk06.3 – Arrays • wk06.5 – Inheritance

Week 07• wk07.3 – Polymorphism

Page 4: Reviewing for the Midterm Covers chapters 1 to 5, 7 to 9 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Wk02.1 Slides:Introduction to JavaChapters 1 and 2The Java Language – Section 1.1Data & Expressions – Sections 2.1 – 2.5

Instructor: Scott Kristjanson

CMPT 125/125

SFU Burnaby, Fall 2013

Page 5: Reviewing for the Midterm Covers chapters 1 to 5, 7 to 9 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Wk07.5 Slide 5Slides based on Java Foundations 3rd Edition, Lewis/DePasquale/Chase

5

Scott Kristjanson – CMPT 125/126 – SFU

Ch 1&2 - Key Things to take away:

• The print and println methods are two services provided by the System.out object• In Java, the + operator is used both for addition and for string concatenation• An escape character can be used to represent a character that would otherwise cause a

compile error• A variable is a name for a memory location used to hold a value of a particular data type• Accessing data leaves them intact in memory, but an assignment statement overwrites old

data• One cannot assign a value of one type to a variable of an incompatible type• Constants hold a particular value for the duration of their existence• Java has two types of numeric values: integer and floating point. There are four integer data

types and two floating point data types• Java using 16-bit Unicode character set to represent character data• Expressions are combinations of operators and operands used to perform a calculation• The type of result produced by arithmetic division depends on the types of the operands• Java follows a well-defined set of precedence rules that governs the order in which

operators will be evaluated in an expression• Narrowing conversions should be avoided because they can lose information

Page 6: Reviewing for the Midterm Covers chapters 1 to 5, 7 to 9 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Wk02.3 Slides:Using Classes and ObjectsChapters 3Creating Objects – Section 3.1The String Class – Section 3.2The Scanner Class – Section 2.6

Instructor: Scott Kristjanson

CMPT 125/125

SFU Burnaby, Fall 2013

Page 7: Reviewing for the Midterm Covers chapters 1 to 5, 7 to 9 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Wk07.5 Slide 7Slides based on Java Foundations 3rd Edition, Lewis/DePasquale/Chase

7

Scott Kristjanson – CMPT 125/126 – SFU

Key Things to take away:

• Object declarations create place holders which point to an object in memory

• The new operator instantiates a new instance of the class• Strings are immutable – changing a string creates a new instance

• A variable holds either a primitive type or a reference to an object

• Assigning variables of primitive types copies the value

• Assigning variables of class types copies a reference to the object

• The String class provides useful methods for working with strings• length• concat• substring• toUpperCase• Etc

• The System.in object represents the standard input stream• The Scanner Class provides methods for reading input values

Page 8: Reviewing for the Midterm Covers chapters 1 to 5, 7 to 9 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Wk02.5 Slides:Using Classes and ObjectsChapters 3Section 3.3 Packages Section 3.4 Random Class Section 3.5 Math ClassSection 3.7 Enumerated Types

Instructor: Scott Kristjanson

CMPT 125/125

SFU Burnaby, Fall 2013

Page 9: Reviewing for the Midterm Covers chapters 1 to 5, 7 to 9 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Wk07.5 Slide 9Slides based on Java Foundations 3rd Edition, Lewis/DePasquale/Chase

9

Scott Kristjanson – CMPT 125/126 – SFU

Key Things to take away:

• The Java API contains standard set of class definitions

• Class definitions can be reused by importing packages

• Packages exist for creating random numbers, math, and formatting

• You can create your own set of libraries as a package

• Java provides wrapper classes for primitive data types so they can be used just like any other object

Page 10: Reviewing for the Midterm Covers chapters 1 to 5, 7 to 9 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Wk03.1 SlidesConditionals and LoopsChapter 4

Instructor: Scott Kristjanson

CMPT 125/125

SFU Burnaby, Fall 2013

Page 11: Reviewing for the Midterm Covers chapters 1 to 5, 7 to 9 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Wk07.5 Slide 11Slides based on Java Foundations 3rd Edition, Lewis/DePasquale/Chase

11

Scott Kristjanson – CMPT 125/126 – SFU

Key Things to take away:

• Flow of Control determines which statements get executed• Expressions can form complex conditions using logical operators• AND and OR evaluation are short-circuited in Java• Selection statements chose different execution paths based on conditions

• If <condition> then <statement>;• If <condition> then <statement1> else <statement2>;• Switch <integer value> {Case 1, Case 2, … Case N}

• Java supports two styles of Indefinite Loops:• While <condition> <statement>;• Do <statement> while <condition>;

• Java suports two styles of definite Loops:• for ( initialization ; condition ; increment ) <statement>;• For-each using Iterators

Page 12: Reviewing for the Midterm Covers chapters 1 to 5, 7 to 9 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Wk04.1 - Wk05.5 Slides:Writing ClassesChapter 5

Instructor: Scott Kristjanson

CMPT 125/125

SFU Burnaby, Fall 2013

Page 13: Reviewing for the Midterm Covers chapters 1 to 5, 7 to 9 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Wk07.5 Slide 13Slides based on Java Foundations 3rd Edition, Lewis/DePasquale/Chase

13

Scott Kristjanson – CMPT 125/126 – SFU

Key Things to take away:

Class and Method Design• Data Flow Diagrams help get a design started• Problem Specification: Nouns become Classes, Verbs become Methods• Decompose methods that get too complex

Class and Method Coding• Methods and Variables have Scope – where they can be seen• Public Scope allows access by users of the Class• Private Scope used to encapsulate data and internal helper methods• Invoking a method copies the actual parameters into the formal parameters• Object Parameters contain references to the actual object and can be modified

Testing• Methodical Testing is important step, every program has bugs• Testing can be black box (functional) or white box (implementation)• Four types of tests: Unit, Integration, System, Regression• Println’s and the Eclipse Debugger are essential tools for debugging

Page 14: Reviewing for the Midterm Covers chapters 1 to 5, 7 to 9 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Wk04.3 Slides:ReviewChapters 1 to 4

Instructor: Scott Kristjanson

CMPT 125/125

SFU Burnaby, Fall 2013

Page 15: Reviewing for the Midterm Covers chapters 1 to 5, 7 to 9 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Wk07.5 Slide 15Slides based on Java Foundations 3rd Edition, Lewis/DePasquale/Chase

15

Scott Kristjanson – CMPT 125/126 – SFU

Key Things to take away:

• Flow of Control determines which statements get executed• Expressions can form complex conditions using logical operators• AND and OR evaluation are short-circuited in Java• Selection statements chose different execution paths based on conditions

• If <condition> then <statement>;• If <condition> then <statement1> else <statement2>;• Switch <integer value> {Case 1, Case 2, … Case N}

• Java supports two styles of Indefinite Loops:• While <condition> <statement>;• Do <statement> while <condition>;

• Java suports two styles of definite Loops:• for ( initialization ; condition ; increment ) <statement>;• For-each using Iterators

Page 16: Reviewing for the Midterm Covers chapters 1 to 5, 7 to 9 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Wk05.5 Slides:Quiz #1Solutions

Instructor: Scott Kristjanson

CMPT 125/125

SFU Burnaby, Fall 2013

Page 17: Reviewing for the Midterm Covers chapters 1 to 5, 7 to 9 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Wk07.5 Slide 17Slides based on Java Foundations 3rd Edition, Lewis/DePasquale/Chase

17

Scott Kristjanson – CMPT 125/126 – SFU

Question 3

Work out the logic one step at a time: • True when A is false => !A• Either B or C are true => B || C• AND => both the above must be true • => !A && B || CBut remember to check precedence!• => !A && B || C is evaluated as • =>(!A && B)|| C That’s not right!Need to add parenthesis • => !A && (B || C)

!A && (B || C)

A B C !A B||C !A && (B || C)

F F F T F F

F F T T T T

F T F T T T

F T T T T T

T F F F F F

T F T F T F

T T F F T F

T T T F T F

Validate using a Truth Table:

Page 18: Reviewing for the Midterm Covers chapters 1 to 5, 7 to 9 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Wk07.5 Slide 18Slides based on Java Foundations 3rd Edition, Lewis/DePasquale/Chase

18

Scott Kristjanson – CMPT 125/126 – SFU

Question 4

For full marks, • Do not go through the loop 9 times, execute the loop 5 times• Realize that you can increment counter by 2 within the loop, not by 1 twice• Declare the loop variable within the for loop

If you do not remember HOW to write a Java for loop…• Use Appendix J – it was attached at the back!

for (int i=1; i <= 9; i += 2){ System.out.println(i); }

Page 19: Reviewing for the Midterm Covers chapters 1 to 5, 7 to 9 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Wk07.5 Slide 19Slides based on Java Foundations 3rd Edition, Lewis/DePasquale/Chase

19

Scott Kristjanson – CMPT 125/126 – SFU

Question 5

x

x

x

Follow the flow of control:•(Sum > MAX) is true since (100 > 10)• So must call Print1, not Print2• No {} around else, Print3 not part of the if• So Print3 must get printed as well• Print3 is not doing addition, so not 110 • Print3 doing String Concatenation• So will print “100”+”10” => “10010”

x

x

Page 20: Reviewing for the Midterm Covers chapters 1 to 5, 7 to 9 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Wk06.3 Slides:Arrays Chapter 7

Instructor: Scott Kristjanson

CMPT 125/125

SFU Burnaby, Fall 2013

Page 21: Reviewing for the Midterm Covers chapters 1 to 5, 7 to 9 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Wk07.5 Slide 21Slides based on Java Foundations 3rd Edition, Lewis/DePasquale/Chase

21

Scott Kristjanson – CMPT 125/126 – SFU

Key Things to take away:

Arrays are an ordered collection on Array Elements

Array Elements may be Primitive Data Types, or Objects

Arrays are objects

Arrays cannot be extended once created

Command Line Parameters are passed as an Array of Strings

Variable Length Parameter lists passed as an Array of Objects

Array Elements may be other Arrays

Arrays may be one, two, or multidimensional

Page 22: Reviewing for the Midterm Covers chapters 1 to 5, 7 to 9 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Wk06.5 Slides:Inheritance Chapter 8

Instructor: Scott Kristjanson

CMPT 125/125

SFU Burnaby, Fall 2013

Page 23: Reviewing for the Midterm Covers chapters 1 to 5, 7 to 9 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Wk07.5 Slide 23Slides based on Java Foundations 3rd Edition, Lewis/DePasquale/Chase

23

Scott Kristjanson – CMPT 125/126 – SFU

Key Things to take away:

Inheritance

• Inheritance is the process of deriving a new class from an existing class• Inheritance allows software reuse of existing software• Inheritance creates an IS-A relationship between parent and child classes• Protected visibility provides encapsulation and allows inheritance• A parents constructor can and should by invoked by the child via super• A child class can override a parents methods by redefining them• A child of one class can be the parent of another• All Java classes are derived from the Object class• An Abstract class cannot be instantiated, it represents a concept• Abstract classes must be declared in the child before it can be instantiated• Private members of a class and not be seen directly in the child class• The final modifier can be used to restrict inheritance

Page 24: Reviewing for the Midterm Covers chapters 1 to 5, 7 to 9 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Wk07.3 Slides:Polymorphism Chapter 9

Instructor: Scott Kristjanson

CMPT 125/125

SFU Burnaby, Fall 2013

Page 25: Reviewing for the Midterm Covers chapters 1 to 5, 7 to 9 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Wk07.5 Slide 25Slides based on Java Foundations 3rd Edition, Lewis/DePasquale/Chase

25

Scott Kristjanson – CMPT 125/126 – SFU

Key Things to take away:

Polymorphism

• The term polymorphism literally means “having many forms”• A polymorphic reference is a variable that can refer to different types of

objects at different points in time• The method invoked through a polymorphic reference can change from

one invocation to the next• It is the type of the object being referenced, not the reference type, that

determines which method is invoked• Interfaces allow true encapsulation and hide implementation details• Interfaces are critical for supporting GUIs and other functions cleanly

Page 26: Reviewing for the Midterm Covers chapters 1 to 5, 7 to 9 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Wk07.5 Slide 26Slides based on Java Foundations 3rd Edition, Lewis/DePasquale/Chase

26

Scott Kristjanson – CMPT 125/126 – SFU

Review the Quiz Solutions and Assignments

Try to do the Quiz again• It should be now be much easier now. See how long it takes you.• Review the solutions and compare to your answers.• Look at the kind of problems – midterm will be similar

Assignments 2 and 3:• Review the questions – midterm will be similar• There will be programming questions

Page 27: Reviewing for the Midterm Covers chapters 1 to 5, 7 to 9 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Wk07.5 Slide 27Slides based on Java Foundations 3rd Edition, Lewis/DePasquale/Chase

27

Scott Kristjanson – CMPT 125/126 – SFU

Review Assignment 2 Solutions

Part A – Written Question Solutions• a2WriteUp• Operator Precedence • Generating Random Numbers • Definite Loops • Indefinite Loops

Part B – Programming Solutions• RandomAvg.java • AvgNums.java • Distance.java • MoneyChanger.java • MoneyCounter.java

Page 28: Reviewing for the Midterm Covers chapters 1 to 5, 7 to 9 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Wk07.5 Slide 28Slides based on Java Foundations 3rd Edition, Lewis/DePasquale/Chase

28

Scott Kristjanson – CMPT 125/126 – SFU

Review Assignment 3 Questions

Review the Parts A and B Written Questions

Work on the Written Questions (Part A is due Monday in Lab).

Review Part C Programming Problems• Try to solve questions 1 to 4 before the Midterm Exam if you can

Page 29: Reviewing for the Midterm Covers chapters 1 to 5, 7 to 9 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Wk07.5 Slide 29Slides based on Java Foundations 3rd Edition, Lewis/DePasquale/Chase

29

Scott Kristjanson – CMPT 125/126 – SFU

Practice your Programming Skills

Sign up to CodingBat.com and work through the examples.

It will give you immediate feedback and track your progress.

Be sure that you know how to:• write boolean expressions and if-then-else conditions• know how to concatenate, compare, and manipulate Strings• write for, while, and do loops• index into arrays, pass arrays as parameters, 1 and 2 dimensions

Page 30: Reviewing for the Midterm Covers chapters 1 to 5, 7 to 9 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Wk07.5 Slide 30Slides based on Java Foundations 3rd Edition, Lewis/DePasquale/Chase

30

Scott Kristjanson – CMPT 125/126 – SFU

Key things you should know for the midterm:

You should be able to:• Understand flow of control and the importance of braces• Describe the various phases of software testing• Write boolean expressions using correct operator precedence• Write programs using if-then-else, for, while, do, switch• know how to concatenate, compare, and manipulate Strings• Understand how Primitive Data and Objects parameters work• Work with Arrays and indexing of Arrays, arrays of objects• Explain elements of good design including coupling and cohesion• Be able to create a data flow diagram based on a problem description• Understand the principles of inheritance and polymorphism• Understand how dynamic-binding of methods enables polymorphism• Describe what an Abstract class is• Describe what an Interface is used for• Understand the function of modifiers public, private, static, and final• Be able to use the super and this keywords within methods