1 / 6

Recap: Key ideas in WordGames

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.

drodrigues
Download Presentation

Recap: Key ideas in WordGames

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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

  2. 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?

  3. 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?

  4. 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?

  5. 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?

  6. 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?

More Related