Top Banner
COMP 14 Introduction to Programming Mr. Joshua Stough February 7, 2005 Monday/Wednesday 11:00- 12:15 Peabody Hall 218
31

COMP 14 Introduction to Programming Mr. Joshua Stough February 7, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.

Dec 20, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: COMP 14 Introduction to Programming Mr. Joshua Stough February 7, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.

COMP 14Introduction to Programming

Mr. Joshua StoughFebruary 7, 2005

Monday/Wednesday 11:00-12:15

Peabody Hall 218

Page 2: COMP 14 Introduction to Programming Mr. Joshua Stough February 7, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.

Announcements

• Homework 2 and Program 2 are due Today

Page 3: COMP 14 Introduction to Programming Mr. Joshua Stough February 7, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.

Review

• GUI I/O– JOptionPane.showInputDialog– JOptionPane.showMessageDialog

• File I/O– FileReader– FileWriter, PrintWriter

• String class methods– toUpperCase– toLowerCase

Page 4: COMP 14 Introduction to Programming Mr. Joshua Stough February 7, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.

ReviewJOptionPane Methods

• showInputDialogstr = JOptionPane.showInputDialog(strExpression);

• showMessageDialogJOptionPane.showMessageDialog(parentComponent,

strExpression, boxTitleString,

messageType);

Page 5: COMP 14 Introduction to Programming Mr. Joshua Stough February 7, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.

ReviewThe StringTokenizer Class• Default delimiters:

– space, tab, carriage return, new line

• Methods– StringTokenizer (String str)– StringTokenizer (String str, String delimits)

– String nextToken()– boolean hasMoreTokens()– int countTokens()

Page 6: COMP 14 Introduction to Programming Mr. Joshua Stough February 7, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.

ReviewReading From Text FilesString file = "data.dat";

BufferedReader inFile = new BufferedReader (new FileReader (file));

String line = inFile.readLine();

inFile.close();

Page 7: COMP 14 Introduction to Programming Mr. Joshua Stough February 7, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.

ReviewWriting To Text Files

String file = "outfile.dat";

PrintWriter outFile = new PrintWriter(new FileWriter (file));

outFile.print ("Hi");outFile.println(" There!");outFile.close();

Page 8: COMP 14 Introduction to Programming Mr. Joshua Stough February 7, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.

Today in COMP 14

• Control Structures• Relational Operators• Comparing Strings• Boolean Expressions

• Textbook Ref: Ch 4 (pgs. 148-164)

Page 9: COMP 14 Introduction to Programming Mr. Joshua Stough February 7, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.

Control Structures

Three methods of processing a program:1. In sequence

– statements are executed one after another in order

2. Branching– altering the flow of program execution by

making a selection or choice

3. Looping– altering the flow of program execution by

repetition of statement(s)

Page 10: COMP 14 Introduction to Programming Mr. Joshua Stough February 7, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.

Flow of Execution

Page 11: COMP 14 Introduction to Programming Mr. Joshua Stough February 7, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.

Examples of Conditionals

Note: the following are not actual Java statements

1) if (score is greater than or equal to 90)grade is A

2) if (hours worked are less than or equal to 40)wages = rate * hours

otherwisewages = (rate * 40) + 1.5 * (rate * (hours - 40))

3) if (temperature is greater than 70 degrees and it is not raining)

recommended activity is golfing

Page 12: COMP 14 Introduction to Programming Mr. Joshua Stough February 7, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.

Relational Operators

• Relational Operator– allows you to make comparisons in a

program– binary operator

• needs two operands

• Condition is represented by a logical (Boolean) expression– expression that has a value of either true or

false

Page 13: COMP 14 Introduction to Programming Mr. Joshua Stough February 7, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.

Relational Operators

• Less than <• Greater than >• Equal to ==

– not assignment ‘=‘

• Not equal to !=• Less than or equal to <=• Greater than or equal to >=

Page 14: COMP 14 Introduction to Programming Mr. Joshua Stough February 7, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.

Comparing Characters• In Java, characters are ordered according to the

Unicode / ASCII character set (pg. 855)

• ‘a’ comes before ‘b’ in the character set, so we can say ‘a’ < ‘b’

• Order– space character (' ')– digits (‘0’, ‘1’, …)– uppercase– lowercase

Page 15: COMP 14 Introduction to Programming Mr. Joshua Stough February 7, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.

Comparing Characters

• 'a' > 'A'

• '6' < 7

• ' ' <= 's'

97 > 65 true

54 < 7 false

32 <= 115 true

Page 16: COMP 14 Introduction to Programming Mr. Joshua Stough February 7, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.

Comparing Floating-Point• Be careful when using the equality (==)

to compare floating point numbers• Every bit in the representation must be

equal– computer can only store a certain number

of digits after the decimal

• If the numbers are results of computation, it’s unlikely that two floating point numbers will be exactly equal

