Download presentation
Presentation is loading. Please wait.
Published byOsborne Stanley Modified over 9 years ago
1
Fundamentals of Software Development ISlide 1 Recap: Key ideas in WordGames Defining a classDefining a class Extending a class versus implementing an interfaceExtending a class versus implementing an interface Classes versus instancesClasses versus instances The use of fieldsThe use of fields Methods, parameters and local variablesMethods, parameters and local variables Blocks and scopeBlocks and scope ConstructorsConstructors –E.g., to initialize fields We will review these ideas in the next several slides Declaring variablesDeclaring variables ExpressionsExpressions StatementsStatements –Assignment –Conditionals –Blocks –Loops (later) We reviewed these ideas previously Now these ideas
2
Fundamentals of Software Development ISlide 2 Declaring variables We use names (also called variables) to refer to things:We use names (also called variables) to refer to things: –myCheckBox homeURL mobyDick Use the names to ask objects to do things:Use the names to ask objects to do things: –myCheckBox.displayCheck() Every name has a type that must be declared:Every name has a type that must be declared: –CheckBox myCheckBox;Book mobyDick; –double temperature;int weight; 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 int age; boolean passed; JButton button1; double pi; Questions?
3
Fundamentals of Software Development ISlide 3 Expressions Every expression has a type and a valueEvery expression has a type and a value Names (variables) and literals are simple expressionsNames (variables) and literals are simple expressions Expressions can be combined by using operators like:Expressions can be combined by using operators like: –Arithmetic operators: + – * / % –Comparison operators: >= == != –Logical operators: && || ! if ( (temperature > 101) && (bloodPressure > 140) ) { dosage = dosage + 30; } Exercise: Fill in the blanks. In the example below, there are: ___ literals ___ comparison expressions ___ boolean expressions ___ arithmetic expressions ___ assignment expressions 3 2 1 1 1 Questions?
4
Fundamentals of Software Development ISlide 4 Assignment statements Exercises: Given the declarations to the right, write statements that:Exercises: Given the declarations to the right, 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 int age; JButton button1; JButton button2; JButton button3; int weight; double force; double mass; double acceleration; Worm henry; Questions?
5
Fundamentals of Software Development ISlide 5 Conditional statements The statements executed depend on a conditionThe 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 (x < y) { min = x; min = x; } else { min = y; min = y;} if (naomi.winsRace()) { ++ count; ++ count;} if ( (fido != null) && (fido.isAlive() ) { fido.bark(); fido.bark();} if (score >= 90) { grade = ’A’; grade = ’A’; } else if (score >= 80) { grade = ’B’; grade = ’B’; } else { grade = ’C’; grade = ’C’;} Questions?
6
Fundamentals of Software Development ISlide 6 Blocks Use curly-braces to enclose blocks of codeUse curly-braces to enclose blocks of code –Note the code convention for where to place the curly braces if (x < y) { min = x; min = x; max = y; max = y; } else { min = y; min = y; max = x; max = x;} Questions?
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.