Contents

Comparing Two Number Operations (with.Java)

   Aug 17, 2023     3 min read

In this article, we learned about For comparing two numbers (with.Java)

As we go through the Coding Test questions, we’ll look back on the problems we solved and learn about other ways to solve them. Let’s start with the problem.

Problem

The operation βŠ• is an operation on two integers that returns the value written by concatenating the two integers. For example

12 βŠ• 3 = 123 3 βŠ• 12 = 312 Given positive integers a and b, complete a solution function that returns the greater of a βŠ• b and ` 2 * a * b`.

However, if a βŠ• b and ` 2 * a * b` are equal, return a βŠ• b.

Example input and output

a: 2 b: 91 result: 364

This means that the value of ` 2 * a * b, 364, is greater than the value of a βŠ• b, 291, so the value of 2 * a * b` should be stored in the result.

My solution to the problem

import java.util.*;

class Solution {
    public int solution(int a, int b) {
        int answer = 0;

        ArrayList<String> arrList = new ArrayList<>();
        arrList.add(String.valueOf(a));
        arrList.add(String.valueOf(b));

        var i = arrList.get(0)+arrList.get(1);
        var j = 2*a*b;


        answer = Integer.parseInt(i) >= j ? Integer.parseInt(i) : j;

        return answer;
    }
}

Create an ArrayList to store the int types a and b by converting them to strings. We declare a variable i to hold the value of a βŠ• b. We store the value of 2 _ a _ b in variable j. Then, we use a ternary operator to convert i to int and return answer if the value is greater than or equal to j, and j if it is less.

Now, let’s solve the problem using Math.

First of all, what is Math in Java?

The Math class is a utility class used to perform mathematical operations in Java.

Let’s see some of the important methods to use the Math class.

Using the Math class

Math.max(a, b): Returns the greater of the two arguments.

Math.min(a, b): Returns the smaller of two arguments.

Math.abs(a): Returns the absolute value of the given number.

Math.pow(a, b): Returns the value of a to the power of b.

Math.sqrt(a): Returns the square root of a given number.

Math.round(a): Rounds the given number and returns it as an integer.

Math.floor(a): Returns the largest integer that is less than or equal to the given number.

Math.ceil(a): Returns the smallest integer that is greater than or equal to the given number.

Math.random(): Returns a random number greater than 0 and less than 1.

Math.sin(a), Math.cos(a), Math.tan(a): Returns the sine, cosine, and tangent values of a given angle.

Math.exp(a): Returns the value of a squared of e (the natural constant).

Math.log(a): Returns the value of the natural logarithm of a given number.

Math.log10(a): Returns the decimal logarithm of a given number.

Math.PI: Returns the value of the circumference ratio (Ο€).

Math.E: Returns the value of the natural constant e.

Solve problems with Math
class Solution {
    public int solution(int a, int b) {
        return Math.max(Integer.parseInt(String.valueOf(a)+String.valueOf(b)),2*a*b);
    }
}
``` Translated with www.DeepL.com/Translator (free version)