Samual Sam has Published 2310 Articles

Divide one BigInteger from another BigInteger in Java

Samual Sam

Samual Sam

Updated on 29-Jun-2020 05:25:27

136 Views

Use the BigInteger divide() method in Java to divide one BigInteger from another.First, let us create some objects.BigInteger one, two, three; one = new BigInteger("200"); two = new BigInteger("100");Divide the above and assign it to the third object −three = one.divide(two);The following is an example −Example Live Demoimport java.math.*; public class ... Read More

Calculate the power on a BigInteger in Java

Samual Sam

Samual Sam

Updated on 29-Jun-2020 05:24:05

260 Views

Use the BigInteger pow() method in Java to calculate the power on a BigInteger.First, let us create some objects.BigInteger one, two; one = new BigInteger("5");Perform the power operation and assign it to the second object −// power operation two = one.pow(3);The following is an example −Example Live Demoimport java.math.*; public class ... Read More

Shift right in a BigInteger in Java

Samual Sam

Samual Sam

Updated on 29-Jun-2020 05:22:00

191 Views

To shift right in a BigInteger, use the shiftRight() method.The java.math.BigInteger.shiftRight(int n) returns a BigInteger whose value is (this >> n). Sign extension is performed. The shift distance, n, may be negative, in which case this method performs a left shift. It computes floor(this / 2n).The following is an example ... Read More

Enum for days of week in Java

Samual Sam

Samual Sam

Updated on 29-Jun-2020 05:19:04

2K+ Views

To set enum for days of the week, set them as constantsenum Days { Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday }Now create objects and set the above constants −Days today = Days.Wednesday; Days holiday = Days.Sunday;The following is an example −Example Live Demopublic class Demo {    enum Days { ... Read More

The equals and == operator for Enum data type in Java

Samual Sam

Samual Sam

Updated on 29-Jun-2020 05:17:47

191 Views

We have the Devices Enum with four constants.enum Devices { LAPTOP, MOBILE, TABLET, DESKTOP; }We have created some objects and assigned them with the constants.Let us compare them with both equals() and ==. Firstly, begin with equals() −if(d1.equals(d2)) System.out.println("Devices are the same."); else System.out.println("Devices are different.");Now, let us move forward ... Read More

Java Program to use == operator to compare enums

Samual Sam

Samual Sam

Updated on 29-Jun-2020 05:16:12

144 Views

We can use the == operator to compare enums in Java.Let’s say we have the following enum.enum Devices {    LAPTOP, MOBILE, TABLET; }Here are some of the objects and we have assigned some values as well −Devices d1, d2, d3; d1 = Devices.LAPTOP; d2 = Devices.LAPTOP; d3 = Devices.TABLET;Let ... Read More

Create BigInteger via string in Java

Samual Sam

Samual Sam

Updated on 29-Jun-2020 05:12:13

179 Views

BigInteger class is used for big integer calculations which are outside the limit of the primitive data types. It provides operations for modular arithmetic, GCD calculation, primality testing, prime generation, bit manipulation, and a few other miscellaneous operations.Firstly, set a string −String str = "268787878787687";Now, create a new object for ... Read More

Java Program to compare dates if a date is before another date

Samual Sam

Samual Sam

Updated on 29-Jun-2020 05:11:29

223 Views

To compare dates if a date is before another date, use the Calendar.before() method.The Calendar.before() method returns whether this Calendar's time is before the time represented by the specified Object. First, let us set a date which is before the current dateCalendar date1 = Calendar.getInstance(); date1.set(Calendar.YEAR, 2010); date1.set(Calendar.MONTH, 11); date1.set(Calendar.DATE, ... Read More

Checks if two calendar objects represent the same local time in Java

Samual Sam

Samual Sam

Updated on 29-Jun-2020 04:51:44

171 Views

Use the == operator to compare two calendar objects.Let us first create the first calendar object and set date −Calendar date1 = Calendar.getInstance(); date1.set(Calendar.YEAR, 2040); date1.set(Calendar.MONTH, 10); date1.set(Calendar.DATE, 25); date1.set(Calendar.HOUR_OF_DAY, 11); date1.set(Calendar.MINUTE, 30); date1.set(Calendar.SECOND, 10);Now, the following is the second calendar object −Calendar date2 = Calendar.getInstance(); date2.set(Calendar.YEAR, 2040); date2.set(Calendar.MONTH, 10); ... Read More

Get the days remaining in current year in Java

Samual Sam

Samual Sam

Updated on 29-Jun-2020 04:44:06

2K+ Views

To get the days remaining in the current year, find the difference between the total days in the current year with the total number of days passed.First, calculate the day of year.Calendar calOne = Calendar.getInstance(); int dayOfYear = calOne.get(Calendar.DAY_OF_YEAR);Now, calculate the total days in the current year 2018.int year = ... Read More

Advertisements