42
CSCI1226 Introduction to Computing Science and Programming Lecture 6: Midterm Review Tami Meredith

Lecture 6: Midterm Review Tami Meredith. Programming Process How do we fill in the yellow box? Text Editor Compiler (javac) Interpreter (JVM: java) User

Embed Size (px)

Citation preview

Slide 1

CSCI1226Introduction to Computing Science and Programming Lecture 6: Midterm Review Tami MeredithProgramming ProcessHow do we fill in the yellow box?TextEditorCompiler(javac)Interpreter (JVM: java)UserLibrariesOperating System (Windows)OutputInputJDKProgramming RequiresIdentification of the problemUnderstanding it, identifying correct solutionsSolving the problem:Selecting the data structures, andData Management is the key hereIdentifying an algorithmControl Flow is the basis of algorithmsCoding the solutionTesting, debugging, verifying the solutionWhat is a Program?Programs = Data + AlgorithmsData: the nouns, the things we manipulateAlgorithms: the verbs, the actions we performA program manipulates the input (data) to produce the required output (data)Some Common Data TypesStrings: Sequences of characters"hi there", "Hello, Bob!", "x", "RESULT"Integers: Whole numbers-3097, -2, -1, 0, 1, 10, 10340Floating Point Numbers: Things with a decimal1.0, 3.14, .333, 100000.0000001Booleans: Truth values for teststrue, false (thats it, just two Boolean values)IntegersThere are four kinds of integersThe main difference is the minimum and maximum valuesbyte: -128 to 127short: -32 768 to 32 767int: -2 147 483 648 to 2 147 483 647long: -9 223 372 036 854 775 808 to 9 223 372 036 854 775 807Difference is caused by the amount of memory used to store each onebyte = 1 byte, short = 2 bytes int = 4 bytes long = 8 bytesVariablesint i;Defines a section of memory large enough to hold an integer and names that area of memory "i", the memory contains a random value from whatever it was used for previouslyi = 5;Stores the value 5 in the area or memory named "i"int i = 5; Combines 1. and 2.A variable holds one, and only one, thingA variable always has a value ... if you put nothing there then assume it has a random value

VariablesVariables describe memory locationsValues are stored in those locationsIdentifiers name locations

int x = 13; location x 13

Right Hand Side (of =): Use the value stored in the location (used also when no = in expression)Left Hand Side (of =): Use the location to store the valueOperatorsStandard math: +, -, *, /Modulus (Remainder): %Assignment to a variable name uses =Can change the type of something using a castint x = 1;float y = (float) x;+ is also string concatenatione.g., "Hi " + "there" creates "Hi there"

Delimiters" "A string (sequence of zero or more characters)'a'A character (a single character, never empty){ }A block (sequence of zero or more statements)/* */A multi-line comment( )A math expressionA test in a loop or conditionalThe parameters of a method call The parameters in a method declarationStringsStrings are a special data type (Strings are actually objects)Ordered and numbered sequences of charactersIndexed from 0Delimited by double quotesE.g., "Hello World!", "", "tami"Large library of String manipulation methods existsString MethodsSee Figure 2.5 in the text (page 86)length() returns the length of a string (as an integer)indexOf(string2) returns the index of string2 in string or -1 if string2 is contained in stringequals(string2) returns TRUE if string equals string2 otherwise it returns FALSEString sentence = "Hello programming class";int len = sentence.length();boolean same = sentence.equals("Good bye!");

ExerciseWrite a program that has a single String variable containing your name (as 2 words)e.g., String name = "Tami Meredith";The program will print out your initialse.g., Tami Meredith's initials are: TM

Solutionpublic class initials { public static void main (String[] args) {

String name = "Tami Meredith"; int first = 0; int second = name.indexOf(" ") + 1;

System.out.print(name + "'s initials are: "); System.out.print(name.charAt(first)); System.out.println(name.charAt(second));

} // end main()} // end class initialsControl is Power!Control flow is actually very simple:Everything is sequential, unless ...Something is optionalConditional Statements, i.e,. ifSomething is repeatedLooping Statements, i.e., do, for, whileSomething complex is broken into simpler partsMethods (and classes, objects)Programs in JavaPrograms are structured hierarchically:Programs have 1 or more classesFiles are named after the classes they containClasses contain 1 or more methodsThe class that is used to start execution must contain a method named mainMethods perform 1 or more actionsActions are performed by statements

About Statements ...Statements are like sentences in a programming languageStatements usually end with a ";" (semi-colon)Statements are performed sequentially, one after the other (generally left to right, top to bottom)For example: x = x + 1;y = 2 * x;System.out.println("Y is " + y); StatementsExpressions and assignmentse.g., x = 3 * y;Conditional statements to make choicese.g., if (x == 0) System.exit(0);Loops to repeat thingse.g., while (i > 0) System.out.println(i--);Blocks to group statements into a single statemente.g., { statement1; statement2; statement3; ... }BlocksStatements can be grouped into a blockA block is treated like a single statement and can be used where a single statement is expectedBlocks are formed by surrounding a group of statements with curly braces, "{" and "}"For example: { y = x * 2; System.out.print("Two times " + x + " is " + y);}

Conditional StatementsConditionals make a choice of whether a statement should be executed:Syntax: if (test) statementWhen test evaluates to true, statement is executedstatement is singular, a block must be used to do more than one thing when test is trueAlternate Syntax:if (test) statement1 else statement2If test is equal to true then do statement1If test is equal to false then do statement2Note that there is no ";" at the end of the conditionalLoopsExecutes a statement more than onceSyntax:while (test) statementEvaluate testIf test is equal to true, do step 3, otherwise donePerform statement then go back to 1Note that statement is singular and that the loop does not end with a ";" (but statement does)

Example // Code fragment, counts from 0 to // the value "stop" input by the user

int count = 0;int stop = user.nextInt();

while (count