Java Review. Java Program Structure // comments about the class public class MyProgram { Variables

Size: px
Start display at page:

Download "Java Review. Java Program Structure // comments about the class public class MyProgram { Variables"

Transcription

1 Java Program Structure // comments about the class public class MyProgram { Java Review class header class body Comments can be placed almost anywhere This class is written in a file named: MyProgram.java 2 Java Program Structure // comments about the class public class MyProgram { // comments about the method public static void main(string[] args) { method header method body Variables Variable is a name for a location in memory 2 types of variables Primitive Reference Must have a type Must have a name Starts with a letter, underscore (_), or dollar sign ($) Cannot be a reserved word (public, void, static, int, ) data type variable name (see p.6 in the text for further discussion) 3 int total; Variables A variable can be given an initial value in the declaration int sum = 0; int base = 32, max = 149; Assignment An assignment statement changes the value of a variable The assignment operator is the = sign total = 55; When a variable is not initialized, the value of that variable is undefined. When a variable is referenced in a program, its current value is used The expression on the right is evaluated and the result is stored in the variable on the left The value that was in total is overwritten You can assign only a value to a variable that is consistent with the variable's declared type 1

2 Constants A constant is an identifier that is similar to a variable except that it holds one value while the program is active The compiler will issue an error if you try to change the value of a constant during execution In Java, we use the final modifier to declare a constant final int MIN_HEIGHT = 69; Constants: give names to otherwise unclear literal values facilitate updates of values used throughout a program prevent inadvertent attempts to change a value Primitive types There are exactly eight primitive data types in Java Four of them represent integers: byte, short, int, long Two of them represent floating point numbers: float, double One of them represents characters: char And one of them represents boolean values: boolean Numeric Primitive Types The difference between the various numeric primitive types is their size, and therefore the values they can store: Type byte short int long Storage 8 bits 16 bits 32 bits 64 bits Min Value ,768-2,147,483,648 < -9 x Max Value ,767 2,147,483,647 > 9 x Non-numeric Primitive Types A boolean value represents a or condition A char variable stores a single character from the Unicode character set Character literals are delimited by single quotes: 'a' 'X' '7' '$' ',' '\n' float double 32 bits 64 bits +/- 3.4 x with 7 significant digits +/- 1.7 x with 15 significant digits Sidetrack- System.out The System.out object represents a destination to which we can send output System.out.println("Whatever you are, be a good one."); Our first small program public class MyProgram { public static void main(string[] args) { int x = 22; char y = A ; object method information provided to the method (parameters) System.out.println(x); System.out.println(y); 2

3 Arithmetic Expressions An expression is a combination of one or more operands and their operators Arithmetic expressions compute numeric results and make use of the arithmetic operators: Addition + Subtraction - Multiplication * Division / Remainder % If either or both operands associated with an arithmetic operator are floating point, the result is a floating point Operator Precedence Operators can be combined into complex expressions result = total + count / max - offset; Operators have a well-defined precedence which determines the order in which they are evaluated Multiplication, division, and remainder are evaluated prior to addition, subtraction, and string concatenation Arithmetic operators with the same precedence are evaluated from left to right Parentheses can be used to force the evaluation order Increment and Decrement Increment and Decrement The increment and decrement operators are arithmetic and operate on one operand The increment operator (++) adds one to its operand The decrement operator (--) subtracts one from its operand The statement count++; is functionally equivalent to count = count + 1; The increment and decrement operators can be applied in prefix form (before the operand) or postfix form (after the operand) When used alone in a statement, the prefix and postfix forms are functionally equivalent. That is, count++; is equivalent to ++count; Increment and Decrement When used in a larger expression, the prefix and postfix forms have different effects In both cases the variable is incremented (decremented) But the value used in the larger expression depends on the form used: Increment and Decrement If count currently contains 45, then the statement total = count++; assigns 45 to total and 46 to count If count currently contains 45, then the statement Expression count++ ++count count-- --count Operation add 1 add 1 subtract 1 subtract 1 Value Used in Expression old value new value old value new value total = ++count; assigns the value 46 to both total and count

4 Assignment Operators Often we perform an operation on a variable, and then store the result back into that variable Java provides assignment operators to simplify that process For example, the statement is equivalent to num += count; Assignment Operators There are many assignment operators, including the following: Operator += -= *= /= %= Example x += y x -= y x *= y x /= y x %= y Equivalent To x = x + y x = x - y x = x * y x = x / y x = x % y num = num + count; Assignment Operators Relational operators The right hand side of an assignment operator can be a complex expression The entire right-hand expression is evaluated first, then the result is combined with the original variable Therefore result /= (total-min) % num; is equivalent to result = result / ((total-min) % num); > >= < <= ==!= greater than greater than or equal to less than less than or equal to equal to not equal to 21 Conditional Test Conditional test is an expression that results in a boolean value. Uses relational operators If we have the statement int x = 3; the conditional test (x >= 2) evaluates to. Conditional Statements A conditional statement lets us choose which statement will be executed next by using a conditional test Therefore they are sometimes called selection statements Conditional statements give us the power to make basic decisions Java's conditional statements are the if statement the if-else statement the switch statement 4

5 The if Statement The if statement has the following syntax: if is a Java reserved word The condition must be a boolean expression. It must evaluate to either or. if (condition) statement; If the condition is, the statement is executed. If it is, the statement is skipped. The if-else Statement An else clause can be added to an if statement to make an if-else statement if ( condition ) statement1; else statement2; If the condition is, statement1 is executed; if the condition is, statement2 is executed One or the other will be executed, but not both Block Statements Several statements can be grouped together into a block statement A block is delimited by braces : { A block statement can be used wherever a statement is called for by the Java syntax Example: if (guess == answer) { System.out.println( You guessed correct! ); correct++; else { System.out.println( You guessed wrong. ); wrong++; Nested if Statements The statement executed as a result of an if statement or else clause could be another if statement These are called nested if statements An else clause is matched to the last unmatched if (no matter what the indentation implies!) Braces can be used to specify the if statement to which an else clause belongs Multiway Selection: Else if Sometime you want to select one option from several alternatives if (conditon1) statement1; else if (condition2) statement2; else if (condition3) statement3; else statement4; conditon1 evaluated conditon2 evaluated conditon3 evaluated statement4 statement1 statement2 statement3 Else if example double numbergrade = 83.6; char lettergrade; if (numbergrade >= 89.5) { lettergrade = A ; else if (numbergrade >= 79.5) { lettergrade = B ; else if (numbergrade >= 69.5) { lettergrade = C ; else if (numbergrade >= 59.5) { lettergrade = D ; else { lettergrade = F ; Output: My Grade is 83.6, B System.out.println( My Grade is + numbergrade +, + lettergrade); 5

6 The switch Statement The switch statement provides another means to decide which statement to execute next The switch statement evaluates an expression, then attempts to match the result to one of several possible cases Each case contains a value and a list of statements The flow of control transfers to statement associated with the first value that matches 31 The switch Statement The general syntax of a switch statement is: switch and case are reserved words switch (expression) { case value1: statement-list1 case value2: statement-list2 case value3: statement-list3 If expression matches value2, control jumps to here The switch Statement Often a break statement is used as the last statement in each case's statement list A break statement causes control to transfer to the end of the switch statement If a break statement is not used, the flow of control will continue into the next case Sometimes this can be appropriate, but usually we want to execute only the statements associated with one case The switch Statement A switch statement can have an optional default case The default case has no associated value and simply uses the reserved word default If the default case is present, control will transfer to it if no other case value matches Though the default case can be positioned anywhere in the switch, usually it is placed at the end If there is no default case, and no other value matches, control falls through to the statement after the switch The switch Statement The expression of a switch statement must result in an integral type, meaning an int or a char It cannot be a boolean value, a floating point value (float or double), a byte, a short, or a long The implicit boolean condition in a switch statement is equality - it tries to match the expression with a value You cannot perform relational checks with a switch statement Switch example char letter = 'b'; switch (letter) { case 'a': System.out.println("A"); break; case 'b': System.out.println("B"); break; case 'c': System.out.println("C"); break; case 'd': System.out.println("D"); break; default: System.out.println(?"); B char letter = 'b'; switch (letter) { case 'a': System.out.println("A"); case 'b': System.out.println("B"); case 'c': System.out.println("C"); break; case 'd': System.out.println("D"); break; default: System.out.println(?"); B C 6

7 The Conditional Operator Java has a conditional operator that evaluates a boolean condition that determines which of two other expressions is evaluated The result of the chosen expression is the result of the entire conditional operator Its syntax is: condition? expression1 : expression2 The Conditional Operator The conditional operator is similar to an if-else statement, except that it forms an expression that returns a value For example: larger = ((num1 > num2)? num1 : num2); If num1 is greater that num2, then num1 is assigned to larger; otherwise, num2 is assigned to larger If the condition is, expression1 is evaluated; if it is, expression2 is evaluated 37 The conditional operator is ternary because it requires three operands 38 The Conditional Operator Another example: System.out.println ("Your change is " + count + ((count == 1)? "Dime" : "Dimes")); If count equals 1, then "Dime" is printed If count is anything other than 1, then "Dimes" is printed 39 Logical Operators Boolean expressions can use the following logical operators:! Logical NOT && Logical AND Logical OR They all take boolean operands and produce boolean results Logical NOT is a unary operator (it operates on one operand) Logical AND and logical OR are binary operators (each operates on two operands) 40 Logical NOT The logical NOT operation is also called logical negation or logical complement If some boolean condition a is, then!a is ; if a is, then!a is Logical AND and Logical OR The logical AND expression a && b is if both a and b are, and otherwise Logical expressions can be shown using truth tables a!a 41 The logical OR expression a b is if a or b or both are, and otherwise 42 7

8 Truth Tables A truth table shows the possible / combinations of the terms Since && and each have two operands, there are four possible combinations of conditions a and b a b a && b a b Logical Operators Conditions can use logical operators to form complex expressions if ((total < MAX+5) &&!found) System.out.println ("Processing "); Logical operators have precedence relationships among themselves and with other operators all logical operators have lower precedence than the relational or arithmetic operators logical NOT has higher precedence than logical AND and logical OR 44 Short Circuited Operators The processing of logical AND and logical OR is short-circuited If the left operand is sufficient to determine the result, the right operand is not evaluated if (count!= 0 && total/count > MAX) System.out.println ("Testing "); This type of processing must be used carefully Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional statements, they are controlled by boolean expressions Java has three kinds of repetition statements: the while loop the do loop the for loop The programmer should choose the right kind of loop for the situation The while Statement Logic of a while Loop The while statement has the following syntax: while is a reserved word while (condition) statement; If the condition is, the statement is executed. Then the condition is evaluated again. condition evaluated statement The statement is executed repeatedly until the condition becomes. 47 Note that if the condition of a while statement is initially, the statement is never executed. Therefore, the body of a while loop will execute zero or more times 8

9 while Loop Example final int LIMIT = 5; int count = 1; while (count <= LIMIT) { System.out.println(count); count += 1; Output: Infinite Loops The body of a while loop eventually must make the condition If not, it is an infinite loop, which will execute until the user interrupts the program This is a common logical error You should always double check to ensure that your loops will terminate normally 50 Nested Loops The do Statement Similar to nested if statements, loops can be nested as well That is, the body of a loop can contain another loop Each time through the outer loop, the inner loop goes through its full set of iterations The do statement has the following syntax: do and while are reserved words do{ statement; while (condition); The statement is executed once initially, and then the condition is evaluated The statement is executed repeatedly until the condition becomes do-while Example Comparing while and do while loop do loop final int LIMIT = 5; int count = 1; do { System.out.println(count); count += 1; while (count <= LIMIT); Output: condition evaluated statement statement condition evaluated 9

10 The for Statement The for statement has the following syntax: Reserved word The initialization is executed once before the loop begins The statement is executed until the condition becomes for (initialization; condition; increment) statement; The for Statement A for loop is functionally equivalent to the following while loop structure: initialization; while (condition) { statement; increment; The increment portion is executed at the end of each iteration The condition-statement-increment cycle is executed repeatedly Logic of a for loop The for Statement initialization condition evaluated statement increment Like a while loop, the condition of a for statement is tested prior to executing the loop body Therefore, the body of a for loop will execute zero or more times It is well suited for executing a loop a specific number of times that can be determined in advance for Example The for Statement final int LIMIT = 5; for (int count = 1; count <= LIMIT; count++) { System.out.println(count); Output: Each expression in the header of a for loop is optional If the initialization is left out, no initialization is performed If the condition is left out, it is always considered to be, and therefore creates an infinite loop If the increment is left out, no increment operation is performed Both semi-colons are always required in the for loop header 10

11 Choosing a Loop Structure When you can t determine how many times you want to execute the loop body, use a while statement or a do statement Object Basics If it might be zero or more times, use a while statement If it will be at least once, use a do statement If you can determine how many times you want to execute the loop body, use a for statement Introduction to Objects An object represents something with which we can interact in a program Objects and Classes A class (the concept) An object (the realization) A class represents a concept, and an object represents the embodiment of a class A class is a blueprint for an object. A class can be used to create multiple objects Bank Account Multiple objects from the same class (Illustrates the idea of encapsulation) John s Bank Account Balance: $5,257 Bill s Bank Account Balance: $1,245,069 Janna s Bank Account Balance: $0.22 Classes A class has a state and behavior. Song title artist settitle() setartist() play() Instance variables (state) Things an object knows about itself Methods (behavior) Things an object can do. Character Strings Every character string is an object in Java, defined by the String class Every string literal, delimited by double quotation marks, represents a String object Look at the Java 2 API to see some of the things the String class can do 11

12 Creating Objects A variable holds either a primitive type or a reference to an object A class name can be used as a type to declare an object reference variable String title; No object is created with this declaration An object reference variable holds the address of an object The object itself must be created separately: You can t stuff an object into a variable Creating Objects Generally, we use the new operator to create an object title = new String ( Head First Java"); This calls the String constructor, which is a special method that sets up the object Creating an object is called instantiation An object is an instance of a particular class Creating Objects Because strings are so common, we don't have to use the new operator to create a String object title = "Java Software Solutions"; This is special syntax that works only for strings Once an object has been instantiated, we can use the dot operator to invoke its methods title.length() Design a Coin Class In class work We will be discussing: Constructors Method declarations Object instantiation (new operator) Data scope Tester programs Method Declarations A method declaration specifies the code that will be executed when the method is invoked (or called) When a method is invoked, the flow of control jumps to the method and executes its code Method Header A method declaration begins with a method header char calc (int num1, int num2, String message) When complete, the flow returns to the place where the method was called and continues The invocation may or may not return a value, depending on how the method is defined method name return type (primitive type, class name, or void) parameter list The parameter list specifies the type and name of each parameter The name of a parameter in the method declaration is called a formal argument 12

13 Method Body The method header is followed by the method body char calc (int num1, int num2, String message) { int sum = num1 + num2; char result = message.charat(sum); sum and result return result; are local data The return expression must be consistent with the return type. They are created each time the method is called, and are destroyed when it finishes executing Data Scope The scope of data is the area in a program in which that data can be used (referenced) Data declared at the class level can be used by all methods in that class Data declared within a method can be used only in that method Data declared within a method is called local data The return Statement The return type of a method indicates the type of value that the method sends back to the calling location A method that does not return a value has a void return type A return statement specifies the value that will be returned return expression; Its expression must conform to the return type It is good practice for a method to only have one return statement. (Unless things would become overly complex otherwise) 75 Parameters Each time a method is called, the actual parameters in the invocation are copied into the formal parameters The types of the actual parameters must be consistent with the specified types of the formal parameters. char calc (int num1, int num2, String message) { ch = obj.calc (25, count, "Hello"); int sum = num1 + num2; char result = message.charat(sum); return result; Local Data Local variables can be declared inside a method The formal parameters of a method create automatic local variables when the method is invoked When the method finishes, all local variables are destroyed (including the formal parameters) Meaning, a local variable does not exist outside the method in which it is declared. Keep in mind that instance variables, declared at the class level, exists as long as the object exists Any method in the class can refer to instance data Constructors A constructor is a special method that is used to initialize a newly created object When writing a constructor: it has the same name as the class it does not return a value it has no return type, not even void it typically sets the initial values of instance variables Each class has a default constructor that takes no parameters and it generally has no effect on the object

14 References More Object Topics Recall that an object reference variable holds the memory address of an object Rather than dealing with arbitrary addresses, we often depict a reference graphically as a pointer to an object ChessPiece bishop1 = new ChessPiece(); bishop1 80 The null Reference An object reference variable that does not currently point to an object is called a null reference The reserved word null can be used to explicitly set a null reference: name = null; or to check to see if a reference is currently null: The null Reference An object reference variable declared at the class level (an instance variable) is automatically initialized to null The programmer must carefully ensure that an object reference variable refers to a valid object before it is used if (name == null) System.out.println ("Invalid"); The this Reference The this reference allows an object to refer to itself this is a reserved word That is, the this reference, used inside a method, refers to the object through which the method is being executed The this reference The this reference can also be used to distinguish the parameters of a constructor from the corresponding instance variables with the same names public Account (Sring name, long acctnumber, double balance){ this.name = name; this.acctnumber = acctnumber; this.balance = balance; instance variables formal parameters 14

15 Assignment Revisited The act of assignment takes a copy of a value and stores it in a variable For primitive types: num2 = num1; Before After Reference Assignment For object references, assignment copies the memory location: bishop1 bishop2 = bishop1; bishop2 bishop1 bishop2 num1 num2 num1 num Before After Aliases Testing Objects for Equality Two or more references that refer to the same object are called aliases of each other One object (and its data) can be accessed using different reference variables Aliases can be useful, but should be managed carefully Changing the object s state (its variables) through one reference changes it for all of its aliases 87 The == operator compares object references for equality, returning if the references are aliases of each other bishop1 == bishop2 A method called equals is defined for all objects, but unless we redefine it when we write a class, it has the same semantics as the == operator bishop1.equals(bishop2) We can redefine the equals method to return under whatever conditions we think are appropriate Garbage Collection When an object no longer has any valid references to it, it can no longer be accessed by the program The object is useless, and therefore is called garbage Java performs automatic garbage collection periodically, returning an object's memory to the system for future use In other languages, the programmer is responsible for performing garbage collection After bishop2= bishop1; bishop1 bishop2 garbage 89 Parameter Passing All parameters in a Java method are passed by value This means that a copy of the actual parameter (the value passed in) is stored into the formal parameter (in the method header) Passing parameters is therefore similar to an assignment statement When an object is passed to a method, the actual parameter and the formal parameter become aliases of each other What are the implications of this? This is very important!!! 15

16 Parameter Passing Implications Primitive types The value of a primitive type remains unchanged outside of a method, even if the copy is changed inside of the method. Objects Any changes made to an object from within a method will persist when the method is done. public void testparams(int primtype, Cat testcat1, Cat testcat2) { primtype = 5; testcat1.setbreed( Tabby ); testcat2 = new Cat(); testcat2.setbreed( Tabby ); return; Before calling testparams: int x = 3; x cat1 cat2 Cat cat1 = new Cat(); Cat cat2 = new Cat(); cat1.setbreed( Manx ); 3 breed = Manx breed = Manx cat2.setbreed( Manx ); After calling testparams: x cat1 cat2 testparams(x, cat1, cat2); 3 breed = Tabby breed = Manx The static Modifier Static methods (also called class methods) can be invoked through the class name rather than through a particular object For example, the methods of the Math class are static: Math.sqrt(25) To write a static method, we apply the static modifier to the method definition The static modifier can be applied to variables as well It associates a variable or method with the class rather than with an object 93 Static Variables Static variables are also called class variables Normally, each object has its own data space, but if a variable is declared as static, only one copy of the variable exists private static float price; All objects created from the class share static variables 94 Static Variables Changing the value of a static variable in one object changes it for all others Constants are often declared using the static modifier as well. Since the values of constants cannot be changed, they might as well only be one copy of the value across all objects of the class. Static Methods class Helper public static int triple(int num) { int result; result = num * 3; return result; Because it is static, the method can be invoked as: value = Helper.triple(5); 96 16

17 Static Methods Wrapper Classes The order of the modifiers can be interchanged, but by convention visibility modifiers come first Recall that the main method is static; it is invoked by the system without creating an object Static methods cannot reference instance variables, because instance variables don't exist until an object exists! However, a static method can reference static variables or local variables 97 Handling both primitive data types and object references can sometimes be challenging. You may want to treat an integer as an object. We need a way to wrap a primitive type into a class so it can be treated as an object. A wrapper class represents a particular primitive type For example Integer ageobj = new Integer(20); uses the Integer class to create an object which effectively represents the integer 20 as an object This is useful when a program requires an object instead of a primitive type Wrapper Classes There is a wrapper class in the java.lang package for each primitive type: Primitive Type Wrapper Class byte Byte short Short int Integer long Long float Float double Double char Character boolean Boolean void Void Wrapper Classes Wrapper classes contain static methods that help manage the associated type For example, the Integer class contains a method to convert an integer stored in a String to an int value: num = Integer.parseInt(str); The wrapper classes often contain useful static constants as well For example, the Integer class contains MIN_VALUE and MAX_VALUE which hold the smallest and largest int values Some methods of Integer class Integer(int value) byte bytevalue() double doublevalue() int intvalue() long longvalue() static int parseint(string str) static String tobinarystring(int num) static String tohexstring(int num) Wrapper class example int value; String five = "5"; Integer intobj = new Integer(100); value = Integer.parseInt(five); value++; System.out.println(value); System.out.println(Integer.toBinaryString(value)); System.out.println(intObj.doubleValue()); output:

18 Overloading Methods Method overloading is the process of using the same method name for multiple methods The signature of each overloaded method must be unique The signature includes the number, type, and order of the parameters The return type of the method is not part of the signature The compiler determines which version of the method is being invoked by analyzing the parameters Overloading Methods Version 1 Version 2 float tryme (int x) { float tryme (int x, float y) { return x +.375; return x * y; Invocation result = tryme (25, 4.32) 103 Overloaded Methods The println method is overloaded: println(string s) println(int i) println(double d) and so on... The following lines invoke different versions of the println method: Overloading Methods Constructors can be overloaded Overloaded constructors provide multiple ways to initialize a new object System.out.println("The total is:"); System.out.println(total); Method Decomposition A method should be relatively small, so that it can be understood as a single entity A potentially large method should be decomposed into several smaller methods as needed for clarity A service method of an object may call one or more support methods to accomplish its goal Support methods could call other support methods if appropriate Arrays Arrays are objects that help us organize large amounts of information An array is an ordered list of values The entire array Each value has a numeric index has a single name scores An array of size N is indexed from zero to N-1 This array holds 10 values that are indexed from 0 to 9 18

19 Arrays A particular value in an array is referenced using the array name followed by the index in brackets For example, the expression scores[2] refers to the value 94 (the 3rd value in the array) That expression represents a place to store a single integer and can be used wherever an integer variable can be used Arrays For example, an array element can be assigned a value, printed, or used in a calculation: scores[2] = 89; scores[first] = scores[first] + 2; mean = (scores[0] + scores[1])/2; System.out.println ("Top = " + scores[5]); Arrays The values held in an array are called array elements An array stores multiple values of the same type (the element type) The element type can be a primitive type or an object reference Therefore, we can create an array of integers, or an array of characters, or an array of String objects, etc. In Java, the array itself is an object Therefore the name of the array is a object reference variable, and the array itself must be instantiated Declaring Arrays The scores array could be declared as follows: int[] scores = new int[10]; The type of the variable scores is int[] (an array of integers) Note that the type of the array does not specify its size. The reference variable scores is set to a new array object that can hold 10 integers Once an array has been declared to be a certain size, the number of values it can hold cannot be changed. Declaring Arrays Some examples of array declarations: float[] prices = new float[500]; boolean[] flags; flags = new boolean[20]; char[] codes = new char[1750]; Handling Arrays Is is often convenient to use for loops when handling arrays because the number of positions in the array is constant. Arrays are objects and their size is held in a public constant called length. Note that it holds the size of the array, not the value of the last index into the array. 19

20 double[] list = new double[5]; for (int i = 0; i < list.length; i++) { list[i] = i * 0.5; list[0] = 5.0; for (int i = 0; i < list.length; i++) { System.out.println(list[i]); Initializer Lists An initializer list can be used to instantiate and initialize an array in one step The values are delimited by braces and separated by commas Examples: int[] units = {147, 323, 89, 933, 540, 269, 97, 114, 298, 476; char[] lettergrades = {'A', 'B', 'C', 'D', F'; Initializer Lists Note that when an initializer list is used: the new operator is not used no size value is specified The size of the array is determined by the number of items in the initializer list An initializer list can only be used only in the array declaration char[] lettergrades = {'A', 'B', 'C', 'D', F'; System.out.println(letterGrades.length); System.out.println(letterGrades[1]); 5 B Command-Line Arguments The signature of the main method indicates that it takes an array of String objects as a parameter These values come from command-line arguments that are provided when the interpreter is invoked For example, the following invocation of the interpreter passes an array of three String objects into main: > java StateEval pennsylvania texas arizona These strings are stored at indexes 0-2 of the parameter public class NameTag { /** * Prints a simple name tag using a greeting and a name * specified by the user. * args command line arguments. The first argument is * a greeting. The second argument is a name. */ public static void main(string[] args) { System.out.println(); System.out.println( + args[0]); System.out.println( My name is + args[1]); System.out.println(); >java NameTag Hello Janna Hello My name is Janna >java NameTag HiYa Billy HiYa My name is Billy 20

Review Chapters 1 to 4. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Review Chapters 1 to 4. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013 Review Chapters 1 to 4 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013 Introduction to Java Chapters 1 and 2 The Java Language Section 1.1 Data & Expressions Sections 2.1 2.5 Instructor:

More information

A variable is a name for a location in memory A variable must be declared

A variable is a name for a location in memory A variable must be declared Variables A variable is a name for a location in memory A variable must be declared, specifying the variable's name and the type of information that will be held in it data type variable name int total;

More information

Conditionals and Loops

Conditionals and Loops Conditionals and Loops Conditionals and Loops Now we will examine programming statements that allow us to: make decisions repeat processing steps in a loop Chapter 5 focuses on: boolean expressions conditional

More information

CSCI 2010 Principles of Computer Science. Data and Expressions 08/09/2013 CSCI

CSCI 2010 Principles of Computer Science. Data and Expressions 08/09/2013 CSCI CSCI 2010 Principles of Computer Science Data and Expressions 08/09/2013 CSCI 2010 1 Data Types, Variables and Expressions in Java We look at the primitive data types, strings and expressions that are

More information

Learning objectives: Enhancing Classes. CSI1102: Introduction to Software Design. More about References. The null Reference. The this reference

Learning objectives: Enhancing Classes. CSI1102: Introduction to Software Design. More about References. The null Reference. The this reference CSI1102: Introduction to Software Design Chapter 5: Enhancing Classes Learning objectives: Enhancing Classes Understand what the following entails Different object references and aliases Passing objects

More information

COMP-202. Objects, Part III. COMP Objects Part III, 2013 Jörg Kienzle and others

COMP-202. Objects, Part III. COMP Objects Part III, 2013 Jörg Kienzle and others COMP-202 Objects, Part III Lecture Outline Static Member Variables Parameter Passing Scopes Encapsulation Overloaded Methods Foundations of Object-Orientation 2 Static Member Variables So far, member variables

More information

Program Development. Java Program Statements. Design. Requirements. Testing. Implementation

Program Development. Java Program Statements. Design. Requirements. Testing. Implementation Program Development Java Program Statements Selim Aksoy Bilkent University Department of Computer Engineering saksoy@cs.bilkent.edu.tr The creation of software involves four basic activities: establishing

More information

Data and Expressions. Outline. Data and Expressions 12/18/2010. Let's explore some other fundamental programming concepts. Chapter 2 focuses on:

Data and Expressions. Outline. Data and Expressions 12/18/2010. Let's explore some other fundamental programming concepts. Chapter 2 focuses on: Data and Expressions Data and Expressions Let's explore some other fundamental programming concepts Chapter 2 focuses on: Character Strings Primitive Data The Declaration And Use Of Variables Expressions

More information

CSI Introduction to Software Design. Prof. Dr.-Ing. Abdulmotaleb El Saddik University of Ottawa (SITE 5-037) (613) x 6277

CSI Introduction to Software Design. Prof. Dr.-Ing. Abdulmotaleb El Saddik University of Ottawa (SITE 5-037) (613) x 6277 CSI 1102 Introduction to Software Design Prof. Dr.-Ing. Abdulmotaleb El Saddik University of Ottawa (SITE 5-037) (613) 562-5800 x 6277 elsaddik @ site.uottawa.ca abed @ mcrlab.uottawa.ca http://www.site.uottawa.ca/~elsaddik/

More information

More Programming Constructs -- Introduction

More Programming Constructs -- Introduction More Programming Constructs -- Introduction We can now examine some additional programming concepts and constructs Chapter 5 focuses on: internal data representation conversions between one data type and

More information

Introduction to Java Chapters 1 and 2 The Java Language Section 1.1 Data & Expressions Sections

Introduction to Java Chapters 1 and 2 The Java Language Section 1.1 Data & Expressions Sections Introduction to Java Chapters 1 and 2 The Java Language Section 1.1 Data & Expressions Sections 2.1 2.5 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013 2 Scope Introduce the Java programming

More information

COMP 202. More on OO. CONTENTS: static revisited this reference class dependencies method parameters variable scope method overloading

COMP 202. More on OO. CONTENTS: static revisited this reference class dependencies method parameters variable scope method overloading COMP 202 CONTENTS: static revisited this reference class dependencies method parameters variable scope method overloading More on OO COMP 202 - Week 7 1 Static member variables So far: Member variables

More information

CMPT 125: Lecture 3 Data and Expressions

CMPT 125: Lecture 3 Data and Expressions CMPT 125: Lecture 3 Data and Expressions Tamara Smyth, tamaras@cs.sfu.ca School of Computing Science, Simon Fraser University January 3, 2009 1 Character Strings A character string is an object in Java,

More information

Full file at

Full file at Java Programming: From Problem Analysis to Program Design, 3 rd Edition 2-1 Chapter 2 Basic Elements of Java At a Glance Instructor s Manual Table of Contents Overview Objectives s Quick Quizzes Class

More information

Chapter 3: Program Statements

Chapter 3: Program Statements Chapter 3: Program Statements Presentation slides for Java Software Solutions for AP* Computer Science 3rd Edition by John Lewis, William Loftus, and Cara Cocking Java Software Solutions is published by

More information

Arrays. Outline 1/7/2011. Arrays. Arrays are objects that help us organize large amounts of information. Chapter 7 focuses on:

Arrays. Outline 1/7/2011. Arrays. Arrays are objects that help us organize large amounts of information. Chapter 7 focuses on: Arrays Arrays Arrays are objects that help us organize large amounts of information Chapter 7 focuses on: array declaration and use bounds checking and capacity arrays that store object references variable

More information

CONTENTS: Compilation Data and Expressions COMP 202. More on Chapter 2

CONTENTS: Compilation Data and Expressions COMP 202. More on Chapter 2 CONTENTS: Compilation Data and Expressions COMP 202 More on Chapter 2 Programming Language Levels There are many programming language levels: machine language assembly language high-level language Java,

More information

Program Development. Chapter 3: Program Statements. Program Statements. Requirements. Java Software Solutions for AP* Computer Science A 2nd Edition

Program Development. Chapter 3: Program Statements. Program Statements. Requirements. Java Software Solutions for AP* Computer Science A 2nd Edition Chapter 3: Program Statements Presentation slides for Java Software Solutions for AP* Computer Science A 2nd Edition Program Development The creation of software involves four basic activities: establishing

More information

Sequence structure. The computer executes java statements one after the other in the order in which they are written. Total = total +grade;

Sequence structure. The computer executes java statements one after the other in the order in which they are written. Total = total +grade; Control Statements Control Statements All programs could be written in terms of only one of three control structures: Sequence Structure Selection Structure Repetition Structure Sequence structure The

More information

Anatomy of a Class Encapsulation Anatomy of a Method

Anatomy of a Class Encapsulation Anatomy of a Method Writing Classes Writing Classes We've been using predefined classes. Now we will learn to write our own classes to define objects Chapter 4 focuses on: class definitions instance data encapsulation and

More information

The Basic Parts of Java

The Basic Parts of Java Data Types Primitive Composite The Basic Parts of Java array (will also be covered in the lecture on Collections) Lexical Rules Expressions and operators Methods Parameter list Argument parsing An example

More information

Chapter 3. Selections

Chapter 3. Selections Chapter 3 Selections 1 Outline 1. Flow of Control 2. Conditional Statements 3. The if Statement 4. The if-else Statement 5. The Conditional operator 6. The Switch Statement 7. Useful Hints 2 1. Flow of

More information

COMP 202. More on OO. CONTENTS: static revisited this reference class dependencies method parameters variable scope method overloading

COMP 202. More on OO. CONTENTS: static revisited this reference class dependencies method parameters variable scope method overloading COMP 202 CONTENTS: static revisited this reference class dependencies method parameters variable scope method overloading More on OO COMP 202 Objects 3 1 Static member variables So far: Member variables

More information

JAVA OPERATORS GENERAL

JAVA OPERATORS GENERAL JAVA OPERATORS GENERAL Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups: Arithmetic Operators Relational Operators Bitwise Operators

More information

Chapter 3: Operators, Expressions and Type Conversion

Chapter 3: Operators, Expressions and Type Conversion 101 Chapter 3 Operators, Expressions and Type Conversion Chapter 3: Operators, Expressions and Type Conversion Objectives To use basic arithmetic operators. To use increment and decrement operators. To

More information

ECE 122. Engineering Problem Solving with Java

ECE 122. Engineering Problem Solving with Java ECE 122 Engineering Problem Solving with Java Lecture 5 Anatomy of a Class Outline Problem: How do I build and use a class? Need to understand constructors A few more tools to add to our toolbox Formatting

More information

Encapsulation. You can take one of two views of an object: internal - the structure of its data, the algorithms used by its methods

Encapsulation. You can take one of two views of an object: internal - the structure of its data, the algorithms used by its methods Encapsulation You can take one of two views of an object: internal - the structure of its data, the algorithms used by its methods external - the interaction of the object with other objects in the program

More information

Program Elements -- Introduction

Program Elements -- Introduction Program Elements -- Introduction We can now examine the core elements of programming Chapter 3 focuses on: data types variable declaration and use operators and expressions decisions and loops input and

More information

ECE 122 Engineering Problem Solving with Java

ECE 122 Engineering Problem Solving with Java ECE 122 Engineering Problem Solving with Java Lecture 3 Expression Evaluation and Program Interaction Outline Problem: How do I input data and use it in complicated expressions Creating complicated expressions

More information

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

Weiss Chapter 1 terminology (parenthesized numbers are page numbers) Weiss Chapter 1 terminology (parenthesized numbers are page numbers) assignment operators In Java, used to alter the value of a variable. These operators include =, +=, -=, *=, and /=. (9) autoincrement

More information

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups:

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups: JAVA OPERATORS GENERAL Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups: Arithmetic Operators Relational Operators Bitwise Operators

More information

Object oriented programming. Instructor: Masoud Asghari Web page: Ch: 3

Object oriented programming. Instructor: Masoud Asghari Web page:   Ch: 3 Object oriented programming Instructor: Masoud Asghari Web page: http://www.masses.ir/lectures/oops2017sut Ch: 3 1 In this slide We follow: https://docs.oracle.com/javase/tutorial/index.html Trail: Learning

More information

ECE 122. Engineering Problem Solving with Java

ECE 122. Engineering Problem Solving with Java ECE 122 Engineering Problem Solving with Java Lecture 15 Class Relationships Outline Problem: How can I create and store complex objects? Review of static methods Consider static variables What about objects

More information

Chapter. Let's explore some other fundamental programming concepts

Chapter. Let's explore some other fundamental programming concepts Data and Expressions 2 Chapter 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design 2007 Pearson Addison-Wesley. All rights reserved Data and Expressions Let's explore some

More information

Java Primer 1: Types, Classes and Operators

Java Primer 1: Types, Classes and Operators Java Primer 1 3/18/14 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldwasser, Wiley, 2014 Java Primer 1: Types,

More information

CS 231 Data Structures and Algorithms, Fall 2016

CS 231 Data Structures and Algorithms, Fall 2016 CS 231 Data Structures and Algorithms, Fall 2016 Dr. Bruce A. Maxwell Department of Computer Science Colby College Course Description Focuses on the common structures used to store data and the standard

More information

COMP 202 Java in one week

COMP 202 Java in one week CONTENTS: Basics of Programming Variables and Assignment Data Types: int, float, (string) Example: Implementing a calculator COMP 202 Java in one week The Java Programming Language A programming language

More information

Objects and Classes -- Introduction

Objects and Classes -- Introduction Objects and Classes -- Introduction Now that some low-level programming concepts have been established, we can examine objects in more detail Chapter 4 focuses on: the concept of objects the use of classes

More information

Primitive Data, Variables, and Expressions; Simple Conditional Execution

Primitive Data, Variables, and Expressions; Simple Conditional Execution Unit 2, Part 1 Primitive Data, Variables, and Expressions; Simple Conditional Execution Computer Science S-111 Harvard University David G. Sullivan, Ph.D. Overview of the Programming Process Analysis/Specification

More information

Operators and Expressions

Operators and Expressions Operators and Expressions Conversions. Widening and Narrowing Primitive Conversions Widening and Narrowing Reference Conversions Conversions up the type hierarchy are called widening reference conversions

More information

\n is used in a string to indicate the newline character. An expression produces data. The simplest expression

\n is used in a string to indicate the newline character. An expression produces data. The simplest expression Chapter 1 Summary Comments are indicated by a hash sign # (also known as the pound or number sign). Text to the right of the hash sign is ignored. (But, hash loses its special meaning if it is part of

More information

Chapter 4: Writing Classes

Chapter 4: Writing Classes Chapter 4: Writing Classes Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus Writing Classes We've been using predefined classes. Now we will learn to write our own

More information

Introduction to Programming Using Java (98-388)

Introduction to Programming Using Java (98-388) Introduction to Programming Using Java (98-388) Understand Java fundamentals Describe the use of main in a Java application Signature of main, why it is static; how to consume an instance of your own class;

More information

Course Outline. Introduction to java

Course Outline. Introduction to java Course Outline 1. Introduction to OO programming 2. Language Basics Syntax and Semantics 3. Algorithms, stepwise refinements. 4. Quiz/Assignment ( 5. Repetitions (for loops) 6. Writing simple classes 7.

More information

We now start exploring some key elements of the Java programming language and ways of performing I/O

We now start exploring some key elements of the Java programming language and ways of performing I/O We now start exploring some key elements of the Java programming language and ways of performing I/O This week we focus on: Introduction to objects The String class String concatenation Creating objects

More information

Topics. Chapter 5. Equality Operators

Topics. Chapter 5. Equality Operators Topics Chapter 5 Flow of Control Part 1: Selection Forming Conditions if/ Statements Comparing Floating-Point Numbers Comparing Objects The equals Method String Comparison Methods The Conditional Operator

More information

Java+- Language Reference Manual

Java+- Language Reference Manual Fall 2016 COMS4115 Programming Languages & Translators Java+- Language Reference Manual Authors Ashley Daguanno (ad3079) - Manager Anna Wen (aw2802) - Tester Tin Nilar Hlaing (th2520) - Systems Architect

More information

COMP 202. Java in one week

COMP 202. Java in one week COMP 202 CONTENTS: Basics of Programming Variables and Assignment Data Types: int, float, (string) Example: Implementing a calculator Java in one week The Java Programming Language A programming language

More information

Outline. Overview. Control statements. Classes and methods. history and advantage how to: program, compile and execute 8 data types 3 types of errors

Outline. Overview. Control statements. Classes and methods. history and advantage how to: program, compile and execute 8 data types 3 types of errors Outline Overview history and advantage how to: program, compile and execute 8 data types 3 types of errors Control statements Selection and repetition statements Classes and methods methods... 2 Oak A

More information

CS111: PROGRAMMING LANGUAGE II

CS111: PROGRAMMING LANGUAGE II 1 CS111: PROGRAMMING LANGUAGE II Computer Science Department Lecture 1: Introduction Lecture Contents 2 Course info Why programming?? Why Java?? Write once, run anywhere!! Java basics Input/output Variables

More information

1007 Imperative Programming Part II

1007 Imperative Programming Part II Agenda 1007 Imperative Programming Part II We ve seen the basic ideas of sequence, iteration and selection. Now let s look at what else we need to start writing useful programs. Details now start to be

More information

Operators. Java operators are classified into three categories:

Operators. Java operators are classified into three categories: Operators Operators are symbols that perform arithmetic and logical operations on operands and provide a meaningful result. Operands are data values (variables or constants) which are involved in operations.

More information

Chapter 4: Conditionals and Loops

Chapter 4: Conditionals and Loops Chapter 4: Conditionals and Loops CS 121 Department of Computer Science College of Engineering Boise State University August 21, 2017 Chapter 4: Conditionals and Loops CS 121 1 / 69 Chapter 4 Topics Flow

More information

CMPT 125: Lecture 4 Conditionals and Loops

CMPT 125: Lecture 4 Conditionals and Loops CMPT 125: Lecture 4 Conditionals and Loops Tamara Smyth, tamaras@cs.sfu.ca School of Computing Science, Simon Fraser University January 17, 2009 1 Flow of Control The order in which statements are executed

More information

Packages & Random and Math Classes

Packages & Random and Math Classes Packages & Random and Math Classes Quick review of last lecture September 6, 2006 ComS 207: Programming I (in Java) Iowa State University, FALL 2006 Instructor: Alexander Stoytchev Objects Classes An object

More information

Chapter 5: Enhancing Classes

Chapter 5: Enhancing Classes Chapter 5: Enhancing Classes Presentation slides for Java Software Solutions for AP* Computer Science 3rd Edition by John Lewis, William Loftus, and Cara Cocking Java Software Solutions is published by

More information

Conditional Programming

Conditional Programming COMP-202 Conditional Programming Chapter Outline Control Flow of a Program The if statement The if - else statement Logical Operators The switch statement The conditional operator 2 Introduction So far,

More information

ECE 122. Engineering Problem Solving with Java

ECE 122. Engineering Problem Solving with Java ECE 122 Engineering Problem Solving with Java Lecture 10 For Loops and Arrays Outline Problem: How can I perform the same operations a fixed number of times? Considering for loops Performs same operations

More information

Chapter 1 Summary. Chapter 2 Summary. end of a string, in which case the string can span multiple lines.

Chapter 1 Summary. Chapter 2 Summary. end of a string, in which case the string can span multiple lines. Chapter 1 Summary Comments are indicated by a hash sign # (also known as the pound or number sign). Text to the right of the hash sign is ignored. (But, hash loses its special meaning if it is part of

More information

Constants. Why Use Constants? main Method Arguments. CS256 Computer Science I Kevin Sahr, PhD. Lecture 25: Miscellaneous

Constants. Why Use Constants? main Method Arguments. CS256 Computer Science I Kevin Sahr, PhD. Lecture 25: Miscellaneous CS256 Computer Science I Kevin Sahr, PhD Lecture 25: Miscellaneous 1 main Method Arguments recall the method header of the main method note the argument list public static void main (String [] args) we

More information

3. Java - Language Constructs I

3. Java - Language Constructs I Educational Objectives 3. Java - Language Constructs I Names and Identifiers, Variables, Assignments, Constants, Datatypes, Operations, Evaluation of Expressions, Type Conversions You know the basic blocks

More information

11/19/2014. Objects. Chapter 4: Writing Classes. Classes. Writing Classes. Java Software Solutions for AP* Computer Science A 2nd Edition

11/19/2014. Objects. Chapter 4: Writing Classes. Classes. Writing Classes. Java Software Solutions for AP* Computer Science A 2nd Edition Chapter 4: Writing Classes Objects An object has: Presentation slides for state - descriptive characteristics Java Software Solutions for AP* Computer Science A 2nd Edition by John Lewis, William Loftus,

More information

Method OverLoading printf method Arrays Declaring and Using Arrays Arrays of Objects Array as Parameters

Method OverLoading printf method Arrays Declaring and Using Arrays Arrays of Objects Array as Parameters Outline Method OverLoading printf method Arrays Declaring and Using Arrays Arrays of Objects Array as Parameters Variable Length Parameter Lists split() Method from String Class Integer & Double Wrapper

More information

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups:

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups: Basic Operators Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups: Arithmetic Operators Relational Operators Bitwise Operators

More information

Objectives. In this chapter, you will:

Objectives. In this chapter, you will: Objectives In this chapter, you will: Become familiar with functions, special symbols, and identifiers in C++ Explore simple data types Discover how a program evaluates arithmetic expressions Learn about

More information

Prof. Navrati Saxena TA: Rochak Sachan

Prof. Navrati Saxena TA: Rochak Sachan JAVA Prof. Navrati Saxena TA: Rochak Sachan Operators Operator Arithmetic Relational Logical Bitwise 1. Arithmetic Operators are used in mathematical expressions. S.N. 0 Operator Result 1. + Addition 6.

More information

Zheng-Liang Lu Java Programming 45 / 79

Zheng-Liang Lu Java Programming 45 / 79 1 class Lecture2 { 2 3 "Elementray Programming" 4 5 } 6 7 / References 8 [1] Ch. 2 in YDL 9 [2] Ch. 2 and 3 in Sharan 10 [3] Ch. 2 in HS 11 / Zheng-Liang Lu Java Programming 45 / 79 Example Given a radius

More information

Chapter 2. Elementary Programming

Chapter 2. Elementary Programming Chapter 2 Elementary Programming 1 Objectives To write Java programs to perform simple calculations To obtain input from the console using the Scanner class To use identifiers to name variables, constants,

More information

Index COPYRIGHTED MATERIAL

Index COPYRIGHTED MATERIAL Index COPYRIGHTED MATERIAL Note to the Reader: Throughout this index boldfaced page numbers indicate primary discussions of a topic. Italicized page numbers indicate illustrations. A abstract classes

More information

CS313D: ADVANCED PROGRAMMING LANGUAGE

CS313D: ADVANCED PROGRAMMING LANGUAGE CS313D: ADVANCED PROGRAMMING LANGUAGE Computer Science department Lecture 2 : C# Language Basics Lecture Contents 2 The C# language First program Variables and constants Input/output Expressions and casting

More information

References. Chapter 5: Enhancing Classes. Enhancing Classes. The null Reference. Java Software Solutions for AP* Computer Science A 2nd Edition

References. Chapter 5: Enhancing Classes. Enhancing Classes. The null Reference. Java Software Solutions for AP* Computer Science A 2nd Edition Chapter 5: Enhancing Classes Presentation slides for Java Software Solutions for AP* Computer Science A 2nd Edition by John Lewis, William Loftus, and Cara Cocking Java Software Solutions is published

More information

CONTENTS: Array Usage Multi-Dimensional Arrays Reference Types. COMP-202 Unit 6: Arrays

CONTENTS: Array Usage Multi-Dimensional Arrays Reference Types. COMP-202 Unit 6: Arrays CONTENTS: Array Usage Multi-Dimensional Arrays Reference Types COMP-202 Unit 6: Arrays Introduction (1) Suppose you want to write a program that asks the user to enter the numeric final grades of 350 COMP-202

More information

Programming Constructs Overview. Method Call System.out.print( hello ); Method Parameters

Programming Constructs Overview. Method Call System.out.print( hello ); Method Parameters Programming Constructs Overview Method calls More selection statements More assignment operators Conditional operator Unary increment and decrement operators Iteration statements Defining methods 27 October

More information

Introduction to Java & Fundamental Data Types

Introduction to Java & Fundamental Data Types Introduction to Java & Fundamental Data Types LECTURER: ATHENA TOUMBOURI How to Create a New Java Project in Eclipse Eclipse is one of the most popular development environments for Java, as it contains

More information

Reviewing for the Midterm Covers chapters 1 to 5, 7 to 9. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Reviewing for the Midterm Covers chapters 1 to 5, 7 to 9. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013 Reviewing for the Midterm Covers chapters 1 to 5, 7 to 9 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013 2 Things to Review Review the Class Slides: Key Things to Take Away Do you understand

More information

1 Shyam sir JAVA Notes

1 Shyam sir JAVA Notes 1 Shyam sir JAVA Notes 1. What is the most important feature of Java? Java is a platform independent language. 2. What do you mean by platform independence? Platform independence means that we can write

More information

CPS122 Lecture: From Python to Java last revised January 4, Objectives:

CPS122 Lecture: From Python to Java last revised January 4, Objectives: Objectives: CPS122 Lecture: From Python to Java last revised January 4, 2017 1. To introduce the notion of a compiled language 2. To introduce the notions of data type and a statically typed language 3.

More information

Introduction To Java. Chapter 1. Origins of the Java Language. Origins of the Java Language. Objects and Methods. Origins of the Java Language

Introduction To Java. Chapter 1. Origins of the Java Language. Origins of the Java Language. Objects and Methods. Origins of the Java Language Chapter 1 Getting Started Introduction To Java Most people are familiar with Java as a language for Internet applications We will study Java as a general purpose programming language The syntax of expressions

More information

Basics of Java Programming

Basics of Java Programming Basics of Java Programming Lecture 2 COP 3252 Summer 2017 May 16, 2017 Components of a Java Program statements - A statement is some action or sequence of actions, given as a command in code. A statement

More information

Java Bytecode (binary file)

Java Bytecode (binary file) Java is Compiled Unlike Python, which is an interpreted langauge, Java code is compiled. In Java, a compiler reads in a Java source file (the code that we write), and it translates that code into bytecode.

More information

cis20.1 design and implementation of software applications I fall 2007 lecture # I.2 topics: introduction to java, part 1

cis20.1 design and implementation of software applications I fall 2007 lecture # I.2 topics: introduction to java, part 1 topics: introduction to java, part 1 cis20.1 design and implementation of software applications I fall 2007 lecture # I.2 cis20.1-fall2007-sklar-leci.2 1 Java. Java is an object-oriented language: it is

More information

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program Objectives Chapter 2: Basic Elements of C++ In this chapter, you will: Become familiar with functions, special symbols, and identifiers in C++ Explore simple data types Discover how a program evaluates

More information

Chapter 2: Basic Elements of C++

Chapter 2: Basic Elements of C++ Chapter 2: Basic Elements of C++ Objectives In this chapter, you will: Become familiar with functions, special symbols, and identifiers in C++ Explore simple data types Discover how a program evaluates

More information

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction Chapter 2: Basic Elements of C++ C++ Programming: From Problem Analysis to Program Design, Fifth Edition 1 Objectives In this chapter, you will: Become familiar with functions, special symbols, and identifiers

More information

Chapter 2: Data and Expressions

Chapter 2: Data and Expressions Chapter 2: Data and Expressions CS 121 Department of Computer Science College of Engineering Boise State University January 15, 2015 Chapter 2: Data and Expressions CS 121 1 / 1 Chapter 2 Part 1: Data

More information

BRANCHING if-else statements

BRANCHING if-else statements BRANCHING if-else statements Conditional Statements A conditional statement lets us choose which statement t t will be executed next Therefore they are sometimes called selection statements Conditional

More information

Chapter 2: Data and Expressions

Chapter 2: Data and Expressions Chapter 2: Data and Expressions CS 121 Department of Computer Science College of Engineering Boise State University August 21, 2017 Chapter 2: Data and Expressions CS 121 1 / 51 Chapter 1 Terminology Review

More information

CS313D: ADVANCED PROGRAMMING LANGUAGE

CS313D: ADVANCED PROGRAMMING LANGUAGE CS313D: ADVANCED PROGRAMMING LANGUAGE Computer Science Department Lecture 3: C# language basics Lecture Contents 2 C# basics Conditions Loops Methods Arrays Dr. Amal Khalifa, Spr 2015 3 Conditions and

More information

Definite Loops. Computer Science S-111 Harvard University David G. Sullivan, Ph.D. Using a Variable for Counting

Definite Loops. Computer Science S-111 Harvard University David G. Sullivan, Ph.D. Using a Variable for Counting Unit 2, Part 2 Definite Loops Computer Science S-111 Harvard University David G. Sullivan, Ph.D. Using a Variable for Counting Let's say that we're using a variable i to count the number of times that

More information

Object Oriented Software Design

Object Oriented Software Design Object Oriented Software Design Introduction to Java - II Giuseppe Lipari http://retis.sssup.it/~lipari Scuola Superiore Sant Anna Pisa September 30, 2010 G. Lipari (Scuola Superiore Sant Anna) Introduction

More information

Object-Oriented Programming. Topic 2: Fundamental Programming Structures in Java

Object-Oriented Programming. Topic 2: Fundamental Programming Structures in Java Electrical and Computer Engineering Object-Oriented Topic 2: Fundamental Structures in Java Maj Joel Young Joel.Young@afit.edu 8-Sep-03 Maj Joel Young Java Identifiers Identifiers Used to name local variables

More information

1 Lexical Considerations

1 Lexical Considerations Massachusetts Institute of Technology Department of Electrical Engineering and Computer Science 6.035, Spring 2013 Handout Decaf Language Thursday, Feb 7 The project for the course is to write a compiler

More information

Program Fundamentals

Program Fundamentals Program Fundamentals /* HelloWorld.java * The classic Hello, world! program */ class HelloWorld { public static void main (String[ ] args) { System.out.println( Hello, world! ); } } /* HelloWorld.java

More information

IPCoreL. Phillip Duane Douglas, Jr. 11/3/2010

IPCoreL. Phillip Duane Douglas, Jr. 11/3/2010 IPCoreL Programming Language Reference Manual Phillip Duane Douglas, Jr. 11/3/2010 The IPCoreL Programming Language Reference Manual provides concise information about the grammar, syntax, semantics, and

More information

Chapter 2: Data and Expressions

Chapter 2: Data and Expressions Chapter 2: Data and Expressions CS 121 Department of Computer Science College of Engineering Boise State University April 21, 2015 Chapter 2: Data and Expressions CS 121 1 / 53 Chapter 2 Part 1: Data Types

More information

Java Basic Programming Constructs

Java Basic Programming Constructs Java Basic Programming Constructs /* * This is your first java program. */ class HelloWorld{ public static void main(string[] args){ System.out.println( Hello World! ); A Closer Look at HelloWorld 2 This

More information

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal Lesson Goals Understand the basic constructs of a Java Program Understand how to use basic identifiers Understand simple Java data types and

More information

Lexical Considerations

Lexical Considerations Massachusetts Institute of Technology Department of Electrical Engineering and Computer Science 6.035, Spring 2010 Handout Decaf Language Tuesday, Feb 2 The project for the course is to write a compiler

More information

Sir Muhammad Naveed. Arslan Ahmed Shaad ( ) Muhammad Bilal ( )

Sir Muhammad Naveed. Arslan Ahmed Shaad ( ) Muhammad Bilal ( ) Sir Muhammad Naveed Arslan Ahmed Shaad (1163135 ) Muhammad Bilal ( 1163122 ) www.techo786.wordpress.com CHAPTER: 2 NOTES:- VARIABLES AND OPERATORS The given Questions can also be attempted as Long Questions.

More information

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal Lesson Goals Understand the basic constructs of a Java Program Understand how to use basic identifiers Understand simple Java data types

More information