COMPUTER SCIENCE I
COMPUTER SCIENCE I CS 1113
Popular in Course
Popular in ComputerScienence
This 0 page Class Notes was uploaded by Ms. Bridie Kohler on Sunday November 1, 2015. The Class Notes belongs to CS 1113 at Oklahoma State University taught by John Chandler in Fall. Since its upload, it has received 20 views. For similar materials see /class/232837/cs-1113-oklahoma-state-university in ComputerScienence at Oklahoma State University.
Reviews for 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: 11/01/15
Anthony Gaskill 7 CS 1113 7 Exam 3 dY int 2 5 0 5 Magic number 7 number that appears in a 2 0 5 program without explanation bad programming 4 0 habit iX l l l 2 4 7 Portable program 7program that can operate l 5 4 7 independently on any computer l 2 0 7 Charles Babbage invented the rst general l 6 purpose digital computer 5 Ada August Byron Countess Lovelace is dY 01011101 6 l l 4 2 0 considered to be the rst computer programmer CW 19 4 double 2 4 2 O 8 0 Java Variable names forum 7quot i 5239 i I 1 Start with a letter or or 7 for i iigkg 2 11quot 2 Followed with letters or numbers System outprintln i 3 No reserved words 3 l l k 4 N0 Spaces Prints out 1 3 2 8 Primitive Java Types 7 byte short int long i Z oat double char boolean 1 4 1 2 3 2 2 3 1 2 4 2 2 4 1 lfn0 H D 8 int iA3 iB 6 i0 Systemoutprintln Round iC iA iB else ifn l H n H D n n Systemoutprintln Pointy F System39OUt39prlntlnlA Jr 1B else n n System outprintln Other SWitch n Prints out 2 7 10 case 0 C356 8 Because if 7 or comes before the EEStem39out39pmntlm Round quot variable it updates before the case 1 39 expression is carried out If or case 4 comes after the variable it does not case 7 update until after the expression is SystemoutprintlnquotPointyquot carries out break 39 defaul int i dY 15 4 double 2 fori0 ilt9 i 320 Systemoutprinti 60 1x 7 int 93 7 10 int iCount O initialize counter 9 7 10 whileiCount lt 9 Error loss of precision dY 7 double 15 4 SystemoutprintiCount 30 iCount String greeting Hellol char e greetcharAtl String ll greetingsubstring2 4 Anthony Gaskill 7 CS 1113 7 Exam 3 122 import java textDecimalForm Systemoutprintf lO2f price Decm l nmatfmt int price t new DecimalFormatquot00quot Prints out 122 double speedLimit 6566 Systemout printlnfmt format speedLimit Prints out 65 7 Write a public static method that sorts and array into increasing order public static Void sortArraydouble dArr int n double temp temp storage for rearranging array forint iPosMin l 7 O iPosMin lt n iPosMin forint i O i lt n i l ifdArri gt dArriPoSMin l temp dArriPosMin dArriPosMin dArri dArri l end inner for l end outer l temp for end sortArray method Read and count the data value 5 Find the smallest value among the data values Compute the sum of the data values import javautilScanner public class sentinel l public static Void mainStriDgl args l Scanner 5 can new ScannerSystemin int numberOfValues 39 scannextlnt int iSmallest iVal whileiVal l i999 l ifiVal lt iSmalleSt iSmallest iVal numberOfValues iSum iVal iVa y39 scannextlnt l System outprintlnquotNo of Value System outprintlnquotSmallest System outprintlnquotSum l s 7 quot numberOfValueS quot iSmallest quot iSum ifComputer science notes Homework due on paper at lecture Tuesday Beginning of class Check for programming assignment Chapter 1 Java for pc s N 1 Eclipse not for beginners 2 blue 3 Netbeams 4 JBuilder 5 JGrasp 6 JCreator Computer programs a Set of instructions to do something to do something b Who invented the general purpose digital computer 1 Charles babbage likely to be on testpg 260 c Who was the rst programmer I ada augusta Byron countess lovelace rst computer that ran was the mark one IBM and Harvard Lowest level of programming a Machine language i All numbers b Assembly language i Opp code reg symbolic operand Anything can do in machine lang you can do in ass Lang Assembly code is automatically translated to machine language assembler c High level languages i FORTRAN 1957 IBM later standardized ii ALGOL 1960 iii COBOL 1960 iv C C v python who invented the personal computer altair Ed Roberts MS EE OSU 3 java language a Advantage because it s portable Portable computer language a language that will run on any computer system theat supports the language and is bery widely supported free compiler eX Java fortran c c Python always standardized lang What is a compiler Program that translates a high level language into machine language or for java into java byte code Becoming familiar with your programming environment Suppose a le Progl java contains a java program To compile type Javac progljava To run Typejava proglpg 9 Type where the data is after it To back up the program use cp command progl java lename Analyzing your rst program read section 15through pg 14 Errors 1 Syntax errorscompile time errors iafter javac not able to translate the program 2 Executionruntime errors 7 like calc dividing by zero 3 Logic errorsincorrect results no error message 7 Algorithm Step by step procedure to solve a problem They have Unsolvable problems And uncomputable numbers Look at the new turing omnibus By ak dewdeney Debugging program Can a program be debugged with absolute certainty Ans No Can t really prove correct Need to type previous notes up forjan 18 2010 Jan 20 2010 Homework 2 due at 1230 PM next Tuesday Programming assignment 1 due Monday the 31st Instructions at the end of the assignment Suggestionwrite on paper and talk with ta start early Indent 3 spaces Only Do not use tabs Do not go over 80 characters per line Only 80 characters are printed Program mus produce the sample outpur exactly given the sample input Program must work for every number between 1 and 99 p 40 int counter counter 3 counter 7counter 1 equals sign isn t really an equals sign but a left pointing arrow assigning statement straight line code chapter 2 Shorthand Short is good because It s easier to debug Try to nd shorthand ways Another way to write counter counter 1 Counter Add 1 to the value of the counter Subtract l counter Also add it before if that s the only thing you are trying to only that in the line 23 Reading Input more stuff 3 2 mil lJu 39 39uiii reads an int from the keyboard stores it into amount Due cannot type a decimal point wquot lmmlh exlw decimal points are allowed use neXtInt all the time 139 1 Section 24Constants The idea is to declare at the top and give them a name Find double BOTTLEiVOLUME 20 separate words with underscores all caps means nal cannot be changed value examplein Fquot 39u LUJJ all m known as body of the program Rules for body ofthe program can use 0 00 or 1 Any other constant should be the name of the variable or nal named constant Other constants in the body of a program are called magic numbers Very bad magic numbers what is a magic number in programming anything but 0 00 or 1 and are they good bad 25 Arithmatic Int iA iB iC iA 7 1B iA 2 r 7 1 complete left to right unless ntadd the Simple signs on keyboard le 1 7 parenthesese are prese multiplication is represented by an asterisk cannot be represented by 32 iA 32 exampleorder of operations still present levels are parenthesis rst multip and division next and stands for u 1 Elimm l may L311 rm m in 35 discard any fractional part Do not round cannot divide by zero error 702 7020 35 remainder is discarded Jan 25 Int iX iX 3 2 7 10 doubles hold much larger values than int the double after the int changes the functions to a double 50 double 710 40 double error message is precision may have been lost shown as ltintgtltdoublegt pretty much the only error on the basic arithmetic int over double because of roundoff errorEx pg 54 00 is stored exactly in double numbers like 01 is not stored exactly 10001 is not 10 as expected int has no roundoff errors Convelting one primitive type to another iX int 48 int 48 a cast cast applies to whatever comes immediately after the cast ex iX int28 3 00 Int applies to the 28 only and the int cast is gone and the 00 makes the 50 auto widen to a double This in turns creates the error precision may have been lost iX 50 receives error division by zero iX 50 receives same error as above iX 05 is 0 iX 0 5 is 0 souble dY int 48 32 receives 50 255 formatted output skip 26 Strings Not primitive But a class of objects not anything that is primitive Built into java and is a string of variables input String strl str2 str3 Strl cowboys System outprintlnstr1 Strl Str1 What if you want to print double quote Str1 he said the output is he said The backslash says it isn t a dillimiter And is in fact a character new line t tab System outprintlnError nStop Str2 cow boy outputs cowboy and continues a line print a really long line contcatination System0utprintln N N String variable name gives you a string string anything string and the opposite is true System outprintln 4 3 result is a string String inputs Scanner scan mewScannerSystermln Str3 scannext gives younext token delimited by beginning of line blanks and end of line Feb 8 2011 Notes for exam Exam 1 on thurs covers through ch 2 plus lectures Sections 10 and 11 go to ps 101 for test Bring a photo id Alternate seats from the aisles in If OSU Is closed thurs exam 1 is next Tuesday Check daily news Same time that we usually meet Old exams online Section 36 Input Validation Check if the numbers given by the keyboard are valid as double or int Defensive programming System of ifs to check for valid input and computed results and avoid system terminations Java isn t clear so you use the system of user error messages to fully understand the problem at hand Not covered on exam one Very good tool to use to write a program The programs defends itself against bad input resulting in a robust Program and bulletproof code means not so f amountDue scanneXtInt reads the int 6yy takes the 6 lines past the cursor at the first line Then you can drag the phrase changeNeeded amountPaid 7 amountDue m mm Omitting braces that you don t need Pg 82 Pitfall num l Ending an if With a semicolon ie ifchangeNeeded lt 0 the semicolon should not be there the other statements as a result are executed unconditionally instead of dependant upon the if statement The program is still quit Anthony Gaskill 7 CS 1113 7 Exam 2 dY int 2 5 0 5 Magic number 7 number that appears in a 2 0 5 program without explanation bad programming 4 0 habit iX l l l 2 4 7 Portable program 7program that can operate l 5 4 7 independently on any computer l 2 0 7 Charles Babbage invented the rst general l 6 purpose digital computer 5 Ada August Byron Countess Lovelace is dY 01011101 6 l l 4 2 0 considered to be the rst computer programmer CW 19 4 double 2 4 2 O 8 0 Java Variable names forum 7quot i 5239 i I 1 Start with a letter or or 7 for i iigkg 2 11quot 2 Followed with letters or numbers System outprintln i 3 No reserved words 3 l l k 4 N0 Spaces Prints out 1 3 2 8 Primitive Java Types 7 byte short int long i Z oat double char boolean 1 4 1 2 3 2 2 3 1 2 4 2 2 4 1 lfn0 H D 8 int iA3 iB 6 i0 Systemoutprintln Round iC iA iB else ifn l H n H D n n Systemoutprintln Pointy F System39OUt39prlntlnlA Jr 1B else n n System outprintln Other SWitch n Prints out 2 7 10 case 0 C356 8 Because if 7 or comes before the EEStem39out39pmntlm Round quot variable it updates before the case 1 39 expression is carried out If or case 4 comes after the variable it does not case 7 update until after the expression is SystemoutprintlnquotPointyquot carries out break 39 defaul int i dY 15 4 double 2 fori0 ilt9 i 320 Systemoutprinti 60 1x 7 int 93 7 10 int iCount O initialize counter 9 7 10 whileiCount lt 9 Error loss of precision dY 7 double 15 4 SystemoutprintiCount 30 iCount String greeting Hellol char e greetcharAtl String ll greetingsubstring2 4 Anthony Gaskill 7 CS 1113 7 Exam 2 122 import java textDec1malForm Systemoutprintf 102f price Decm l nmatfmt int price t new DeclmalFormatquot00quot Prints out 122 double speedlelt 6566 Systemout prlntlnfmt format speedlelt Prints out 65 7 Read and count the data values Find the smallest value among the data values Compute the sum of the data values import javautilScanner public class sentln public statlc Void malnStrngl args Scanner scan new ScannerSystem1n numberOfValues O iSum 0 HHHH 5555 nnnn scannextlnt lVal whlle1Val 1 i999 Systemoutpr1ntlnquotNo of Values SystemoutprlntlnquotSmallest Systemoutprintlnquotsum quot numberOfValues quot 18mallest quot iSum Anthony Gaskill 7 CS 1113 7 Exam 3 dY int 25 05 2 05 40 iX l 11 2 4 9 7 l 5 4 7 l 20 7 l 6 5 dY double ll 4 20 dY 19 4 double 2 4 20 80 Magic number 7 number that appears in a program without explanation bad programming habit Portable program 7 program that can operate independently on any computer Charles Babbage invented the rst general purpose digital computer Ada August Byron Countess Lovelace is considered to be the rst computer programmer JavaVariablenames MainadvantageofcombSort forint i 1 i lt2 i 1 Startwithaletteroror algorithm comparedto selection forint j 3 j lt 4 jll 2 Followedwith letters or sort forint k 2 k gt 1 k2 mbers Ittxfaner I systemoutprintlni n n 3 No reservedwords Basic idea ofcombSortWith n n k 4 No spaces ia r j Prints out In early passer m combSort 8Primitive Java Types7b e elements that areintemhangedare l 3 2 short int long oat double char arapart so that in later panes 1 3 1 Boolean theydo not have to be movedfar l 4 2 1 4 1 2 3 2 2 3 1 2 4 2 2 4 1 if 1 D Z w int iA 3 iB 6 iC Systemoutprintln Round iC iA iB else ifn H D4 1111 7 n n systamoutprumlnvmoumynn SystemoutprintlniA I 1B else lc Systemoutprintln Other t h Prints out SWl C I 2 7 10 case 0 68 8 Because if 7 or comes before the E MEnmmpmnthm Rmmd variable it updates before the case 1 39 expression is carried out If or case4 comes after the variable it does not C8587 update until after the expression is SystemoutprintlnquotPointyquot carries out brea y39 default System outprintlnquotOtherquot int i fori 0 i lt 9 i Systemoutprinti int iCount O initialize counter whileiCount lt m SystemoutprintiCount iCount dY 15 4 3 20 60 int 93 7 10 97 10 double 2 ix Error double 30 loss of precision dY 15 4 String greeting Hellol char e greetcharAt1L String ll greetingsubstring2 4 int price i 122 Anthony Gaskill 7 CS 1113 7 Exam 3 Systemoutprintf 102f price Prints out Given a String s trl Write a segment of Java code that Will compute 1mport javatextDec1malForm t Dec1malFormat fmt new Dec1malFormatquot00quot double speedL1m1t 65 66F Systemout pr1ntlnfmt format speedL1m1t Pr1nt5 out 65 7 an print the number of numerals digits in the given String strl For example if strl contained Today 395 April 22 2010 quot then your code Would print 6 because the given String contains the six numerals 2 2 2 0 1 0 Of course your code murtbe general 1nt nD1g 0 for1nt 1 O 1 lt strllength 1 fstrlcharAt1 gt 39039 ampamp StrlcharAti lt 39939 g Systemoutpr1ntlnnD1g Write a public static method that sorts and array into publ1 c stat1c vo1d sortArraydouble dArr l double temp temp storage for rearranging ar for1nt 1PosM1n O 1PosM1n lt n 1PosM1n l for1nt 1 O 1 lt n 1 l 1fdArri gt dArr1PoSMin l temp dArr1PosM1n dArr1PosM1n dArr1 dArr1 temp l l end 1nner for l end outer f l end sortArray method Read and count the data values Find the smallest value among the data values Compute the sum of the data values 1mport j avaut11 Scanner publ1c class sent1 System outpr1ntlnquotNo System outpr1ntlnquotSmallest System outpr1ntlnquotSum l nel publ1c stat1c vo1d ma1nStr1ng i args Scanner scan new ScannerSystem1n 1nt numberOfValueS y39 1nt 1Sum 39 1nt 1Val scanneXtInt 1nt 1Smallest 1Val wh1le1Val 7 e999 l l 1f1Val lt 1Smallest lsmallest 1Val numberOfValues 1Sum Va y scannext1nt of Values quot lsmallest 7 quot 1Sum 1nt n increasing order ray quot numberOfValues Anthony Gaskill 7 CS 1113 7 Exam 3 public class Course private String dept private int courseNumber private int enrollment public CourseString deptPar int courseNumberPar int enrollmentPar dept deptPar courseNumber courseNumberPar enrollment enrollmentPar public void setEnrollmentint enrollmentPar enrollment enrollmentPar public String getDept return dept public int getCourseNumber return courseNumber public int getEnrollment return enrollment l public String toString return quotnCourse Descriptio n and Enrollment quot quotn Department quot dept quotn Course number quot courseNumber quot1 Enrollment quot enrollment l public boolean equalsCourse course return deptequals coursedept amp courseNumber coursecourseNumber l public int compareToCourse course Compares only the dept names and course numbers if ldept equals course dept return dept compareTocourse dept else returncourseNumber coursecourseNumber end of public class Course Anthony Gaskill 7 CS 1113 7 Exam 3 Employee empl new Employee Jones l234 How to resize an array Employee emp2 new Employee Brown 5678 empl emp2 public static double dResizedouble data emp2 setName Green Systemoutprintlnempl double dTemp new double2datalength forint i O i lt datalength i Prints dTempi datai Employee Name Green No 5678 return dTemp returns new array with same contents as old array but This is because empl emp2 merely redirects the 2 name empl to point at the contents of emp double the length Don t forget to use toString formatting when printing Writea public static method sortOnKs to sortanarray pAr of n an int objects oftype Pitcher into increasing order of their values of iKs Use this to compare ifpArri geth lt pArriMin geth Given a le of data such as 234 Smith 3 55 Jones 307 Brown 0 Where 999 0 is a sentinel value not a data value a static method readstudents with parameters array of Student objects and scan a Scanner object that Will read in data formatted as above instantiate Student objects store those objects into the array stuArr and return the number of objects stored into the array stuArr Declare the sentinel value as a named constant For full credit use a sentinel loop You may assume that each data line is valid consisting of one double and one String Your method must check that the array is large enough to hold all of the data lines and i programming is required Of course your code muxtbe general fnot print an error message and terminate No other defensive public static int readStudentsStudent stuArr Scanner scan final double SENTINEL i9990 double gpa while gpa l SENTINEL 1 String name scannext ifcounter gt stuArrlength l Systemoutprintln Error array overflowl Systemexit0 uArrcounter new Studentgpa name counterr gpa scannextDouble l return counter
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'