Problems Based On Java
What is Java?
Java is a popular, third-generation programming language, which can be used to do any of the thousands of things that a computer software can do. With the features it offers, Java has become the language of choice for Internet and Internet applications. Java plays an important role in the proper functioning of many software-based devices attached to a network.
Java is a very vast and huge topic. But in this wiki, we are going to discuss only the various types of problems which are the very basics for a programming language. Although this is a small topic without this knowledge, it is almost impossible to understand Java programming language. In each section, before going to the problems, there is theory part which will help you solve the problems.
Operators in Java
The operations being carried out are represented by operators. The operations (specific tasks) are represented by operators and the objects of the operations are referred to as operands.
There are many types of operators and the main ones are discussed here.
1.1 Binary Operators
Operators that act upon two operands are referred to as binary operators.
To do arithmetic, Java uses operators. It provides operators for basic arithmetic calculations : addition , subtraction , multiplication , division , and modulus (%).
Note:
- Modulus operator is used to find the remainder.
- If the keyword is used before any operation, you should write the answer only in integer and not in decimal. However, when either of the keywords or is used, you should write the answer only in decimals. is used for or 2 decimal places and is used for more than 2 decimal places.
Find all the values of all 5 arithmetic operations for the numbers and . Assume that the keyword used is for all.
We have the following:
- Addition:
- Subtraction:
- Multiplication:
- Division:
- Modulus: 5 % 3 = 2
Given and find the following:
- double (b % a)
We have the following:
- (b % a) = %
Note: If you got the answer as an integer but the keyword or is used, you must enter the answer only as decimals.
1.2 Unary Operators
Operators that act on one operand are referred to as unary operators.
How to use unary operators is explained in the following example:
We have
So, if
Now, we have
So, if
If and then find the value of
We have
1.3 Increment and Decrement Operators
Java includes two useful operators not generally found in other computer languages (except C and C++). These are increment and decrement operators, and The operator adds 1 to its operand, and the operator subtracts 1. In other words, However, both the increment and decrement operators come in two varieties: prefix and postfix.
Prefix increment and decrement operators of the form
The prefix increment or decrement operator follows the "change-then-use rule"; that is, they first change (add or subtract) the value of their operand, and then use the new value in calculating the problem. If you encounter with a prefix operator, the best thing to do is to first increase the value by 1 if it is an increment or decrease the value by 1 if it is a decrement operator.
Postfix increment and decrement operators of the form
The postfix increment or decrement operator follows the "use-then-change rule"; that is, they first use the value of their operand in evaluating the expression, and then change (add or subtract) the operand value. If you encounter with a postfix operator for the first time, do not change the value of operand, but if you encounter it for the second time, then change (add or subtract) the value.
You will better understand this concept by the following examples:
Evaluate if initially
In this problem, only prefix operator is used, so we can solve this easily. First, we will change the value of
Then
Evaluate if initially
In this problem, too, only prefix operator is used, so we can solve this easily:
Evaluate if initially
Here, we have to be careful because postfix operator is used. So, for the first operand of the value remains the same, but for the next operand of the value increments (1 is added):
Therefore,
Evaluate if initially
Here, we also have to be careful because postfix operator is used. So, for the first operand of the value remains the same, but for the next operand of the value decrements (1 is added):
Therefore,
Not only this we can also use multiplication and division operators and also we can use both prefix and postfix operators in one problem.
Evaluate if initially .
Initially, . So, andSo,
Evaluate if initially
Initially, . So,Here, will be but not directly. In the before operand the operator is postfix so now the value should be . But there is also a prefix operator for this so
So,