Review String Class String name String addr object
Review: String Class String name String addr object type = "Paige Turner"; = "45 Library Lane"; variables objects A String object holds a string ● String methods are called on String objects using dot notation. ● name. length() addr. to. Upper. Case() -> -> 12 "45 LIBRARY LANE" The characters in a String are each internally numbered with an index, starting with 0 ● index: char: 0 P 1 a 2 i 3 g 4 e 5 ' ' 6 T 7 u 8 r 9 n 10 e 11 r
Commonly used String methods: Method str 1. length() str 1. char. At(index) str 1. index. Of(arg) str 1. equal(str 2) str 1. compare. To(str 2) str 1. starts. With(str 2) str 1. substring(begin. Index, end. Index) str 1. to. Upper. Case() Description number of characters in str 1 get character at a specific index find starting index of argument char or substring do str 1 and str 2 match? ordering str 1 vs. str 2 {negative, 0, positive} does str 1 start with str 2 extract a substring [begin, end) create an upper case version of str 1
Midterm: The exam is closed book, but you are allowed a 1 page crib sheet of notes, front and back. Expect questions like the Zybooks activities: understanding small bits of code and writing small bits of code.
Midterm Review of key ideas: • Java always starts at main() • The computer just executes statements in order. • For output use: System. out. print() System. out. println() • For input use: Scanner scnr = new Scanner(System. in); int val = scnr. next. Int(); also. next. Double(). next. Line()
• Variables correspond to locations in memory. • Assignment statements calculate a value and store it in a variable average = (a + b + c) / 3; • Operators: + - * / % • The normal rules of order of operations are followed. (precedence) • Assignment operators: += -= *= /= %= • The Math class contains useful math methods: hypotenuse = Math. sqrt(a * a + b * b); • Primitive types: int long double boolean char • integer range: ± 2 billion • long range: ± 9 quintillion • doubles: 0. 001 6. 022 e 23 16 digits of precision • Round off errors: some numbers can’t be represented exactly • Type casting: ratio = (double)width / height;
• booleans: true false • Comparison operators: == != < <= > • Logical operators: && || ! • Short circuit evaluation of logical expressions >= • Character constants use single quotes: char c = 'A'; • Character class has useful character methods if ( Character. is. Letter(ch) ) • String constants use double quotes: String str = "Java is fun"; • Strings are objects and have methods that operate upon them len = str. length(); 11 • Character position numbering for strings starts at 0 String first. Word = str. substring(0, 4); gets chars [0, 4) • Use. equals to compare strings if (str 1. equals(str 2)) • Strings are immutable. String operations create new strings.
• The Random class creates random numbers. Random rand = new Random(); int number = rand. next. Int(100); // range [0, 100) • Graphics: Drawing. Panel panel = new Drawing. Panel(400, 400); Graphics 2 D g = panel. get. Graphics(); g. draw. Line(50, 100, 100); g. fill. Oval(250, 50, 50); g. set. Color(Color. RED); g. draw. String("Drawing is fun!", 50); • if / else statement: if (gpa >= 3. 0) { System. out. println("Welcome to USM"); } else { System. out. println("Your application is denied. "); }
• switch statement: switch (place) { case 1: System. out. println("You won the gold medal!"); break; case 2: System. out. println("You won the silver medal!"); break; case 3: System. out. println("You won the bronze medal!"); break; default: System. out. println("Thanks for participating. "); }
Java Style Guidelines: • Choose meaningful variable names: count, index, usage. Hours. Per. Year • Add spaces around operators: c = Math. sqrt(a * a + b * b); • Indent to show code structure: if (score > best. Score) { System. out. println(“you set a new record!”); best. Score = score; } • Avoid long lines that go past the end of a line: if ((search. Name. length() > 0) && (search. Name. char. At(0) != '. ')) { search. Name = ". " + search. Name; }
Zybooks’ debugging suggestions: • • Manually set a variable to a value. Insert print statements to observe variable values. Comment out unused code. Visually inspect the code (not every test requires modifying/running the code). My suggestions: • Understand what is wrong. • Don’t make random changes and hope it works. • Learn to use a program debugger.
Eclipse demo of debugger on: Zybooks Participation Activity 3. 15. 2 Domain Name Checking
- Slides: 12