More Things We Can Do With It! Overview. Circle Calculations. πr 2. π = More operators and expression types More statements

Size: px
Start display at page:

Download "More Things We Can Do With It! Overview. Circle Calculations. πr 2. π = More operators and expression types More statements"

Transcription

1 More Things We Can Do With It! More operators and expression types More s 11 October 2007 Ariel Shamir 1 Overview Variables and declaration More operators and expressions String type and getting input Assignment Constants Selection 11 October 2007 Ariel Shamir 2 Circle Calculations 2πr r? π = πr 2 11 October 2007 Ariel Shamir 3 1

2 Outline of a Program class CircleCalc { public static void main(string[] args) { // my code should come here // as s 11 October 2007 Ariel Shamir 4 CircleCalc First Attempt class CircleCalc {??? public static void main(string[] args) { double r = 3.0; System.out.println(2.0 * * r); System.out.println( * * r); Can you see a problem? 11 October 2007 Ariel Shamir 5 Variables Usage One of the most important roles of programs is to deal with data. Data is most often put in variables for example: Numbers in input to be computed. A word written. A word read by the program. The date. 11 October 2007 Ariel Shamir 6 2

3 Variables A variable is an identifier that represents a storage to hold a particular type of data. Variables are actually a better name for an address in memory. count (integer) radius (real) October 2007 Ariel Shamir 7 Variable Types Variables must have a type. The type is the inner representation of the variable in the computer. The size of the memory allocated depends on the type. Type and size are very close terms in the computer world. 11 October 2007 Ariel Shamir 8 Variable Declaration Variables must be declared before they can be used (declaration ): Syntax: <Type> <variable_name> [,<name>] ; Examples: char c; int x1,x2,x3; double r; 11 October 2007 Ariel Shamir 9 3

4 Variables Memory Allocation Upon declaration memory of appropriate size is allocated. The address of the beginning of the piece of memory is associated with the variable name. short count; float radius; October 2007 Ariel Shamir 10 Variable Initialization Variables must be initialized before they can be used. Syntax: <variable_name> = <value>; Examples: x = 3; c = c ; myvalue = ; 11 October 2007 Ariel Shamir 11 Variable List Initialization A list of any size of variables of the same type can be initialized to the same value: Syntax: <variable1> = <variable2> = <variable3> = <value> ; Examples: x = y = my = account = degrees = 9 ; 11 October 2007 Ariel Shamir 12 4

5 Initialization During Declaration Both operations (declaration and initialization) can be performed in the same line: <type> Examples: int x=3; char c= c ; <variable_name> = <value>; float myvalue=1.0003f; 11 October 2007 Ariel Shamir 13 Variables Example class VariablesDeclerationExample { public static void main (String[] args) { short weeks = 14; int numberofstudents = 120; double averagefinalgrade = 78.6; System.out.println(weeks); System.out.println(numberOfStudents); System.out.println(averageFinalGrade); 11 October 2007 Ariel Shamir 14 CircleCalc Second Attempt class CircleCalc { public static void main(string[] args) { double r = 3.0; double circum, area; circum = 2 * * r; area = * r * r; System.out.println( Circumference: + circum); System.out.println( Area: + area); 11 October 2007 Ariel Shamir 15 5

6 Operators Java has 37 tokens that are operators. Java operators can be either: Unary operator - takes a single value Binary operator - takes two values Java binary operators are written in the infix notation: <operand1> <operator> <operand2> The operator is written between operands. 11 October 2007 Ariel Shamir 16 Operators Tokens + - * / % > < ==!= <= >= =? :! && ^ ~ & << >> >>> += -= *= /= %= &= = ^= <<= >>= >>>= 11 October 2007 Ariel Shamir 17 Expressions An expression can consist of a combination of operators and operands. Operands can be literal values, variables, or expressions by themselves (recursive). Types of expressions: Arithmetic expression: *4 Boolean expression: grade > 100 String expression: abc + d 11 October 2007 Ariel Shamir 18 6

7 Arithmetic Operators Binary: + addition x+y - subtraction x-y * multiplication x*y / division x/y % modulo remainder x%y Unary: - negation a = -34; 11 October 2007 Ariel Shamir 19 Operators Semantics The result of an arithmetic expression depends on the type of the operands: 3.0 / 2.0 == / 2.0 == / 2 == / 2 == 1-3 / 2 == October 2007 Ariel Shamir 20 Precedence The order in which operands are evaluated in expressions is determined by a welldefined precedence hierarchy. Operators at the same level of precedence are evaluated according to their associativity (right to left or left to right). Parentheses can be used to change precedence. 11 October 2007 Ariel Shamir 21 7

8 Precedence Example The usual rules apply: multiplication and division before addition and subtraction etc. and from left to right: / 3 * ( ) - 7 / October 2007 Ariel Shamir 22 More Examples Expression * 4 / 2 3 * (3 * 13) * (13 + 2) 4 * (11-6) * ( ) (5 * (4-1)) / 2 Result October 2007 Ariel Shamir 23 String Type String is the type of a line of text: I am a String. It is a first example of a reference data type (not a primitive type). It is widely used. 11 October 2007 Ariel Shamir 24 8

9 String Concatenation The + operator between strings has the meaning of String concatenation. When we apply + upon a String and a value of another type, that value is first converted into a String and the result is the concatenation of the two Strings. 11 October 2007 Ariel Shamir 25 Concatenation Example class AntarticaCode { public static void main(string[] args) { System.out.print( The international + code ); System.out.println( for Antarctica is + 762); Output: The international code for Antarctica is October 2007 Ariel Shamir 26 Getting Input You are already acquainted with one way of output: System.out.println(... ) How do we get input? read from keyboard? Just as System.out is an object that represents the standard output (screen), System.in is an object that represents the standard input (keyboard). We can read directly from System.in, however it is a bit complicated. An easier way: use the Scanner class. 11 October 2007 Ariel Shamir 27 9

10 The Scanner Class System.out.println( Enter number of items: ); Scanner sc = new Scanner(System.in); int numberofitems = sc.nextint(); You may use it to read any primitive data types by calling its methods: bollean b = sc.nextboolean(); float f = sc.nextfloat(); double d = sc.nextdouble(); To use the Scanner class, you must include the line: import java.util.scanner; at the head of your file. 11 October 2007 Ariel Shamir 28 CircleCalc Third Attempt class CircleCalc { public static void main(string[] args) { double r, circum, area; Scanner sc = new Scanner(System.in); System.out.println( Please enter radius: ); r = sc.nextint(); //or maybe nextdouble()? circum = 2 * * r; area = * r * r; System.out.println( Circumference: + circum); System.out.println( Area: + area); 11 October 2007 Ariel Shamir 29 Example - Adding Two Numbers // Requests two integers and prints their sum class AdditionExample { public static void main(string[] args) { int a,b; Scanner sc = new Scanner(System.in); System.out.println( Please enter first number: ); a = sc.nextint(); System.out.println( Please enter second number: ); b = sc.nextint(); int sum = a + b; System.out.println( The sum is + sum); 11 October 2007 Ariel Shamir 30 10

11 The Assignment Statement Stores a new value into a variable. The most common computer instruction. Syntax: <variable_name> = <expression>; Examples: x = 3+(2*7); c = c ; myvalue = / ; 11 October 2007 Ariel Shamir 31 Assignment Evaluation The assignment has a left side and a right side : <left_side> = <right_side>; The left side is usually a variable name and the right side some expression. The computer first evaluates the right side and only then assigns the value of the this evaluation to the left side. 11 October 2007 Ariel Shamir 32 Assignment Example class MyIncome { public static void main (String[] args) { Assignment Initialization int year = 2000; float monthincome = 10.4f; float bonus = 8.8f; float income = 12 * monthincome + bonus; System.out.println( in +year+ income was +income); year = 2001; bonus = 0; income = 12 * monthincome + bonus; System.out.println( in +year+ income was +income); 11 October 2007 Ariel Shamir 33 11

12 Blocks Block is the simplest type of composite. { // s It implements grouping of a sequence of s into a single. Block can be empty without s! 11 October 2007 Ariel Shamir 34 Block Scope { float k; // declaration k = (k+5)/32; // assignment (?) System.out.println(k); // call A variable k declared inside a block is completely inaccessible and invisible from outside that block. Such a variable is called local to the block. 11 October 2007 Ariel Shamir 35 Constants A constant is an identifier that is similar to a variable except that its value cannot be changed. You cannot assign to a constant! The final modifier is used for constant declaration: final <type> <Variable_name> = <value>; Example: final int MAX_GRADE=100; 11 October 2007 Ariel Shamir 36 12

13 CircleCalc Final Version! // Reads the radius of a circle and prints // its circumference and area class CircleCalc { static final double PI = ; public static void main(string[] args) { double r, circum, area; Scanner sc = new Scanner(System.in); system.out.println( Please enter radius: ); r = sc.nextint(); circum = 2 * PI * r; area = PI * r * r; System.out.println( Circumference: + circum); System.out.println( Area: + area); 11 October 2007 Ariel Shamir 37 Relational and Conditional Operators > greater than >= greater than or equal to < less than <= less than or equal to = = equal to! = not equal to 11 October 2007 Ariel Shamir 38 Boolean Expressions Expressions that use conditional operators are called Boolean expressions. Boolean expressions can have only two values: true False Examples: 5 >= == 1+7 5!= October 2007 Ariel Shamir 39 13

14 Boolean Operators Operator && ^! Use exp1 && exp2 exp1 exp2 exp1 ^ exp2!exp Returns true if exp1 and exp2 are both true, conditionally evaluates exp2 either exp1 or exp2 is true, conditionally evaluates exp2 if exp1 or exp2 is true but not both exp is false 11 October 2007 Ariel Shamir 40 Truth Tables or && and ^ xor T F T F T F T T T T T F T F T F T F F F F F T F 11 October 2007 Ariel Shamir 41 Bit-wise Operators Perform the operation on each bit Operator Use Evaluates ~ ~exp1 Flips all the bits of exp1 & exp1 & exp2 Always evaluates exp1 and exp2 exp1 exp2 Always evaluates exp1 and exp2 ~ = = & = October 2007 Ariel Shamir 42 14

15 Program Flow (Up Till Now) 11 October 2007 Ariel Shamir 43 Selection Statement selection? 11 October 2007 Ariel Shamir 44 If Statement Doing something under some condition: if (Boolean_condition) ; Boolean_condition is a Boolean expression that can have true or false values. If the value is true than is performed, otherwise (the value is false) is skipped. 11 October 2007 Ariel Shamir 45 15

16 If Flow Chart If? true false 11 October 2007 Ariel Shamir 46 Boolean Expression Example if ( x<1 x%2!=0 ) { System.out.println( x is not a positive even number! ); return; if ( y+2 < x &&!(y == 17) ) { System.out.println( y is not 17 and is smaller than x by more than 2! ); return; 11 October 2007 Ariel Shamir 47 If Statement Example class FailTest { public static void main(string[] args) { System.out.println( Enter your grade: ); Scanner sc = new Scanner(System.in); int grade = sc.nextint(); System.out.println( Your grade is: + grade); if (grade < 60) { System.out.println( You failed! ); 11 October 2007 Ariel Shamir 48 16

17 If.. Else Statement A choice between doing two things: if (Boolean_condition) 1; else 2; If the Boolean_condition is true, 1 is executed; if the condition is false, 2 is executed. 11 October 2007 Ariel Shamir 49 If..Else Flow Chart true If..else? false (else clause) 11 October 2007 Ariel Shamir 50 If.. Else Example class FailTest { public static void main(string[] args) { System.out.println( Enter your grade: ); Scanner sc = new Scanner(System.in); int grade = sc.nextint(); System.out.println( Your grade is: + grade); if (grade < 60){ else { System.out.println( You failed!! ); System.out.println( You passed!! ); 11 October 2007 Ariel Shamir 51 17

18 Nested If s if (a!= b) { if (a > b) { System.out.println(a + is greater ); else { System.out.println(b + is greater ); else { System.out.println( They are equal! ); 11 October 2007 Ariel Shamir 52 Always Use Blocks! if (grade >= 60) sum = sum + grade; avg = sum / NUM_STUDENTS; Problem?... So after thinking the programmer adds: if (grade >= 60) sum = sum + grade; count = count+1; avg = sum / count; 11 October 2007 Ariel Shamir 53 18

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

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

Lecture 6. Assignments. Summary - Variables. Summary Program Parts 1/29/18. Reading: 3.1, 3.2, 3.3, 3.4

Lecture 6. Assignments. Summary - Variables. Summary Program Parts 1/29/18. Reading: 3.1, 3.2, 3.3, 3.4 Assignments Lecture 6 Complete for Project 1 Reading: 3.1, 3.2, 3.3, 3.4 Summary Program Parts Summary - Variables Class Header (class name matches the file name prefix) Class Body Because this is a program,

More information

Introduction to Computer Programming

Introduction to Computer Programming Introduction to Computer Programming Lecture 2- Primitive Data and Stepwise Refinement Data Types Type - A category or set of data values. Constrains the operations that can be performed on data Many languages

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

Building Java Programs

Building Java Programs Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading: 2.1-2.2 1 Data and expressions reading: 2.1 self-check: 1-4 videos: Ch. 2 #1 2 Data types type: A category or set of data

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

COSC 123 Computer Creativity. Introduction to Java. Dr. Ramon Lawrence University of British Columbia Okanagan

COSC 123 Computer Creativity. Introduction to Java. Dr. Ramon Lawrence University of British Columbia Okanagan COSC 123 Computer Creativity Introduction to Java Dr. Ramon Lawrence University of British Columbia Okanagan ramon.lawrence@ubc.ca Key Points 1) Introduce Java, a general-purpose programming language,

More information

Programming with Java

Programming with Java Programming with Java Data Types & Input Statement Lecture 04 First stage Software Engineering Dep. Saman M. Omer 2017-2018 Objectives q By the end of this lecture you should be able to : ü Know rules

More information

Building Java Programs

Building Java Programs Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading: 2.1-2.2 Copyright 2009 by Pearson Education Data and expressions reading: 2.1 self-check: 1-4 videos: Ch. 2 #1 Copyright

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

Motivating Examples (1.1) Selections. Motivating Examples (1.2) Learning Outcomes. EECS1022: Programming for Mobile Computing Winter 2018

Motivating Examples (1.1) Selections. Motivating Examples (1.2) Learning Outcomes. EECS1022: Programming for Mobile Computing Winter 2018 Motivating Examples (1.1) Selections EECS1022: Programming for Mobile Computing Winter 2018 CHEN-WEI WANG 1 import java.util.scanner; 2 public class ComputeArea { 3 public static void main(string[] args)

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

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

Lecture Set 2: Starting Java

Lecture Set 2: Starting Java Lecture Set 2: Starting Java 1. Java Concepts 2. Java Programming Basics 3. User output 4. Variables and types 5. Expressions 6. User input 7. Uninitialized Variables 0 This Course: Intro to Procedural

More information

Computer Programming, I. Laboratory Manual. Experiment #2. Elementary Programming

Computer Programming, I. Laboratory Manual. Experiment #2. Elementary Programming Think Twice Code Once The Islamic University of Gaza Engineering Faculty Department of Computer Engineering Fall 2017 ECOM 2005 Khaleel I. Shaheen Computer Programming, I Laboratory Manual Experiment #2

More information

Computer Programming CS F111

Computer Programming CS F111 Computer Programming CS F111 BITS Pilani Dubai Campus NAND KUMAR Basics of C Programming BITS Pilani Dubai Campus Write a program that 1. Asks 5 marks from the user, find the average of the marks and print

More information

Lecture Set 2: Starting Java

Lecture Set 2: Starting Java Lecture Set 2: Starting Java 1. Java Concepts 2. Java Programming Basics 3. User output 4. Variables and types 5. Expressions 6. User input 7. Uninitialized Variables 0 This Course: Intro to Procedural

More information

Operators in java Operator operands.

Operators 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 information

JAVA Ch. 4. Variables and Constants Lawrenceville Press

JAVA Ch. 4. Variables and Constants Lawrenceville Press JAVA Ch. 4 Variables and Constants Slide 1 Slide 2 Warm up/introduction int A = 13; int B = 23; int C; C = A+B; System.out.print( The answer is +C); Slide 3 Declaring and using variables Slide 4 Declaring

More information

Building Java Programs. Chapter 2: Primitive Data and Definite Loops

Building Java Programs. Chapter 2: Primitive Data and Definite Loops Building Java Programs Chapter 2: Primitive Data and Definite Loops Copyright 2008 2006 by Pearson Education 1 Lecture outline data concepts Primitive types: int, double, char (for now) Expressions: operators,

More information

Lecture 6. Assignments. Java Scanner. User Input 1/29/18. Reading: 2.12, 2.13, 3.1, 3.2, 3.3, 3.4

Lecture 6. Assignments. Java Scanner. User Input 1/29/18. Reading: 2.12, 2.13, 3.1, 3.2, 3.3, 3.4 Assignments Reading: 2.12, 2.13, 3.1, 3.2, 3.3, 3.4 Lecture 6 Complete for Lab 4, Project 1 Note: Slides 12 19 are summary slides for Chapter 2. They overview much of what we covered but are not complete.

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

Lecture 3 Tao Wang 1

Lecture 3 Tao Wang 1 Lecture 3 Tao Wang 1 Objectives In this chapter, you will learn about: Arithmetic operations Variables and declaration statements Program input using the cin object Common programming errors C++ for Engineers

More information

Selections. EECS1021: Object Oriented Programming: from Sensors to Actuators Winter 2019 CHEN-WEI WANG

Selections. EECS1021: Object Oriented Programming: from Sensors to Actuators Winter 2019 CHEN-WEI WANG Selections EECS1021: Object Oriented Programming: from Sensors to Actuators Winter 2019 CHEN-WEI WANG Learning Outcomes The Boolean Data Type if Statement Compound vs. Primitive Statement Common Errors

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

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

Numerical Data. CS 180 Sunil Prabhakar Department of Computer Science Purdue University

Numerical Data. CS 180 Sunil Prabhakar Department of Computer Science Purdue University Numerical Data CS 180 Sunil Prabhakar Department of Computer Science Purdue University Problem Write a program to compute the area and perimeter of a circle given its radius. Requires that we perform operations

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

Selec%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 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 information

What did we talk about last time? Examples switch statements

What did we talk about last time? Examples switch statements Week 4 - Friday What did we talk about last time? Examples switch statements History of computers Hardware Software development Basic Java syntax Output with System.out.print() Mechanical Calculation

More information

Selec%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 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 information

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

AP COMPUTER SCIENCE A

AP COMPUTER SCIENCE A AP COMPUTER SCIENCE A CONTROL FLOW Aug 28 2017 Week 2 http://apcs.cold.rocks 1 More operators! not!= not equals to % remainder! Goes ahead of boolean!= is used just like == % is used just like / http://apcs.cold.rocks

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

Building Java Programs

Building Java Programs Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading: 2.1-2.2 1 2 Data and expressions reading: 2.1 3 The computer s view Internally, computers store everything as 1 s and 0

More information

Topic 4 Expressions and variables

Topic 4 Expressions and variables Topic 4 Expressions and variables "Once a person has understood the way variables are used in programming, he has understood the quintessence of programming." -Professor Edsger W. Dijkstra Based on slides

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

COMP 250: Java Programming I. Carlos G. Oliver, Jérôme Waldispühl January 17-18, 2018 Slides adapted from M. Blanchette

COMP 250: Java Programming I. Carlos G. Oliver, Jérôme Waldispühl January 17-18, 2018 Slides adapted from M. Blanchette COMP 250: Java Programming I Carlos G. Oliver, Jérôme Waldispühl January 17-18, 2018 Slides adapted from M. Blanchette Variables and types [Downey Ch 2] Variable: temporary storage location in memory.

More information

Data and Variables. Data Types Expressions. String Concatenation Variables Declaration Assignment Shorthand operators. Operators Precedence

Data and Variables. Data Types Expressions. String Concatenation Variables Declaration Assignment Shorthand operators. Operators Precedence Data and Variables Data Types Expressions Operators Precedence String Concatenation Variables Declaration Assignment Shorthand operators Review class All code in a java file is written in a class public

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

First Java Program - Output to the Screen

First Java Program - Output to the Screen First Java Program - Output to the Screen These notes are written assuming that the reader has never programmed in Java, but has programmed in another language in the past. In any language, one of the

More information

I. Variables and Data Type week 3

I. Variables and Data Type week 3 I. Variables and Data Type week 3 variable: a named memory (i.e. RAM, which is volatile) location used to store/hold data, which can be changed during program execution in algebra: 3x + 5 = 20, x = 5,

More information

CEN 414 Java Programming

CEN 414 Java Programming CEN 414 Java Programming Instructor: H. Esin ÜNAL SPRING 2017 Slides are modified from original slides of Y. Daniel Liang WEEK 2 ELEMENTARY PROGRAMMING 2 Computing the Area of a Circle public class ComputeArea

More information

Software Practice 1 Basic Grammar

Software Practice 1 Basic Grammar Software Practice 1 Basic Grammar Basic Syntax Data Type Loop Control Making Decision Prof. Joonwon Lee T.A. Jaehyun Song Jongseok Kim (42) T.A. Sujin Oh Junseong Lee (43) 1 2 Java Program //package details

More information

Introduction to Java Applications

Introduction to Java Applications 2 Introduction to Java Applications OBJECTIVES In this chapter you will learn: To write simple Java applications. To use input and output statements. Java s primitive types. Basic memory concepts. To use

More information

Chapter 3 Selections. Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved.

Chapter 3 Selections. Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. Chapter 3 Selections Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 1 Motivations If you assigned a negative value for radius

More information

Boolean Expressions. So, for example, here are the results of several simple Boolean expressions:

Boolean Expressions. So, for example, here are the results of several simple Boolean expressions: Boolean Expressions Now we have the ability to read in some information, calculate some formulas and display the information to the user in a nice format. However, the real power of computer programs lies

More information

Software Practice 1 - Basic Grammar Basic Syntax Data Type Loop Control Making Decision

Software Practice 1 - Basic Grammar Basic Syntax Data Type Loop Control Making Decision Software Practice 1 - Basic Grammar Basic Syntax Data Type Loop Control Making Decision Prof. Hwansoo Han T.A. Minseop Jeong T.A. Wonseok Choi 1 Java Program //package details public class ClassName {

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

Chapter 2: Basic Elements of Java

Chapter 2: Basic Elements of Java Chapter 2: Basic Elements of Java TRUE/FALSE 1. The pair of characters // is used for single line comments. ANS: T PTS: 1 REF: 29 2. The == characters are a special symbol in Java. ANS: T PTS: 1 REF: 30

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

Program Control Flow

Program Control Flow Lecture slides for: Chapter 3 Program Control Flow Java Actually: A Comprehensive Primer in Programming Khalid Azim Mughal, Torill Hamre, Rolf W. Rasmussen Cengage Learning, 2008. ISBN: 978-1-844480-933-2

More information

Program Control Flow

Program Control Flow Lecture slides for: Chapter 3 Program Control Flow Java Actually: A Comprehensive Primer in Programming Khalid Azim Mughal, Torill Hamre, Rolf W. Rasmussen Cengage Learning, 2008. ISBN: 978-1-844480-933-2

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

Chapter 3 Selection Statements

Chapter 3 Selection Statements Chapter 3 Selection Statements 3.1 Introduction Java provides selection statements that let you choose actions with two or more alternative courses. Selection statements use conditions. Conditions are

More information

Handout 4 Conditionals. Boolean Expressions.

Handout 4 Conditionals. Boolean Expressions. Handout 4 cs180 - Programming Fundamentals Fall 17 Page 1 of 8 Handout 4 Conditionals. Boolean Expressions. Example Problem. Write a program that will calculate and print bills for the city power company.

More information

Chapter 4: Basic C Operators

Chapter 4: Basic C Operators Chapter 4: Basic C Operators In this chapter, you will learn about: Arithmetic operators Unary operators Binary operators Assignment operators Equalities and relational operators Logical operators Conditional

More information

Lesson 7 Part 2 Flags

Lesson 7 Part 2 Flags Lesson 7 Part 2 Flags A Flag is a boolean variable that signals when some condition exists in a program. When a flag is set to true, it means some condition exists When a flag is set to false, it means

More information

CONDITIONAL EXECUTION

CONDITIONAL EXECUTION CONDITIONAL EXECUTION yes x > y? no max = x; max = y; logical AND logical OR logical NOT &&! Fundamentals of Computer Science I Outline Conditional Execution if then if then Nested if then statements Comparisons

More information

COMP-202: Foundations of Programming. Lecture 3: Boolean, Mathematical Expressions, and Flow Control Sandeep Manjanna, Summer 2015

COMP-202: Foundations of Programming. Lecture 3: Boolean, Mathematical Expressions, and Flow Control Sandeep Manjanna, Summer 2015 COMP-202: Foundations of Programming Lecture 3: Boolean, Mathematical Expressions, and Flow Control Sandeep Manjanna, Summer 2015 Announcements Slides will be posted before the class. There might be few

More information

Overview: Programming Concepts. Programming Concepts. Names, Values, And Variables

Overview: Programming Concepts. Programming Concepts. Names, Values, And Variables Chapter 18: Get With the Program: Fundamental Concepts Expressed in JavaScript Fluency with Information Technology Third Edition by Lawrence Snyder Overview: Programming Concepts Programming: Act of formulating

More information

Overview: Programming Concepts. Programming Concepts. Chapter 18: Get With the Program: Fundamental Concepts Expressed in JavaScript

Overview: Programming Concepts. Programming Concepts. Chapter 18: Get With the Program: Fundamental Concepts Expressed in JavaScript Chapter 18: Get With the Program: Fundamental Concepts Expressed in JavaScript Fluency with Information Technology Third Edition by Lawrence Snyder Overview: Programming Concepts Programming: Act of formulating

More information

Selenium Class 9 - Java Operators

Selenium Class 9 - Java Operators Selenium Class 9 - Java Operators Operators are used to perform Arithmetic, Comparison, and Logical Operations, Operators are used to perform operations on variables and values. public class JavaOperators

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

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

Computational Expression

Computational Expression Computational Expression Variables, Primitive Data Types, Expressions Janyl Jumadinova 28-30 January, 2019 Janyl Jumadinova Computational Expression 28-30 January, 2019 1 / 17 Variables Variable is a name

More information

Data Types. 1 You cannot change the type of the variable after declaration. Zheng-Liang Lu Java Programming 52 / 87

Data Types. 1 You cannot change the type of the variable after declaration. Zheng-Liang Lu Java Programming 52 / 87 Data Types Java is a strongly-typed 1 programming language. Every variable has a type. Also, every (mathematical) expression has a type. There are two categories of data types: primitive data types, and

More information

Le L c e t c ur u e e 2 To T p o i p c i s c t o o b e b e co c v o e v r e ed e Variables Operators

Le L c e t c ur u e e 2 To T p o i p c i s c t o o b e b e co c v o e v r e ed e Variables Operators Course Name: Advanced Java Lecture 2 Topics to be covered Variables Operators Variables -Introduction A variables can be considered as a name given to the location in memory where values are stored. One

More information

Elementary Programming

Elementary Programming Elementary Programming EECS1022: Programming for Mobile Computing Winter 2018 CHEN-WEI WANG Learning Outcomes Learn ingredients of elementary programming: data types [numbers, characters, strings] literal

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

COMP 202. Built in Libraries and objects. CONTENTS: Introduction to objects Introduction to some basic Java libraries string

COMP 202. Built in Libraries and objects. CONTENTS: Introduction to objects Introduction to some basic Java libraries string COMP 202 Built in Libraries and objects CONTENTS: Introduction to objects Introduction to some basic Java libraries string COMP 202 Objects and Built in Libraries 1 Classes and Objects An object is an

More information

A Quick and Dirty Overview of Java and. Java Programming

A Quick and Dirty Overview of Java and. Java Programming Department of Computer Science New Mexico State University. CS 272 A Quick and Dirty Overview of Java and.......... Java Programming . Introduction Objectives In this document we will provide a very brief

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

2: Basics of Java Programming

2: Basics of Java Programming 2: Basics of Java Programming CSC 1051 Algorithms and Data Structures I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website: www.csc.villanova.edu/~map/1051/

More information

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I BASIC COMPUTATION x public static void main(string [] args) Fundamentals of Computer Science I Outline Using Eclipse Data Types Variables Primitive and Class Data Types Expressions Declaration Assignment

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

Basic computer skills such as using Windows, Internet Explorer, and Microsoft Word. and Java. Chapter 2 Primitive Data Types and Operations

Basic computer skills such as using Windows, Internet Explorer, and Microsoft Word. and Java. Chapter 2 Primitive Data Types and Operations Basic computer skills such as using Windows, Internet Explorer, and Microsoft Word Chapter p 1 Introduction to Computers, p, Programs, g, and Java Chapter 2 Primitive Data Types and Operations Chapter

More information

Lecture Notes. System.out.println( Circle radius: + radius + area: + area); radius radius area area value

Lecture Notes. System.out.println( Circle radius: + radius + area: + area); radius radius area area value Lecture Notes 1. Comments a. /* */ b. // 2. Program Structures a. public class ComputeArea { public static void main(string[ ] args) { // input radius // compute area algorithm // output area Actions to

More information

Building Java Programs Chapter 2

Building Java Programs Chapter 2 Building Java Programs Chapter 2 Primitive Data and Definite Loops Copyright (c) Pearson 2013. All rights reserved. Data types type: A category or set of data values. Constrains the operations that can

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

H212 Introduction to Software Systems Honors

H212 Introduction to Software Systems Honors Introduction to Software Systems Honors Lecture #04: Fall 2015 1/20 Office hours Monday, Wednesday: 10:15 am to 12:00 noon Tuesday, Thursday: 2:00 to 3:45 pm Office: Lindley Hall, Room 401C 2/20 Printing

More information

CS 106 Introduction to Computer Science I

CS 106 Introduction to Computer Science I CS 106 Introduction to Computer Science I 05 / 31 / 2017 Instructor: Michael Eckmann Today s Topics Questions / Comments? recap and some more details about variables, and if / else statements do lab work

More information

Chapter 2 ELEMENTARY PROGRAMMING

Chapter 2 ELEMENTARY PROGRAMMING Chapter 2 ELEMENTARY PROGRAMMING Lecture notes for computer programming 1 Faculty of Engineering and Information Technology Prepared by: Iyad Albayouk ١ Objectives To write Java programs to perform simple

More information

false, import, new 1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4

false, import, new 1 class Lecture2 { 2 3 Data types, Variables, and Operators 4 1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4 5 } 6 7 // Keywords: 8 byte, short, int, long, char, float, double, boolean, true, false, import, new Zheng-Liang Lu Java Programming 45

More information

CSE 142, Summer 2014

CSE 142, Summer 2014 CSE 142, Summer 2014 Lecture 2: Static Methods Expressions reading: 1.4 2.1 Algorithms algorithm: A list of steps for solving a problem. Example algorithm: "Bake sugar cookies" Mix the dry ingredients.

More information

Chapter 2 Primitive Data Types and Operations. Objectives

Chapter 2 Primitive Data Types and Operations. Objectives Chapter 2 Primitive Data Types and Operations Prerequisites for Part I Basic computer skills such as using Windows, Internet Explorer, and Microsoft Word Chapter 1 Introduction to Computers, Programs,

More information

AP CS Unit 3: Control Structures Notes

AP CS Unit 3: Control Structures Notes AP CS Unit 3: Control Structures Notes The if and if-else Statements. These statements are called control statements because they control whether a particular block of code is executed or not. Some texts

More information

Exam 1. Programming I (CPCS 202) Instructor: M. G. Abbas Malik. Total Marks: 45 Obtained Marks:

Exam 1. Programming I (CPCS 202) Instructor: M. G. Abbas Malik. Total Marks: 45 Obtained Marks: كلية الحاسبات وتقنية المعلوما Exam 1 Programming I (CPCS 202) Instructor: M. G. Abbas Malik Date: October 18, 2015 Student Name: Student ID: Total Marks: 45 Obtained Marks: Instructions: Do not open this

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

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

Condensed Java. 12-Oct-15

Condensed Java. 12-Oct-15 Condensed Java 12-Oct-15 Python and Java Python and Java are both object-oriented languages Conceptually, the languages are very similar The syntax, however, is quite different, and Java syntax is much

More information

Getting started with Java

Getting started with Java Getting started with Java Magic Lines public class MagicLines { public static void main(string[] args) { } } Comments Comments are lines in your code that get ignored during execution. Good for leaving

More information

Basic operators, Arithmetic, Relational, Bitwise, Logical, Assignment, Conditional operators. JAVA Standard Edition

Basic operators, Arithmetic, Relational, Bitwise, Logical, Assignment, Conditional operators. JAVA Standard Edition Basic operators, Arithmetic, Relational, Bitwise, Logical, Assignment, Conditional operators JAVA Standard Edition Java - Basic Operators Java provides a rich set of operators to manipulate variables.

More information

Chapter 2 Elementary Programming

Chapter 2 Elementary Programming Chapter 2 Elementary Programming Part I 1 Motivations In the preceding chapter, you learned how to create, compile, and run a Java program. Starting from this chapter, you will learn how to solve practical

More information

Expressions and Variables

Expressions and Variables Expressions and Variables Expressions print(expression) An expression is evaluated to give a value. For example: 2 + 9-6 Evaluates to: 5 Data Types Integers 1, 2, 3, 42, 100, -5 Floating points 2.5, 7.0,

More information

Section 2: Introduction to Java. Historical note

Section 2: Introduction to Java. Historical note The only way to learn a new programming language is by writing programs in it. - B. Kernighan & D. Ritchie Section 2: Introduction to Java Objectives: Data Types Characters and Strings Operators and Precedence

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

AL GHURAIR UNIVERSITY College of Computing. Objectives: Examples: Text-printing program. CSC 209 JAVA I

AL GHURAIR UNIVERSITY College of Computing. Objectives: Examples: Text-printing program. CSC 209 JAVA I AL GHURAIR UNIVERSITY College of Computing CSC 209 JAVA I week 2- Arithmetic and Decision Making: Equality and Relational Operators Objectives: To use arithmetic operators. The precedence of arithmetic

More information

Introduction to Computer Science Unit 2. Notes

Introduction to Computer Science Unit 2. Notes Introduction to Computer Science Unit 2. Notes Name: Objectives: By the completion of this packet, students should be able to describe the difference between.java and.class files and the JVM. create and

More information