1 More on Assignment and Console Input Overview l Increment & Decrement Operators l Short-cut Operators l Casting l Math class l Console Input (TextIO.

Slides:



Advertisements
Similar presentations
Types, Variables and Operators Computer Engineering Department Java Course Asst. Prof. Dr. Ahmet Sayar Kocaeli University - Fall 2013.
Advertisements

Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
Variables Pepper. Variable A variable –box –holds a certain type of value –value inside the box can change Example –A = 2B+1 –Slope = change in y / change.
C Programming Basics Lecture 5 Engineering H192 Winter 2005 Lecture 05
1 Fundamental Data types Overview l Primitive Data Types l Variable declaration l Arithmetical Operations l Expressions l Assignment statement l Increment.
Mathematical Operators: working with floating point numbers and more operators  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this.
1 Data types, operations, and expressions Overview l Primitive Data Types l Variable declaration l Arithmetical Operations l Expressions.
Aalborg Media Lab 21-Jun-15 Software Design Lecture 2 “ Data and Expressions”
1 Fundamental Data Types. 2 Outline  Primitive Data Types  Variable declaration  Numbers and Constants  Arithmetic Operators  Arithmetic Operator.
Program Elements We can now examine the core elements of programming (as implemented in Java) We focuse on: data types variable declaration and use, constants.
ECE122 L3: Expression Evaluation February 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 3 Expression Evaluation and Program Interaction.
1 Data types, operations, and expressions Overview l Format of a Java Application l Primitive Data Types l Variable Declaration l Arithmetic Operations.
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
Review Java.
CSci 142 Data and Expressions. 2  Topics  Strings  Primitive data types  Using variables and constants  Expressions and operator precedence  Data.
1 Data types, operations, and expressions Continued l Overview l Assignment statement l Increment and Decrement operators l Short hand operators l The.
What is a variable?  A variable holds data in memory so the program may use that data, or store results.  Variables have a data type. int, boolean, char,
1 Chapter Two Using Data. 2 Objectives Learn about variable types and how to declare variables Learn how to display variable values Learn about the integral.
Expressions, Data Conversion, and Input
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 2 Elementary Programming.
Chapter 2: Introducing Data Types and Operators.  Know Java’s primitive types  Use literals  Initialize variables  Know the scope rules of variables.
***** SWTJC STEM ***** Chapter 2-3 cg 29 Java Operators Recall Java’s programming components: Packages - Collection of classes (Programs) Classes - Collections.
Chapter 2 Primitive Data Types and Operations F Introduce Programming with an Example  The MyInput class F Identifiers, Variables, and Constants F Primitive.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
CIS 260: App Dev I. 2 Programs and Programming n Program  A sequence of steps designed to accomplish a task n Program design  A detailed _____ for implementing.
Department of Computer Engineering Computer Programming for International Engineers I NTERNATIONAL S CHOOL OF E NGINEERING C HULALONGKORN U NIVERSITY.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Assignment Statements Operator Precedence. ICS111-Java Programming Blanca Polo 2 Assignment, not Equals  An assignment statement changes the value of.
Integer numerical data types. The integer data types The integer data types use the binary number system as encoding method There are a number of different.
CIS 260: App Dev I. 2 Programs and Programming n Program  A sequence of steps designed to accomplish a task n Program design  A detailed _____ for implementing.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 5P. 1Winter Quarter C Programming Basics.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 5P. 1Winter Quarter C Programming Basics Lecture 5.
Mathematical Calculations in Java Mrs. C. Furman.
Java Programming, Second Edition Chapter Two Using Data Within a Program.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]
Chapter 2 Input, Variables and Data Types. JAVA Input JAVA input is not straightforward and is different depending on the JAVA environment that you are.
GUJARAT KNOWLEDGE VILLAGE Faculty Name:- Prof H.M.Patel Students Name:- SONI VISHAL THAKKER BHAVIN ZALA JAYDIP ZALA.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes.
1 Lecture 5 More Programming Constructs Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung Institute of Technology.
Software Technology - I Tools to do operations.....
CSM-Java Programming-I Spring,2005 Fundamental Data Types Lesson - 2.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
1 1 Chapter 2 Elementary Programming. 2 2 Motivations In the preceding chapter, you learned how to create, compile, and run a Java program. Starting from.
CSCI 1100/1202 January 18, Arithmetic Expressions An expression is a combination of operators and operands Arithmetic expressions compute numeric.
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
Expressions.
Lecture 3 Java Operators.
Expressions.
Lecture 3: Operators, Expressions and Type Conversion
Data Conversion & Scanner Class
Multiple variables can be created in one declaration
Assignment and Arithmetic expressions
Type Conversion, Constants, and the String Object
Arithmetic Operator Operation Example + addition x + y
Increment and Decrement
Lecture 3 Expressions Richard Gesick.
Chapter 2 Edited by JJ Shepherd
With Assignment Operator
Fundamentals 2.
Expressions and Assignment
elementary programming
CS2011 Introduction to Programming I Elementary Programming
Data Types and Expressions
Chapter 2: Java Fundamentals cont’d
Chapter 2 Primitive Data Types and Operations
Data Types and Expressions
Data Types and Expressions
Presentation transcript:

