DOC PREVIEW
Duke CPS 006 - Announcements

This preview shows page 1-2-3 out of 9 pages.

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

Unformatted text preview:

AnnouncementsObjects, Classes, MethodsExample - class ChickenMethod FeaturesConstructing Objects - newPrimitive TypesAnother Class - StringObject ReferencesAssign: one = two;CompSci 63.1AnnouncementsRead Chap 3 for next time Homework will be postedConsulting Hours will start soonCompSci 63.2Objects, Classes, MethodsClasses define the state (data), usually privatebehavior (methods) for an object, usually publicMany objects can be created based on one class.Method – sequence of instructions that access and/or manipulates the data of an objectAccessor method – access, don’t change dataMutator method – changes the dataCompSci 63.3Example - class ChickenState weight, height, nameBehavior (methods)Accessor methodsogetWeight(), getHeight(), getName()Mutator methods oeat() – adds weight, adds some height if under 12.0osick() – lose weight ochangeName()Syntax Note: in code method name always followed by parenthesesCompSci 63.4Method FeaturesReturn values Methods can return informationoAccessor methods require thatoHave return type in header specifying type of infooUse: w = chick.getWeight();ParametersMethods may receive information thru parametersoMutator methods usually require that oMethod header includes parameter definition in parenthesesoUse: chick.newName("Elsa");May have both parameters and return valuesCompSci 63.5Constructing Objects - newThe new operatorUsed to create objectsCreate three chickens“Fred”, weight 2.0, height 3.8“Sallie Mae”, weight 3.0, height 4.5“Momma”, weight 6.0, height 8.3Use Chicken constructorChicken one = new Chicken(2.0, 3.8, "Fred");Chicken two = new Chicken(3.0, 4.5, "Sallie Mae");Chicken three = new Chicken(6.0, 8.3, "Momma");CompSci 63.6Primitive TypesJava builds in primitive types for dealing with numbersEight primitive types; only use few at firsto int - holds whole numbers onlyo double – can deal with fractionsthink scientific notationThese are not objectsoThey have no methodsConstants:o int – no decimal point: 256o double – contains decimal point: 3.14159265CompSci 63.7Another Class - StringUse the API methods for String classTo get to API from CompSci 6 web pageoClick on “resources”Print length of Chicken one’s name.length() is a method in String class.System.out.println(one.getName() + " has " + one.getName().length() + " letters.");CompSci 63.8Object ReferencesVariable of type object – value is memory locationCompSci 63.9Assign: one = two;Now they reference the same


View Full Document

Duke CPS 006 - Announcements

Download Announcements
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 Announcements 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 Announcements 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?