cs211sp08-02

cs211sp08-02 - Classes making the manufacturer It's now...

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

View Full Document Right Arrow Icon
CS 211 Java structure Classes . .. making the manufacturer. It’s now time to turn our attention to the manufacturer of all these reference objects. As an example . .. public class BankAcc { private Foat balance ; // data ±eld to hold values for each BankAcc manufactured public Foat getBalance ( ) { // accessor method return balance ; } public void setBalance ( Foat bal ) { // mutator method balance = bal ; } public Foat spend ( Foat amt ) { // methods to do stuff balance -= amt ; } public Foat deposit ( Foat amt ) { balance += amt ; } public BankAcc ( ) { // default constructor balance = 0.0 ; } public BankAcc ( Foat amt ) { // another constructor balance = amt ; } } // end class BankAcc So then, how does this get used? 1 can’t access except via methods actually in the manufacturer class -- it’s a visibility modiFer can access via ANY object of ‘type’ BankAcc but any use of data Felds grabs only the values for that actual incarnation of the object no return type same name as the class name public class GRQ { public static void main ( String [ ] args ) { BankAcc owen = new BankAcc ( ) ; owen.deposit ( 5000.75 ) ; System.out.println ( “Owen has $” + owen.getBalance ( ) ) ; BankAcc feit = new BankAcc ( 2000.96 ) ; feit.spend ( 3000.50 ) ; System.out.println ( “²eit has $” + feit.getBalance ( ) ) ; } // end main method } // end class GRQ owen can’t get feit’s money, and v.v. In general it’s a very good idea to default to making as many of the data Felds private as possible, and use accessor/mutator (settor/gettor) methods to control access to them - classes
Background image of page 1

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

View Full DocumentRight Arrow Icon
CS 211 Java structure We can enhance the previous class . .. public class BankAcc { // static felds ‘belong’ to the manuFacturer, not the made object, so only one copy oF it is created, but each object can share it private static int numAccs = 0 ; // data feld to hold CLASS values, initialised at compile time when the class is loaded private static int [ ] accNos = new int [ 100000 ] ; // data feld to hold CLASS array For account numbers private static ±oat totalAssets = 0.0 ; // to hold total assets oF accounts, iF it weren’t private then could get by BankAcc.totalAssets private ±oat balance ; // data feld to hold values For each instantiation static { // this is (surprisingly?) a method run at compile time (hence no name and no input parentheses!) For ( int i = 0 ; i < accNos.length ; i++ ) { accNos [ i ] = 100001 + i ; } // end For loop initialising account numbers } // end static compile-time method public static int getBankAssets ( ) { // this method can be called in main (For example) by BankAcc.getBankAssets ( ) ; return totalAssets ; } public ±oat getBalance ( ) { // accessor method - perhaps should ask For authorisation? return balance ;
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.

This note was uploaded on 03/04/2008 for the course CS 2110 taught by Professor Francis during the Spring '07 term at Cornell.

Page1 / 14

cs211sp08-02 - Classes making the manufacturer It's now...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online