DOC PREVIEW
UW-Madison CS 302 - Implementing Classes

This preview shows page 1-2-3-4-5-6-7-46-47-48-49-50-51-92-93-94-95-96-97-98 out of 98 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 98 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 98 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 98 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 98 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 98 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 98 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 98 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 98 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 98 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 98 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 98 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 98 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 98 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 98 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 98 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 98 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 98 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 98 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 98 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 98 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 98 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Chapter 3 Implementing Classes 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 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 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 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 Black Boxes What is a black box Why do we call it a black box What are some instances of a black box 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 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 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 Example Cars Black boxes in a car transmission electronic control module etc 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 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 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 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 Software Design 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 Encapsulation Solution Encapsulate routine computations to software black boxes Abstraction used to invent higher level data types In object oriented programming objects are black boxes Encapsulation Encapsulation Programmer using an object knows about its behavior but not about its internal structure In software design you can design good and bad abstractions objects understanding what makes good design is an important part of the education of a software engineer Abstraction Who designs objects you use Other programmers What do these objects contain Other objects Lesson Multiple levels of abstraction Almost always you will be designing an abstraction while simultaneously using another one Review Until now you ve only used objects Analogous to engineer who puts final parts together in car Now you will learn how to design objects Analogous to designing the parts of the car This is a complex process Designing a Class Recall that a class is a template for objects It s the cookie cutter not the cookies You need to decide What does the black box need to do methods What does the black box need to know data Designing a Class Remember you are designing a class that another programmer could use Make it easy to understand Design your class as a black box Methods The first thing a class requires is a list of methods what should the black box do For our Path example calcDistance calcAngle setX setY getX getY Which methods are accessors and which are mutators Methods access specifier eg public return type eg String or void method name eg deposit list of parameters eg New value for x method body in braces Methods Examples public double calcDistance public int getX public void setY int newY Methods Syntax accessSpecifier returnType methodName parameterType parameterName method body Example public double calcDistance Purpose To define the behavior of a method Methods Notes Methods always have parentheses The return type is what type of output the black box will give the user Parameters are what types of input the user needs to provide The method body is a sequence of instructions Constructors Recall that we need to be able to create objects in order to use them The set of instructions that does is this a constructor Note Constructor name Class name public Path body filled in later Constructors Constructor body is executed when a new object is created Statements in constructor body will set up the object so it can be used A constructor initializes all data members How does the compiler know which constructor to call Constructors vs Methods Constructors are a specialization of methods Goal to set the internal data of the object to a valid state 2 major differences All constructors are named after the class Therefore all constructors of a class have the same name No return type is EVER listed Constructors Syntax accessSpecifier ClassName parameterType parameterName constructor body Example public Path int x int y Purpose To define the behavior of a constructor Public Interface The public constructors and methods of a class form the public interface of the class public class Path data fields filled in later Constructors public Path body filled in later public Path int initX int initY body filled in later Methods public double calcDistance body filled in later public int getX body filled in later public void setY body filled in later more methods Class Definition Syntax accessSpecifier class ClassName fields constructors methods Example public class Path public Path int initY int initY public double calcDistance Purpose To define a class its public interface and its implementation details Review Public methods and constructors provide the public interface to a class They are how


View Full Document

UW-Madison CS 302 - Implementing Classes

Download Implementing Classes
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Implementing Classes and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Implementing Classes 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?