CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 14: OCT. 25TH INSTRUCTOR: JIAYIN WANG

Similar documents
CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 9: OCT. 4TH INSTRUCTOR: JIAYIN WANG

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 8: SEP. 29TH INSTRUCTOR: JIAYIN WANG

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 19: NOV. 15TH INSTRUCTOR: JIAYIN WANG

Repetition Structures

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 10: OCT. 6TH INSTRUCTOR: JIAYIN WANG

Condition-Controlled Loop. Condition-Controlled Loop. If Statement. Various Forms. Conditional-Controlled Loop. Loop Caution.

Lecture 9. Assignment. Logical Operations. Logical Operations - Motivation 2/8/18

Please write your name and username here legibly: C212/A592 6W2 Summer 2017 Early Evaluation Exam: Fundamental Programming Structures in Java

(6-1) Iteration in C H&K Chapter 5. Instructor - Andrew S. O Fallon CptS 121 (February 11, 2019) Washington State University

CS 106 Introduction to Computer Science I

Functions 3.6. Fall Math (Math 1010) M / 13

ECE 156A - Syllabus. Description

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 3: SEP. 13TH INSTRUCTOR: JIAYIN WANG

AL GHURAIR UNIVERSITY College of Computing. Objectives: Examples: if Single-Selection Statement CSC 209 JAVA I. week 3- Control Statements: Part I

Looping Subtasks. We will examine some basic algorithms that use the while and if constructs. These subtasks include

CMPSCI 250: Introduction to Computation. Lecture #1: Things, Sets and Strings David Mix Barrington 22 January 2014

Operators & Expressions

Fundamentals of Programming Session 7

Reviewing all Topics this term

WYSE Academic Challenge 2017 Software Changes

CPS 110 Final Exam. Spring 2011

TOPIC 8 MORE ON WHILE LOOPS

Full file at

Rules of Exponents Part 1[Algebra 1](In Class Version).notebook. August 22, 2017 WARM UP. Simplify using order of operations. SOLUTION.

Announcements. 1. Forms to return today after class:

Stepwise Refinement. Lecture 12 COP 3014 Spring February 2, 2017

Data Types primitive, arrays, objects Java overview Primitive data types in Java

Control Flow. COMS W1007 Introduction to Computer Science. Christopher Conway 3 June 2003

Introduction to Sets and Logic (MATH 1190)

(Refer Slide Time: 00:26)

Lecture Transcript While and Do While Statements in C++

McGill University School of Computer Science COMP-202A Introduction to Computing 1

Over and Over Again GEEN163

Simplifying Square Root Expressions[In Class Version][Algebra 1 Honors].notebook August 26, Homework Assignment. Example 5 Example 6.

Final Examination Semester 2 / Year 2010

CS 135 Lab Assignments Week 1

Fundamentals of Programming Session 4

CS110D: PROGRAMMING LANGUAGE I

Announcements. Lab Friday, 1-2:30 and 3-4:30 in Boot your laptop and start Forte, if you brought your laptop

Arrays: Higher Dimensional Arrays. CS0007: Introduction to Computer Programming

Computer Science 210: Data Structures

CS 117 Fall Compound boolean expressions. Control Statements, Part 2. Using boolean operators. Boolean operators

Algebraically Speaking Chalkdust Algebra 1 Fall Semester

Chapter 3. Iteration

CSC 111 Introduction to Computer Science (Section C)

CS 455 Midterm Exam 1 Fall 2015 [Bono] Thursday, Oct. 1, 2015

Lecture 9. Monday, January 31 CS 205 Programming for the Sciences - Lecture 9 1

1. Textbook #1: Our Digital World (ODW). 2. Textbook #2: Guidelines for Office 2013 (GFO). 3. SNAP: Assessment Software

You must pass the final exam to pass the course.

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

COSC 115: Introduction to Web Authoring Fall 2013

During the first 2 weeks of class, all students in the course will take an in-lab programming exam. This is the Exam in Programming Proficiency.

Problem Solving for Intro to Computer Science

CSE548, AMS542: Analysis of Algorithms, Fall 2012 Date: October 16. In-Class Midterm. ( 11:35 AM 12:50 PM : 75 Minutes )

Reviewing for the Midterm Covers chapters 1 to 5, 7 to 9. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

