1d. Construct The Expressions Using Implicit and Explicit Type Conversions To Solve The Given Problem.
1d. Construct The Expressions Using Implicit and Explicit Type Conversions To Solve The Given Problem.
Expression:
When An expression is a
associativity combination of operators,
is used? constants and variables.
Ans:
An expression may consist
When an
of one or more operands,
expression
and zero or more
contains
operators to produce a
operators
value.
from the
same group,
associativity Expression evaluation in
rules are Java is based upon the
applied to following concepts:
determine Operator
which precedence
operation Associativity
should be rules
performed
first.
What is
Explanation of Concept:
Precedence
Operator Precedence in Java
order
When several operations occur in an expression, each part is evaluated
Ans:
and resolved in a predetermined order called operator precedence.
When two
Parenthesis can be used to override the order of precedence and force
operators
some parts of an expression to be evaluated before other parts.
share an
Operations within parenthesis are always performed before those
operand, the
outside. Within parenthesis, however, normal operator precedence is
operator
maintained.
with the
When expressions contain operators from more than one category,
higher
arithmetic operators are evaluated first, comparison operators are
precedence
evaluated next and logical operators are evaluated last. Comparison
goes first.
operators all have equal precedence; that is they are evaluated in the
left-to- right order in which they appear. Arithmetic and logical
operators are evaluated in the following order of precedence.
Table shows all java operators from highest to lowest precedence, along
with their associativity.
Operator Description Level Associativity
Precedence order: When two operators share an operand, the operator with the
higher precedence goes first.
For example 1+2*3 is treated as 1+(2*3), whereas 1*2+3 is treated as (1*2)+3
since multiplication has a higher precedence than addition.
Associativity: When two operators with the same precedence, the expression is
evaluated according to its associativity.
For example, x=y=z=17 is treated as x=(y=(z=17))), leaving all three variables with
the value 17, since the = operator has right-to-left associativity. On the other
hand, 70/2/3 is treated as (70/2)/3 since the / operator has left-to-right
associativity.
Expression of Evaluation:
An expression is a construct which is made up of literals, variables, method calls
and operators following the syntax of Java. Every expressions consists of at least
one operator and an operand. Operand can be either a literal, variable or a
method invocation.
It is common for an expression to have more than one operator.
For example, consider the below example:
(20 * 5) + (10 / 2) – (3 * 10)
Example:
class Precedence
{
public static void main(String[] args)
{
int a = 10, b = 5, c = 1, result;
result = a-++c-++b;
System.out.println(result);
}
}
Output:
2
Mathematical Function:
Mathematical function such as sin, cos, sqrt used to analyze of real life problems.
Java supports these basic math functions through Math class defined in the
java.lang package. The mathematical functions are:
Pow():Power of the number. Here variable1 is base value and variable2 is power
value
Syntax: Math.pow(variable1,variable2)
Here variable is double datatype
Example: Math.pow(2,3;
Output: 8
Ceil():In arithmetic, the ceiling of a number is the closest integer that is greater or
higher than the number considered. ceil method is used round up the variable.
Syntax: Math.ceil(variable)
Example: Math.ceil(12.15)=13
Math.ceil(-24.06)=-24
Consider a floating-point number such as 12.155. This number is between integer
12 and integer 13. In the same way, consider a number such as –24.06. As this
number is negative, it is between –24 and –25, with –24 being greater.
Floor()=This is the floor() function which returns you the largest value but less
than the argument. floor method is used round down the variable.
Syntax: Math.floor(variable)
Here variable is double datatype
Example: Math.floor(8.4) = 8
Math.floor(-24.06)=-25
copySign()= copySign method is used to copy sign of variable2 and assign that sign
to variable1.
Syntax: Math.copySign(variable1, variable2)
Here variable is float and double datatype
Example: Math.copySign(2,-8.3) = -2.0
Math.copySign(-2,8.3) = 2.0
Random()=This is the random() function which returns you the random number. It
is absolutely system generated. This method can be used to produce a random
number between 0 and 100
Example:
public class JavaMathExample1
{
public static void main(String[] args)
{
double x = 28;
double y = 4;
// return the maximum of two numbers
System.out.println("Maximum number of x and y is: " +Math.max(x, y));
10.
11. // return the square root of y
12. System.out.println("Square root of y is: " + Math.sqrt(y));
13.
14. //returns 28 power of 4 i.e. 28*28*28*28
15. System.out.println("Power of x and y is: " + Math.pow(x, y));
16.
17. // return the logarithm of given value
18. System.out.println("Logarithm of x is: " + Math.log(x));
19. System.out.println("Logarithm of y is: " + Math.log(y));
20.
21. // return the logarithm of given value when base is 10
22. System.out.println("log10 of x is: " + Math.log10(x));
23. System.out.println("log10 of y is: " + Math.log10(y));
24.
25. // return the log of x + 1
26. System.out.println("log1p of x is: " +Math.log1p(x));
27.
28. // return a power of 2
29. System.out.println("exp of a is: " +Math.exp(x));
30.
31. // return (a power of 2)-1
32. System.out.println("expm1 of a is: " +Math.expm1(x));
33. }
34. }
Output:
Maximum number of x and y is: 28.0
Square root of y is: 2.0
Power of x and y is: 614656.0
Logarithm of x is: 3.332204510175204
Logarithm of y is: 1.3862943611198906
log10 of x is: 1.4471580313422192
log10 of y is: 0.6020599913279624
log1p of x is: 3.367295829986474
exp of a is: 1.446257064291475E12
expm1 of a is: 1.446257064290475E12
Type Casting:
Converting one primitive datatype into another is known as type casting (type
conversion) in Java. You can cast the primitive datatypes in two ways namely,
Widening and, Narrowing.
Widening − Converting a lower datatype to a higher datatype is known as
widening. In this case the casting/conversion is done automatically therefore, it is
known as implicit type casting. In this case both datatypes should be compatible
with each other.
Example:
public class Main
{
public static void main(String[] args)
{
int a = 9;
double b = a; // Automatic casting: int to double
System.out.println(a);
System.out.println(b);
}
}
Output:
9
9.0
Example:
public class Main
{
public static void main(String[] args)
{
double a = 9.78;
int b= (int) a; // Explicit casting: double to int
System.out.println(a);
System.out.println(b);
}
}
Output:
9.78
9