


































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!



































CS 2110 Java structure
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
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
CS 2110 Java structure
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? totalAssets += balance ; balance += amt ; } public BankAcc ( float balance ) { // another constructor totalAssets += balance ; this.balance = balance ; numAccs++ ; } // note that this gets incremented each time a new account is created! public BankAcc ( ) { // default constructor this ( 0.0 ) ; } // note the use of this to refer to one of the constructor -- has to be first statement in the constructor } // end class BankAcc
CS 2110 Java structure
}
public class TextBook { String author , title , publisher ; int n , isbn , cryear ; Preface pre = new Preface ( ) ; Acknow ack = new Acknow ( ) ; Contents cont = new Contents ( ) ; Chapters [ ] chaps = new Chapters [ n ] ; Index indy = new Index ( ) ; Exercises trouble = new Exercises ( ) ;
} // end class TextBook
CS 2110 Java structure
CS 2110 Java structure
CS 2110 Java structure
Some other standard checked exceptions ...
These checked exceptions must be dealt with either by try/catch blocks within the method, or by having a try/catch arrangement higher up in one of the calling programs to catch the exception coupled with an appropriate throws statement in the method declaration(s) to throw the exception âupstairsâ.
CS 2110 Java structure
public class PingPong extends Thread { // from the java.sun.com thread tutorial private String word ; // the word to print private int delay ; // the delay in millisecs to pause public PingPong ( String parole , int pendant ) { this.word = parole ; this.delay = pendant ; } // end constructor public void run ( ) { // overriding Threadâs run ( ) method try { // since sleep can throw an InterruptedException for ( ; ; ) { // never stop (unless interrupted) !!!! System.out.print ( word + â â ) ; sleep ( delay ) ; } } catch ( InterruptedException ie ) { return ; } } // end overriding of the run ( ) method } // end class PingPong
public static void main ( String [ ] args ) { new PingPong ( âpingâ , 333 ). start ( ) ; new PingPong ( âPONGâ , 1000 ). start ( ) ; } // end main method
ping PONG ping ping ping PONG ping ping ping PONG ping
CS 2110 Java structure
public class RunPingPong implements Runnable { // from the âjava programming languageâ book private String word ; private int delay ; public RunPingPong ( String parole , int pendant ) { this.word = parole ; this.delay = pendant ; } // end constructor public void run ( ) { try { for ( ; ; ) { System.out.print ( word + â â ) ; Thread. sleep ( delay ) ; } // only exit from for loop is via an interrupt } catch ( InterruptedException ie ) { return ; } } // end implementing the run ( ) method } // end class RunPingPong
public static void main ( String [ ] args ) { Runnable little , bigger ; little = new RunPingPong ( âpingâ , 333 ) ; bigger = new RunPingPong ( âPONGâ , 1000 ) ; new Thread ( little ). start ( ) ; new Thread ( bigger ). start ( ) ; } // end main method
CS 2110 Java structure
CS 2110 Java structure
CS 2110 Data Structures
CS 2110 Data Structures
CS 2110 Data Structures
null null joining a linked list leaving a linked list
CS 2110 Data Structures