JavaRush /Java Blog /Random EN /Modulo division operator
L2CCCP
Level 9

Modulo division operator

Published in the Random EN group
The modulo division operator is the operator mod, denoted by the symbol %. Modulo division operator - 1This operator returns the remainder when the first operand is divided by the second. modThe " " operator %in Java works not only with integer (such as: byte/int/short/long), but also with floating point (such as: float/double) numbers\types. The program below illustrates how this statement works:
package com.l2cccp.work;

public class Mod
{
	public static void main(String args[])
	{
		int i = 17; // Integers
		double d = 17.3; // floating point

		System.out.println("i mod 10 = " + i % 10);
		System.out.println("d mod 10 = " + d % 10);
	}
}
By running this program you will get the following result:
i mod 10 = 7
d mod 10 = 7.300000000000001
We figured out how this operator works, but how to use it. As an option for inflecting words depending on quantity. Since I work as a developer of a java game emulator, it is very important for me that many words in messages are inflected. Eg:
  1. You have been playing for 1 day already .
  2. You've been playing for 2 days already .
  3. You have been playing for 5 days already .
Let's write a declination program:
package com.l2cccp.work;

public class Mod
{
	public static void main(String args[])
	{
		int[] day= new int[] { 1, 2, 5 };

		System.out.println("You're already playing" + day[0] + " " + declension(day[0]));
		System.out.println("You're already playing" + day[1] + " " + declension(day[1]));
		System.out.println("You're already playing" + day[2] + " " + declension(day[2]));
	}

	public static String declension(int count)
	{
		String one = "day";
		String two = "of the day";
		String five = "days";

		if(count > 100)
			count %= 100;

		if(count > 20)
			count %= 10;

		switch(count)
		{
			case 1:
				return one;
			case 2:
			case 3:
			case 4:
				return two;
			default:
				return five;
		}
	}
}
By running this program you will get the following result:
Вы играете уже 1 день
Вы играете уже 2 дня
Вы играете уже 5 дней
Everything works as we need, but what if we need to get several declinations in one result? Eg:
  1. You have been playing for 1 day and 1 hour .
  2. You have been playing for 2 days and 4 hours .
  3. You have been playing for 5 days 9 hours .
We can’t write an additional declension method for every word we need. Let's write a declination program like this:
package com.l2cccp.work;

public class Mod
{
	public static void main(String args[])
	{
		int[] day = new int[] { 1, 2, 5 };
		int[] hour = new int[] { 1, 4, 9 };

		System.out.println("You're already playing" + day[0] + " " + declension(day[0], "Days") + " And " + hour[0] + " " + declension(hour[0], "Hour"));
		System.out.println("You're already playing" + day[1] + " " + declension(day[1], "Days") + " And " + hour[1] + " " + declension(hour[1], "Hour"));
		System.out.println("You're already playing" + day[2] + " " + declension(day[2], "Days") + " And " + hour[2] + " " + declension(hour[2], "Hour"));
	}

	public static String declension(int count, String type)
	{
		String one = "";
		String two = "";
		String five = "";

		if(type.equals("Days"))
		{
			one = "day";
			two = "of the day";
			five = "days";
		}
		else if(type.equals("Hour"))
		{
			one = "hour";
			two = "hours";
			five = "hours";
		}

		if(count > 100)
			count %= 100;

		if(count > 20)
			count %= 10;

		switch(count)
		{
			case 1:
				return one;
			case 2:
			case 3:
			case 4:
				return two;
			default:
				return five;
		}
	}
}
By running this program you will get the following result:
Вы играете уже 1 день и 1 час
Вы играете уже 2 дня и 4 часа
Вы играете уже 5 дней и 9 часов
Since our method is described as a public " public", you can easily call it from any package:
Mod.declension(count, type);
That's all, I hope this will be useful to someone. Good luck in mastering the Java language.
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION