• No results found

ObjectsAndThings.pdf

N/A
N/A
Protected

Academic year: 2020

Share "ObjectsAndThings.pdf"

Copied!
36
0
0

Loading.... (view fulltext now)

Full text

(1)

Objects and Things

(2)

2

Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671

Agenda

✦ Announcements

✦ Grade Reports and Midterms ✦ Review Lab

✦ Break

✦ Exception Handling

(3)

Review Java Syntax (Lab)

Create a new project called Review

Create a package called edu.aubih.reviewCreate a new class in the package called

HelloWorld

Write HelloWorld program from scratchWrite Circle class from scratch

(4)

4

Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671

HelloWorld

HelloWorld program should

output Hello World! when invokeddefine the Hello World message in a

(5)

Circle

Circle program should

declare radius as instance variable (private)create getters and setters for the radius

define a no-arg constructor that sets the

radius to 1.0

define a constructor that accepts a double as

input and set the radius to that value

(6)

6

Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671

Statistics

Statistics program should

calculateMean(double[] array) as class

method

calculateMedian(double[] array) as class

method

calculateMax(double[] array) as class

method

(7)

Test Your Code

Verify that your code worksRun sample data

Try to break it

Use the main method to test each of your

objects

(8)

8

Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671

Exception Handling

Why exception handling important?

quality software demands itsecure software demands it

runtime errors cause program to terminate

abnormally

(9)

Runtime error example (p. 626)

Write a program that accepts two integers as

input (Quotient.java)

the program should divide the two numbers

and display the result to the user

After testing it, see what happens if you use

(10)

10

Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671

Runtime error example

java.lang.ArithmeticException

These error messages confuse usersThey also can result in more serious

problems – security, etc

(11)

Prevent the Error (p. 626-627)

Adjust Quotient.java to prevent the divide

by zero error.

This is a perfectly acceptable solution

In some cases, however, it is not so simple

to prevent errors.

For those cases, Java provides exception

(12)

12

Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671

Handle the Error

try{

// code to execute }

catch(type ex){ // exception we handle // code to handle exception

(13)

Handle the Error

Rewrite the divide program with Java try

catch block (p. 627)

If you have time, rewrite the divide program

(14)

14

Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671

Exception Handling Advantages

Exception handling enables a method to

throw an exception to its caller.

Without this capability, a method must

(15)

Common Exceptions

ClassNotFoundException

use a class that does not exist

IOException

– input/output problem: file does not exist

ArithmeticException

divide by zero

NullPointerException

(16)

16

Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671

Finally...

Sometimes you want code to execute

regardless of whether an exception occurs or not

finally{ } block accomplishes this...

Often used to clean up resources

close file handles

(17)

Objects and Things

Additional Keywords: this, protectedEncapsulation

Inheritance

Overriding vs. OverloadingPolymorphism

(18)

18

Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671

This keyword

How does an object refer to itself?

this

Common uses

reference a class's hidden data fields

invoke another constructor of the same class

Example:

(19)

Design and Test Course Class

Write the programs to model a class on

pages 343 and 342

The Course class models the process of

registering a student in a class

Review the following concepts:

state and behavior

(20)

20

Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671

Inheritance

Inheritance allows new classes to be derived

from existing classes

Every class you define in Java is inherited

from an existing class

Think of shapes: square and circleThey have many things in common

Can we use these common features to

(21)

Inheritance Concepts

subclass: a subclass inherits accessible

fields and methods from its superclass

subclass also called: child class, extended

class, derived class

(22)

22

Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671

Modeling Geometric Objects

Write code on pp. 359 – 362.

Note that the keyword “extends” defines an

inheritance relationship

public class Circle4 extends GeometricObject1

• Therefore, if we want a class to be derived from another, we simply extend it in the class definition

(23)

Overriding vs. Overloading

Overloading methods: methods with the

same name but different signatures

Overriding methods: methods in a subclass

with the same method signature as methods in a superclass

A method override provides different

(24)

24

Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671

Polymorphism

a variable of a supertype can refer to a

subtype object

an object of a subclass can be used

whenever its superclass is required

The JVM uses dynamic binding to

determine which implementation will be used

Dynamic binding allows new classes to be

(25)

Polymorphism Example

Write the code on page 369-370

Notice that we can invoke m with any type

(26)

26

Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671

Protected data and methods

protected is a visibility modifier like public

and private

protected resources can be accessed by any

class in the same package or its subclasses

Three visibility modifiers

public

(27)

Another use for final

If you want methods or classes not to be

extended, use the final modifier

public final class C will prevent extensions

to this class

public final void m() will prevent the m

(28)

28

Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671

Abstraction

Abstract class cannot have any specific

instances

Abstract classes are like regular classes

except that you cannot create an instance with the new operator

Abstract methods represent behavior that is

only appropriate for subclasses

(29)

Abstraction Example

Write new version of GeometricObject,

Circle, Rectangle, and

(30)

30

Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671

Interfaces

An interface is a classlike construct that

contains only constants and methods.

Interfaces specify the behavior that a class

must implement (contract)

class Chicken extends Animal implements Edible

implements keyword denotes using

(31)

Interface Example

Write code on page 397 – 398 to

demonstrate how interfaces work

If you want to implement features similar to

(32)

32

Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671

Interfaces vs. Abstractions

Interfaces

variables must be public static finalno constructors

all methods are public instance methods

Abstraction

all variables permitted

(33)

Class Design and Modeling

Association

Students take coursesFaculty teach courses

Aggregation – has-a relationship

Composition – exclusive has-a relationshipDependency – one class uses another

(34)

34

Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671

Class Design and Modeling

Cohesion: classes should describe single

entities and class operations should fit together logically

Consistency: choose names consistently,

follow a standard

Encapsulation: data protection

(35)

Class Design and Modeling

Completeness: classes can be configured for

many purposes

Instance vs. Static

Inheritance vs. Aggregation: prefer

composition whenever possible

(36)

36

Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671

Design Patterns

Design patterns are proven sound strategies

for designing classes.

Help to arrange classes into meaningful

software solutions.

Exercise: Write singleton pattern on page

442 – 443.

References

Related documents

Bighead carp and silver carp (Invader Species – Asian Carp) invaded the Illinois River waterway over a decade ago.. Populations of these fishes have apparently grown dense in the

•Also includes other software packages and libraries commonly used in Python programming and data science.

Analyze: We are asked to determine the pH at the equivalence point of the titration of a weak acid with a strong base.. Because the neutralization of a weak acid produces its

• The travel speeds estimates from the local travel demand model are generally higher than all three data sources, especially for the mid-day period.. Common

In particular, this Article (1) catalogues and analyzes the nonsectoral data privacy, security, and breach notification statutes of all fifty states and the District of Columbia;

Liang, Introduc tion to J ava Programming, Tenth Edition, (c ) 2013 Pearson Educ ation,

There is clear and convincing evidence that the Respondent has violated Rule 8.4(c) of the MRPC by misappropriating payments received from his clients that represented [legal fees

Surface Finishing Materials manufacture and marketing of plating chemicals, procurement and marketing of industrial chemicals and non-ferrous metals Surface Finishing