Question 1a) What is printed by the following Java program? int s;

Slides:



Advertisements
Similar presentations
Fundamental Programming Structures in Java: Control Flow, Arrays and Vectors.
Advertisements

Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.1 Chapter 3 Selections.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 5, 2005.
1 Repetition structures Overview while statement for statement do while statement.
Conditions What if?. Flow of Control The order of statement execution is called the flow of control Unless specified otherwise, the order of statement.
ECE122 L11: For loops and Arrays March 8, 2007 ECE 122 Engineering Problem Solving with Java Lecture 11 For Loops and Arrays.
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 2004.
SELECTION CSC 171 FALL 2004 LECTURE 8. Sequences start end.
Loops Chapter 4. It repeats a set of statements while a condition is true. while (condition) { execute these statements; } “while” structures.
Loops Chapter 4. It repeats a set of statements while a condition is true. while (condition) { execute these statements; } “while” structures.
COMP 110 Introduction to Programming Mr. Joshua Stough September 10, 2007.
COMP 110 Introduction to Programming Mr. Joshua Stough September 24, 2007.
Control Structures II. Why is Repetition Needed? There are many situations in which the same statements need to be executed several times. Example: Formulas.
Chapter 4: Control Structures II
General Features of Java Programming Language Variables and Data Types Operators Expressions Control Flow Statements.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Chapter 8: Collections: Arrays. 2 Objectives One-Dimensional Arrays Array Initialization The Arrays Class: Searching and Sorting Arrays as Arguments The.
Chapter 5 Loops.
1 Arrays An array is a collection of data values, all of which have the same type. The size of the array is fixed at creation. To refer to specific values.
October 28, 2015ICS102: For Loop1 The for-loop and Nested loops.
Copyright © 2012 Pearson Education, Inc. Chapter 6 More Conditionals and Loops Java Software Solutions Foundations of Program Design Seventh Edition John.
Chapter 4: Control Structures II
Introduction to Java Java Translation Program Structure
Chapter 5: Control Structures II
Question 1a) What is printed by the following Java program? int s; int r; int i; int [] x = {4, 8, 2, -9, 6}; s = 1; r = 0; i = x.length - 1; while (i.
ITI 1120 Lab #5 Contributors: S. Boyd, R. Plesa, A. Felty, D. Inkpen, A. Williams, D. Amyot.
CS 100Lecture 21 CS100J: Lecture 2 n Previous Lecture –Programming Concepts n problem, algorithm, program, computer, input, output, sequential execution,
Repetition. Loops Allows the same set of instructions to be used over and over again Starts with the keyword loop and ends with end loop. This will create.
Java iteration statements ● Iteration statements are statements which appear in the source code only once, but it execute many times. ● Such kind of statements.
Review TEST 2 Chapters 4,5,7. QUESTION For which type of operands does the == operator always work correctly: (a) int, (b) double, or (c) String?
ITI 1120 Lab #3 Tracing and Branching Contributors: G. Arbez, M. Eid, D. Inkpen, A. Williams, D. Amyot.
CONTROL STATEMENTS LOOPS. WHY IS REPETITION NEEDED?  There are many situations in which the same statements need to be executed several times.  Example:
How do you do the following? Find the number of scores within 3 points of the average of 10 scores? What kind of a tool do you need? Today’s notes: Include.
Application development with Java Lecture 6 Rina Zviel-Girshin.
Expressions Methods if else Statements Loops Potpourri.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Decisions and Iterations.
Chapter 5 – Part 3 Conditionals and Loops. © 2004 Pearson Addison-Wesley. All rights reserved2/19 Outline The if Statement and Conditions Other Conditional.
(Java Looping and Conditional Statements). Flow of control Sequential Executes instructions in order Method Calls Transfer control to the methods, then.
Introduction to Programming using Java Day 3 and 4 Java Language Basics Review The “For” loop Subroutines The “String” class.
Repetition Statements
Array in C# Array in C# RIHS Arshad Khan
CSC111 Quick Revision.
Chapter 4 Repetition Statements (loops)
Introduction to programming in java
Chapter 6 More Conditionals and Loops
Chapter 5: Control Structures II
Repetition-Counter control Loop
Repetition-Sentinel,Flag Loop/Do_While
Chapter 5: Control Structures II
SELECTION STATEMENTS (1)
Control Statement Examples
Chapter 6 More Conditionals and Loops
CSS161: Fundamentals of Computing
OPERATORS (2) CSC 111.
Outline Altering flow of control Boolean expressions
The for-loop and Nested loops
CSc 110, Spring 2017 Lecture 9: Advanced if/else; Cumulative sum
An Introduction to Java – Part I, language basics
Arrays We often want to organize objects or primitive data in a way that makes them easy to access and change. An array is simple but powerful way to.
AP Java Review If else.
3 Control Statements:.
OBJECT ORIENTED PROGRAMMING I LECTURE 11 GEORGE KOUTSOGIANNAKIS
Arrays October 6, 2006 ComS 207: Programming I (in Java)
Arrays in Java.
CS2011 Introduction to Programming I Selections (I)
AP Java Review If else.
Repetition Statements
CSC 1051 – Data Structures and Algorithms I
Chap 7. Advanced Control Statements in Java
CSS161: Fundamentals of Computing
Presentation transcript:

Question 1a) What is printed by the following Java program? int s; int r; int i; int [] x = {4, 8, 2, -9, 6}; s = 1; r = 0; i = x.length - 1; while (i > 0) { s = s * -1; i = i - 1; r = r + s * x[i]; } System.out.println(r); a) -13 b) 20 c) -9 d) -21 e) 11 f) 7

