60 likes | 71 Views
This recap covers key ideas in word games, including defining and extending classes, using fields and methods, declaring variables, and working with expressions, statements, and conditionals.
E N D
Recap: Key ideas in WordGames • Defininga class • Extendinga class versus implementing an interface • Classes versus instances • The use of fields • Methods, parameters and local variables • Blocks and scope • Constructors • E.g., to initialize fields • Declaring variables • Expressions • Statements • Assignment • Conditionals • Blocks • Loops (later) We reviewed these ideas previously Now these ideas We will review these ideas in the next several slides
Declaring variables • We use names (also called variables) to refer to things: • myCheckBox homeURL mobyDick • Use the names to ask objects to do things: • myCheckBox.displayCheck() • Every name has a type that must be declared: • CheckBox myCheckBox; Book mobyDick; • double temperature; int weight; int age; boolean passed; JButton button1; double pi; • Exercise: Declare a variable appropriate for: • Storing a person’s age • Indicating whether or not Sonia passed her test • Referring to a JButton • Storing an approximation to PI Questions?
Expressions Exercise: Fill in the blanks. In the example below,there are: ___ literals ___ comparison expressions ___ boolean expressions ___ arithmetic expressions ___ assignment expressions • Every expression has a type and a value • Names (variables) and literals are simple expressions • Expressions can be combined by using operatorslike: • Arithmetic operators:+ –* / % • Comparison operators:< <= > >= == != • Logical operators:&& || ! 3 2 1 1 1 if ( (temperature > 101) && (bloodPressure > 140) ) { dosage = dosage + 30;} Questions?
Assignment statements int age; JButton button1;JButton button2;JButton button3; int weight; double force;double mass;double acceleration; Worm henry; • Exercises: Given the declarations to theright, write statements that: • Give age the value 12 age = 12; • Make button1 refer to the same JButton as button2 button1 = button2; • Make button3 refer to a new JButton button3 = new JButton(); • Increment weight by 10 weight = weight + 10; or equivalently: weight += 10; • Sets force appropriately (given mass and acceleration) force = mass * acceleration; • Makes henry wiggle henry.wiggle(); assuming the method’s name is wiggle Questions?
Conditional statements if (x < y) { min = x; } else { min = y; } • The statements executed depend on a condition • The condition is evaluated left-to-right and may be short-circuited • Note the indentation and curly-brace style in multiple cases if (score >= 90) { grade = ’A’; } else if (score >= 80) { grade = ’B’; } else { grade = ’C’; } if (naomi.winsRace()) { ++ count; } if ( (fido != null) && (fido.isAlive() ) { fido.bark(); } Questions?
Blocks • Use curly-braces to enclose blocks of code • Note the code convention for where to place the curly braces if (x < y) { min = x; max = y; } else { min = y; max = x; } Questions?