Intro to Computer Science I
Intro to Computer Science I CS 110
Popular in Course
verified elite notetaker
Popular in ComputerScienence
verified elite notetaker
This 8 page Class Notes was uploaded by Michele Herzog on Thursday October 29, 2015. The Class Notes belongs to CS 110 at University of San Francisco taught by David Wolber in Fall. Since its upload, it has received 10 views. For similar materials see /class/231231/cs-110-university-of-san-francisco in ComputerScienence at University of San Francisco.
Reviews for Intro to Computer Science I
Report this Material
What is Karma?
Karma is the currency of StudySoup.
You can buy or earn more Karma at anytime and redeem it for class notes, study guides, flashcards, and more!
Date Created: 10/29/15
CS 110 Introduction to Computer Science Spring 2008 Java Strings and Lexical Analysis Consider the process you perform to read The first thing your brain does is lexical analysis which identi es the distinct words in a sentence Consider the job of a compiler translator Source code gt TRANSLATOR gt machine code The rst thing the translator does is also lexical analysis It does the computing equivalent to reading In this case it identifies the distinct tokens in a program statement or statements A lexer would take input such as balance4 balance4 05 and return a list of tokens identi ed by token type balance4 identi er assignment operator balance4 identi er multiplication operator 05 oat literal semicolon Note that the lexer must know things about the language e g What is considered a legal identi er an identi er is a variablefunction name What is considered a legal oat literal Write an English sentence de ning what is considered a legal identi er For our sample the lexer would scan the 39b of balance4 then go into a loop to read and collect the rest of the word stopping when the space was encountered Given how Java defines identifiers what should the loop to read and collect the rest of the word look like What should the condition be After lexical analysis the next job of a translator is to parse the code to see if the tokens form a legal sentence in the programming language We ll talk about parsing later CS 110 Introduction to Computer Science Spring 2008 Java Strings Java Strings are very similar to Python strings String s note double quotes not single C s quotabcquot s s quotdefquot concatenation Java also has a scalar type called char A char is a single ASCII Unicode symbol The literal value of a char is denoted using single quotes eg char c a if c 39 is c a space Underneath the hood a Java String is a sequence of chars delimited by the endofstring char 0 What data members do you think the class String has In Java you can t index a string as in Python Normal object dot notation is used instead The String class provides the methods char charAtint i int length Example String s quotabcdefgquot char c scharAt3 value ofc is 39d39 int length slength value oflength is 7 Looping through the characters of a String int i 0 while iltslength char c scharAti do something to c How wouldyou ask ifa char is a letter A digit CS 110 Introduction to Computer Science Spring 2008 Other String Methods there are many more boolean startsWith String pre x String substringint beginIndex int endIndex Instructor Demo httpwwwcsusfcaedu7Ewolbercoursesl 10f07 javaSamplesWordCounter java Write a Java class WordCounter with a main method that a reads in a string of text from the user b prints the number of distinct words in the string where words are delimited by any nonletter So quotbig boyquot has two distinct words and quotJoeBob Jerryquot has three InClass Problem Write a class Lexer with 39 a data member programStatemen of type String 39 a constructor which takes in parameter of type String 39 a method getIdenti erint index which returns a string which is the identi er beginning at index So if the programStatement is bobjoe45 getIdenti er0 would return bob getIdenti er4 would return joe45 The Lexer Program GetNextToken method and how it will be used by the parser Write out an algorithm for the main program and for getNextToken What classes should you de ne for this program CS 110 Introduction to Computer Science Spring 2008 Conditional Conditional statements are ones that you want to execute only if some condition is true Here39s a boring example x 5 if xlt6 print 39Iess than 639 Note the form of the 39if39 statement if ltconditiongt ltstatementgt ltstatementgt An else clause says what to do ifthe 39if condition is false eg if xlt6 print 39Iess than 639 else print 39more or equal to 639 fyou have more than two alternatives use the keyword elif which is short for else if if xlt6 print 39Iess than 639 elif xlt 10 print 39between 6 and 1039 else print 39ten or bigger You can have as many 39elif s as you want Comparison and Logical Operators Here are the comparison operators that you can use in conditional statements and iteration statements xlt3 xgt3 CS 110 Introduction to Computer Science Spring 2008 Representing Lists in Java Java provides a few ways to represent lists including the array which his part of the Java language proper and a number of classes in the Java library Perhaps the easiest way to represent lists is with a library class called ArrayList Take a look at the following Java and Python samples Java httQwwwcsusfcaeduwolberlcoursesl110f0739avaSamQIesListTest39ava Python httpwww rs nefra Pd wobercourse5110fO739 39 lListTestDv Here s the key differences in terms of how you do things Java ArrayList Python list A1rayListlttypegt list new ArrayListlttypegt list listaddobject listappendobject list geti listi listsize lenlist list2 listclone list2list ArrayList requires the type of the elements in the array to be specified just like everything in Java Consider how you declare and set an integer in Java int X39 declare X as an integer X 4 set Xto 4 ArrayList is similar but you declare that the variable is a list of a specific type using ltgt ArrayListltStringgt list new ArrayListltStringgt listaddquotabcquot ArrayList can only hold object types not scalers like 39int ArrayList is not 39builtin to the language but part of the core java library must import java util Instructor Coding Sample Write a class Person and a main program that creates three person s puts them in a list and totals up the age of each person in the list In Class Programming Assignment To practice using ArrayList and get started with the ParkingLot program write the following code in the files Carj ava and ParkingLot java CS 110 Introduction to Computer Science Spring 2008 Working with Objects in Python The Google API There are many Python libraries around that do interesting things Some allow you to call web services like the one Google provides The pygoogle library is one such library Here39s a simple example that uses the Python39s pygoogle library import google googlesetLicensequotH2delvNQFHl9ilszXth17lan7Pp4quot data googledoGoogleSearch wolber39 print dataresults0URL print dataresults0title The doGoogleSearch function returns a complex object data catches it It has one field called results which is itselfa list of objects each having a URL and a title and some other things For a full description ofthe the pygoogle Google API see httpzpygooglesourceforgenetZdistZdocZindexhtml As the API documentation shows doGoogleSearch returns an object of type SearchReturnValue Here39s the text from the API Returns the search results encapsulated in an object typeSearchReturnValue SearchReturnValue has two fields meta SearchResultsMetaData results list of SearchResult meta is the name ofthe first part lts type is searchResultsMetaData It contains information such as the total number of results for the query cs llu Inlmdumzunlu Computer SEIEnce Spring 2m Bits and Bytes We want to speak English or some natural language to computers but there39s really only electric currents running inside them Levels of Abstraction Henrit Currents 0s and 1se ON current is 1 and an OFF current is 0 Machine Lanaguag 7 Numbers for commands and data AssemhlLLannum Some symbols allowed eg Mov x 34 55 Still dealing directly with machine Refer directly to memory cells registers devices etc cs nu Infmduman tn Campmex Scum Spring 2003 nghrLevd Language Java C Python Svmbohc ahg1og1a1whh few fanvdwec t vefevences O addvesses yegmeys OY othev havdwave Components Natuva Language e g Ehghsh 5 to nynes Integers and strings We have ohw0s and 1s but we Want 0 vepvesent numbevs aha chavactevsand othevdata thch veqmve numevous 0s and 1s A bu 15 a smg e 01 Abym 15 3 b1tseg 01010101 Award whwch 15 used 0 stove mtegevs 15 often 32 bus 4 bytes To vepvesent a posmve mtegev we must convey bmawuhe 32 bws mto geomax We do Ms bv assghhg be 1 whh he vame bwt39Z Anothev Wavto put 1 15 that Stamng ow the tht most b1 and gomg wen Weass gn 12431632 etc 0 each b1 hams 1 80 101015 302010 The decwma 15515 mmmquot
Are you sure you want to buy this material for
You're already Subscribed!
Looks like you've already subscribed to StudySoup, you won't need to purchase another subscription to get this material. To access this material simply click 'View Full Document'