Transcript wk07.5

Reviewing for the Midterm
Covers chapters 1 to 5, 7 to 9
Instructor: Scott Kristjanson
CMPT 125/125
SFU Burnaby, Fall 2013
Things to Review
2
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
Scott Kristjanson – CMPT 125/126 – SFU
Slides based on Java Foundations 3rd Edition, Lewis/DePasquale/Chase
Wk07.5 Slide 2
Review the Class Slides
3
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
Scott Kristjanson – CMPT 125/126 – SFU
Slides based on Java Foundations 3rd Edition, Lewis/DePasquale/Chase
Wk07.5 Slide 3
Wk02.1 Slides:
Introduction to Java
Chapters 1 and 2
The Java Language – Section 1.1
Data & Expressions – Sections 2.1 – 2.5
Instructor: Scott Kristjanson
CMPT 125/125
SFU Burnaby, Fall 2013
Ch 1&2 - Key Things to take away:
5
• 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
Scott Kristjanson – CMPT 125/126 – SFU
Slides based on Java Foundations 3rd Edition, Lewis/DePasquale/Chase
Wk07.5 Slide 5
Wk02.3 Slides:
Using Classes and Objects
Chapters 3
Creating Objects
– Section 3.1
The String Class
– Section 3.2
The Scanner Class – Section 2.6
Instructor: Scott Kristjanson
CMPT 125/125
SFU Burnaby, Fall 2013
Key Things to take away:
7
• 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
Scott Kristjanson – CMPT 125/126 – SFU
Slides based on Java Foundations 3rd Edition, Lewis/DePasquale/Chase
Wk07.5 Slide 7
Wk02.5 Slides:
Using Classes and Objects
Chapters 3
Section 3.3
Section 3.4
Section 3.5
Section 3.7
Packages
Random Class
Math Class
Enumerated Types
Instructor: Scott Kristjanson
CMPT 125/125
SFU Burnaby, Fall 2013
Key Things to take away:
9
•
•
•
•
•
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
Scott Kristjanson – CMPT 125/126 – SFU
Slides based on Java Foundations 3rd Edition, Lewis/DePasquale/Chase
Wk07.5 Slide 9
Wk03.1 Slides
Conditionals and Loops
Chapter 4
Instructor: Scott Kristjanson
CMPT 125/125
SFU Burnaby, Fall 2013
Key Things to take away:
11
•
•
•
•
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
Scott Kristjanson – CMPT 125/126 – SFU
Slides based on Java Foundations 3rd Edition, Lewis/DePasquale/Chase
Wk07.5 Slide 11
Wk04.1 - Wk05.5 Slides:
Writing Classes
Chapter 5
Instructor: Scott Kristjanson
CMPT 125/125
SFU Burnaby, Fall 2013
Key Things to take away:
13
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
Scott Kristjanson – CMPT 125/126 – SFU
Slides based on Java Foundations 3rd Edition, Lewis/DePasquale/Chase
Wk07.5 Slide 13
Wk04.3 Slides:
Review
Chapters 1 to 4
Instructor: Scott Kristjanson
CMPT 125/125
SFU Burnaby, Fall 2013
Key Things to take away:
15
•
•
•
•
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
Scott Kristjanson – CMPT 125/126 – SFU
Slides based on Java Foundations 3rd Edition, Lewis/DePasquale/Chase
Wk07.5 Slide 15
Wk05.5 Slides:
Quiz #1
Solutions
Instructor: Scott Kristjanson
CMPT 125/125
SFU Burnaby, Fall 2013
Question 3
17
!A && (B || C)
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 || C
But remember to check precedence!
•
=> !A && B || C is evaluated as
•
=>(!A && B)|| C
That’s not right!
Need to add parenthesis
•
=> !A && (B || C)
Validate using a Truth Table:
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
Scott Kristjanson – CMPT 125/126 – SFU
Slides based on Java Foundations 3rd Edition, Lewis/DePasquale/Chase
Wk07.5 Slide 17
Question 4
18
for (int i=1; i <= 9; i += 2)
{ System.out.println(i); }
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!
Scott Kristjanson – CMPT 125/126 – SFU
Slides based on Java Foundations 3rd Edition, Lewis/DePasquale/Chase
Wk07.5 Slide 18
Question 5
19
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
x
x
Scott Kristjanson – CMPT 125/126 – SFU
Slides based on Java Foundations 3rd Edition, Lewis/DePasquale/Chase
Wk07.5 Slide 19
Wk06.3 Slides:
Arrays
Chapter 7
Instructor: Scott Kristjanson
CMPT 125/125
SFU Burnaby, Fall 2013
Key Things to take away:
21
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
Scott Kristjanson – CMPT 125/126 – SFU
Slides based on Java Foundations 3rd Edition, Lewis/DePasquale/Chase
Wk07.5 Slide 21
Wk06.5 Slides:
Inheritance
Chapter 8
Instructor: Scott Kristjanson
CMPT 125/125
SFU Burnaby, Fall 2013
Key Things to take away:
23
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
Scott Kristjanson – CMPT 125/126 – SFU
Slides based on Java Foundations 3rd Edition, Lewis/DePasquale/Chase
Wk07.5 Slide 23
Wk07.3 Slides:
Polymorphism
Chapter 9
Instructor: Scott Kristjanson
CMPT 125/125
SFU Burnaby, Fall 2013
Key Things to take away:
25
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
Scott Kristjanson – CMPT 125/126 – SFU
Slides based on Java Foundations 3rd Edition, Lewis/DePasquale/Chase
Wk07.5 Slide 25
Review the Quiz Solutions and Assignments
26
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
Scott Kristjanson – CMPT 125/126 – SFU
Slides based on Java Foundations 3rd Edition, Lewis/DePasquale/Chase
Wk07.5 Slide 26
Review Assignment 2 Solutions
27
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
Scott Kristjanson – CMPT 125/126 – SFU
Slides based on Java Foundations 3rd Edition, Lewis/DePasquale/Chase
Wk07.5 Slide 27
Review Assignment 3 Questions
28
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
Scott Kristjanson – CMPT 125/126 – SFU
Slides based on Java Foundations 3rd Edition, Lewis/DePasquale/Chase
Wk07.5 Slide 28
Practice your Programming Skills
29
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
Scott Kristjanson – CMPT 125/126 – SFU
Slides based on Java Foundations 3rd Edition, Lewis/DePasquale/Chase
Wk07.5 Slide 29
Key things you should know for the midterm:
30
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
Scott Kristjanson – CMPT 125/126 – SFU
Slides based on Java Foundations 3rd Edition, Lewis/DePasquale/Chase
Wk07.5 Slide 30