Public-Service Announcements

Similar documents
Administrivia. Last modified: Fri Aug 25 10:59: CS61B: Lecture #2 1

Public-Service Announcements

CS61B Lecture #2. Public Service Announcements:

JAVA PROGRAMMING LAB. ABSTRACT In this Lab you will learn to define and invoke void and return java methods

Lec 3. Compilers, Debugging, Hello World, and Variables

Public-Service Announcement

CIS 110: Introduction to Computer Programming

CSE115 / CSE503 Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall Office hours:

Announcements. 1. Forms to return today after class:

Public-Service Announcement

CONTENTS: While loops Class (static) variables and constants Top Down Programming For loops Nested Loops

Final Examination Semester 2 / Year 2011

UCT Algorithm Circle: Number Theory

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

COMP-202: Foundations of Programming. Lecture 4: Flow Control Loops Sandeep Manjanna, Summer 2015

Control Statements. if for while

CS 11 java track: lecture 1

COMP-202: Foundations of Programming. Lecture 26: Review; Wrap-Up Jackie Cheung, Winter 2016

CS111: PROGRAMMING LANGUAGE II

COMP-202: Foundations of Programming. Lecture 8: for Loops, Nested Loops and Arrays Jackie Cheung, Winter 2016

COMP 110 Introduction to Programming. What did we discuss?

PROGRAMMING FUNDAMENTALS

Recursion. Recursion [Bono] 1

COMP-202 Unit 4: Programming with Iterations

Administration. Conditional Statements. Agenda. Syntax. Flow of control. Lab 2 due now on floppy Lab 3 due tomorrow via FTP

CS212 Midterm. 1. Read the following code fragments and answer the questions.

CS 101 Fall 2005 Midterm 2 Name: ID:

CSCI 1101 Winter 2017 Laboratory No Submission deadline is p.m. (5 minutes to midnight) on Saturday, February 4th, 2017.

Lab Assignment Three

AP Computer Science A Summer Assignment 2017

lecture24: Disjoint Sets

Chapter 4: Conditionals and Recursion

Controls Structure for Repetition

Nested Loops ***** ***** ***** ***** ***** We know we can print out one line of this square as follows: System.out.

Warmup : Name that tune!

CSC 1051 Villanova University. CSC 1051 Data Structures and Algorithms I. Course website:

CS 1110: Introduction to Computing Using Python Loop Invariants

Arrays. Eng. Mohammed Abdualal

CS61C Machine Structures. Lecture 3 Introduction to the C Programming Language. 1/23/2006 John Wawrzynek. www-inst.eecs.berkeley.

CSC 1051 Data Structures and Algorithms I

CS 177 Week 15 Recitation Slides. Review

Public-Service Announcement

CS18000: Programming I

CompSci 125 Lecture 02

CS111: PROGRAMMING LANGUAGE II

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

Lecture #10: Sequences. Last modified: Mon Feb 22 16:33: CS61A: Lecture #10 1

Question: Total Points: Score:

CS61BL. Lecture 1: Welcome to CS61BL! Intro to Java and OOP Testing Error-handling

CS 101 Spring 2007 Midterm 2 Name: ID:

CS 177 Week 5 Recitation Slides. Loops

Introduction to Java Programming

APCS Semester #1 Final Exam Practice Problems

Lecture 5: Methods CS2301

CS 112 Introduction to Programming

Admin. CS 112 Introduction to Programming. Recap: Java Static Methods. Recap: Decomposition Example. Recap: Static Method Example

Building Java Programs

Example: Monte Carlo Simulation 1

COMP-202. Recursion. COMP Recursion, 2011 Jörg Kienzle and others

Lecture 2. COMP1406/1006 (the Java course) Fall M. Jason Hinek Carleton University

Last time s big ideas

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

Building Java Programs

Basic computer skills such as using Windows, Internet Explorer, and Microsoft Word. Chapter 1 Introduction to Computers, Programs, and Java

Lecture 6: While Loops and the Math Class

Building Java Programs

