COMP 110: Introduction to Programming Tyler Johnson Feb 4, 2009 MWF 11:00AM-12:15PM Sitterson 014
COMP 110: Spring Announcements Program 1 due tonight by midnight Lab 2 due tomorrow by midnight Lab 1 has been graded
COMP 110: Spring Questions?
COMP 110: Spring Today in COMP 110 Lab 1 In-Class Exercise Floating-Point Numbers Loops
COMP 110: Spring Floating-Point Numbers Floating-point numbers (double, float) store real numbers in a computer Numbers with some fractional component such as 8.5, 99.9, Floating-point numbers are approximate quantities and are not exact!
COMP 110: Spring Floating-Point Numbers Consider the number 1/3 = Computers have finite storage and cannot store infinitely repeating decimals In a computer, the number 1/3 is stored as , with part of the fraction missing or truncated Since is < 1/3, this representation is inexact
COMP 110: Spring Floating-Point Numbers When using floating-point numbers, small errors will accumulate over time as operations are performed == tests whether two numbers are exactly equal You should never use == or != to compare floating-point numbers
COMP 110: Spring Floating-Point Comparison To compare two floating-point numbers, check if the difference is within some tolerance float a,b; final float EPSILON = 1e-6; //10^-6 … if(Math.abs(a – b) < EPSILON) //Math.abs() is absolute value System.out.println(a and b are the same!); else System.out.println(a and b are different!);
COMP 110: Spring Loops Programs often need to repeat some action For example, when grading a test, the process of grading is repeated for each student
COMP 110: Spring Loops A portion of a program that repeats some action is called a loop The statements that are repeated are called the loop body Each repetition of the loop body is called an iteration
COMP 110: Spring Loops All loops must also have a stopping condition Describes under what condition(s) iteration should cease Example When the process of grading is completed for all students, the instructor will stop grading
COMP 110: Spring Loop Example A loop that counts up to a certain number n Evaluate i <= n ? Execute Print i i = i + 1; Execute End Program Start i = 1 false true Loop Body Stopping Condition
COMP 110: Spring While Statements The while statement can be used to construct a loop in Java while(Boolean_Expression) { Statement_1 Statement_2 … Statement_N } As long as Boolean_Expression is true, the statements in the loop body are executed
COMP 110: Spring While Example A program that counts up to a certain number int number; … //number is set to some value, e.g. user input int count = 1; while(count <= number) { System.out.print(count + ", "); count++; }
COMP 110: Spring While Example int number = 4; int count = 1; while(count <= number) { System.out.print(count + ", "); count++; } IterationcountOutput 111, 221, 2, 331, 2, 3, 441, 2, 3, 4,
COMP 110: Spring Programming Demo How can we extend MenuCalculator.java to allow the user to re-enter input when the input is invalid? Step 1: Ask the user for input Step 2: Check the input Step 3: If the input is invalid, go to Step 1 Step 4: Perform the selected operation
COMP 110: Spring Programming Demo Evaluate Input Valid? Execute Present Menu Read Input Execute Perform User Selection Start true false Loop Body Stopping Condition Allowing the user to re-enter input
COMP 110: Spring Friday Recitation Bring Laptops (fully charged) Textbook