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

Info icon This 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 float balance ; // data field to hold values for each BankAcc manufactured public float getBalance ( ) { // accessor method return balance ; } public void setBalance ( float bal ) { // mutator method balance = bal ; } public float spend ( float amt ) { // methods to do stuff balance -= amt ; } public float deposit ( float amt ) { balance += amt ; } public BankAcc ( ) { // default constructor balance = 0.0 ; } public BankAcc ( float 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 modifier can access via ANY object of ‘type’ BankAcc but any use of data fields 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 ( “Feit 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 fields private as possible, and use accessor/mutator (settor/gettor) methods to control access to them - classes
Image of page 1

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

View Full Document Right Arrow Icon
CS 211 Java structure We can enhance the previous class ... public class BankAcc { // static fields ‘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 field to hold CLASS values, initialised at compile time when the class is loaded private static int [ ] accNos = new int [ 100000 ] ; // data field to hold CLASS array for account numbers private static float totalAssets = 0.0 ; // to hold total assets of accounts, if it weren’t private then could get by BankAcc.totalAssets private float balance ; // data field 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 float getBalance ( ) { // accessor method - perhaps should ask for authorisation? return balance ; } public void setBalance ( float balance ) { // mutator method - who should authorise this? totalAssets += balance; this.balance = balance ; } public float spend ( float amt ) { // methods to do stuff - should authenticate user? boolean bounce = ( balance < amt ) ; // bounce only exists within this method balance -= !bounce ? amt : 0.0 ; if (bounce) System.out.println ( “Sorry, not enough money there at the moment (: ” ) ; else totalAssets -= amt ; } public float deposit ( float amt ) { // perhaps should offer a receipt?
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 ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern