[Go to site: main page, start]

0% found this document useful (0 votes)
21 views13 pages

Java Operators and Math Class

The document provides an overview of Java operators, including arithmetic, assignment, relational, increment/decrement, and logical operators. It also introduces the Math class, detailing its methods for mathematical operations such as exponentiation and rounding. Additionally, it describes a practice program that analyzes student scores using various operators and the Math class functionalities.

Uploaded by

aclc.sy.2526
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views13 pages

Java Operators and Math Class

The document provides an overview of Java operators, including arithmetic, assignment, relational, increment/decrement, and logical operators. It also introduces the Math class, detailing its methods for mathematical operations such as exponentiation and rounding. Additionally, it describes a practice program that analyzes student scores using various operators and the Math class functionalities.

Uploaded by

aclc.sy.2526
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

PROGRAMMING

JAVA NC III
PART 1

JAVA OPERATORS AND MATH CLASS


Java Operators
Are special symbols or keywords that perform operations on one or more
operands (variables, values, or expressions) to produce a result.

In simpler terms, operators tell Java what to do with the data.

Arithmetic Operators (perform basic math)


Assignment Operators (assign values to variables)
Relational Operators (compare values)
Increment and Decrement
Logical Operators (combine boolean conditions)
Arithmetic Operators (perform basic math)
Operator Meaning Example Result

+ Addition 5+3 8

- Subtraction 5 - 3' 2

* Multiplication 5*3 15

/ Division 10 / 2' 5

% Modulus / Remainder 10 % 3 1
Assignment Operators (assign values to variables)
Operator Meaning Example
'= Assign x = 5;
+= Add then assign x += 3; or x = x + 3
-= Subtract then assign x -= 2; or x = x - 2
*= Multiply then assign x *= 4; or x = x * 4
/= Divide then assign x /= 2; or x = x / 2
%= Modulus then assign x %= 3; or x = x % 3
Relational Operators (compare values)
Operator Meaning Example Result
> Greater than 5>3 TRUE
< Less than 5<3 FALSE
>= Greater or equal 5 >= 5 TRUE
<= Less or equal 3 <= 5 TRUE
'== Equal to 5 == 5 TRUE
!= Not equal 5 != 3 TRUE
Increment & Decrement
Type Syntax Description Example Result

Increment first,
Prefix ++x int x = 5; int y = ++x; x = 6, y = 6
then use the value
Use the value first,
Postfix x++ int x = 5; int y = x++; x = 6, y = 5
then increment
Decrement first,
Prefix --x int x = 5; int y = --x; x = 4, y = 4
then use the value
Use the value first,
Postfix x-- int x = 5; int y = x--; x = 4, y = 5
then decrement
Logical Operators (combine boolean conditions)
Operator Meaning Example Result
&& AND (both true) (5>3 && 2<4) TRUE
|| OR (either) (5>3 || 2>4) TRUE
! NOT (negates) !(5>3) FALSE
Math Class
Is a built-in utility class in the [Link] package that provides methods
for performing mathematical operations like exponentiation, rounding,
trigonometry, absolute values, and more.

You don’t need to create an object of the Math class because all its
methods are static, which means you can call them directly using
[Link]().
Math Class
Reminders:

1. Part of [Link] package → automatically imported.


2. Provides methods for:
a. Arithmetic e.g., abs(), max(), min()
b. Exponentials e.g., pow(), sqrt(), cbrt()
c. Rounding - e.g., ceil(), floor(), round()
d. Trigonometry - e.g., sin(), cos(), tan()
e. Random numbers - e.g., random()
Commonly Used Methods from Math Class
Method Description Example Result

[Link](x) Returns absolute value [Link](-5) 5


[Link](a, b) Returns maximum of two numbers [Link](5, 10) 10
[Link](a, b) Returns minimum of two numbers [Link](5, 10) 5
[Link](x) Returns square root [Link](25) 5
[Link](a, b) Returns a raised to the power of b [Link](2, 3) 8
[Link](x) Rounds up to nearest integer [Link](4.2) 5
[Link](x) Rounds down to nearest integer [Link](4.8) 4
[Link](x) Rounds to nearest integer [Link](4.5) 5
[Link]() Returns a random double between 0.0 and 1.0 [Link]() 0.7234
Practice
Practice: Student Score Analyzer
Program Description:
Create a Java program that calculates and analyzes a student’s scores in three subjects. The program will:

[Link] the user to input scores for three subjects.


[Link] total, average, and percentage using arithmetic and assignment operators.
[Link] scores using increment/decrement operators (simulate bonus or penalty).
[Link] scores using comparison operators to check the highest and lowest score.
[Link] logical operators to check if the student passed all subjects.
[Link] the Math class to round, find absolute differences, and calculate powers/square roots.
[Link] a summary report with all results.

Note:
Max is score is 50
Passing score is 38
Questions?

You might also like