AnnouncementsObjects, Classes, MethodsExample - class ChickenMethod FeaturesConstructing Objects - newPrimitive TypesAnother Class - StringObject ReferencesAssign: one = two;CompSci 63.1AnnouncementsRead Chap 3 for next time Homework will be postedConsulting Hours will start soonCompSci 63.2Objects, Classes, MethodsClasses define the state (data), usually privatebehavior (methods) for an object, usually publicMany objects can be created based on one class.Method – sequence of instructions that access and/or manipulates the data of an objectAccessor method – access, don’t change dataMutator method – changes the dataCompSci 63.3Example - class ChickenState weight, height, nameBehavior (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 FeaturesReturn values Methods can return informationoAccessor methods require thatoHave return type in header specifying type of infooUse: w = chick.getWeight();ParametersMethods 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 - newThe new operatorUsed to create objectsCreate three chickens“Fred”, weight 2.0, height 3.8“Sallie Mae”, weight 3.0, height 4.5“Momma”, weight 6.0, height 8.3Use 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 TypesJava builds in primitive types for dealing with numbersEight primitive types; only use few at firsto int - holds whole numbers onlyo double – can deal with fractionsthink scientific notationThese are not objectsoThey have no methodsConstants:o int – no decimal point: 256o double – contains decimal point: 3.14159265CompSci 63.7Another Class - StringUse the API methods for String classTo 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 ReferencesVariable of type object – value is memory locationCompSci 63.9Assign: one = two;Now they reference the same
View Full Document