Lect04B

advertisement
More on Assignment and Console Input
Overview

Increment & Decrement Operators

Short-cut Operators

Casting

Math class

Console Input (TextIO class)

Preview: Control Structures
1
Increment or Decrement Operators
Increment/decrement operations (count = count
+1) are very common in programming. Java provides
operators that make these operations shorter.
Operator
Use
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
2
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;
3
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-Form
+=
-=
*=
/=
%=
op1
op1
op1
op1
op1
+=
-=
*=
/=
%=
Equivalent to
op2
op2
op2
op2
op2
op1
op1
op1
op1
op1
=
=
=
=
=
op1
op1
op1
op1
op1
+
*
/
%
op2
op2
op2
op2
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
4
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
5
Primitive Casting
• Conversion of primitives is accomplished by
(1) assignment and/or (2) explicit casting:
int total = 100;
float temp = total;
now holds 100.0
// temp
• 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;
6
Methods in java.lang.Math
Method
Argument type(s)
Functionality
Math.abs(a)
Int/ long/ float/ double
Absolute value
Math.ceil (a)
Double
Smallest whole number
greater than or equal to a
Math.cos(a)
Double
Cosine
Math.exp(a)
Double
Exponential number to the
power of a
Math.floor(a)
Double
Largest whole number less
than or equal to a
Math.log(a)
Double
Natural logarithm of a
Math.max(a, b) Int/ long/ float/ double
Maximum
Math.min(a, b)
Minimum
Int/ long/ floa / double
Math.pow(a, b) Double
a to the power of b
Math.random() None
Random number generator
Math.rint(a)
Double
Converts double value to
integral value in double
format
Math.round(a)
Double
Rounds into closest long
Math.sin(a)
Double
Sine
Math.sqrt(a)
Double
Square root
Math.tan(a)
Double
Tangent
7
Console Input



Reading input from the console (text mode) is a bit complex
for beginners to Java.
To avoid this complexity, we shall initially be using a class,
TextIO, which has been designed to simplify console input
operations.
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
8
Console Input

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;
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
9
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);
}
}
10
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);
}
}
11
Download