CSE 131 Introduction to Computer Science Fall 2016 Exam I. Print clearly the following information:

CS 101 Fall 2006 Midterm 3 Name: ID:

ECE573 Introduction to Compilers & Translators

APPM 2460: Week Three For, While and If s

TEST ACCOMMODATIONS: Process Overview

CS 61B Data Structures and Programming Methodology. June David Sun

CSc 2310 Principles of Programming (Java) Jyoti Islam

Laboratory 5: Implementing Loops and Loop Control Strategies

Introduction to Software Development (ISD) David Weston and Igor Razgon

Fundamentals of Programming. Lecture 3: Introduction to C Programming

Computer Organization & Assembly Language Programming

More on Arrays CS 16: Solving Problems with Computers I Lecture #13

Linear Algebra Math 203 section 003 Fall 2018

QUIZ Lesson 4. Exercise 4: Write an if statement that assigns the value of x to the variable y if x is in between 1 and 20, otherwise y is unchanged.

Database Programming - Section 16. Instructor Guide

Quiz 1: Functions and Procedures

Advanced Algorithms and Data Structures

Problem Solving through Programming In C Prof. Anupam Basu Department of Computer Science & Engineering Indian Institute of Technology, Kharagpur

Prelim One Solution. CS211 Fall Name. NetID

Computer Programming: C++

Chapter 5. Repetition. Contents. Introduction. Three Types of Program Control. Two Types of Repetition. Three Syntax Structures for Looping in C++

CS 210 Algorithms and Data Structures College of Information Technology and Engineering Weisberg Division of Engineering and Computer Science

Slide 1 CS 170 Java Programming 1 Expressions Duration: 00:00:41 Advance mode: Auto

Notes Lesson 3 4. Positive. Coordinate. lines in the plane can be written in standard form. Horizontal

SaM. 1. Introduction. 2. Stack Machine

Repetition CSC 121 Fall 2014 Howard Rosenthal

Definition: A data structure is a way of organizing data in a computer so that it can be used efficiently.

Chem 3BA3 Quantum Mechanics and Spectroscopy I Course Content: Textbook: Dykstra Recommended Additional Reading: Garland Nibler Shoemaker Levine

Computer Science & Engineering 150A Problem Solving Using Computers. Chapter 5. Repetition in Programs. Notes. Notes. Notes. Lecture 05 - Loops

CSC 121 Spring 2017 Howard Rosenthal

Post Experiment Interview Questions

CS31 Discussion 1E. Jie(Jay) Wang Week1 Sept. 30

AP Computer Science Principles Programming Question Tips. 1: Which algorithm/code segment achieves some result?

Advanced Algorithms and Data Structures

Chapter Goals. Contents LOOPS

COMP-202 Unit 4: Programming with Iterations

QUIZ. Name all the 4 parts of the fetch-execute cycle.

CS 101 Fall 2006 Midterm 1 Name: ID:

CS157a Fall 2018 Sec3 Home Page/Syllabus

CS 323 Lecture 1. Design and Analysis of Algorithms. Hoeteck Wee

The Specification Phase

Searching Algorithms/Time Analysis

CS 200, Section 1, Programming I, Fall 2017 College of Arts & Sciences Syllabus

CS61A Notes Week 1A: Basics, order of evaluation, special forms, recursion

Note: This is a miniassignment and the grading is automated. If you do not submit it correctly, you will receive at most half credit.

Transcription:

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 14: OCT. 25TH INSTRUCTOR: JIAYIN WANG 1

Notice Assignments No new homework this week. Please make up the homework 1 5 & class exercises this week. 2

Notice: Mid-Term Mid-term Exam Date: the class on October 27 th (11:00 AM 12:15 PM) It covers the knowledge from Class 1 to Class 12 (October 13) Open-book but no electronic devices allowed You will be expected to read and write short segments of code Prepare Mid-term Exam Review class notes & class quizzes: 1 12 Weekly quizzes: 1 5 Homework: 2-5 Mid-term Review is posted here Mid-term Practice is posted here TA will go through the Mid-term Practice on October 20 th 3

New Materials Outline Object Equality Multiple Conditions Cumulative Sum Min/Max Loops Cumulative Sum with if 4

Object Equality You can't use the equality relational operator, ==, with objects Primitive types have only one value... but objects can have many values inside them... so deciding when one object equals another... is not an easy thing to do If you try to use the equality operator, ==... on objects... you will get a logic error 5

