// APS101, Winter 2009: Lecture 12 (Feb. 3) // // Review: last time we looked at static variables/methods, and constants. // Examples from the Math class: Math.PI Math.log10(5) Math.abs(-5) Math.round(5.6) Math.ceil(1.2) Math.floor(1.9) Math.max(10, 15) Math.max(10, 15.0) Math.max(10.0, 15) Math.max(1, 2, 3) Math.max(1, Math.max(2, 3)) Math.pow(2, 3) // what is a static variable? // what is a static method? // what is a constant? format: // public static final = BankAccount.getTotalNumAccounts() BankAccount.DEPOSIT_FEE BankAccount.BANK_NAME Band.NUM_MEMBERS Band.canFormBand() // false - not enough Musicians! Musician.totalNumMusicians Musician m1 = new Musician("LilWayne", 10); Musician.totalNumMusicians Musician m2 = new Musician("PDiddy", 100); Musician.totalNumMusicians Band.canFormBand() // still false. Musician m3 = new Musician("Gene Simmons", 1000); Musician.totalNumMusicians Band.canFormBand() // true! Band b = new Band(m1, m2, m3); b.canFormBand() // can call a static method from an object too // Today we'll be looking at various String methods: // length() => the number of characters in this String. "abc".length() String s = "abcd"; s.length() // charAt(int i) => the character at index i (starting from 0). "abc".charAt(1) s.charAt(10) // error! // indexOf(String s) => the index of s in this String; -1 if s is not found. s.indexOf("e") s.indexOf("ab") s.indexOf("abcd") s.indexOf("abcde") // indexOf(String s, int i) => the index of s in this String after index i; -1 if s is not found. String s = "aba" s.indexOf("a") s.indexOf("a", 1) s.indexOf("a", s.indexOf("a") + 1) // substring(int i, int j) => the letters between i (inclusive) and j (non-inclusive). String s = "aba" s.substring(0, 1) s.substring(0, 2) s.substring(0, 3) s.substring(0, 4) s.substring(1, 3) // substring(int i) => the letters starting from index i (inclusive) to the end. s.substring(1) // We started writing static method getFirstVowel(String) in class OurString // exercise: take a look at this class and think of a way to write this method, // without using any if-statements. // also, think of a way to incorporate constants: // Hint: public static final String VOWELS = "aeiou"