1 More on Assignment and Console Input Overview l Increment & Decrement Operators l Short-cut Operators l Casting l Math class l Console Input (TextIO class) l Preview: Control Structures

2 Increment or Decrement Operators Increment/decrement operations ( count = count +1 ) are very common in programming. Java provides operators that make these operations shorter. OperatorUse Description ++ op ++ Increments op by 1; evaluates to a value before incrementing op Increments op by 1; evaluates to a value after incrementing -- op -- Decrements op by 1; evaluates to a value before decrementing op Decrements op by 1; evaluates to a value after decrementing

3 Increment or Decrement Operators Example : 1. What is the value of j and i after executing the following code? i = 1; j = 5; j = ++i; 2. What is the value of j and i after executing the following code? i = 10; j = 50; j = i--; 3. What is the value of j and i after executing the following code? i = 5; j = 10; i++; ++j;

4 Short Hand Operators Java also provides a number of operators that can be used as a short-cut for performing arithmetic operations on a variable and assigning the result to the same variable. Operator Short-FormEquivalent to += op1 += op2 op1 = op1 + op2 -= op1 -= op2 op1 = op1 - op2 *= op1 *= op2 op1 = op1 * op2 /= op1 /= op2 op1 = op1 / op2 %= op1 %= op2 op1 = op1 % op2 Example : Instead of writing a = a + 5 We can write a += 5 If the variable name on both sides of assignment operator are same then bring the operator, before the = operator. a = a + 5

5 Casting We learnt earlier that the following division 5 / 2 results in 2 Because the / operator is operating between 2 integer type constants and so the result will be an integer. To get 2.5, we need to convert either 1 or both the operands to double. Then the division will look like 5.0 / 2.0 But what if we have integer variables to divide each other, like a / b ? For this, cast operator is used. (double) a / (double) b

6 Primitive Casting Conversion of primitives is accomplished by (1) assignment and/or (2) explicit casting: int total = 100; float temp = total; // temp now holds When changing type that will result in a loss of precision, an explicit ‘cast’ is needed. This is done by placing the new type in parenthesis: float total = 100F; int temp = total; // ERROR! int start = (int) total;

7 Methods in java.lang.Math MethodArgument type(s)Functionality Math.abs(a)Int/ long/ float/ doubleAbsolute value Math.ceil (a)DoubleSmallest whole number greater than or equal to a Math.cos(a)DoubleCosine Math.exp(a)DoubleExponential number to the power of a Math.floor(a)DoubleLargest whole number less than or equal to a Math.log(a)DoubleNatural logarithm of a Math.max(a, b)Int/ long/ float/ doubleMaximum Math.min(a, b)Int/ long/ floa / doubleMinimum Math.pow(a, b)Doublea to the power of b Math.random()NoneRandom number generator Math.rint(a)DoubleConverts double value to integral value in double format Math.round(a)DoubleRounds into closest long Math.sin(a)DoubleSine Math.sqrt(a)DoubleSquare root Math.tan(a)DoubleTangent

8 Console Input l Reading input from the console (text mode) is a bit complex for beginners to Java. l To avoid this complexity, we shall initially be using a class, TextIO, which has been designed to simplify console input operations. l The TextIO class has the following methods which are used to read the desired type of input. readByte() : reads a byte from the data source. readChar() : reads a character. readDouble() : reads a double readFloat() : reads a floate. readInt(): reads an integer. readLong(): reads a long integer. readShort() : reads a short integer. readString(): reads a string made out of non-white space

9 Console Input l To use the TextIO class, you need to add the following pieces of code to your programs: »Import the TextIO class by placing the following import statement at the beginning of your program : import TextIO; »Create a TextIO object as a field inside your class: static TextIO stdin = new TextIO(System.in); Note: The parmeter to TextIO can be keyboard ( System.in ), a data file or a URL »Change the heading of the main method as follows: public static void main(String[] args) throws java.io.IOException; l For all these to work, you need to copy the file TextIO.class to the same folder containing your program. You can get the TextIO.class as well as its source (.java ) on the following URL:

10 Evaluating Expressions Arithmetic Expression Example import TextIO; class Expressions { static TextIO stdin = new TextIO(System.in); public static void main(String[]args) throws java.io.IOException { double e; int a, b, c; a = stdin.readInt(); b = stdin.readInt(); c = stdin.readInt(); e = a + b * c; System.out.println("a+b*c = " + e); }

11 Evaluating Arithmetic Expression With Simple Math functions - Example import TextIO; class Expressions { static TextIO stdin = new TextIO(System.in); public static void main(String[]args) throws java.io.IOException { double a,c; int r; r = stdin.readInt(); a = Math.PI * Math.pow(r,2); c = 2 * Math.PI * r; System.out.println("Area = " + a); System.out.println("Circum. =" + c); }