Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 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 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 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 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 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 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 100.0 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 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 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 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: http://www.ccse.kfupm.edu.sa/~bmghandi/001/utils.html

10 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 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); }


Download ppt "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."

Similar presentations


Ads by Google