CPSC 111 Introduction to Computation October 15 th, 2009 Based on slides by Eiselt, Carter, Murphy, Pottinger.

Slides:



Advertisements
Similar presentations
Introduction to Computing Concepts Note Set 7. Overview Variables Data Types Basic Arithmetic Expressions ▫ Arithmetic.
Advertisements

Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
5-1 Flow of Control Recitation-01/25/2008  CS 180  Department of Computer Science  Purdue University.
Introduction to Computers and Programming Lecture 7:
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 Static Methods, Conditionals Lecture 15, Mon Feb 8.
Aalborg Media Lab 21-Jun-15 Software Design Lecture 2 “ Data and Expressions”
Introduction to Computers and Programming Lecture 5 Boolean type; if statement Professor: Evan Korth New York University.
University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 Loops III Lecture 20, Fri Mar
Program Elements We can now examine the core elements of programming (as implemented in Java) We focuse on: data types variable declaration and use, constants.
University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 Loops I Lecture 17, Fri Feb
University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner Loops II Lecture 13, Thu Feb
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Primitive Data Types There are exactly eight primitive data types in Java four of them represent integers: byte (class Byte), short (class Short), int.
University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 Loops III Lecture 19, Wed Mar
University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner Loops Lecture 12, Tue Feb
University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner Conditionals II Lecture 11, Thu Feb
Lecture 7. Review Homework 1 (sample solution) Project 1 will be assigned next week –Draw a picture (whatever you want) in the world by using turtles.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 Loops II Lecture 18, Mon Mar
COMP 14: Primitive Data and Objects May 24, 2000 Nick Vallidis.
11 Chapter 4 LOOPS AND FILES. 22 THE INCREMENT AND DECREMENT OPERATORS To increment a variable means to increase its value by one. To decrement a variable.
While Loops and Do Loops. Suppose you wanted to repeat the same code over and over again? System.out.println(“text”); System.out.println(“text”); System.out.println(“text”);
Week 2 - Friday.  What did we talk about last time?  Data representation  Binary numbers  Types  int  boolean  double  char  String.
 Value, Variable and Data Type  Type Conversion  Arithmetic Expression Evaluation  Scope of variable.
BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA TYPES AND OPERATIONS.
Mathematical Calculations in Java Mrs. G. Chapman.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
Mathematical Calculations in Java Mrs. C. Furman.
Copyright 2010 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
1 Lecture 5 More Programming Constructs Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung Institute of Technology.
1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
Topic 4 Expressions and variables Based on slides bu Marty Stepp and Stuart Reges from "Once a person has understood.
1 Flow of Control Chapter 5. 2 Objectives You will be able to: Use the Java "if" statement to control flow of control within your program.  Use the Java.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
Copyright 2010 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
CompSci 230 S Programming Techniques
Building Java Programs
Lecture 2: Operations and Data Types
Introduction to Computer Science / Procedural – 67130
Object Oriented Programming
Multiple variables can be created in one declaration
Assignment and Arithmetic expressions
Primitive Data, Variables, Loops (Maybe)
Type Conversion, Constants, and the String Object
Type Conversion, Constants, and the String Object
Chapter 2 Edited by JJ Shepherd
Building Java Programs
Building Java Programs
Arithmetic Expressions & Data Conversions
Building Java Programs Chapter 2
Building Java Programs
Building Java Programs
Chapter 2 Programming Basics.
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs Chapter 2
Building Java Programs
Review of Previous Lesson
Building Java Programs
Loops and Iteration CS 21a: Introduction to Computing I
Building Java Programs
Building Java Programs
Arithmetic Expressions & Data Conversions
Presentation transcript:

CPSC 111 Introduction to Computation October 15 th, 2009 Based on slides by Eiselt, Carter, Murphy, Pottinger

The good news from the exam ******** 44 ******** 43 ***************** A+ 42 *********** 41 ******** 40 ******* 39 * 38 **** A 37 ** 36 ** 35 ****** 34 ******* 33 **** B 32 **** 31 * 30 ** 29 * 28 *** C 27 * 26 ** 25 *** 24 D 23 * 22 *** 21 ** 20 * 19 ** F ** ** 12 * * 9 * ** Average = 79.2%

