programming in java unit 4. learning outcome: lo2: be able to design java solutions lo3: be able...

10
Programming in Java Unit 4

Upload: jane-lee

Post on 16-Jan-2016

212 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Programming in Java Unit 4. Learning outcome:  LO2: Be able to design Java solutions  LO3: Be able to implement Java solutions Assessment criteria:

Programming in JavaUnit 4

Page 2: Programming in Java Unit 4. Learning outcome:  LO2: Be able to design Java solutions  LO3: Be able to implement Java solutions Assessment criteria:

• Learning outcome: LO2: Be able to design Java solutions LO3: Be able to implement Java solutions

• Assessment criteria: AC2.1: Design a Java programming solution to a given problem AC2.2: Explain the components and data and file structures required to implement a given

design AC3.3: Implement object behaviors using control structures to meet the design algorithms

• Learning objectives

•  After studying this unit, you should be able to: Understand the essentials of counter controlled repetition Understand relational operators Use if and if…else to choose alternative actions Use while statement to execute statements in a program repeatedly Use the for and do…while statements to execute statements in a program repeatedly Use the switch statement for multiple selection Use break and continue statements to alter the flow of control Use logical operators to perform complex conditional expressions

Page 3: Programming in Java Unit 4. Learning outcome:  LO2: Be able to design Java solutions  LO3: Be able to implement Java solutions Assessment criteria:

Control structures• Sequence, selection and repetition structures

• Sequence: The order in which you type the statements in a program, will be executed sequentially from top to bottom, unless you use some of the selection and repetition structures to have execution move to other parts of the program.

• Selection: Allows you to specify code to be executed depending on the outcome of a test. (If, if-else, switch statements)

• Repetition: Allows you to repeat certain parts of code dependent the value of a counter. (while, while-do, for)

Page 4: Programming in Java Unit 4. Learning outcome:  LO2: Be able to design Java solutions  LO3: Be able to implement Java solutions Assessment criteria:

Single selection

• Will execute only if the value of grade is larger or equal to 60.

• Otherwise that line of code will be skipped during execution.

Page 5: Programming in Java Unit 4. Learning outcome:  LO2: Be able to design Java solutions  LO3: Be able to implement Java solutions Assessment criteria:

Double selection

• Will test the value of grade. Since grade is set to 50, the test evaluates to false, and execution moves to the else part.

Page 6: Programming in Java Unit 4. Learning outcome:  LO2: Be able to design Java solutions  LO3: Be able to implement Java solutions Assessment criteria:

Counter controlled repetition • The following are the elements that are needed in order to perform

counter controlled repetition. A control variable (or loop counter) The initial value of the control variable The increment (or decrement) by which the control variable is modified each

time through the loop (also known as each iteration of the loop) The loop-continuation condition that determines if looping should continue.

Page 7: Programming in Java Unit 4. Learning outcome:  LO2: Be able to design Java solutions  LO3: Be able to implement Java solutions Assessment criteria:

for repetition statement • This specifies the counter controlled repetition details in a single line

of code.

• General structure: for ( initialization; loopContinuationCondition; increment ){

//statements

}

Page 8: Programming in Java Unit 4. Learning outcome:  LO2: Be able to design Java solutions  LO3: Be able to implement Java solutions Assessment criteria:
Page 9: Programming in Java Unit 4. Learning outcome:  LO2: Be able to design Java solutions  LO3: Be able to implement Java solutions Assessment criteria:

While Repetition statement initialization;

while ( loopContinuationCondition ) {

statement

increment;

}

Page 10: Programming in Java Unit 4. Learning outcome:  LO2: Be able to design Java solutions  LO3: Be able to implement Java solutions Assessment criteria:

Test your knowledge • Attempt Self review questions and exercises from your e-book, pages

114 - 120.

• Toolbox Lab exercises: 13, 14 Tutorial: 5, 6, 7, 8, 9.