Trace of program x s r i Initial values ? x = {4, 8, 2, -9, 6} 1 r = 0 i = x.length - 1 4 while (i > 0): true s = s * -1 -1 i = i - 1 3 r = r + s * x[i] 9 2 11

Trace of program System.out.println(r) x s r i Values from previous table {4, 8, 2, -9, 6} 1 11 2 while (i > 0): true s = s * -1 -1 i = i - 1 r = r + s * x[i] 3 7 while (i > 0): false System.out.println(r)

Question 1a) What is printed by the following Java program? int s; int r; int i; int [] x = {4, 8, 2, -9, 6}; s = 1; r = 0; i = x.length - 1; while (i > 0) { s = s * -1; i = i - 1; r = r + s * x[i]; } System.out.println(r); a) -13 b) 20 c) -9 d) -21 e) 11 f) 7 r = -(-9) + 2 – 8 + 4

Question 1b) The array of integers "a" (type int) initially contains {1, 3, 8}. Only one of the following Boolean expressions evaluates to false. Circle the letter next to the expression that evaluates to false. a) (a[1] < 3) || ( (a[1] / 2) == 1) b) !( (a[0] % a[2]) == (a[0] % a[1]) ) c) !(!(false) && (a[2] < a[1]) ) d) Math.pow(a[1],2) > a[2] e) Math.abs(a[0] - a[1] + 2) <= 0

Question 1b) The array of integers "a" (type int) initially contains {1, 3, 8}. Only one of the following Boolean expressions evaluates to false. Circle the letter next to the expression that evaluates to false. a) (a[1] < 3) || ( (a[1] / 2) == 1) 3 3 F 1 T T

Question 1b) The array of integers "a" (type int) initially contains {1, 3, 8}. Only one of the following Boolean expressions evaluates to false. Circle the letter next to the expression that evaluates to false. b) !( (a[0] % a[2]) == (a[0] % a[1]) ) 1 8 1 3 1 1 T F

Question 1b) The array of integers "a" (type int) initially contains {1, 3, 8}. Only one of the following Boolean expressions evaluates to false. Circle the letter next to the expression that evaluates to false. c) !(!(false) && (a[2] < a[1]) ) 8 3 T F F T

Question 1b) The array of integers "a" (type int) initially contains {1, 3, 8}. Only one of the following Boolean expressions evaluates to false. Circle the letter next to the expression that evaluates to false. d) Math.pow(a[1],2) > a[2] 3 8 9 T

Question 1b) The array of integers "a" (type int) initially contains {1, 3, 8}. Only one of the following Boolean expressions evaluates to false. Circle the letter next to the expression that evaluates to false. e) Math.abs(a[0] - a[1] + 2) <= 0 1 3 -2 T

Question 1b) The array of integers "a" (type int) initially contains {1, 3, 8}. Only one of the following Boolean expressions evaluates to false. Circle the letter next to the expression that evaluates to false. a) (a[1] < 3) || ( (a[1] / 2) == 1) b) !( (a[0] % a[2]) == (a[0] % a[1]) ) c) !(!(false) && (a[2] < a[1]) ) d) Math.pow(a[1],2) > a[2] e) Math.abs(a[0] - a[1] + 2) <= 0

Question 1c) Suppose that X and Y are given integers; C and D are intermediates; A and B are results. What will the values of A and B at the end of this algorithm, if the initial values are X = 8 and Y = 3? C  0 D  X Y ≤ D ? false true D  D - Y C  C + 1 A  C B  D

