Found 9321 Articles for Object Oriented Programming

Java Program to remove the leading and trailing quotes from a string

karthikeya Boyini
Updated on 25-Jun-2020 12:11:51

2K+ Views

Firstly, let us consider a string with quotesString originalStr = "\"Demo Text\"";Now, consider the following logic for the beginning quote.if (originalStr.startsWith("\"")) {    originalStr = originalStr.substring(1, originalStr.length()); }Now, consider the following logic for the ending quote.if (originalStr.endsWith("\"")) {    originalStr = originalStr.substring(0, originalStr.length() - 1); }Example Live Demopublic class Demo {    public static void main(String[] args) {       String originalStr = "\"Demo Text\"";       System.out.println("String with double quotes= "+originalStr);       if (originalStr.startsWith("\"")) {          originalStr = originalStr.substring(1, originalStr.length());       }       if (originalStr.endsWith("\"")) {       ... Read More

Shift left in a BigInteger in Java

Samual Sam
Updated on 25-Jun-2020 10:59:45

143 Views

To shift left in a BigInteger, use the shiftLeft() method.The java.math.BigInteger.shiftLeft(int n) returns a BigInteger whose value is (this

Java Program to shift bits in a BigInteger

karthikeya Boyini
Updated on 25-Jun-2020 11:01:12

75 Views

To shift bits in a BigInteger, use the shiftLeft() or shiftRight() method.shiftLeft() methodThe java.math.BigInteger.shiftLeft(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).Example Live Demoimport java.math.*; public class Demo {    public static void main(String[] args) {       BigInteger one;       one = new BigInteger("25");       one = one.shiftRight(3);       System.out.println("Result: " +one);    } }OutputResult: 3

Java Program to flip a bit in a BigInteger

Samual Sam
Updated on 25-Jun-2020 11:02:24

103 Views

To flip a bit in a BigInteger in Java, use the flipBit() method. This method returns a BigInteger whose value is equivalent to this BigInteger with the designated bit flipped.Example Live Demoimport java.math.*; public class Demo {    public static void main(String[] args) {       BigInteger one, two;       one = new BigInteger("7");       one = one.flipBit(3);       System.out.println("Result: " +one);    } }OutputResult: 15Let us see another example.Example Live Demoimport java.math.*; public class Demo {    public static void main(String[] args) {       BigInteger bi1, bi2;       bi1 = ... Read More

Clear a bit in a BigInteger in Java

karthikeya Boyini
Updated on 25-Jun-2020 11:06:01

195 Views

To clear a bit in a BigInteger in Java, use the clearBit() method. It returns a BigInteger whose value is equivalent to this BigInteger with the designated bit cleared.Example Live Demoimport java.math.*; public class BigIntegerDemo {    public static void main(String[] args) {       BigInteger one, two;       one = new BigInteger("7");       two = one.clearBit(2);       System.out.println("Result: " +two);    } }OutputResult: 3Let us see another example.Example Live Demoimport java.math.*; public class Demo {    public static void main(String[] args) {       BigInteger bi1, bi2;       bi1 = new ... Read More

Set a bit for BigInteger in Java

Samual Sam
Updated on 25-Jun-2020 11:07:41

140 Views

The setBit() method is used in Java to return a BigInteger whose value is equivalent to this BigInteger with the designated bit set.Example Live Demoimport java.math.*; public class BigIntegerDemo {    public static void main(String[] args) {       BigInteger one, two;       one = new BigInteger("7");       two = one.setBit(3);       System.out.println("Result: " +two);    } }OutputResult: 15Let us see another example.Example Live Demoimport java.math.*; public class Demo {    public static void main(String[] args) {       BigInteger bi1, bi2;       bi1 = new BigInteger("9");       // setbit ... Read More

Java program to check if string is pangram

karthikeya Boyini
Updated on 27-Jun-2024 17:51:16

7K+ Views

A pangram is a string that contains all the letters of the English alphabet. An example of a pangram is "The quick brown fox jumps over the lazy dog". Problem Statement Given a string, write a Java program to check whether it is pangram or not.Consider the following example - Input The quick brown fox jumps over the lazy dog. Output String: The quick brown fox jumps over the lazy dog. The above string is a pangram. Algorithm The algorithm below is focusing to check if the string is pangram. Step 1: Create a boolean array `alphaList` ... Read More

Ternary Operator in Java

Samual Sam
Updated on 25-Jun-2020 11:14:54

1K+ Views

A ternary operator uses 3 operands and it can be used to replace the if else statement. This can be done to make the code simpler and more compact.The syntax of the ternary operator is given as follows −Expression ? Statement 1 : Statement 2In the above syntax, the expression is a conditional expression that results in true or false. If the value of the expression is true, then statement 1 is executed otherwise statement 2 is executed.A program that demonstrates the ternary operator in Java is given as follows.Example Live Demopublic class Example {    public static void main(String[] args) ... Read More

Java Program to print the diamond shape

Samual Sam
Updated on 25-Jun-2020 11:19:22

206 Views

A diamond shape can be printed by printing a triangle and then an inverted triangle. An example of this is given as follows −* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *A program that demonstrates this is given as follows.Example Live Demopublic class Example {    public static void main(String[] args) {       int n = 6;       int s = n - 1; ... Read More

Basic calculator program using Java

karthikeya Boyini
Updated on 25-Jun-2020 11:21:45

17K+ Views

A basic calculator is able to add, subtract, multiply or divide two numbers. This is done using a switch case. A program that demonstrates this is given as follows −Exampleimport java.util.Scanner; public class Calculator {    public static void main(String[] args) {       double num1;       double num2;       double ans;       char op;       Scanner reader = new Scanner(System.in);       System.out.print("Enter two numbers: ");       num1 = reader.nextDouble();       num2 = reader.nextDouble();       System.out.print("Enter an operator (+, -, *, /): "); ... Read More

Advertisements