DOC PREVIEW
Berkeley COMPSCI 61B - Lecture Notes

This preview shows page 1 out of 2 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 2 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 2 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

CS61B Lecture #1Course OrganizationProgramming, not JavaReally simple exampleLessons from Simple ExamplePrime NumbersPlanTesting for PrimesCS61B Lecture #1• Lab reader and Java reference manual available at Copy Central,2483 Hearst.• Labs and discussions sections start this week. Get an account (ifneeded) and register electronicallythis week• Go to any sections, labs where you fit.• Class web page and newsgroup set up: read them regularly!• Reading for today and Wednesday:Assorted Materials on Java(reader)1.1–1.9.• Concurrent enrollment students: bring me your forms.Last modified: Mon Aug 30 12:13:50 2004 CS61B: Lecture #1 1Course Organization• You read; we illustrate.• Labs are important: practical dirty details go there.• Homework is important, but really not graded: use it as you see fitandturn it in!• Individual projects arereallyimportant! Expect to learn a lot.• Use of toolsispart of the course: debugging (gjdb), compilationcontrol (make), version control (cvs, p rcs).• Tests are challenging: better to stay on top than to cram.• Tests, 90%; Projects, 90%; HW, 20%• Stressed? Tell us!• Now’s your opportunity to decide.Last modified: Mon Aug 30 12:13:50 2004 CS61B: Lecture #1 2Programming, not Java• Here, we learnprogramming,not Java (or Unix, or NT, or. . . )• Programming principles span many languages– Look for connections.– Syntax (x+yvs.(+ x y )) is superficial.– E.g., Java and Scheme have a lot in common.• Whether you use GUIs, text interfaces, embedded systems, impor-tant ideas are the same.Last modified: Mon Aug 30 12:13:50 2004 CS61B: Lecture #1 3Really simple examplepublic c lass Greet {/** Print a gr eeting me ssage on sta ndard output . */public s tatic void main (St ring[] args) {System.out.print ("Hello, " );if (args.length > 0)System.out.println (args[0]);elseSystem.out.println ();}}% j avac -g Greet.java # C reates Gr eet.class% j ava Greet world # I nterpreter cal ls Greet.mainHello, w orld # O utput% j ava Greet me warmly # A nother ru nHello, m e # a rgs[0] = "me "Last modified: Mon Aug 30 12:13:50 2004 CS61B: Lecture #1 4Lessons from Simple Example• All definitions are inside some class.• Syntax A.B means “the B that is defined (or contained) inside A,”– E.g., S ystem.out.pr intln, Greet.main• Ordinary function isstatic method,like Greet.main.• Methods declar e what kinds (types) of a rguments they take, andwhat kind of value they return (void means “no value”).• Method calls use familiar prefix syntax.• Command-line arguments become a narray of strings.• Array is indexed sequence: args[0], a rgs[1], . .., args[args.leng th-1]• Conditional statement: if (condition) ...els e ....• Access control: publ ic and others control what par ts of the pro-gram may use a definition.Last modified: Mon Aug 30 12:13:50 2004 CS61B: Lecture #1 5Prime NumbersProblem: want java PrintPrimes0 L U to print prime numbe rs be-tween L and U.You type:java primes 101It types:2 3 5 7 11 13 17 19 23 2931 37 41 43 47 53 59 61 67 7173 79 83 89 97 101Definition: Aprimenumber is an integer greater than 1 that has nodivisors smaller than itself other than 1.Useful Facts:• If k ≤√N, then N/k ≥√N, for N, k > 0.• k divides N iff N/k di vide s N.So: Try all potential di vis ors up to and i ncluding the squar e root.Last modified: Mon Aug 30 12:13:50 2004 CS61B: Lecture #1 6Planclass primes {/** Print a ll p rimes up to ARG S[0] (interpreted as an* integer), 10 to a li ne. */public s tatic void main (St ring[] args) {printPrimes (Integer.parseInt (args[0])) ;}/** Print a ll p rimes up to and including LIMIT, 10 to* a l ine. */private static v oid printPrimes (i nt limit) {/*{ For every integer, x, be tween 2 and LIMIT, print it ifisPrime (x), 10 t o a line. }*/}/** True iff X is pr ime */private static b oolean is Prime (int x) {return /*( X i s prime )*/;}}Last modified: Mon Aug 30 12:13:50 2004 CS61B: Lecture #1 7Testing for Primesprivate static b oolean is Prime (int x) {if (x <= 1)return false;elsereturn ! isDivisible (x, 2); // "!" means "not"}/** True iff X is divisible by any positive numbe r >=K and < X,* given K > 1. */private static b oolean is Divisible (int x, int k) {if (k >= x) // a "guard"return false;else if ( x % k == 0) // "%" mean s "remainder"return true;else // i f (k < x && x % k != 0)return isDivisible (x, k+1);}Last modified: Mon Aug 30 12:13:50 2004 CS61B: Lecture #1


View Full Document

Berkeley COMPSCI 61B - Lecture Notes

Documents in this Course
Lab

Lab

4 pages

Matrix

Matrix

3 pages

Numbers

Numbers

14 pages

Lectures

Lectures

12 pages

Project 1

Project 1

24 pages

Exam

Exam

8 pages

Load more
Download Lecture Notes
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture Notes and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture Notes 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?