Java language provides 7 arithmetic operators: addition (+), subtraction (-), multiplication (*), division (/), remainder (%), increment (++) and decrement (--). The operators addition (+), subtraction (-) and multiplication (*) are so obvious, I don't need to go into detail of these operators. For more reference, you can find tutorial links at Java References link. Here we will discuss only 4 operators division (/), remainder (%), increment (++) and decrement (--) operators. Remainder operator (%) is also called modulus operator.
Division (/) and Remainder or Modulus (%) Operators
Here is the example -
Division (/) and Remainder or Modulus (%) Operators
Here is the example -
Class Divide (Divide.java)
import java.util.Scanner;In the above example, I have used Scanner class to get the input for 2 integer numbers - one for dividend and one for divisor. I have already explained in my earlier post - How to get input from console. Following instructions create an instance / object of Scanner class and prompt user to enter 2 integer numbers and assign the entered values to integer variables dividend and divisor.
public class Divide {
public static void main(String args[]) {
// Create Scanner object for input
Scanner sc = new Scanner(System.in);
// Asks to user to enter two integer numbers
System.out.print("Enter the dividend and divisor number: ");
// Prompts user to enter dividend integer number
int dividend = sc.nextInt();
// Prompts user to enter divisor integer number
int divisor = sc.nextInt();
// Calculates quotient by dividing dividend by divisor
int quotient = dividend / divisor;
// Calculates remainder by dividing dividend
// by divisor using remainder operator
int remainder = dividend % divisor;
// Prints the quotient
System.out.println("Quotient: " + quotient);
// Prints the remainder
System.out.println("Remainder: " + remainder);
}
}
// Create Scanner object for inputIn next steps, I used the division and remainder operators between dividend and divisor. When the program is run, dividend is divided by divisor and result is assigned to the integer variable quotient. And with remainder operator, again same division is executed but here remainder part of the division is assigned to integer variable remainder.
Scanner sc = new Scanner(System.in);
// Asks to user to enter two integer numbers
System.out.print("Enter the dividend and divisor number: ");
// Prompts user to enter dividend integer number
int dividend = sc.nextInt();
// Prompts user to enter divisor integer number
int divisor = sc.nextInt();
// Calculates quotient by dividing dividend by divisorAnd then the last 2 instructions are used to print the quotient and remainder values to the console. Output on console will be like this :
int quotient = dividend / divisor;
// Calculates remainder by dividing dividend
// by divisor using remainder operator
int remainder = dividend % divisor;
// Prints the quotient
System.out.println("Quotient: " + quotient);
// Prints the remainder
System.out.println("Remainder: " + remainder);
Enter the dividend and divisor number: 198 45
Quotient: 4
Remainder: 18
These operators are also called unary operators, as operation is performed on only one operand. These operators can be applied before or after the operand. If the operator is used before the operand as (++x) then it's called pre-increment operator. And if the operator is used after the operand as (x++) then it's called post-increment operator. Suppose x = 10, let's see how post / pre increment operator works.
Pre-Increment operator
++x means, first increment and then assign the value to the operand.
System.out.println(++x);System.out.println(x);
++x => x = x + 1 => x = 11 so, both instructions will print 11.
Post-Increment operator
x++ means, first assign and then increment and again assign the value to the operand.
System.out.println(x++);System.out.println(x);
x++ => x = x; x = x + 1 => x = 10; x = 11 so, first instruction will print 10 and next one will print 11.
Here is the example for post / pre increment operators -
Class Increment (Increment.java)
public class Increment {In the above example, I have printed original value of x, and also values after the post-incremented and pre-incremented values. Post and pre decrement (--) operators also perform same ways. Here is the output -
public static void main(String args[]) {
int x = 20;
System.out.println("Increment Operator");
// Original Value x = 20
System.out.println("x = " + x);
// Post increment operator x = 20; print; x = 21
System.out.println("x++ = " + x++);
// After post increment x = 21
System.out.println("x = " + x);
// Pre increment operator x = 22
System.out.println("++x = " + ++x);
// After pre increment
System.out.println("x = " + x);
}
}
Increment Operator
x = 20
x++ = 20
x = 21
++x = 22
x = 22
NOTE: There are more unary operators, and we will learn with other kind of operators.
Example: Write a Java program to calculate the total money to be paid for the stationary items. Ask user for what items and how many items he / she wants to buy. Then calculate the total price and print the price to be paid as output. Costs for each item are below -
Item | Cost |
---|---|
Pencil | $0.10 |
Eraser | $0.20 |
Sharpener | $0.25 |
Notebook | $0.50 |
import java.util.Scanner; public class CalCost { public static void main(String args[]) { Scanner sc = new Scanner(System.in); double pencilPrice = .10; double eraserPrice = .20; double sharpenerPrice = .25; double notebookPrice = .50; System.out.println("How many Pencils?"); int pencils = sc.nextInt(); System.out.println("How many Erasers?"); int erasers = sc.nextInt(); System.out.println("How many Sharpeners?"); int sharpeners = sc.nextInt(); System.out.println("How many Notebooks?"); int notebooks = sc.nextInt(); double totCost = 0.0; double pencilsCost = pencils * pencilPrice; double erasersCost = erasers * eraserPrice; double sharpenersCost = sharpeners * sharpenerPrice; double notebooksCost = notebooks * notebookPrice; totCost = pencilsCost + erasersCost + sharpenersCost + notebooksCost; System.out.println("Total Cost is $" + totCost); } }
Code can be found at Github Link.
No comments:
Post a Comment