Page 17: COMP 14 Introduction to Programming Mr. Joshua Stough February 7, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.

Comparing Floating-Point

3.0 / 7.0

2.0 / 7.0

(3.0 / 7.0) + (2.0 / 7.0) + (2.0 / 7.0) == 1

0.4285714285714285

false

0.2857142857142857

If we did this math with exact arithmetic, this expression would be true

0.9999999999999999

Page 18: COMP 14 Introduction to Programming Mr. Joshua Stough February 7, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.

Comparing Strings

• Strings are compared on a character-by-character basis– first character not in common determines

how the strings compare– ex: "Airplane" is less than "Airport"

• If two strings have different lengths, and one is a substring of the other, the shorter one is evaluated as less– ex: "Air" is less than "Airplane"

Page 19: COMP 14 Introduction to Programming Mr. Joshua Stough February 7, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.

Comparing Strings

Don't use relational operators (==, <, >)

for Strings

str1.compareTo (str2)Returns an integer value:

– < 0 if str1 is less than str2– 0 if str1 is equal to str2– >0 if str1 is greater than str2

str1.equals (str2)Returns a boolean value of true or false

method parameter

Page 20: COMP 14 Introduction to Programming Mr. Joshua Stough February 7, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.

Questions

Given String str = "Homer";

str.compareTo("Marge")

str.equals("homer")

str.compareTo("Bart")

negative value

false

positive value

Page 21: COMP 14 Introduction to Programming Mr. Joshua Stough February 7, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.

Boolean Expressions• Remember that booleans in Java are

– true– false

• A boolean expression is an expression that can be evaluated to either true or false

• Examples of boolean expressions:– 2+2 is not equal to 5– a is equal to 0– 10 is less than 5

• Named for George Boole (1815-1864), the founder of symbolic logic

Page 22: COMP 14 Introduction to Programming Mr. Joshua Stough February 7, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.

Boolean Operators

• NOT! (unary)!(2+2==5)

• AND&& (binary)(2+2==5) && (1+1==2)

• OR || (binary)(2+2==5) || (1+1==2)

true

false

true

Page 23: COMP 14 Introduction to Programming Mr. Joshua Stough February 7, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.

NOT Operator

Page 24: COMP 14 Introduction to Programming Mr. Joshua Stough February 7, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.

AND and OR Operators

Page 25: COMP 14 Introduction to Programming Mr. Joshua Stough February 7, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.

Boolean ExpressionsExamples• Simple:

– 2+2 is not equal to 5 2+2 != 5– 10 is less than 5 10 < 5– 'A' is equal to 7 'A' == 7

• Compound:– a is equal to 3 and b is equal to 5 (a==3) && (b==5)– x is not equal to 2 or y is less than 4 (x!=2) || (y<4)

– s is not equal to b and t is greater than or equal to 3 or t is less than 0 (s!=b) && (t>=3) || (t<0)

Page 26: COMP 14 Introduction to Programming Mr. Joshua Stough February 7, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.

Short Circuited Operators• The processing of logical AND (&&) and

logical OR (||) is “short-circuited”

• If the left operand is sufficient to determine the result, the right operand is not evaluated

count != 0 && total/count > MAX

Page 27: COMP 14 Introduction to Programming Mr. Joshua Stough February 7, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.

Precedence of Operators

Page 28: COMP 14 Introduction to Programming Mr. Joshua Stough February 7, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.

Boolean Expressions

• Logical expression that evaluates to true if the value of num is between 0 and 10

0 <= num <= 10

0 <= num && num <= 10

incorrect syntax

correct syntax

Page 29: COMP 14 Introduction to Programming Mr. Joshua Stough February 7, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.

Questions

1. What type of primitive variable can the result of a logical expression be stored in?

2. Under what conditions would the expression (ch >= 'A' && ch <= 'Z') evaluate to false?

3. What method do you use to compare two Strings?

4. Why is 'a' greater than 'A'?

boolean

ch < 'A' or ch > 'Z'

compareTo

Because 'a' comes after 'A' in the ASCII(or Unicode) character table.

Page 30: COMP 14 Introduction to Programming Mr. Joshua Stough February 7, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.

QuestionsGiven the following declarations, evaluate each Boolean

expression:

int count = 0, sum = 53;double x = 4.3, y = 1.2;boolean wrong = true;1. (wrong && sum > 60)

2. ((x > 5) || !(sum == 55))

3. !((y > 1.0) && (x < 4))

4. ((count != 4) || (sum > 100) && wrong)

false

true

true

true

(false || !(false))

!(true && false)

(true || false && true)

(true && false)

(true || false)

Page 31: COMP 14 Introduction to Programming Mr. Joshua Stough February 7, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.

Next Time in COMP 14

• Conditional Statements– if statements– if-else statements– switch statements