LAB Data Structures
LAB Data Structures CS 230
Popular in Course
Popular in ComputerScienence
This 4 page Class Notes was uploaded by Jordon Hermiston on Thursday October 29, 2015. The Class Notes belongs to CS 230 at Wellesley College taught by Brian Tjaden in Fall. Since its upload, it has received 10 views. For similar materials see /class/230928/cs-230-wellesley-college in ComputerScienence at Wellesley College.
Reviews for LAB Data Structures
Report this Material
What is Karma?
Karma is the currency of StudySoup.
Date Created: 10/29/15
51 Creating Classes 7 W A class can contain data declarations and method declarations Data declarations Method declarations 9 The values ofthe data de ne the state ofan object created 39om the class 0 The Jnctionality ofthe methods de ne the behaviors ofthe object 51 Identifying Classes and Objects i177 lt1 A class represents a group of objects with the same behaviors 9 Generally classes that represent objects should be given names that are s39ngular nouns Examples Com Student Message lt9 Sometimes it is challenging to decide whether something should be represented as a class Snuuld an employee s address be represerted s a set briristariee variables br as an Addxess bbjeet7 0 The more you examine the problem and its details the more clearthese issues become lt6 When a class becomes too comp e x It often should be decomposed into multiple smaller classes to distribute the responsibilities 51 Identifying Classes and Objects 477 16gt We want to de ne classes with the proper amount of detail ltall depends bri me detaiis uftne prbbiem being solved lt1 Part of identifying the classes we need is e process of assigning responsibilities to each class Every activity that a program must accomplish must be represented by one or more methods in one or more classes lt6 We generally use verbs for the names of methods 39 39 39 every class begin with primary responsibilities and evolve the design 52 Anatomy of a Class 0 Consider a sixsided die singular of dice I lLS state can be de ned as which face is showing its primary behavior istnat it can be rolled 0 We can represent a die in Java by designing a class called Dle that models this state and behavior 0 We39ll want to design the Dle class with other data and methods to make i a versatile and reusa e resource 0 Any given program will not necessarily use all aspects of a given class 9 Let s see how we would use someone39s class of the Die to play snakeyes diel Ej die2 Ej u Java Found die 1ingn s penansuau m m a a Imagannnueaefinea c1a ll Diejava amns Repxesenls one lax of dice win faces shwinq values hum l and SnakeEyesjava milit class SnakeEyes public class Die ll maximum face value pxivale final int mix nie nhjzcls an mus men evual limes stunning x cmxenl value shining on an die d f snake eyes hat n pxivale int facevalue ll caeau m H the numhex n 1 public static vnid nain 1suinql alqu 1 final int Imus 5nquot M mm mm M Z n H Cnnslxuclnx sets the initial face value nf mi die Die die new oieu Public Dian Die liez e new nieH fox int xn1l ml lt anus mum quotV h39 139 nnnl dielxn11H m2 autumn Cmnpules a new face value fax this die and xeunns the xesull if 1nmnl l u numZ 11 check 01 snake eyes 1 m quot11quot culmui facevalue e inLHMaULXandan a mm l Sys utmxintln 1 Ilulnhez nf 10115 unusp Syslmnutpxinlln 1 Ihnnhex nf snake eyes cannn quotmlquot mm Systemnlltpxinlln 1 Ratio nalbcnunl unusp 1 max II Fan value mulalnx The face value is not modified if the 52 UML Class Diagrams syecified value is n public vnid selFaceVallle Hut valIIED if 1valne gt n 121 value lt unxp r Newquot 1 1quot O A UML class diagram for the SnakeEyes program ll race value accessnx public int yeli acevaln 11 xeulxn faceValne Rzulzns a suing xepxesenlalinn of mi die public suing usuing11 lnuguusuing1faceValne1 suing xesull xeulxn xesull 53 Encapsulation 7 An encapsulated object can be thought of as a black box its inner workings are hidden 39om the client The client invokes the interface methods ofthe object which manages the instance data 53 Visibility Modifiers hi 0 In Java we accomplish encapsulation through the appropriate use of visibility modifiers 0 A modi er specifies particular characteristics of a method or data lt1 Java has three visibility modi ers publlc protected and prlvate 9 The protected modifier involves inheritance public private Variables 53 Reusing Classes 53 Program 1 using Coin Cninjava Java raahaataahs Repxesents a coin with two sides that can be flipped puhlic e1ass Coin pxivale instanchlaliEhIES as need ta know the II cunstxuctnx cxeates a new cnin public Coin it I Flips this sea public void 11 ii i netuhs use if the culenl fan nf this coin is heaas puhlic hnnlean asheads H l netmns the cement face of this man as a slxinq public String LnSinan Cnunll lipsjava Java raahaataahs puhlic class Cnunli lips Flips public static void main istnnql 31qu nn final int mm 1n iht head n tans Coin mycnin new caihii m uht cunnll count lt nrps cnunll mycninflir i is eads l l l if imycni headsi e1se La si Systemnutuht1h i Immh ex nf flips urysi Systemnlllpxinlln i ulnhex of he d5 heaasi Systemnlllpxinlln ulnhex of tans tans a coin multirle times and counts the n lmhel of heads and tails 53 Program 2 using Coin Flirkacejava Java Foundal puhlic class Flipkace t Flips um coins until one of them came I head thxee time in a low guhlic static void main tstxinyU nqst t in int sum a e n cm coin new cnintl cuinZ new Cninl while cmm lt sum as EMIan lt I3an t coin Lcnin 2 cninzl aunt in xesel the cumquot 1 toninlds eadstll a Cnllnlll n 1enin2isneadsm a cunntzu n if remnu lt sunny Systemnulylinlln cm 2 m r 15 if remM2 lt snnu Syslelnnlllpxinlln 1 cm 1 uh r else Systemnlllpxinlln 1 It s a Tml r 53 Mutator Restrictions Private data are controlled via accessors getters and mutators setters or mutators gives the class designer the ability to restrict a client s options to modify an obiect s state A mutator l5 often design ed so mat the values or variables can be set only Within particular llmltS For example the sereseevme mutator of he the class should have restricted the value to the valid range m to m Such rest tions can be implemented through tne use or an is staternent in tne body or tne constructor 54 Method Control Flow lt lfthe called method is in the same class only the method name is needed 0 The called method is often part of another class or object 0 Understanding the control flow is essential to debug do It helpMe gt gt obj doIt 0 helm 54 Driver Programs A driverprogram drives the use of other more interesting parts of a program Testcoln class could contain a mam method at drives the use ofthe Coln clas exercising its methods 0A th I personally preferto have the driver inside the class de nition as the class main method That way the testing becomes pan ofthe development c In any case you should be able to write the drivermainO before writing the class itself