D0010E Object- oriented programming and design. Today. Today An introduc<on to the basic syntax and seman<cs of Java
|
|
- Alexandrina Blair
- 6 years ago
- Views:
Transcription
1 D0010E Object- oriented programming and design An introduc<on to the basic syntax and seman<cs of Java Håkan Jonsson LTU 1 1
2 1. Java programs Java is a modern programming language that is object- oriented. A Java program consists of classes. Also something called interfaces. Classes are stored in files (or databases). A file may also contain import and package declara<ons that effect the classes in the file. Exactly one class per file must be declared public. If the public class is named X, the file must have the name X.java. Class names could be of any length but have start with an upper- case lewer (A, B, C, ) and may not contain blanks. A class contains declara<ons of awributes (members). A declara<on specifies details and makes the declared known to the compiler. An awribute is a variable or a method. There are also other kinds of awributes, which we will learn about later on Håkan Jonsson LTU 4 Main Driver Car Tire... Engine > ls -la! total 600! drwxr-xr-x 7 hj staff 238 Jan 21 16:16.! drwxr-xr-x 9 hj staff 306 Jan 21 16:14..! -rw-r--r-- 1 hj staff Jan 21 16:16 Car.java! -rw-r--r-- 1 hj staff Jan 21 16:13 Driver.java! -rw-r--r-- 1 hj staff Jan 21 16:14 Engine.java! -rw-r--r-- 1 hj staff Jan 21 16:13 Main.java! -rw-r--r-- 1 hj staff Jan 21 16:15 Tire.java! >! Håkan Jonsson LTU /**! * This class represents a car.! *! hj! */! public class Car {! private static int currentspeed;! private static double fuel = 0.0d;!! /**! * This methods starts the car.! */! public static void start() {! //...!!! /**! * A call to this method stops the car by reducing the speed to! * 0, turning off the engine, and unlocking the doors.! *! */! public static void stop() {! Brakes.activate(100);! Engine.halt();! Doors.unlock();!!! /**! * Computes the current speed of the car.! the speed.! */! public static double speed() {! return currentspeed;!! //...! Håkan Jonsson LTU 6! 2
3 1a. The awribute variable A variable holds a value. Part of the state of the program. In Java, variables must be declared. Not in python, for instance. A declara<on always specifies the type. It could state the ini<al value (recommended). A variable in a method is a local variable. For non- local variables, the declara<on also specifies proper<es of the variable. public, protected, private, sta<c, final, vola<le, int numberofcars = 0;! long stars = 0L;! float degreeswarm = 21.34f;! char firstchar = A ;! double errormargin = 4.67e-16d;! boolean isok = false;! String s = "";! public static final double acc = 0.5d;! private static int steps = 0;! Håkan Jonsson LTU 8 3
4 1b. The awribute method Methods must be declared. public class MyClass {! Proper<es, type, formal public static int maximum(int a, int b) {! parameters.! boolean test;! void instead of a type means the! method does not return a result. test = a < b;! It consists of statements and! declara<ons of local variables. if (test) {! return b;! Note: A method can not contain else {! other methods. return a;! Local variables are only available in! their methods.!! The variables contain data used in the method or arguments to the method....! int x, y;! The statements specify the...! sequence of ac<ons that should // how to call a static method! be performed when the method! if (MyClass.maximum(x, y) < MAXRANGE) {! is called....! A method can be called.! Håkan Jonsson LTU Expressions An expression is a precise rule that describes how a value of some sort is computed. There are three basic expressions: The name of a variable. Stands for the current value of the variable. Constants. 0, 1, A, 3.14f, C etc. Values returned by methods. Moreover, there is one composite expression: Expressions containing operators opera<ng on expressions. Yes, a recursive defini<on. Example: (x 1) / Math.sqrt(2.0 * y) Håkan Jonsson LTU 12 4
5 Operators Associa<vity Type.! right- to- lem unary * / % lem- to- right mul<plica<ve + - lem- to- right addi<ve < <= > >= lem- to- right comparison ==!= lem- to- right comparison && lem- to- right boolean lem- to- right boolean?: right- to- lem condi<onal = right- to- lem assignment Håkan Jonsson LTU 13 Precedence Operators have precedence. Higher up in the table means higher precedence. In an expression, an operator with high precedence is computed before operators with low precedence. Examples: Operators Associa<vity Type.! right- to- lem unary + - * / % lem- to- right mul<plica<ve + - lem- to- right addi<ve < <= > >= lem- to- right comparison ==!= lem- to- right comparison && lem- to- right boolean lem- to- right boolean?: right- to- lem condi<onal = right- to- lem assignment a) * 4 means 1 + (3 * 4). b) x &&! y z means (x && (! y)) z Håkan Jonsson LTU 14 Associa<vity Operators can be associa<ve. Most are. Associa<vity determines the order in which operators with the same precedence should be applied. Lem(- to- right)- associa<ve: a b c means (a b) c. Right(- to- lem)- associa<ve: a b c means a (b c). Example: Binary subtrac<on ( ) is lem- to- right, so Operators Associa<vity Type.! right- to- lem unary + - * / % lem- to- right mul<plica<ve + - lem- to- right addi<ve < <= > >= lem- to- right comparison ==!= lem- to- right comparison && lem- to- right boolean lem- to- right boolean?: right- to- lem condi<onal = right- to- lem assignment means (2 4) 2 = 4 (and not 2 (4 2) = 2 2 = 0.) Håkan Jonsson LTU 15 5
6 Parentheses Parentheses are used to break precedence and associa<vity. They work the same way in Java as in other languages. Example: In (1 + 2) / 3 the parentheses are needed, or / 3 is interpreted as 1 + (2 / 3). Because of the precedence, and associa<vity, parentheses can omen be omiwed. However, this makes the program hard to read and easily leads to errors. Advice: Use parentheses! Operators Associa<vity Type.! right- to- lem unary + - * / % lem- to- right mul<plica<ve + - lem- to- right addi<ve < <= > >= lem- to- right comparison ==!= lem- to- right comparison && lem- to- right boolean lem- to- right boolean?: right- to- lem condi<onal = right- to- lem assignment Håkan Jonsson LTU 16 3a. Arithme<c operators +, -, *, / works as usual on numbers. Note that there are unary and binary forms of + and -. The binary forms starts with compu<ng the le4 operand. % gives the reminder amer an integer division. 13 % 3 = 1 (because 4*3+1=13). Numbers divide evenly if the reminder is and - - Unary operators that increments or decrements by 1 respec<vely. Could be prefixes as well as suffixes: x = ++p =» p is first increased by 1, then x=p. x = p++ =» first x=p, then p is increased by 1. (Similar for - -.) (These are also called assignment operators; see 3d.) Operators Associa<vity Type.! right- to- lem unary + - * / % lem- to- right mul<plica<ve + - lem- to- right addi<ve < <= > >= lem- to- right comparison ==!= lem- to- right comparison && lem- to- right boolean lem- to- right boolean?: right- to- lem condi<onal = right- to- lem assignment Examples: int k = 9, num = 3, code; code = num * 23 - k / 2; // 65 // Is 2 because 65 % 3 = (3*21 + 2) % 3 = 2 code % num // Is 0 because 65 = 13*5 + 0 code % Håkan Jonsson LTU 18 6
7 Be careful with ++ and - - Example with ++ (- - similar): int p = 0;! int i = 0;! for (i = 0; i < 10; i++) {! System.out.println( ++p );!!! gives 1,2,3,4,5,6,7,8,9,10. Advice: Be careful with ++ and - - if you do not know exactly what you are doing. Omen bewer to use p = p + 1 instead of ++p or p++ (stand- alone). int p = 0, i = 0;! for (i = 0; i < 10; i++) {! System.out.println( p++ );!!! gives 0,1,2,3,4,5,6,7,8, Håkan Jonsson LTU 19 3b. Logical/Rela<onal operators Works on comparable values: <, <=, >, >=, ==,!= as usual. Works on boolean values:! means NOT and negates a logical expression. && means AND. means OR. Short- circui:ng (lazy evalua:on) Only effects the two expressions A && B A B First, A is evaluated. Then, B is only evaluated if needed. false && x = false, regardless of x true x = true, regardsless of x The idea is to skip calcula<ons that are not needed. Operators Associa<vity Type.! right- to- lem unary + - * / % lem- to- right mul<plica<ve + - lem- to- right addi<ve < <= > >= lem- to- right comparison ==!= lem- to- right comparison && lem- to- right boolean lem- to- right boolean?: right- to- lem condi<onal = right- to- lem assignment boolean a, b, c, d; 1791 > 1657!a a && b a (b!= c) c = a (1 / 0 == 0); // works c = (1 / 0 == 0) a; // fails Håkan Jonsson LTU 21 7
8 3c. The ternary operator If B is a logical expression, B? x : y means x if B==true and y otherwise. The types of x and y must be related. For now: The same. Later on: x and y must conform to the type of the expression. Similar to the if statement, but an expression. Can be used everywhere an expression of the type of x and y can be used. Operators Associa<vity Type.! right- to- lem unary + - * / % lem- to- right mul<plica<ve + - lem- to- right addi<ve < <= > >= lem- to- right comparison ==!= lem- to- right comparison && lem- to- right boolean lem- to- right boolean?: right- to- lem condi<onal = right- to- lem assignment Example: 7 <= 4? "yes" : "no x < y? x < z? x : z : y < z? y : z Håkan Jonsson LTU 23 8
9 (3d. The assignment operator) The assignment operator = works as follows: <var> = <expr> means <expr> but also assigns <expr> to the variable x. If apple is one of +, -, *, /, % (among a few), wri<ng <var> apple= <expr> is a short form for <var> = <var> apple <expr> Assignment expressions have liwle, if any, value today. They easy cause confusion and bugs. Advice: Avoid using them unless the context is very clear or their use is unavoidable. Operators Associa<vity Type.! right- to- lem unary + - * / % lem- to- right mul<plica<ve + - lem- to- right addi<ve < <= > >= lem- to- right comparison ==!= lem- to- right comparison && lem- to- right boolean lem- to- right boolean?: right- to- lem condi<onal = right- to- lem assignment Examples: x = is 29 and (also) assigns x this value. x += 6 means x = x + 6 // so our x becomes 35 if ( (x /= 7) > 0 ) { // true, since 35/7>0 so... System.out.println(x); //... prints Håkan Jonsson LTU Java statements Statements: Assignment Call to a method. Compound statement. Condi<onal statements: if, if- else, switch, (extended if- else) Repe<<ons: while, for, do- while Control statements: return, con<nue, break All statements, except the compound statement, must end with a semicolon, ;. However, not when used as (part of) an expression Håkan Jonsson LTU 27 9
10 4a. Assignment An assignment changes the value of a variable: <var> = <expr> NB! Remember that variables must be declared before used. (Since assignments are also expressions, <var1> = <var2> = <expr> Since = is right- to- lem, <var2> is first assigned <expr>, and then <var1> is assigned the value of <var2>. We could have any number of variables: <var1> = <var2> = = <varn> = <expr> Assignments: numberoftigers = 10; radius = 22.7; crossmark = X ; wellknownparadox = This sentence is false. ; Assignments with assignment expressions: (Assuming x and y are both doubles) x = y = 0.0; (Assuming k and i and both int:s) k = i = 5; Håkan Jonsson LTU 29 4b. Call to a method A call to a method is wriwen <method- name> ( <args> ) when <method- name> is in the same class as the method call, or <class>. <method- name> ( <args> ) where <class> is the name of the (some other) class containing the method, or <ref>. <method- name> ( <args> ) where <ref> is a reference to an object that contains the method If the method returns a value, the call is an expression. Otherwise, it is a statement. It is called a call no mawer what. As an expression it can, for instance, be used in an assignment: <var> = <method- name> ( <args> ) Even if it is an expression, it is allowed to write <method- name> ( <args> ); on its own as a statement, in which case the call is made but the result is lost. Example: x = Math.sqrt(4.0); Engine.halt(); exit(0); Håkan Jonsson LTU 30 10
11 4c. Compound statement A compound statement is a sequence of statements and declara<ons enclosed by braces, {. The contents becomes a unit. Example: { sessionid += 1; Brakes.ac<vate(sessionID); Brakes.process(sessionID, SLOW); Compound statements are very common and useful constructs. The Java Code Conven:ons s<pulates that they should be used whenever possible to reduce the number of bugs. All examples follow this conven<on (well, they should J ). It is required in this course. Eclipse helps with this, if you like Håkan Jonsson LTU 32 4d. Condi<onal statements if statement: if ( <logical expr> ) { <statements> <statements> is executed if, and only if, <logical expr> is true. if- else statement: if ( <logical expr> ) { <statements 1> else { <statements 2> If <logical expr> is true, <statements 1> is executed, and otherwise, <statements 2> is executed. Examples: if (a<b) { x = a; if (numberoftigers == hunters) { evenodds = true; else { evenodds = false; This last example is bewer wriwen: evenodds = numberoftigers == hunters? true : false; and even bewer as evenodds = numberoftigers == hunters Håkan Jonsson LTU 33 11
12 Condi<onal statements A usual construct, although not a statement, is the extended if- else: if ( <logical expr 1> ) { <statements 1> else if (<logical expr 2> ) { <statements 2> else if else { <statements N> The logical expressions are tested one amer another star<ng with the first. As soon as one is true, its statements are executed. If none is true, the last statements are executed. Example: if (answer == 1) { ac<on = 1; ac<vate(); else if (answer == 2) { ac<on = 3; ac<vate(); else if (answer > 2) { ac<on = 4; inac<vate(ac<on); else { ac<on = 0; Håkan Jonsson LTU 34 Condi<onal statements A special case of the extended if- else is the switch statement: switch ( <control expr> ) { case <expr 1> : <statements 1> break; case <expr 2> : <statements 2> break; case <expr 3> : <statements 3> break; default: <statements N> break; A switch is useful when we like to do different things depending on the value of an expression, the control expression (<control expr>). This value is first computed. It is then compared to the expressions <expr 1>, <expr 2>, <expr 3>, in order downwards. When a case with a match is encountered, the statements of that case are executed. If none is found, the statements at default are executed. Warning: Without the break statements, execu<on in one case will con<nue into the cases following it!! Forge ng breaks is a common cause of bugs - be careful! Håkan Jonsson LTU 35 12
13 while statement: while ( <logical expr> ) { <statements> <statements> is repeatedly executed as long as <logical expr> is true. <logical expr> is tested before each execu<on of <statements>. If/when <logical expr> is false, execu<on con<nues amer the while- statement. 4e. Repe<<on Examples: while (number > 1) { performtask(); number = number 1; while (x < 0.0 && y < 0.0) { x = x + randomchange(); y = y + randomchange(); while (x < 0.0 && y < 0.0) { x += randomchange(); y += randomchange(); Håkan Jonsson LTU 37 Repe<<on for statement: for ( <init> ; <logical expr> ; <expr> ) { <statements> First the expression <init> (usually an assignment) is computed. Then <statements> is repeatedly executed as long as <logical expr> is true. <logical expr> (usually a comparison) is tested before each execu<on of <statement>. If/when <logical expr> is false, execu<on con<nues amer the for- statement. Amer each execu<on of <statements>, <expr> (usually changing the loop variable thereby effec<ng the logical expression) is computed. Examples: int i, sum = 0; for (i = 100; i >= 1; i- - ) { System.out.println(i); for (i = 1; i <= 5; i = i + 1) { sum += i; // 15 double lengths = 0.0d; for (int j = 1; j <= 5; j++) { int j2 = j * j; lengths += Math.sqrt(j2 + j2); // Håkan Jonsson LTU 38 More on for The for statement is very general and has been inherited from the (old) programming language C. It is perfectly fine to leave out any or all of the three expressions(!) For instance, for ( ; ; ) { <statements> and this results in an infinite loop. First, nothing is done; between itera<ons, nothing is computed as the logical expression and at this place in a Java program (and in C programs) this means true; finally, amer each itera<on nothing is done. There is also an enhanced for loop that iterates over collec:ons. We will learn about them shortly Håkan Jonsson LTU 39 13
14 Repe<<on A do- while statement is very similar to a while- statement: do { <statements> while ( <logical expr> ) However, as indicated by the syntax, <statements> are always executed once before <logical expr> is tested. This is the point with do- while, compared to for and while. The <statements> are then repeatedly executed as long as <logical expr> is true. Exactly as while works. Example: int laps = 1; do { System.out.println("Lap number " + Integer.toString(laps)); laps++; while (laps < 4); do { LaunchPad.fireMissile(); while (!MissileCommand.stopFiring()); Håkan Jonsson LTU 40 while, for, and do- while The three loops are convenient to use in different situa<ons. However, note the equivalent rela<ons to the right. Since, for and do- while both can be formulated using while, this means we really only need while. But for and do- while help improve readability so they are s:ll essen:al. for (A; B; C) { D; is the same as A; while (B) { D; C; do { E; while (F) is the same as E; while (F) { E; Håkan Jonsson LTU 41 14
15 4f. Control statements The return statement is used to return a result from a method and terminate the method: return <expression> Note that the type of <expression> has to conform with the return type of the method. Be the same. In methods that do not return a result, return without the <expression> can be used merely to terminate the execu<on of the method. Amer a return, program execu<on con<nues where the call was made. The program returns or jumps back. Examples: public sta<c double f(double x) { return 4*x*x + 3*x - 10; sta<c void firemissil() { if (vetoedbyhal9000) { return; else { LaunchPad.fireOne(); Håkan Jonsson LTU 43 Control statements A con:nue statement ends the current itera<on in a for, while, or do- while statement. con:nue <label> ends the current itera<on of the loop whose first line contains <label>:. It has the same effect as if program control is immediately moved to directly amer the last statement inside the loop (as if the last statement had just been executed). Execu<on con<nues with more itera<ons if the normal condi<ons for doing so are fulfilled. while (getnext(line)) { if (line.isempty() line.iscomment()) { con<nue; // More code here to process real lines outer: for (int i = 0; i < 3; i++) { while (true) { System.out.println("Hello + Integer.toString(i)); con2nue outer; System.out.println("outer"); System.out.println("Good bye"); This outputs: Hello 0 Hello 1 Hello 2 Good bye Håkan Jonsson LTU 44 Control statements A break statement jumps out of the current compound statement, for instance a switch, a loop, or an if statement: break break <label> ends the switch/loop/if etc whose first line contains <label>:. Note that this is different from how con:nue works. Examples: int shoes = 10; while (shoes > 0) { if (shoes == 3) { break; System.out.println(shoes); - - shoes; Prints: 10, 9, 8, 7, 6, 5, and 4 only. outer: for (int i = 500; i > 0; i- - ) { while (true) { System.out.println("Hello"); break outer; System.out.println( and"); System.out.println("Good Bye"); Prints: Hello, Good Bye (Note that and is not printed because the break jumps out of both loops.)... and that s IT for this <me! J Håkan Jonsson LTU 45 15
16 You have now covered 16
In Java, data type boolean is used to represent Boolean data. Each boolean constant or variable can contain one of two values: true or false.
CS101, Mock Boolean Conditions, If-Then Boolean Expressions and Conditions The physical order of a program is the order in which the statements are listed. The logical order of a program is the order in
More informationSequence 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 informationRelated Course Objec6ves
Syntax 9/18/17 1 Related Course Objec6ves Develop grammars and parsers of programming languages 9/18/17 2 Syntax And Seman6cs Programming language syntax: how programs look, their form and structure Syntax
More informationCondi(onals and Loops
Condi(onals and Loops 1 Review Primi(ve Data Types & Variables int, long float, double boolean char String Mathema(cal operators: + - * / % Comparison: < > = == 2 A Founda(on for Programming any program
More informationCOSC 111: Computer Programming I. Dr. Bowen Hui University of Bri>sh Columbia Okanagan
COSC 111: Computer Programming I Dr. Bowen Hui University of Bri>sh Columbia Okanagan 1 First half of course SoEware examples From English to Java Template for building small programs Exposure to Java
More informationObject-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018
Object-oriented programming 1 and data-structures CS/ENGRD 2110 SUMMER 2018 Lecture 1: Types and Control Flow http://courses.cs.cornell.edu/cs2110/2018su Lecture 1 Outline 2 Languages Overview Imperative
More informationLecture Set 4: More About Methods and More About Operators
Lecture Set 4: More About Methods and More About Operators Methods Definitions Invocations More arithmetic operators Operator Side effects Operator Precedence Short-circuiting main method public static
More informationPROGRAMMING FUNDAMENTALS
PROGRAMMING FUNDAMENTALS Q1. Name any two Object Oriented Programming languages? Q2. Why is java called a platform independent language? Q3. Elaborate the java Compilation process. Q4. Why do we write
More informationREPETITION CONTROL STRUCTURE LOGO
CSC 128: FUNDAMENTALS OF COMPUTER PROBLEM SOLVING REPETITION CONTROL STRUCTURE 1 Contents 1 Introduction 2 for loop 3 while loop 4 do while loop 2 Introduction It is used when a statement or a block of
More informationFlow of Control. Flow of control The order in which statements are executed. Transfer of control
1 Programming in C Flow of Control Flow of control The order in which statements are executed Transfer of control When the next statement executed is not the next one in sequence 2 Flow of Control Control
More informationV2 2/4/ Ch Programming in C. Flow of Control. Flow of Control. Flow of control The order in which statements are executed
Programming in C 1 Flow of Control Flow of control The order in which statements are executed Transfer of control When the next statement executed is not the next one in sequence 2 Flow of Control Control
More informationCS101: Fundamentals of Computer Programming. Dr. Tejada www-bcf.usc.edu/~stejada Week 1 Basic Elements of C++
CS101: Fundamentals of Computer Programming Dr. Tejada stejada@usc.edu www-bcf.usc.edu/~stejada Week 1 Basic Elements of C++ 10 Stacks of Coins You have 10 stacks with 10 coins each that look and feel
More informationFlow Control. CSC215 Lecture
Flow Control CSC215 Lecture Outline Blocks and compound statements Conditional statements if - statement if-else - statement switch - statement? : opertator Nested conditional statements Repetitive statements
More informationMore 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 informationThe Arithmetic Operators. Unary Operators. Relational Operators. Examples of use of ++ and
The Arithmetic Operators The arithmetic operators refer to the standard mathematical operators: addition, subtraction, multiplication, division and modulus. Op. Use Description + x + y adds x and y x y
More informationThe Arithmetic Operators
The Arithmetic Operators The arithmetic operators refer to the standard mathematical operators: addition, subtraction, multiplication, division and modulus. Examples: Op. Use Description + x + y adds x
More informationCS111: PROGRAMMING LANGUAGE II
CS111: PROGRAMMING LANGUAGE II Computer Science Department Lecture 1(c): Java Basics (II) Lecture Contents Java basics (part II) Conditions Loops Methods Conditions & Branching Conditional Statements A
More informationSelec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Spring 2016 Howard Rosenthal
Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Spring 2016 Howard Rosenthal Lesson Goals Understand Control Structures Understand how to control the flow of a program
More informationLecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan
Lecture 08-1 Programming in C++ PART 1 By Assistant Professor Dr. Ali Kattan 1 The Conditional Operator The conditional operator is similar to the if..else statement but has a shorter format. This is useful
More informationLecture Set 4: More About Methods and More About Operators
Lecture Set 4: More About Methods and More About Operators Methods Definitions Invocations More arithmetic operators Operator Side effects Operator Precedence Short-circuiting main method public static
More informationComputer Components. Software{ User Programs. Operating System. Hardware
Computer Components Software{ User Programs Operating System Hardware What are Programs? Programs provide instructions for computers Similar to giving directions to a person who is trying to get from point
More informationChapter 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 informationRepe$$on CSC 121 Spring 2017 Howard Rosenthal
Repe$$on CSC 121 Spring 2017 Howard Rosenthal Lesson Goals Learn the following three repetition structures in Java, their syntax, their similarities and differences, and how to avoid common errors when
More informationProf. 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 informationJava 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 informationObjectives. Chapter 4: Control Structures I (Selection) Objectives (cont d.) Control Structures. Control Structures (cont d.) Relational Operators
Objectives Chapter 4: Control Structures I (Selection) In this chapter, you will: Learn about control structures Examine relational and logical operators Explore how to form and evaluate logical (Boolean)
More informationAnother Simple Program: Adding Two Integers
Another Simple Program: Adding Two Integers Another Simple Program: Adding Two Integers This program uses the input stream object std::cin and the stream extrac>, to obtain two integers
More informationSchool of Computer Science CPS109 Course Notes 5 Alexander Ferworn Updated Fall 15
Table of Contents 1 INTRODUCTION... 1 2 IF... 1 2.1 BOOLEAN EXPRESSIONS... 3 2.2 BLOCKS... 3 2.3 IF-ELSE... 4 2.4 NESTING... 5 3 SWITCH (SOMETIMES KNOWN AS CASE )... 6 3.1 A BIT ABOUT BREAK... 7 4 CONDITIONAL
More informationOperators in java Operator operands.
Operators in java Operator in java is a symbol that is used to perform operations and the objects of operation are referred as operands. There are many types of operators in java such as unary operator,
More informationSelec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal
Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal Lesson Goals Understand Control Structures Understand how to control the flow of a program
More informationChapter 4: Control Structures I (Selection) Objectives. Objectives (cont d.) Control Structures. Control Structures (cont d.
Chapter 4: Control Structures I (Selection) In this chapter, you will: Objectives Learn about control structures Examine relational and logical operators Explore how to form and evaluate logical (Boolean)
More informationCS313D: 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 informationIntroduction to the Java Basics: Control Flow Statements
Lesson 3: Introduction to the Java Basics: Control Flow Statements Repetition Structures THEORY Variable Assignment You can only assign a value to a variable that is consistent with the variable s declared
More information(Not Quite) Minijava
(Not Quite) Minijava CMCS22620, Spring 2004 April 5, 2004 1 Syntax program mainclass classdecl mainclass class identifier { public static void main ( String [] identifier ) block } classdecl class identifier
More informationComputer Programming : C++
The Islamic University of Gaza Engineering Faculty Department of Computer Engineering Fall 2017 ECOM 2003 Muath i.alnabris Computer Programming : C++ Experiment #1 Basics Contents Structure of a program
More informationJava 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 informationCourse 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 informationAlgorithms Lecture 11. UC Davis, ECS20, Winter Discrete Mathematics for Computer Science
UC Davis, ECS20, Winter 2017 Discrete Mathematics for Computer Science Prof. Raissa D Souza (slides adopted from Michael Frank and Haluk Bingöl) Lecture 11 Algorithms 3.1-3.2 Algorithms Member of the House
More informationJava 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 informationArray Basics: Outline
Array Basics: Outline More Arrays (Savitch, Chapter 7) TOPICS Array Basics Arrays in Classes and Methods Programming with Arrays Searching and Sorting Arrays Multi-Dimensional Arrays Static Variables and
More informationLanguage Fundamentals Summary
Language Fundamentals Summary Claudia Niederée, Joachim W. Schmidt, Michael Skusa Software Systems Institute Object-oriented Analysis and Design 1999/2000 c.niederee@tu-harburg.de http://www.sts.tu-harburg.de
More informationIntroduction. C provides two styles of flow control:
Introduction C provides two styles of flow control: Branching Looping Branching is deciding what actions to take and looping is deciding how many times to take a certain action. Branching constructs: if
More information8. Control statements
8. Control statements A simple C++ statement is each of the individual instructions of a program, like the variable declarations and expressions seen in previous sections. They always end with a semicolon
More informationChapter 3. More Flow of Control. Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Chapter 3 More Flow of Control Overview 3.1 Using Boolean Expressions 3.2 Multiway Branches 3.3 More about C++ Loop Statements 3.4 Designing Loops Slide 3-3 Flow Of Control Flow of control refers to the
More informationAccelerating Information Technology Innovation
Accelerating Information Technology Innovation http://aiti.mit.edu Cali, Colombia Summer 2012 Lesson 02 Variables and Operators Agenda Variables Types Naming Assignment Data Types Type casting Operators
More informationControl Structures. Lecture 4 COP 3014 Fall September 18, 2017
Control Structures Lecture 4 COP 3014 Fall 2017 September 18, 2017 Control Flow Control flow refers to the specification of the order in which the individual statements, instructions or function calls
More informationObject 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 information1007 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 informationBBM 101 Introduc/on to Programming I Fall 2014, Lecture 5. Aykut Erdem, Erkut Erdem, Fuat Akal
BBM 101 Introduc/on to Programming I Fall 2014, Lecture 5 Aykut Erdem, Erkut Erdem, Fuat Akal 1 Today Itera/on Control Loop Statements for, while, do- while structures break and con/nue Some simple numerical
More informationOperators. 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 informationOutline. 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 informationChapter 5 Control Statements: Part 2 Section 5.2 Essentials of Counter-Controlled Repetition
Chapter 5 Control Statements: Part 2 Section 5.2 Essentials of Counter-Controlled Repetition 5.2 Q1: Counter-controlled repetition requires a. A control variable and initial value. b. A control variable
More informationOperators & Expressions
Operators & Expressions Operator An operator is a symbol used to indicate a specific operation on variables in a program. Example : symbol + is an add operator that adds two data items called operands.
More informationAll copyrights reserved - KV NAD, Aluva. Dinesh Kumar Ram PGT(CS) KV NAD Aluva
All copyrights reserved - KV NAD, Aluva Dinesh Kumar Ram PGT(CS) KV NAD Aluva Overview Looping Introduction While loops Syntax Examples Points to Observe Infinite Loops Examples using while loops do..
More informationBasics 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 informationLecture 2: Variables and Operators. AITI Nigeria Summer 2012 University of Lagos.
Lecture 2: Variables and Operators AITI Nigeria Summer 2012 University of Lagos. Agenda Variables Types Naming Assignment Data Types Type casting Operators Declaring Variables in Java type name; Variables
More informationswitch-case Statements
switch-case Statements A switch-case structure takes actions depending on the target variable. 2 switch (target) { 3 case v1: 4 // statements 5 break; 6 case v2: 7. 8. 9 case vk: 10 // statements 11 break;
More informationLoops and Files. Chapter 04 MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz
Loops and Files Chapter 04 MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz Chapter Topics o The Increment and Decrement Operators o The while Loop o Shorthand Assignment Operators o The do-while
More informationLogic is the anatomy of thought. John Locke ( ) This sentence is false.
Logic is the anatomy of thought. John Locke (1632 1704) This sentence is false. I know that I know nothing. anonymous Plato (In Apology, Plato relates that Socrates accounts for his seeming wiser than
More information1 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 informationMobile Computing Professor Pushpendra Singh Indraprastha Institute of Information Technology Delhi Java Basics Lecture 02
Mobile Computing Professor Pushpendra Singh Indraprastha Institute of Information Technology Delhi Java Basics Lecture 02 Hello, in this lecture we will learn about some fundamentals concepts of java.
More informationJava+- 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 informationLoops and Expression Types
Software and Programming I Loops and Expression Types Roman Kontchakov / Carsten Fuhs Birkbeck, University of London Outline The while, for and do Loops Sections 4.1, 4.3 and 4.4 Variable Scope Section
More informationControl Flow. COMS W1007 Introduction to Computer Science. Christopher Conway 3 June 2003
Control Flow COMS W1007 Introduction to Computer Science Christopher Conway 3 June 2003 Overflow from Last Time: Why Types? Assembly code is typeless. You can take any 32 bits in memory, say this is an
More informationProgramming 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 informationExpressions 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 informationProgramming for Engineers Iteration
Programming for Engineers Iteration ICEN 200 Spring 2018 Prof. Dola Saha 1 Data type conversions Grade average example,-./0 class average = 23450-67 893/0298 Grade and number of students can be integers
More informationReview. Primitive Data Types & Variables. String Mathematical operators: + - * / % Comparison: < > <= >= == int, long float, double boolean char
Review Primitive Data Types & Variables int, long float, double boolean char String Mathematical operators: + - * / % Comparison: < > = == 1 1.3 Conditionals and Loops Introduction to Programming in
More informationObject-Oriented Programming
Object-Oriented Programming Java Syntax Program Structure Variables and basic data types. Industry standard naming conventions. Java syntax and coding conventions If Then Else Case statements Looping (for,
More informationJava Programming: Guided Learning with Early Objects Chapter 5 Control Structures II: Repetition
Java Programming: Guided Learning with Early Objects Chapter 5 Control Structures II: Repetition Learn about repetition (looping) control structures Explore how to construct and use: o Counter-controlled
More informationRepe$$on CSC 121 Fall 2015 Howard Rosenthal
Repe$$on CSC 121 Fall 2015 Howard Rosenthal Lesson Goals Learn the following three repetition methods, their similarities and differences, and how to avoid common errors when using them: while do-while
More informationBBM 101 Introduc/on to Programming I Fall 2014, Lecture 3. Aykut Erdem, Erkut Erdem, Fuat Akal
BBM 101 Introduc/on to Programming I Fall 2014, Lecture 3 Aykut Erdem, Erkut Erdem, Fuat Akal 1 Today Introduc/on to Programming Basic Concepts Developing Algorithms Crea
More informationThere are algorithms, however, that need to execute statements in some other kind of ordering depending on certain conditions.
Introduction In the programs that we have dealt with so far, all statements inside the main function were executed in sequence as they appeared, one after the other. This type of sequencing is adequate
More informationLanguage Reference Manual
ALACS Language Reference Manual Manager: Gabriel Lopez (gal2129) Language Guru: Gabriel Kramer-Garcia (glk2110) System Architect: Candace Johnson (crj2121) Tester: Terence Jacobs (tj2316) Table of Contents
More informationx = 3 * y + 1; // x becomes 3 * y + 1 a = b = 0; // multiple assignment: a and b both get the value 0
6 Statements 43 6 Statements The statements of C# do not differ very much from those of other programming languages. In addition to assignments and method calls there are various sorts of selections and
More informationEntry Point of Execution: the main Method. Elementary Programming. Learning Outcomes. Development Process
Entry Point of Execution: the main Method Elementary Programming EECS1021: Object Oriented Programming: from Sensors to Actuators Winter 2019 CHEN-WEI WANG For now, all your programming exercises will
More informationObjec+ves. Review. Basics of Java Syntax Java fundamentals. What are quali+es of good sooware? What is Java? How do you compile a Java program?
Objec+ves Basics of Java Syntax Java fundamentals Ø Primi+ve data types Ø Sta+c typing Ø Arithme+c operators Ø Rela+onal operators 1 Review What are quali+es of good sooware? What is Java? Ø Benefits to
More informationControl Structures in Java if-else and switch
Control Structures in Java if-else and switch Lecture 4 CGS 3416 Spring 2017 January 23, 2017 Lecture 4CGS 3416 Spring 2017 Selection January 23, 2017 1 / 26 Control Flow Control flow refers to the specification
More informationArray Basics: Outline
Array Basics: Outline More Arrays (Savitch, Chapter 7) TOPICS Array Basics Arrays in Classes and Methods Programming with Arrays Searching and Sorting Arrays Multi-Dimensional Arrays Static Variables and
More informationCT 229 Java Syntax Continued
CT 229 Java Syntax Continued 29/09/2006 CT229 Lab Assignments One Week Extension for Lab Assignment 1. Due Date: Oct 8 th Before submission make sure that the name of each.java file matches the name given
More informationImportant Java terminology
1 Important Java terminology The information we manage in a Java program is either represented as primitive data or as objects. Primitive data פרימיטיביים) (נתונים include common, fundamental values as
More informationObject 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 informationCS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University
CS 112 Introduction to Computing II Wayne Snyder Department Boston University Today: Java expressions and operators concluded Java Statements: Conditionals: if/then, if/then/else Loops: while, for Next
More informationSTUDENT LESSON A12 Iterations
STUDENT LESSON A12 Iterations Java Curriculum for AP Computer Science, Student Lesson A12 1 STUDENT LESSON A12 Iterations INTRODUCTION: Solving problems on a computer very often requires a repetition of
More informationCOMP-202 Unit 2: Java Basics. CONTENTS: Using Expressions and Variables Types Strings Methods
COMP-202 Unit 2: Java Basics CONTENTS: Using Expressions and Variables Types Strings Methods Assignment 1 Assignment 1 posted on WebCt and course website. It is due May 18th st at 23:30 Worth 6% Part programming,
More informationLearning objec<ves. Classes, Objects, and Methods. Problem Decomposi<on. Problem Decomposi<on (cont d)
Learning objec
More informationBasic computer skills such as using Windows, Internet Explorer, and Microsoft Word. Chapter 1 Introduction to Computers, Programs, and Java
Basic computer skills such as using Windows, Internet Explorer, and Microsoft Word Chapter 1 Introduction to Computers, Programs, and Java Chapter 2 Primitive Data Types and Operations Chapter 3 Selection
More informationYOLOP Language Reference Manual
YOLOP Language Reference Manual Sasha McIntosh, Jonathan Liu & Lisa Li sam2270, jl3516 and ll2768 1. Introduction YOLOP (Your Octothorpean Language for Optical Processing) is an image manipulation language
More informationArray Basics: Outline
Array Basics: Outline More Arrays (Savitch, Chapter 7) TOPICS Array Basics Arrays in Classes and Methods Programming with Arrays Searching and Sorting Arrays Multi-Dimensional Arrays Static Variables and
More informationMultiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)
Name Closed notes, book and neighbor. If you have any questions ask them. Notes: Segment of code necessary C++ statements to perform the action described not a complete program Program a complete C++ program
More informationExample. Generating random numbers. Write a program which generates 2 random integers and asks the user to answer the math expression.
Example Generating random numbers Write a program which generates 2 random integers and asks the user to answer the math expression. For example, the program shows 2 + 5 =? If the user answers 7, then
More informationComputer Components. Software{ User Programs. Operating System. Hardware
Computer Components Software{ User Programs Operating System Hardware What are Programs? Programs provide instructions for computers Similar to giving directions to a person who is trying to get from point
More informationFull 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 informationChapter 2: Functions and Control Structures
Chapter 2: Functions and Control Structures TRUE/FALSE 1. A function definition contains the lines of code that make up a function. T PTS: 1 REF: 75 2. Functions are placed within parentheses that follow
More information1 class Lecture3 { 2 3 "Selections" // Keywords 8 if, else, else if, switch, case, default. Zheng-Liang Lu Java Programming 88 / 133
1 class Lecture3 { 2 3 "Selections" 4 5 } 6 7 // Keywords 8 if, else, else if, switch, case, default Zheng-Liang Lu Java Programming 88 / 133 Flow Controls The basic algorithm (and program) is constituted
More informationExpressions 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 informationTail recursion. Decision. Assignment. Iteration
Computer Programming Tail recursion. Decision. Assignment. Iteration Marius Minea marius@cs.upt.ro 7 October 2014 Two ways of writing recursion unsigned max(unsigned a, unsigned b) { return a > b? a :
More informationContents. Jairo Pava COMS W4115 June 28, 2013 LEARN: Language Reference Manual
Jairo Pava COMS W4115 June 28, 2013 LEARN: Language Reference Manual Contents 1 Introduction...2 2 Lexical Conventions...2 3 Types...3 4 Syntax...3 5 Expressions...4 6 Declarations...8 7 Statements...9
More informationChapter 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 informationJava Programming: Guided Learning with Early Objects Chapter 5 Control Structures II: Repetition
Java Programming: Guided Learning with Early Objects Chapter 5 Control Structures II: Repetition Learn about repetition (looping) control structures Explore how to construct and use: o Counter-controlled
More information