# 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

operatorsand the objects of the operations are referred to asoperands.

There are many types of operators and the main ones are discussed here.

$1. \color{#20A900}\text{ Arithmetic Operators}$

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 $5$ basic arithmetic calculations : **addition $(+)$, subtraction $(-)$, multiplication $(\times)$, division $(\div)$,** and **modulus** *(%)*.

Note:

- Modulus operator is used to find the remainder.
- If the keyword $int$ is used before any operation, you should write the answer
**only in integer and not in decimal**. However, when either of the keywords $float$ or $double$ is used, you should write the answer**only in decimals**. $float$ is used for or 2 decimal places and $double$ is used for more than 2 decimal places.

Find all the values of all 5 arithmetic operations for the numbers $5$ and $3$. Assume that the keyword used is $int$ for all.

We have the following:

- Addition: $5 + 3 = 8$
- Subtraction: $5 - 3 = 2$
- Multiplication: $5 \times 3 = 15$
- Division: $5 \div 3 = 1$
- Modulus:
5 % 3 = 2

Given $a = 10$ and $b = 15,$ find the following:

- $int (a + b)$
- $double (b \div a)$
- $float (a \times b)$
double (b % a)We have the following:

- $int (a + b) = 10 + 15 = 25$
- $double (b \div a) = 15 \div 10 = 1.500$
- $float (a \times b) = 10 \times 15 = 150.0$
- $double$
(b % a)= $15$%$10 = 5.000$

Note:If you got the answer as an integer but the keyword $float$ or $double$ 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

- $a = 5 \implies +a = 5$
- $b = 0 \implies +b = 0$
- $c = -4 \implies +c = -4.$
So, if $\boxed{a = n,~\text{then}~+a = +n}.$

Now, we have

- $d = 5 \implies -d = -5$
- $e = 0 \implies -e = 0$
- $f = -4 \implies -f = 4.$
So, if $\boxed{a = n,~\text{then}~-a = -n}.$

If $a = 9$ and $b = -6,$ then find the value of $(+a) + (-b).$

We have$\begin{aligned} + a &= +9\\\\ -b &= -(-6) \\&= +6\\\\ (+a) + (-b) &= 9 + 6 \\&= 15. \end{aligned}$

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,
$\begin{aligned}
a = a + 1 &\text{ is the same as } ++a\ \text{ or }\ a++\\
a = a - 1 &\text{ is the same as } --a\ \text{ or }\ a--.
\end{aligned}$
However, both the increment and decrement operators come in two varieties: *prefix and postfix*.

**Prefix increment and decrement operators** $($of the form $++a \text{ or } --a)$

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 $a++ \text{ or } a--)$

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 $x = (++y) + 2y$ if initially $y = 6.$

In this problem, only prefix operator is used, so we can solve this easily. First, we will change the value of $y:$

$++y = y + 1 = 6 + 1 = 7.$

Then

$x = 7 + 2(7) = 21.\ _\square$

Evaluate $x = (--y) + 3y$ if initially $y = 10.$

In this problem, too, only prefix operator is used, so we can solve this easily:

$\begin{aligned} --y &= y - 1 \\&= 10 - 1 \\&= 9\\\\ x &= 9 + 3(9) \\&= 36.\ _\square \end{aligned}$

Evaluate $x = (y++) + 2y$ if initially $x = 6.$

Here, we have to be careful because postfix operator is used. So, for the first operand of $y$ the value remains the same, but for the next operand of $y$ the value increments (1 is added):

$y++ = 6\ \text{ but }\ 2y = 2(6 + 1) = 2(7) = 14.$

Therefore,

$x = (y++) + 2y = 6 + 14 = 20.\ _\square$

Evaluate $x = (y--) + 2y$ if initially $x = 10.$

Here, we also have to be careful because postfix operator is used. So, for the first operand of $y$ the value remains the same, but for the next operand of $y$ the value decrements (1 is added):

$y-- = 10\ \text{ but }\ 2y = 2(10 + 1) = 2(9) = 18.$

Therefore,

$x = (y++) + 2y = 10 + 18 = 28.\ _\square$

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 $x = (y++) \times (5 + 2y)$ if initially $y = 5$.

Initially, $y = 5$. So, $(y++) = 5$ and $2(y) = 2(5 + 1) = 2(6) = 12.$So, $x = 5 \times (5 + 12)= 5 \times 17 = 85.\ _\square$

Evaluate $x = (++y) \times (y++) \times \big(1 + (--y)\big)$ if initially $y = 3.$

Initially, $y = 3$. So, $(++y) = 4, (y++) = 4.$Here, $(y--)$ will be $3$ but not directly. In the before operand the operator is postfix so now the value should be $5$. But there is also a prefix operator for this $y,$ so $\big((1 + (--y)\big) = 1 + 5 - 1 = 5.$

So, $x = 4 \times 4 \times 5 = 80.\ _\square$

## Conditional Statements

## Looping Statements

## Functions

## Arrays

## Strings

**Cite as:**Problems Based On Java.

*Brilliant.org*. Retrieved from https://brilliant.org/wiki/problems-based-on-java/