18
COMP 110: Introduction to Programming Tyler Johnson Feb 4, 2009 MWF 11:00AM-12:15PM Sitterson 014

COMP 110: Introduction to Programming Tyler Johnson Feb 4, 2009 MWF 11:00AM-12:15PM Sitterson 014

Embed Size (px)

Citation preview

Page 1: COMP 110: Introduction to Programming Tyler Johnson Feb 4, 2009 MWF 11:00AM-12:15PM Sitterson 014

COMP 110:Introduction to Programming

Tyler JohnsonFeb 4, 2009

MWF 11:00AM-12:15PMSitterson 014

Page 2: COMP 110: Introduction to Programming Tyler Johnson Feb 4, 2009 MWF 11:00AM-12:15PM Sitterson 014

COMP 110: Spring 20092

Announcements

Program 1 due tonight by midnight

Lab 2 due tomorrow by midnight

Lab 1 has been graded

Page 3: COMP 110: Introduction to Programming Tyler Johnson Feb 4, 2009 MWF 11:00AM-12:15PM Sitterson 014

COMP 110: Spring 20093

Questions?

Page 4: COMP 110: Introduction to Programming Tyler Johnson Feb 4, 2009 MWF 11:00AM-12:15PM Sitterson 014

COMP 110: Spring 20094

Today in COMP 110

Lab 1

In-Class Exercise

Floating-Point Numbers

Loops

Page 5: COMP 110: Introduction to Programming Tyler Johnson Feb 4, 2009 MWF 11:00AM-12:15PM Sitterson 014

COMP 110: Spring 20095

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, 3.14159

Floating-point numbers are approximate quantities and are not exact!

Page 6: COMP 110: Introduction to Programming Tyler Johnson Feb 4, 2009 MWF 11:00AM-12:15PM Sitterson 014

COMP 110: Spring 20096

Floating-Point Numbers

Consider the number 1/3 = 0.333333

Computers have finite storage and cannot store infinitely repeating decimals

In a computer, the number 1/3 is stored as 0.333333, with part of the fraction missing or truncated

Since 0.333333 is < 1/3, this representation is inexact

Page 7: COMP 110: Introduction to Programming Tyler Johnson Feb 4, 2009 MWF 11:00AM-12:15PM Sitterson 014

COMP 110: Spring 20097

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

Page 8: COMP 110: Introduction to Programming Tyler Johnson Feb 4, 2009 MWF 11:00AM-12:15PM Sitterson 014

COMP 110: Spring 20098

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

valueSystem.out.println(“a and b are the same!”);

elseSystem.out.println(“a and b are different!”);

Page 9: COMP 110: Introduction to Programming Tyler Johnson Feb 4, 2009 MWF 11:00AM-12:15PM Sitterson 014

COMP 110: Spring 20099

Loops

Programs often need to repeat some action

For example, when grading a test, the process of grading is repeated for each student

Page 10: COMP 110: Introduction to Programming Tyler Johnson Feb 4, 2009 MWF 11:00AM-12:15PM Sitterson 014

COMP 110: Spring 200910

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

Page 11: COMP 110: Introduction to Programming Tyler Johnson Feb 4, 2009 MWF 11:00AM-12:15PM Sitterson 014

COMP 110: Spring 200911

Loops

All loops must also have a stopping condition

Describes under what condition(s) iteration should cease

ExampleWhen the process of grading is completed for all students, the instructor will stop grading

Page 12: COMP 110: Introduction to Programming Tyler Johnson Feb 4, 2009 MWF 11:00AM-12:15PM Sitterson 014

COMP 110: Spring 200912

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

Page 13: COMP 110: Introduction to Programming Tyler Johnson Feb 4, 2009 MWF 11:00AM-12:15PM Sitterson 014

COMP 110: Spring 200913

While Statements

The while statement can be used to construct a loop in Java

while(Boolean_Expression) {Statement_1Statement_2…Statement_N

}

As long as Boolean_Expression is true, the statements in the loop body are executed

Page 14: COMP 110: Introduction to Programming Tyler Johnson Feb 4, 2009 MWF 11:00AM-12:15PM Sitterson 014

COMP 110: Spring 200914

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++;

}

Page 15: COMP 110: Introduction to Programming Tyler Johnson Feb 4, 2009 MWF 11:00AM-12:15PM Sitterson 014

COMP 110: Spring 200915

While Example

int number = 4;int count = 1;

while(count <= number) {System.out.print(count + ", ");count++;

}

Iteration

count Output

1 1 1,

2 2 1, 2,

3 3 1, 2, 3,

4 4 1, 2, 3, 4,

Page 16: COMP 110: Introduction to Programming Tyler Johnson Feb 4, 2009 MWF 11:00AM-12:15PM Sitterson 014

COMP 110: Spring 200916

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 inputStep 2: Check the inputStep 3: If the input is invalid, go to Step 1Step 4: Perform the selected operation

Page 17: COMP 110: Introduction to Programming Tyler Johnson Feb 4, 2009 MWF 11:00AM-12:15PM Sitterson 014

COMP 110: Spring 200917

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

Page 18: COMP 110: Introduction to Programming Tyler Johnson Feb 4, 2009 MWF 11:00AM-12:15PM Sitterson 014

COMP 110: Spring 200918

Friday

Recitation

BringLaptops (fully charged)Textbook