Object Equality 6

Object Equality We need a different way of testing for equality when dealing with objects Every Java object must have an equals method It is this method we should use... when testing for equality between objects The String method provides an equals method We have to use this method when comparing strings 7

Object Equality 8

New Materials Outline Object Equality Multiple Conditions Cumulative Sum Min/Max Loops Cumulative Sum with if 9

Multiple Conditions Sometimes when writing an if statement... you need to test for more than one condition Suppose you wanted to check that a number input by a user... is between 1 and 5 You could so something like this if (number >= 1){ if (number <= 5) { processnumber(number); } } 10

Multiple Conditions But Java gives you an alternative You can combine the two tests by using the logical AND operator The logical AND operator evaluates to true... if both of the expressions it operates upon are true Otherwise, it evaluates to false In Java, the symbol for the logical AND operator is two ampersands, && Using the logical AND operator we can rewrite this code as follows if (number >= 1 && number <= 5){ processnumber(number); } 11

Multiple Conditions There is also a logical OR operator The logical OR operator evaluates to true... if either of of its two operands is true In Java, the symbol for the logical OR operator... is two vertical bars, Again, there can't be any spaces between them So if we wanted to test whether a number is 1 or 2, we could write if (number == 1 number == 2){ processnumber(number); } 12

New Materials Outline Object Equality Multiple Conditions Cumulative Sum Min/Max Loops Cumulative Sum with if 13

Cumulative Sum We often need to compute the sum of a list of numbers The best way to do this is to keep a running total You declare variable to hold the sum and initialize it to 0 Then work your way through the list of numbers... adding each one, in turn to this running total Here is the pseudocode for this approach sum = 0 for (all numbers) { obtain next number sum += next } 14

Cumulative Sum To turn this algorithm into code... you need to know how many numbers to expect Let's look at a program which prompts the user... for the number of values to be entered... and then asks for the the values themselves 15

Cumulative Sum Notice that we declared the variable sum outside the loop We need to initialize sum to 0.0 With each pass through the loop... we add the next number to sum 16

New Materials Outline Object Equality Multiple Conditions Cumulative Sum Min/Max Loops Cumulative Sum with if 17

Min/Max Loops Another common programming problem is to find the maximum or minimum of a list of numbers Here's what we need to do to find a maximum Define a variable to hold the current maximum value Set this variable to an initial value For each value in the list, compare the new value to the current maximum... if the current value is larger then the current maximum... make this new value the current maximum 18

Min/Max Loops Here's what it looks like in pseudocode initialize max for (all numbers) { obtain next if (next > max) { max = next } } But what should we use for the initial value... of the variable that will hold the current maximum value? 19

Min/Max Loops We can't use zero If we did, we would get the wrong result for the following series of numbers: -87, -101, -35, -42, -200, -50 All of these numbers are less than 0... so the result would be 0... which is the wrong answer 20

Min/Max Loops There are two ways to deal with this problem If we knew the range of values to expect... We could set the initial value... to the lowest possible number in this range We need to use the lowest possible number... so that this initial value will be replaced... by one of the numbers in the series The second approach is to use the first values entered... to set the initial value of the maximum This is a little inconvenient... because it means reading the first value outside the loop 21

Min/Max Loops The pseudocode that covers these two possibilities is initialize max either to lowest possible value or to first value for (all numbers) { obtain next if (next > max) { max = next; } } 22

New Materials Outline Object Equality Multiple Conditions Cumulative Sum Min/Max Loops Cumulative Sum with if 23

Cumulative Sum with if Whenever we take the average of a series of numbers, we Add all the number together Divide the total by the number of values in the series So the code would look something like this The variable numberofnumbers... contains the length of the list of numbers we are averaging There is a problem with this code What happens if numberofnumbers is 0? 24

Cumulative Sum with if 25

Cumulative Sum with if NaN stands for Not A Number... because division by zero is not defined So we need to test for zero... before trying to determine the average 26

Cumulative Sum with if You can also use an if statement... to count how many negative numbers are entered We can do this by using a counter A counter is a variable that keeps track... of the number of times something has happened We need to initialize the counter to 0... and use an if statement to increment the counter... each time we encounter a negative number 27

Cumulative Sum with if 28