Trace of algorithm X Y C D A B Initial values 8 3 ? C  0 D  X D  X Y ≤ D : true D  D – Y 5 C  C + 1 1 2 A  C B  D

This algorithm calculates A = X/Y and B = X%Y Question 1c) Suppose that X and Y are given integers; C and D are intermediates; A and B are results. What will the values of A and B at the end of this algorithm, if the initial values are X = 8 and Y = 3? C  0 D  X Y ≤ D ? false true D  D - Y C  C + 1 A  C B  D This algorithm calculates A = X/Y and B = X%Y A = 2, B = 2

Question 2a) X < Length Y ← 0 Y < Length Z ← Z + 1 X ← X + 1 while ( x < length ) { y = 0; if ( y < length ) y = y + 1; } else x = x + 1; z = z + 1; X < Length true false Y ← 0 Y < Length false true Z ← Z + 1 X ← X + 1 Y ← Y + 1 Briefly, this translation of an algorithm body diagram is incorrect because … A loop has been translated as an if-else statement.

Question 2a) X < Length Y ← 0 Y < Length Z ← Z + 1 X ← X + 1 while ( x < length ) { y = 0; while ( y < length ) y = y + 1; } x = x + 1; z = z + 1; X < Length true false Y ← 0 Y < Length false true Z ← Z + 1 X ← X + 1 Y ← Y + 1 Correct translation

Question 2b) X > 0 ? X ← X - 1 R ← A A < X? A ← A + 5 A ← A + 1 true false X ← X - 1 A < X? R ← A false true A ← A + 5 A ← A + 1 Briefly, this algorithm body diagram is incorrect because … A loop block always has the loop return to the test. This could not be translated to Java.

Question 2b) X > 0 ? X ← X - 1 R ← A A < X? A ← A + 5 A ← A + 1 true false X ← X - 1 A < X? R ← A false true A ← A + 5 A ← A + 1 Corrected

Question 3) Translate to Java GIVENS: X (array of integers) N (number of items in array X) V (a limit value) INTERMEDIATES: Index (index for array X going from 0 to N-1) Sum (sum of values in the array) RESULT: Exceeds (Boolean: True if Sum > V and false otherwise) HEADER: Exceeds  SumExceedsV( X, N, V )

Question 3 (first part) import java.io.* ; class exam2004Q3 { public static void main( String[] args ) throws IOException // DECLARE VARIABLES / DATA DICTIONARY int [] x; // GIVEN: array of integers int n; // GIVEN : number of items in the array x int v; // GIVEN : a limit value int index; // INTERMEDIATE: index for array x int sum; // INTERMEDIATE : sum of values in array x boolean exceeds; // RESULT: true if sum > v and false otherwise // READ IN GIVENS System.out.print( “Enter an array of integers: " ); x = CSI1100.readIntLine( ); n = x.length; System.out.print( “Enter a limit value: " ); v = CSI1100.readInt( );

Question 3 (second part) BODY: Index  0 Sum  0 Index < N AND Sum  V? true false Sum  Sum + X[Index] Index  Index + 1 Exceeds  (Sum > V)

Question 3 (second part) // BODY OF ALGORITHM index = 0; sum = 0; while( index < n && sum <= v ) { sum = sum + x[index]; index = index + 1; } exceeds = (sum > v); // PRINT OUT RESULTS AND MODIFIEDS System.out.println( "The result is: " + exceeds );

Question 4: Algorithm The Canadian income tax system has a series of tax brackets, where if a person’s annual income is greater than or equal to the lower limit for the tax bracket, and less than the upper limit, the person is contained within the tax bracket. Suppose that you are given the upper and lower limits for a single tax bracket for a specific year, and an array Income of length NumPersons where each entry is the annual income for some person. Write an algorithm to determine the percentage of people that are contained in the tax bracket.

Question 4) GIVENS: Lower (Tax bracket lower limit) Upper (Tax bracket upper limit) Income (Array of annual incomes) NumPersons (Size of array Income) INTERMEDIATES: Index (Index for the Income array) Count (Number of people in the tax bracket) RESULT: Percentage (Percentage of incomes within the tax bracket [0-100]) HEADER: Percentage  PctInBracket (Lower, Upper, Income, NumPersons)

Question 4) BODY: Index  0 Count  0 Index < NumPersons ? false true (Income[Index]  Lower) AND (Income[Index] < Upper) ? true false Count  Count + 1  Percentage  Count / NumPersons *100 Index  Index + 1