Lesson 5: Introduction to the Java Basics: Java Arithmetic THEORY. Arithmetic Operators

Problem Solving for Intro to Computer Science

Intensive Introduction to Computer Science. Course Overview Programming in Scratch

Lecture 3 (02/06, 02/08): Condition Statements Decision, Operations & Information Technologies Robert H. Smith School of Business Spring, 2017

CS 110 Practice Final Exam originally from Winter, Instructions: closed books, closed notes, open minds, 3 hour time limit.

COMP-202: Foundations of Programming. Lecture 26: Image Manipulation; Wrap-Up Jackie Cheung, Winter 2015

Question: Total Points: Score:

Project #1 rev 2 Computer Science 2334 Fall 2013 This project is individual work. Each student must complete this assignment independently.

You will need to collect samples of external documentation that is not software related--directions to build; manual for TV, etc.

This exam is open book. Each question is worth 3 points.

CSC 1051 Data Structures and Algorithms I

Topic 4 Expressions and variables

Expressions and Control Statements

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

TOPIC 2 INTRODUCTION TO JAVA AND DR JAVA

Simple Java Programming Constructs 4

61A Lecture 7. Monday, September 15

Announcements. Lab 03 inbase late submissions Midterm #1 Friday. Project 2 is posted. Covers everything through basics of OOP

CS 101 Fall 2006 Midterm 3 Name: ID:

CS 126 Lecture P1: Introduction to C

COMP Primitive and Class Types. Yi Hong May 14, 2015

Lab 6: P.S. it s a stack Due 11:59 pm Monday, Mar 31, 2008

} Evaluate the following expressions: 1. int x = 5 / 2 + 2; 2. int x = / 2; 3. int x = 5 / ; 4. double x = 5 / 2.

CS 231 Data Structures and Algorithms Fall Binary Search Trees Lecture 23 October 29, Prof. Zadia Codabux

CS61B Lecture #3. Homework: Please see Homework #1 on the lab page. Last modified: Wed Sep 10 20:34: CS61B: Lecture #3 1

Python as a First Programming Language Justin Stevens Giselle Serate Davidson Academy of Nevada. March 6th, 2016

LAB C Translating Utility Classes

Inf1-OOP. Textbooks. Who and What. Organizational Issues. Why Java? Course Overview. Hello, World! in Java. Ewan Klein, Perdita Stevens

61A Lecture 3. Friday, September 5

What did we talk about last time? Examples switch statements

CS 135 Lab Assignments Week 12

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

UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N. Hilfinger.

4CCS1PRP, Programming Practice 2012 Lecture 6: Arrays - Part 1

Transcription:

Public-Service Announcements The Computer Science Undergraduate Association(CSUA) welcomes all students interested in computer science to join them at their Welcome BBQ on Saturday, 8/27 from 12-4pm at Wozniak Lounge. Come get to know the members of the oldest computer science club on campus as well as fellow students in the CS community! Last modified: Fri Aug 26 14:43:25 2016 CS61B: Lecture #2 1

Administrivia Please make sure you have obtained a Unix account. IfyoudidnotcompleteLab#1,pleasetrytodosoovertheweekend (usually, they are due Friday midnight). It is esepcially important to set up your central reppository. If you decide not to take this course after all, please tell CalCentral ASAP, so that we can adjust the waiting list accordingly. Thoseofyouonthewaitinglistshouldfindalabsectionthatisopen, remove yourself from the waiting list, and re-add with this open lab section. The waiting list is processed twice daily. HW #0 now up; due next Friday at midnight. You get credit for any submission, but we suggest you give the problems a serious try. Readings for next week should be up tonight. Last modified: Fri Aug 26 14:43:25 2016 CS61B: Lecture #2 2

Lecture #2: Let s Write a Program: Prime Numbers Problem: want java Primes U to print prime numbers through U. You type: java Primes 101 It types: 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 101 Definition: A prime number is an integer greater than 1 that has no divisors smaller than itself other than 1. Useful Facts: k N iff N/k N, for N,k > 0. If k divides N then N/k divides N. So: Try all potential divisors up to and including the square root. Last modified: Fri Aug 26 14:43:25 2016 CS61B: Lecture #2 3

