Institute of Computer Science
  1. Courses
  2. 2017/18 spring
  3. Object-Oriented Programming (Narva College) (P2NC.01.083)
ET
Log in

Object-Oriented Programming (Narva College) 2017/18 spring

  • Home
  • Materials
  • Java Glossary
  • Source Example
  • Cheat sheet (S1-S6)
  • Grading
  • Links
Session 1

Operations on numbers

Basic arithmetic operators

OperatorMeaningPriority
-unary minus (negative values)highest
*multiplicationmiddle
/divisionmiddle
%remaindermiddle
+additionlow
-subtractionlow

PS! We can use parentheses () to force an evaluation to have a higher priority.

Extra arithmetic operators

These operators work with both integers and doubles:

OperatorExampleEquivalent
++x++;x = x + 1;
--x--;x = x – 1;
+= x += y;x = x + y;
-=x -= y;x = x – y;
*=x *= y;x = x * y;
/=x /= y;x = x / y;
%=x %= y;x = x % y;

Useful link: Check examples here.

Self-assessment

Class Math

The Java Math class contains many math functions. That means we do not have to create math methods on our own, but we can use the implemented ones (check the available methods here).

The methods usually take parameters and return values (generally, double type).

Some examples:

SyntaxExampleSummary
double random()double x = Math.random();returns a double value [0.0; 1.0)
with a positive sign
double abs(double x)double x = Math.abs(-12.5);returns the absolute value
of a double value
double floor(double x)int x = Math.floor(4.2);returns the closest to positive
infinity double value that is less than
or equal to the argument
long round(double x)long x = Math.round(0.66);if x's decimal is .5 or more
round up to the next int;
otherwise round down
double ceil(double x)int x = Math.ceil(4.2);returns the closest to negative
infinity double value that is greater
or equal to the argument
double sin(double radians)double x = Math.sin(Math.PI / 2.0);sine of angle in radians

Self-assessment

What is the output of the following program?

public class HelloWorld {
    public static void main(String[] args){
        double rand_integer = Math.random()*5+15;
        long round_rand_num = Math.round(Math.random()*5+15);
        System.out.println(rand_integer);
        System.out.println(round_rand_num);
    }
}

Run the code in IntelliJ and check your answer!

Session 1
  • Institute of Computer Science
  • Faculty of Science and Technology
  • University of Tartu
In case of technical problems or questions write to:

Contact the course organizers with the organizational and course content questions.
The proprietary copyrights of educational materials belong to the University of Tartu. The use of educational materials is permitted for the purposes and under the conditions provided for in the copyright law for the free use of a work. When using educational materials, the user is obligated to give credit to the author of the educational materials.
The use of educational materials for other purposes is allowed only with the prior written consent of the University of Tartu.
Terms of use for the Courses environment