And now the bad news… The second midterm exam will be much more challenging than the first exam. There will be more material. The material will be more complex. We'll expect that you'll know the material we've covered so far much better than you know it now. If your mark on the first midterm exam was 23 or lower, you should come meet with me in office hours.

Administrative stuff You'll get your exams back in your labs next week. If you think your exam should be re-evaluated, don't take your exam from the lab. You must return the exam to your TA before you leave. We will not re-evaluate any exam that was not returned to the TA in the lab.

H1N1-related absences If (you missed the exam because you were sick && you submitted the H1N1 report or a doctor note) The weight of this midterm (10%) will be shifted to the second midterm (which will then have a weight of 20%)

Administrative Stuff

Big reading in Big Java (2nd or 3rd edition): Chapter 1.1 through 1.8 Chapter 2.1 through 2.10 Chapter 4.1, 4.7 before your next lab Chapter 3.1 through 3.8 Chapter 4 Chapter 5 (3 rd ed.) or 6 (2 nd ed.) Chapter (3 rd ed.) or (2 nd ed.)

Review of last class 1.Conditionals in Depth

To summarize We’d been discussing conditionals In particular, we discussed the if-then statement: If (condition) { //things done } else { //do something else }

What does a condition look like? Relational operators (e.g., a =a, a != b) Possibly combined by logical operators: – a && b (a AND b) – a || b (a OR b) – !a

Today’s plan 1.Comparing Data and More on Data Conversion 2.Introduction to loops: the While statement

Comparing data Is 0.3 the same thing as 1.0/ / /10 ??? Let’s try it out...

Comparing data Is 0.3 the same thing as 1.0/ / /10 ??? No. Math with floating point numbers often gives results that are very close to what you’d expect but not exactly the same. The calculation of 1.0/ / /10 yields So beware. And write tests for “darn near equal” that look like this: if (Math.abs(f1 - f2) < TOLERANCE) System.out.println (“Essentially equal.”); where TOLERANCE is a small number appropriate to the problem like

Comparing data Is 0.3 the same thing as 1.0/ / /10 ??? No. Math with floating point numbers often gives results that are very close to what you’d expect but not exactly the same. The calculation of 1.0/ / /10 yields The details of why this happens are beyond the scope of this course, but the anomaly basically due to the fact that 0.1 cannot be exactly represented in the standard binary floating point notation It is approximated by the closest 23 bit binary fraction The approximation comes up in funny ways during mathematical operations, as we have seen.

import java.util.Scanner; public class ComparingFloats { public static void main(String[] args) { double result; result = (1.0/ /10.0); System.out.println(result); if (0.2 == result) System.out.println(result + " is equal to 0.2"); else System.out.println("FALSE"); }}

Comparing data You can compare character types: 'a' < 'b' 'a' == 'b' 'a' < 'A' with your relational operators. Why?

Comparing data You can compare character types: 'a' < 'b' 'a' == 'b' 'a' < 'A' with your relational operators. Why? Because each character is associated with a number by the Java coding System (Unicode).

Comparing data But don’t use relational operators to compare character strings. Strings are objects, so you need to call on the equals method from the String class. "abc" == "abc" won’t blow up, it just tests to see if these two strings have the same address or location in memory. They usually don’t. (remember?)

Back to Data Conversion The arithmetic we do in our head isn’t the same as the math that’s done by Java. For example, we think of 1 / 3 as But what happens when we tell Java to do the same thing? Well, it depends... int a = 1 / 3; // a is 0 double b = 1 / 3; // b is 0.0 int c = 1.0 / 3.0; // Java’s not happy double d = 1.0 / 3.0; // d is

Data conversion Let’s look at them one at a time: int a = 1 / 3; // a is 0 The literals 1 and 3 are integers. Arithmetic with integers results in an integer. The fractional part is discarded or truncated. So the value 0 is assigned to a.

Data conversion double b = 1 / 3; // b is 0.0 The literals 1 and 3 are still integers. Arithmetic with integers still results in an integer. But we declared variable b of type double -- a floating point number. So Java takes the result of 0 that comes from the right hand side of the assignment statement and converts it to its floating point equivalent, 0.0, and assigns that converted value to b.

Data conversion int c = 1.0 / 3.0; // Java’s not happy The operands on the right hand side are now floating point numbers, so the result of the division is a floating point representation of But we’ve declared c to be of type int. To convert the result to an integer, Java would have to drop the Java doesn’t want to do this unless it’s really sure you want it to happen. More on this in a moment.

Data conversion double d = 1.0 / 3.0; // d is The operands on the right hand side are again floating point numbers, so the result of the division is again a floating point representation of That’s compatible with the type of the variable d, which is double -- the biggest of the floating point types. Java is happy, we’re happy, except… … what happened to the infinitely repeating series of 3’s? A finite number of bits have been allocated to represent The value, so Java has to round off at some point.

Data conversion There are two categories of data conversions: Widening conversions go from one data type to another type that uses and equal or greater amount of space to store the value. Widening conversions are safer because they usually don’t lose information (though there can be some roundoff). Narrowing conversions typically go from one type to another type that uses less space to store the value. Important information may be lost; avoid narrowing conversions.

Data conversion Which of these is a widening conversion? Which is a narrowing conversion? int a = 1 / 3; // a is 0 double b = 1 / 3; // b is 0.0 int c = 1.0 / 3.0; // Java’s not happy double d = 1.0 / 3.0; // d is

Data conversion Which of these is a widening conversion? Which is a narrowing conversion? int a = 1 / 3; double b = 1 / 3; // b is widening int c = 1.0 / 3.0; // narrowing double d = 1.0 / 3.0;

Data conversion Data conversion can happen in three different ways. 1. Assignment conversion happens when a value of one type is assigned to a variable of another type and the value must be converted to the new type. As we’ve just seen, Java will not allow a narrowing conversion to happen through assignment, but it’s happy to do widening conversions through assignment.

Data conversion Data conversion can happen in three different ways. 2. Promotion can happen when an expression contains mixed data types. Consider this code snippet: int hours_worked = 40; double pay_rate = 5.25; double total_pay = hours_worked * pay_rate; To perform the multiplication, Java promotes the value assigned to hours_worked to a floating point value to produce a floating point result

Data conversion What would happen in this case? int hours_worked = 40; double pay_rate = 5.25; int total_pay = hours_worked * pay_rate; Would the value in pay_rate be “demoted” to an integer? Error: Bad types in assignment >

Data conversion Data conversion can happen in three different ways. 3. You can force data conversion through casting, if the conversion is possible. A cast is a Java operator that is specified by a type name in parentheses placed in front of the value to be converted. For example: double foo = ; int bar = foo; // doesn’t work int bar = (int) foo; // this works What’s the value in bar when it’s all done? Hint: it's an integer.

So where are we? We learned – programming language basics – about classes – about conditionals (if statements) That’s a lot, let’s see if we can do something useful, like…

…Washing dishes (when the dishwasher is broken)

Exercise Using only constructs that we have learned so far, write a procedure (in english, not in java) to wash a huge pile of dishes by hand

Exercise Hard no? We need something that allows java to go around in circles

Today’s plan 1.Comparing Data and More on Data Conversion 2.Introduction to loops: the While statement

Repetition, iteration, and loops One of the most important attributes of a computer is its ability to perform the same task many many times. We give computer programs this ability through the use of iteration statements, also called repetition statements, but more commonly just called loops. We see repetitive operations -- loops -- in real life all the time. For example...

How do I get up the stairs? Am I at the top of the stairs? No. Climb up one step. Am I at the top of the stairs? No. Climb up one step. Am I at the top of the stairs? No. Climb up one step. Am I at the top of the stairs? No. Climb up one step. Am I at the top of the stairs? No. Climb up one step....and so on...

The while statement The simplest form of a loop in Java is the while statement. A while statement evaluates a Boolean expression, just like the if statement, and executes a statement (or block of statements) if the expression is true. while ( ) { // the body } After the statement or statements (called the loop body) are executed, the Boolean expression is evaluated again. If the expression is still true, the body is executed again. This repetition continues until the expression is false, when processing continues with the statement after the body of the while loop.

if versus while statement boolean expression statement truefalse how the if statement works

if versus while statement boolean expression statement truefalse boolean expression statement truefalse how the if statement works how the while statement works

if versus while statement boolean expression statement truefalse boolean expression statement truefalse What can make this boolean expression false if it was previously true?

if versus while statement boolean expression statement truefalse boolean expression statement truefalse Oh, by the way, diagrams like these are called flowcharts.

public class WhileDemo { public static void main (String[] args) { int limit = 3; int counter = 1; while (counter <= limit) { System.out.println("The square of " + counter + " is " + (counter * counter)); counter = counter + 1; } System.out.println("End of demonstration"); } Using the while statement Here's the while statement

public class WhileDemo { public static void main (String[] args) { int limit = 3; int counter = 1; while (counter <= limit) { System.out.println("The square of " + counter + " is " + (counter * counter)); counter = counter + 1; } System.out.println("End of demonstration"); } Using the while statement Here's the boolean expression

public class WhileDemo { public static void main (String[] args) { int limit = 3; int counter = 1; while (counter <= limit) { System.out.println("The square of " + counter + " is " + (counter * counter)); counter = counter + 1; } System.out.println("End of demonstration"); } Using the while statement Here's the body of the while statement

public class WhileDemo { public static void main (String[] args) { int limit = 3; int counter = 1; while (counter <= limit) { System.out.println("The square of " + counter + " is " + (counter * counter)); counter = counter + 1; } System.out.println("End of demonstration"); } Using the while statement Here's the first statement following the while statement. Flow of control resumes here when the boolean expression is false.

public class WhileDemo { public static void main (String[] args) { int limit = 3; int counter = 1; while (counter <= limit) { System.out.println("The square of " + counter + " is " + (counter * counter)); counter = counter + 1; } System.out.println("End of demonstration"); } Using the while statement So what happens when we execute this?

public class WhileDemo { public static void main (String[] args) { int limit = 3; int counter = 1; while (counter <= limit) { System.out.println("The square of " + counter + " is " + (counter * counter)); counter = counter + 1; } System.out.println("End of demonstration"); } Using the while statement limit 3

public class WhileDemo { public static void main (String[] args) { int limit = 3; int counter = 1; while (counter <= limit) { System.out.println("The square of " + counter + " is " + (counter * counter)); counter = counter + 1; } System.out.println("End of demonstration"); } Using the while statement limit counter 3 1

public class WhileDemo { public static void main (String[] args) { int limit = 3; int counter = 1; while (counter <= limit) { System.out.println("The square of " + counter + " is " + (counter * counter)); counter = counter + 1; } System.out.println("End of demonstration"); } Using the while statement limit counter Is counter <= limit? yes 3 1

public class WhileDemo { public static void main (String[] args) { int limit = 3; int counter = 1; while (counter <= limit) { System.out.println("The square of " + counter + " is " + (counter * counter)); counter = counter + 1; } System.out.println("End of demonstration"); } Using the while statement limit counter "The square of 1 is 1" printed on monitor 3 1

public class WhileDemo { public static void main (String[] args) { int limit = 3; int counter = 1; while (counter <= limit) { System.out.println("The square of " + counter + " is " + (counter * counter)); counter = counter + 1; } System.out.println("End of demonstration"); } Using the while statement limit counter 3 2

public class WhileDemo { public static void main (String[] args) { int limit = 3; int counter = 1; while (counter <= limit) { System.out.println("The square of " + counter + " is " + (counter * counter)); counter = counter + 1; } System.out.println("End of demonstration"); } Using the while statement limit counter Is counter <= limit? yes 3 2

public class WhileDemo { public static void main (String[] args) { int limit = 3; int counter = 1; while (counter <= limit) { System.out.println("The square of " + counter + " is " + (counter * counter)); counter = counter + 1; } System.out.println("End of demonstration"); } Using the while statement limit counter "The square of 2 is 4" printed on monitor 3 2

public class WhileDemo { public static void main (String[] args) { int limit = 3; int counter = 1; while (counter <= limit) { System.out.println("The square of " + counter + " is " + (counter * counter)); counter = counter + 1; } System.out.println("End of demonstration"); } Using the while statement limit counter 3 3

public class WhileDemo { public static void main (String[] args) { int limit = 3; int counter = 1; while (counter <= limit) { System.out.println("The square of " + counter + " is " + (counter * counter)); counter = counter + 1; } System.out.println("End of demonstration"); } Using the while statement limit counter Is counter <= limit? yes 3 3

public class WhileDemo { public static void main (String[] args) { int limit = 3; int counter = 1; while (counter <= limit) { System.out.println("The square of " + counter + " is " + (counter * counter)); counter = counter + 1; } System.out.println("End of demonstration"); } Using the while statement limit counter "The square of 3 is 9" printed on monitor 3 3

public class WhileDemo { public static void main (String[] args) { int limit = 3; int counter = 1; while (counter <= limit) { System.out.println("The square of " + counter + " is " + (counter * counter)); counter = counter + 1; } System.out.println("End of demonstration"); } Using the while statement limit counter 3 4

public class WhileDemo { public static void main (String[] args) { int limit = 3; int counter = 1; while (counter <= limit) { System.out.println("The square of " + counter + " is " + (counter * counter)); counter = counter + 1; } System.out.println("End of demonstration"); } Using the while statement limit counter Is counter <= limit? Nooooooo! 3 4

public class WhileDemo { public static void main (String[] args) { int limit = 3; int counter = 1; while (counter <= limit) { System.out.println("The square of " + counter + " is " + (counter * counter)); counter = counter + 1; } System.out.println("End of demonstration"); } Using the while statement limit counter "End of demonstration" printed on monitor 3 4

How do I get up the stairs again? Am I at the top of the stairs? No. Climb up one step. Am I at the top of the stairs? No. Climb up one step. Am I at the top of the stairs? No. Climb up one step. Am I at the top of the stairs? No. Climb up one step. Am I at the top of the stairs? No. Climb up one step....and so on...

How do I get up the stairs again? while (I'm not at top of stairs) { Climb up one step. } Going upstairs is a while loop!

public class WhileDemo { public static void main (String[] args) { int limit = 3; int counter = 1; while (counter >= limit) { System.out.println("The square of " + counter + " is " + (counter * counter)); counter = counter + 1; } System.out.println("End of demonstration"); } Using the while statement How will this change the program's behavior? The body of the loop is never executed.

public class WhileDemo { public static void main (String[] args) { int limit = 3; int counter = 1; while (counter >= counter) { System.out.println("The square of " + counter + " is " + (counter * counter)); counter = counter + 1; } System.out.println("End of demonstration"); } Using the while statement What will this do now?

public class WhileDemo { public static void main (String[] args) { int limit = 3; int counter = 1; while (counter >= counter) { System.out.println("The square of " + counter + " is " + (counter * counter)); counter = counter + 1; } System.out.println("End of demonstration"); } The infinite loop If the boolean expression never evaluates to false, the loop will never end. This is commonly referred to as an infinite loop -- it goes forever.

public class WhileDemo { public static void main (String[] args) { int limit = 3; int counter = 1; while (counter <= limit) { System.out.println("The square of " + counter + " is " + (counter * counter)); counter = counter - 1; } System.out.println("End of demonstration"); } The infinite loop And now what happens?

public class WhileDemo { public static void main (String[] args) { int limit = 3; int counter = 1; while (counter <= limit) { System.out.println("The square of " + counter + " is " + (counter * counter)); counter = counter - 1; } System.out.println("End of demonstration"); } The infinite loop Here the termination condition is good, but the process never gets closer to that termination condition. Another way to get the infinite loop.

public class WhileDemo2 { public static void main (String[] args) { int limit = 0; int counter = 9; while (counter != limit) { System.out.println("The square of " + counter + " is " + (counter * counter)); counter = counter - 2; } System.out.println("End of demonstration"); } The infinite loop This one can be a little bit more tricky.

public class WhileDemo2 { public static void main (String[] args) { int limit = 0; int counter = 9; while (counter != limit) { System.out.println("The square of " + counter + " is " + (counter * counter)); counter = counter - 2; } System.out.println("End of demonstration"); } The infinite loop Here the process gets closer to the termination condition, but never actually satisfies the termination condition and goes past it.

public class PrintFactorials { public static void main (String[] args) { int limit = 10; int counter = 1; int product = 1; while (counter <= limit) { System.out.println("The factorial of " + counter + " is " + product); counter = counter + 1; product = product * counter; } System.out.println("End of demonstration"); } Another while statement example This statement accumulates an ever-growing product obtained by multiplying the integers from 1 through the current value of counter.