Plan public class Primes { /** Print all primes up to ARGS[0] (interpreted as an * integer), 10 to a line. */ public static void main(string[] args) { printprimes(integer.parseint(args[0])); /** Print all primes up to and including LIMIT, 10 to * a line. */ private static void printprimes(int limit) { /*{ For every integer, x, between 2 and LIMIT, print it if isprime(x), 10 to a line. */ /** True iff X is prime */ private static boolean isprime(int x) { return /*( X is prime )*/; Last modified: Fri Aug 26 14:43:25 2016 CS61B: Lecture #2 4

Testing for Primes private static boolean isprime(int x) { if (x <= 1) else return!isdivisible(x, 2); // "!" means "not" /** True iff X is divisible by any positive number >=K and < X, * given K > 1. */ private static boolean isdivisible(int x, int k) { if (k >= x) // a "guard" else if (x % k == 0) // "%" means "remainder" else // if (k < x && x % k!= 0) return isdivisible(x, k+1); Last modified: Fri Aug 26 14:43:25 2016 CS61B: Lecture #2 5

Thinking Recursively Understand and check isdivisible(13,2) by tracing one level. /** True iff X is divisible by * some number >=K and < X, * given K > 1. */ private static boolean isdivisible... if (k >= x) else if (x % k == 0) else return isdivisible(x, k+1); Lesson: Comments aid understanding. Make them count! Call assigns x=13, k=2 Body has form if (k >= x) S 1 else S 2. Since 2 < 13, we evaluate the first else. Check if 13 mod 2 = 0; it s not. Left with isdivisible(13,3). Rather than tracing it, instead use the comment: Since 13 is not divisible by any integer in the range 3..12 (and 3 > 1), isdivisible(13,3) must be false, and we re done! Sounds like that last step begs the question. Why doesn t it? Last modified: Fri Aug 26 14:43:25 2016 CS61B: Lecture #2 6

Iteration isdivisible is tail recursive, and so creates an iterative process. Traditional Algol family production languages have special syntax for iteration. Four equivalent versions of isdivisible: if (k >= x) else if (x % k == 0) else return isdivisible(x, k+1); while (k < x) { //!(k >= x) if (x % k == 0) k = k+1; // or k += 1, or (yuch) k++ int k1 = k; while (k1 < x) { if (x % k1 == 0) k1 += 1; for (int k1 = k; k1 < x; k1 += 1) { if (x % k1 == 0) Last modified: Fri Aug 26 14:43:25 2016 CS61B: Lecture #2 7

Using Facts about Primes We haven t used the Useful Facts from an earlier slide. Only have to check for divisors up to the square root. So, reimplement isprime: private static boolean isprime(int x) { if (x <= 1) else return!isdivisible(x, 2, (int) (Math.round(Math.sqrt(x) + 1.0))); // "(int) E" is "convert to int". Math.round => a long. private static boolean isdivisible(int x, int k, int lim) { if (k >= lim) // a "guard" else if (x % k == 0) // "%" means "remainder" else // if (k < x && x % k!= 0) return isdivisible(x, k+1); Last modified: Fri Aug 26 14:43:25 2016 CS61B: Lecture #2 8

z Last modified: Fri Aug 26 14:43:25 2016 CS61B: Lecture #2 9

Final Task: printprimes /** Print all primes up to and including LIMIT, 10 to * a line. */ private static void printprimes(int limit) { Last modified: Fri Aug 26 14:43:25 2016 CS61B: Lecture #2 10

printprimes: One solution /** Print all primes up to and including LIMIT, 10 to * a line. */ private static void printprimes(int limit) { int np; np = 0; for (int p = 2; p <= limit; p += 1) { if (isprime(p)) { System.out.print(p + " "); np += 1; if (np % 10 == 0) System.out.println(); if (np % 10!= 0) System.out.println(); Last modified: Fri Aug 26 14:43:25 2016 CS61B: Lecture #2 11