{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

lec8 - 1.00 Lecture 8 Classes continued Reading for next...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
1.00 Lecture 8 Classes, continued Reading for next time: Big Java: sections 7.9 Using An Existing Class, cont. From last time: BusRoute is a Java class used by the BusTransfer class BusTransfer uses BusRoute objects: First construct the objects and specify their initial state Constructors are special methods to construct and initialize objects They may take arguments (parameters) Then apply methods to the objects This is the same as “sending messages” to them to invoke their behaviors
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Constructor for BusRoute Object To construct a new BusRoute object, two things are required: Create the object (using its constructor) new BusRoute(1, 300, 80.0); // Use original example // ‘new’ allocates memory and calls constructor Give the object a name or identity: BusRoute bus1; // Object name (bus1) is a reference to the object // BusRoute is the data type of bus1 Combine these two things into a single step: BusRoute bus1= new BusRoute(1, 300, 80.0); We now have a BusRoute object containing the values: Route number 1 Number of passengers 300 Percent transferring 80.0 We can now apply methods to it. Using Methods Methods are invoked using the dot (.) operator Method always ends with parentheses BusRoute bus1= new BusRoute(1, 300, 80.0); BusRoute bus2= new BusRoute(47, 400, 30.0); int r1= bus1.getRteNumber(); // Dot operator int p2= bus2.getPassengers(); // Dot operator bus1.setPassengers(p2+100); // Dot operator Methods are usually public and can be invoked anywhere Data fields are also invoked with the dot (.) operator
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}