DOC PREVIEW
USF CS 112 - Intro to Programming II

This preview shows page 1-2-19-20 out of 20 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 20 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 20 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 20 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 20 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 20 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

{small lecturenumber - hepage :} Objects{small lecturenumber - hepage :} Objects{small lecturenumber - hepage :} Classes and objects{small lecturenumber - hepage :} An example{small lecturenumber - hepage :} An example{small lecturenumber - hepage :} An example{small lecturenumber - hepage :} An example{small lecturenumber - hepage :} An example{small lecturenumber - hepage :} Methods{small lecturenumber - hepage :} Data hiding{small lecturenumber - hepage :} Example{small lecturenumber - hepage :} Visibility modifiers{small lecturenumber - hepage :} Visibility modifiers{small lecturenumber - hepage :} Example{small lecturenumber - hepage :} Example{small lecturenumber - hepage :} Strings in Java{small lecturenumber - hepage :} Overloaded operators{small lecturenumber - hepage :} Overloaded operators{small lecturenumber - hepage :} ExampleIntro to Programming IIObjectsChris BrooksDepartment of Computer ScienceUniversity of San FranciscoDepartment of Computer Science — University of San Francisco – p.1/??2-2: Objects•Java is an object-oriented language.•So what the heck is an object anyway?Department of Computer Science — University of San Francisco – p.2/??2-3: Objects•An object is a type of abstraction•It provides a way of grouping together related data andfunctionality.•Makes it easier to organize and extend your program.•Also gives a “black box” effect.◦Users of your objects don’t need to worry about how theywork internally, just how to use them.Department of Computer Science — University of San Francisco – p.3/??2-4: Classes and objects•A class is a template or category•An object is a particular instance of that class.◦CartoonAnimals might be a class◦BugsBunny, Tweety are instances of that class•Classes let us specify behavior common to a set of objects.Department of Computer Science — University of San Francisco – p.4/??2-5: An example•Say we’re making a drawing program, and we need torepresent a collection of circles.•A circle has an x and y coordinate for its center, plus a radius.•We could do:c1xval = 3;c1yval = 4;c1radius = 10;c2xval = 5;c2yval = 2;c2radius = 6;...•(to be less messy, we could also store these in arrays)Department of Computer Science — University of San Francisco – p.5/??2-6: An example•This is not a good solution, though.•Why not?Department of Computer Science — University of San Francisco – p.6/??2-7: An example•This is not a good solution, though.•No grouping of a circle’s components.•Users need to know all about the internals of a circle.•What if we don’t know the number of circles in advance?Department of Computer Science — University of San Francisco – p.7/??2-8: An example•A better solution:public class circle {public int xval;public int yval;public int radius;}•We have encapsulated the center and radius information insidethe circle.Department of Computer Science — University of San Francisco – p.8/??2-9: An example•But we have two related variables representing the center.•Perhaps we should group them as well.public class point {public int xval;public int yval;}public class circle {public point center;public int radius;}Department of Computer Science — University of San Francisco – p.9/??2-10: Methods•As we know, classes also contain methods.•Methods are pieces of code that can be invoked on an object.•The allow us to encapsulate both state and behavior.Department of Computer Science — University of San Francisco – p.10/??2-11: Data hiding•It’s also important to protect instance data from outside users.•One way to do this is by providing accessors and mutators◦“setters and getters”•Rather than the user modifying your object’s data directly, theyuse a method to do it.◦Reduces error◦Hides implementation from the user.Department of Computer Science — University of San Francisco – p.11/??2-12: Example•For the following classes, add setter and getter methods foreach of the instance variables.public class point {public int xval;public int yval;}public class circle {public point center;public int radius;}Department of Computer Science — University of San Francisco – p.12/??2-13: Visibility modifiers•public and private are used to indicate who can access avariable or method.◦public: anyone can use it.◦private: only available within that object.Department of Computer Science — University of San Francisco – p.13/??2-14: Visibility modifiers•Instance variables should be made private unless there’s acompelling reason not to.◦Use accessors and mutators to access and change data•public methods are available for everyone to use.•private methods can be used only within the object.◦These are nice for “helper” methods that you don’t want auser to call.Department of Computer Science — University of San Francisco – p.14/??2-15: Example•Design a book class.•It should have instance variables for title, author, genre,publishers, copyright date. They should be strings.•It should also have getters and setters for each of these.Department of Computer Science — University of San Francisco – p.15/??2-16: Example•Now create a Name class. It should have two members:firstName and lastName.•Modify Book so that author is of type Name.•Since you used setters and getters, users of your book classwill never know!Department of Computer Science — University of San Francisco – p.16/??2-17: Strings in Java•Strings in Java are objects•This mean that they have a set of methods they respond to:◦compareTo(), equals()◦indexOf()◦length()◦replace()◦startsWith(), endsWith()◦etcDepartment of Computer Science — University of San Francisco – p.17/??2-18: Overloaded operators•Unlike most other objects, Strings also have special behaviorfor creating string literals and for concatenation.•String literals are strings where the value is known at compiletime:◦String s1 = “hello world”◦String s2 = “USF”◦String s3 = “I love Java”•We can create a string without calling new.Department of Computer Science — University of San Francisco – p.18/??2-19: Overloaded operators•We can also use the ’+’ symbol to concatenate strings.◦String s1 = “hello”◦String s2 = “world”◦String s3 = s1 + s2 // s3 = “hello world”•This is a phenomenon called overloading; an operator isredifined to provide different functionality.Department of Computer Science —


View Full Document

USF CS 112 - Intro to Programming II

Documents in this Course
Structs

Structs

4 pages

Trees

Trees

25 pages

Strings

Strings

27 pages

Queues

Queues

3 pages

Trees

Trees

24 pages

Arrays

Arrays

5 pages

ArrayList

ArrayList

24 pages

Stacks

Stacks

2 pages

Stacks

Stacks

8 pages

Trees

Trees

24 pages

Stacks

Stacks

8 pages

Queues

Queues

16 pages

Queues

Queues

17 pages

Queues

Queues

17 pages

Load more
Download Intro to Programming II
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 Intro to Programming II 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 Intro to Programming II 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?