L10 - CompSci 101 Lecture 10 1 Computer Science 1 1 SS C...

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

View Full Document Right Arrow Icon

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

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: CompSci 101 Lecture 10 1 Computer Science 1 1 SS C Lecture 10 Contents More String instance methods boolean variables boolean expressions if statements if … else … statements Coursebook: §6 , §11 2 1 2 3 4 5 Review a "mbc" b mmbc "m" 11010110 1100100 11010110 1100100 11010110 a = new String("mbc"); b = a; a = b.substring(0, 1); System.out.println(a + b); String a, b; 3 1 2 3 4 5 Review - static methods We have seen static methods: Classname • methodName ( list of parameters ) ; int num3 = (int)( Math . random () * 3); int num1 = Math . max ( 23, 21 ) ; int num2 = Math . min ( 2, 5 ) ; String s = Keyboard . readInput () ; int number = Integer . parseInt ( s ) ; 4 1 2 3 4 5 6 Review - instance methods We have also seen instance methods: objectVariable • methodName ( list of values ) String word1 = "Welcome"; int size1 = word1 . length () ; String word2 = word1 . substring ( 3, size1-1 ) ; int size2 = word2 . length () ; char c1 = word2 . charAt ( 2 ) ; int pos = word1 . indexOf ( 'e' ) ; word2 "com" size2 is 3 c1 is 'm' pos is 1 size1 is 7 5 1 2 3 4 5 6 Other String instance methods happy HAPPY IMagINE imagine toLowerCase() toUpperCase() String word1 = "happy"; String word2 = word1 . toUpperCase (); System.out.println(word1 + " " + word2); String word3 = "IMagINE"; String word4 = word3 . toLowerCase (); System.out.println(word3 + " " + word4); 6 1 2 3 4 5 6 7 Other String instance methods trim() String word1 = " over the moon "; System.out.println("***" + word1 + "***"); int length1 = word1.length(); word1 = word1 .trim(); System.out.println(length1 + ", " + length2); System.out.println("***" + word1 + "***"); ***over the moon*** 17, 13 *** over the moon *** int length2 = word1.length(); CompSci 101 Lecture 10 2 7 1100000 1100111 word1 1 2 3 4 5 Strings Applying an instance method to a String which changes the String in some way, always creates a new String object. " sing " " SING " 1101100 "SING" 1100111 1101100 1100000 String word1 = " sing "; word1 = word1. toUpperCase (); System.out.println("***" + word1 + "***"); word1 = word1. trim (); System.out.println("***" + word1 + "***"); *** SING *** ***SING*** 8 1 2 3 4 5 6 7 8 9 10 11 12 13 Review Methods and parameters – what is the output? public class MyProgram { public void start() { 1. raokeKa 2. keKarao String word = s.substring(pos) + s.substring(0, pos); return word; String word = "Karaoke"; word = methodA(word, 2); private String methodA(String s, int pos) { word = methodA(word, 3); } } } 2 "Karaoke" 3 "raokeKa" System.out.println("1. " + word); System.out.println("2. " + word); 9 George Boole George Boole (1815-1964) invented Boolean algebra ....
View Full Document

{[ snackBarMessage ]}

Page1 / 6

L10 - CompSci 101 Lecture 10 1 Computer Science 1 1 SS C...

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