Unformatted text preview:

COMP 14 Introduction to ProgrammingTodayVariablesVariables - StepsDeclaration of VariablesAssignmentSlide 7Slide 8Example (average score)Named ConstantQuestionsSlide 12InputReading Keyboard InputSlide 15Slide 16Slide 17Slide 18String to NumbersSlide 20OutputExamplesString ConcatenationSlide 24Slide 25Writing a Whole ProgramClass LibrariesPackagesUsing PackagesUsing Predefined Classes and MethodsSlide 31Creating a Java ProgramProgramming in JavaSlide 34Anatomy of a Java ProgramThrows ClauseThrowsImport StatementsThe main methodSkeletonstaticStyleSlide 43Style and White SpaceCommentsJava CommentsTo doThe UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian IlieCOMP 14Introduction to ProgrammingAdrian IlieJune 28, 2005The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie2Today•Variables and expressions•Input/Output•Writing a whole programThe UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie3Variables•Associated with data♦Input data♦Output data♦Intermediate data•We need to define:♦Data type♦Identifier•Values will be assigned in expressionsThe UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie4Variables - Steps1. Identify all data from the algorithm2. Determine data types (based on the range and nature of the values)3. Find meaningful names•Example: Ch. 1, Exercise 10. Compute average score.The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie5Declaration of VariablesdataType identifier;•Must be declared before it can be used•Can be (but doesn't have to be) initialized when declared•Identifier should start in lowercase, indicate separate words with uppercase (good style)•Example:♦number of students in classint numStudents;•Multiple variables (of the same data type) can be declared on a single lineint numStudents, numGrades, total;The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie6Assignmentvariable = expresssion;•Assignment Operator (=)•expression can be a value (3) or a mathematical expression (2 + 1)•The expression must evaluate to the same data type as the variable was declaredThe UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie7Assignment•The assignment operator has a lower precedence than the arithmetic operatorsFirst the expression on the right handside of the = operator is evaluatedThen the result is stored in thevariable on the left hand sideanswer = sum / 4 + MAX * lowest;14 32The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie8Assignment•The right and left hand sides of an assignment statement can contain the same variableFirst, one is added to theoriginal value of countThen the result is stored back into count(overwriting the original value)count = count + 1;The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie9Example (average score)•Write assignments and expressions•Declaration of variables (good style)a) At the beginningb) Before being usedThe UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie10Named Constantstatic final dataType IDENTIFIER = value;•Declared by using the reserved word final•Always initialized when it is declared•Identifier should be in ALL CAPS, separate words with underscore (_) (good style)•Example:♦1 inch is always 2.54 centimetersfinal double CM_PER_INCH = 2.54;The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie11QuestionsWhat is stored in the memory location referred to by the identifier num after each of the following statements is executed in order?int num; ______num = 3; ______num = 5 + 4 - 2; ______num = num * 2; ______num = 3.4 + 5; ______unknown3714errorwould give an error since 3.4 + 5 wouldnot result in an intThe UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie12QuestionsWhich of the following are valid Java assignment statements? Assume that i, x, and percent have been declared as double variables and properly initialized.1. i = i + 5; ______2. x + 2 = x; ______3. x = 2.5 * x; ______4. percent = 10%; ______validinvalidvalidinvalidThe UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie13Input1. Standard input2. Dialog windows (tomorrow)3. File (tomorrow)The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie14Reading Keyboard Input•We can let the user assign values to variables through keyboard input•In Java, input is accomplished using objects that represent streams of data•A stream is an ordered sequence of bytes•System.in is the standard input stream object (by default, the keyboard)The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie15Reading Keyboard Input•The idea is that, with System.in, we have access to a stream of bytes coming from the keyboard.•In other words, we have access to the keys pressed on the keyboard.•But how do we actually get the values of the keys that are pressed?The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie16Reading Keyboard InputThe input stream is made up of multiple objects:InputStreamReader charReader = new InputStreamReader (System.in);BufferedReader keyboard = new BufferedReader (charReader);OR, all in one statement:BufferedReader keyboard = new BufferedReader (new InputStreamReader (System.in));1 character at a timeWhole lineat onceThe UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie17Reading Keyboard Input•The readLine method of the BufferedReader class reads an entire line of input as a StringString line = keyboard.readLine();The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie18Example (average score)•Input scores•Input weightsThe UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie19String to Numbers•String to intInteger.parseInt(strExpression)Integer.parseInt("6723") 6723•String to floatFloat.parseFloat(strExpression)Float.parseFloat("345.76") 345.76•String to doubleDouble.parseDouble(strExpression)Double.parseDouble("1234.56") 1234.56The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie20Example (average score)•Convert scores to int•Convert weights to doubleThe UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie21Output•Standard output device is usually the monitor•Access the monitor using the standard output object♦System.out•Two methods to output a string:1. print2. printlnPuts the cursor on the next line at the endThe UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie22ExamplesSystem.out.println ("Hi");System.out.println ("There");HiThereint num = 5;System.out.println ("The sum is " + (num + 3));The sum is 8int num = 5+3;System.out.println (num);8System.out.print


View Full Document

UNC-Chapel Hill COMP 14 - Lecture Notes

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?