chapter3

chapter3 - Chapter 3 Implementing Classes Introduction In...

Info iconThis preview shows pages 1–17. Sign up to view the full content.

View Full Document Right Arrow Icon
Chapter 3 – Implementing Classes
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Introduction • In the previous chapter, we saw how to use objects – Declare an object Book aBook; – Create an object aBook = new Book(“Beloved”,“Toni Morrison”); – Call methods on an object aBook.getAuthor();
Background image of page 2
Introduction • Remember our goal is to write programs that accomplish something useful – How do objects fit in? • Encapsulation • Efficiency – We can also make our own types of objects…
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Example Program public class Example { public static void main(String args[]) { Path p = new Path(3,4); System.out.println(“The distance is ” + p.calcDistance() + “ miles and the” + “ angle is ” + p.calcAngle()); } }
Background image of page 4
Example Program • This is great, except… there is no Path class in the Java API • The program would be easy if we had that type of object… • So we’ll just write a Path class ourselves
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Black Boxes • What is a black box? • Why do we call it a black box? • What are some instances of a black box?
Background image of page 6
Black Boxes • A black box works magically – we press a button and something happens • We don’t know how it actually works Encapsulation : the hiding of unimportant details
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Black Boxes • Not everything is hidden in a black box – We can input some numbers – There is some sort of output • There are ways to interact with it, but it’s all done on the outside – Interaction possibilities are well defined
Background image of page 8
Encapsulation • What is the right concept for each particular black box? • Concepts are discovered through abstraction Abstraction : taking away inessential features, until only the essence of the concept remains – How much does a user really need to know?
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Example: Cars • Black boxes in a car: transmission, electronic control module, etc
Background image of page 10
Example: Car • Users of a car do not need to understand how black boxes work • Interaction of a black box with outside world is well-defined – Drivers interact with car using pedals, buttons, etc. – Mechanic can test that engine control module sends the right firing signals to the spark plugs – For engine control module manufacturers, transistors and capacitors are black boxes magically produced by an electronics component manufacturer
Background image of page 11

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Example: Car • Encapsulation leads to efficiency: – Mechanic deals only with car components (e.g. electronic control module), not with sensors and transistors – Driver worries only about interaction with car (e.g. putting gas in the tank), not about motor or electronic control module
Background image of page 12
Example: Doorbell • What can we do to a doorbell? • What output do we get from a doorbell • Do we actually know how a doorbell works? • Do we need to?
Background image of page 13

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Encapsulation •In Object-oriented programming (OOP) the black boxes we use in our programs are objects String is a black box Rectangle is a black box
Background image of page 14
Software Design
Background image of page 15

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Encapsulation • Old times: computer programs only manipulated primitive types such as numbers and characters • Gradually programs became more complex, vastly increasing the amount of detail a programmer had to remember and maintain
Background image of page 16
Image of page 17
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 98

chapter3 - Chapter 3 Implementing Classes Introduction In...

This preview shows document pages 1 - 17. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online