Programming Articles - Page 2425 of 3363

The abstract keyword in Java

AmitDiwan
Updated on 23-Sep-2019 12:35:35

473 Views

A class which contains the abstract keyword in its declaration is known as abstract class. Abstract classes may or may not contain abstract methods, i.e., methods without body. But, if a class has at least one abstract method, then the class must be declared abstract.If a class is declared abstract, it cannot be instantiated. To use an abstract class, you have to inherit it from another class, provide implementations to the abstract methods in it. If you inherit an abstract class, you have to provide implementations to all the abstract methods in it.Let us see a simple example −Exampleabstract class CricketLeague {    abstract void ... Read More

Traverse through a HashSet in Java

AmitDiwan
Updated on 23-Sep-2019 12:33:48

806 Views

HashSet extends AbstractSet and implements the Set interface. It creates a collection that uses a hash table for storage.A hash table stores information by using a mechanism called hashing. In hashing, the informational content of a key is used to determine a unique value, called its hash code.To traverse through a HashSet, you can use the Iterator in Java. At first, create a HashSet with string values −HashSet hashSet = new HashSet(); hashSet.add("Jack"); hashSet.add("Tom"); hashSet.add("David"); hashSet.add("John"); hashSet.add("Steve");Now, traverse using Iterator −Iterator iterator = hashSet.iterator(); while(iterator.hasNext()){    System.out.println(iterator.next()); }Let us see a simple example wherein we have some HashSet elements and we ... Read More

Java Signature getInstance() method with Examples

AmitDiwan
Updated on 23-Sep-2019 12:31:24

733 Views

A signature object that can implement the required signature algorithm can be obtained using the method getInstance() in the class java.security.Signature.Let us now see an example −Exampleimport java.security.*; import java.util.*; public class Main {    public static void main(String[] argv) {       try {          Signature signature = Signature.getInstance("SHA256withRSA");          String str = signature.toString();          System.out.println(str);       } catch (NoSuchAlgorithmException e) {          System.out.println("Error!!! NoSuchAlgorithmException");       }    } }OutputSignature object: SHA256withRSALet us now see another example −Exampleimport java.security.*; import java.util.*; public ... Read More

Java Signature getAlgorithm() method with Examples

AmitDiwan
Updated on 23-Sep-2019 12:29:24

237 Views

The name of the algorithm for the signature object can be obtained using the method getAlgorithm() in the class java.security.Signature.Let us now see an example −Exampleimport java.security.*; import java.util.*; public class Main {    public static void main(String[] argv) {       try {          Signature signature = Signature.getInstance("SHA256withRSA");          String algorithm = signature.getAlgorithm();          System.out.println("The Algorithm = " + algorithm);       } catch (NoSuchAlgorithmException e) {          System.out.println("Error!! NoSuchAlgorithmException");       }    } }OutputThe Algorithm = SHA256withRSALet us now see another example ... Read More

Log functions in Java

AmitDiwan
Updated on 23-Sep-2019 12:27:32

369 Views

The log functions in Java are part of java.lang.Math. The functions include log, log10, log1p. Let us see an example of each of these log functions −static double log(double a)The java.lang.Math.log(double a) returns the natural logarithm (base e) of a double value. Let us see an example −Exampleimport java.io.*; public class Main {    public static void main(String args[]) {       // get two double numbers       double x = 60984.1;       double y = -497.99;       // get the natural logarithm for x       System.out.println("Math.log(" + x + ")=" ... Read More

Trim a string in Java to remove leading and trailing spaces

AmitDiwan
Updated on 22-Oct-2023 02:12:16

22K+ Views

To remove leading and trailing spaces in Java, use the trim() method. This method returns a copy of this string with leading and trailing white space removed, or this string if it has no leading or trailing white space.Let’s say the following is our string with leading and trailing spaces −String str = new String(" Jack Sparrow ");Now, let us trim the string −str.trim()Following is an example to remove the leading and trailing spaces in Java −Exampleimport java.io.*; public class Main {    public static void main(String args[]) {       String str = new String(" Jack Sparrow "); ... Read More

Trigonometric Functions in Java

AmitDiwan
Updated on 23-Sep-2019 12:23:14

2K+ Views

The java.lang.Math class contains methods for trigonometric operations like cos(), sin(), tan(), tanh(), cosh(), atan(), etc.Let us work around some of these trigonometric functions in Java −static double asin(double a)The java.lang.Math.asin(double a) returns the arc sine of an angle, in the range of -pi/2 through pi/2.Let us now see an example −Exampleimport java.util.*; public class Main {    public static void main(String args[]) {       // get a variable x which is equal to PI/2       double x = Math.PI / 2;       // convert x to radians       x = Math.toRadians(x); ... Read More

C Program for Armstrong Numbers

Sunidhi Bansal
Updated on 23-Sep-2019 12:00:35

673 Views

We are given a task where we have to check a number n entered by the user, whether it’s Armstrong or not.An Armstrong number is when the sum of all the digits power by the number of digits or we can say order of digits n, is same as the digit.So below is a simple representation how to find the Armstrong number −Formula −wxyz…. = pow(w, n) +pow(x, n) + pow(y, n) + pow(z, n) + …..AlgorithmSTART Step 1-> Declare a function to find the value after power operation on the number    int power(int a, int b)     ... Read More

Pretty print JSON using Jackson library in Java?

Aishwarya Naglot
Updated on 12-May-2025 12:30:00

2K+ Views

A Jackson API is a Java-based library, and it can be useful to convert Java objects to JSON and JSON to Java objects. A Jackson API is faster than other API, needs less memory, and is good for large objects. We can process a JSON in three different ways: Streaming API, Tree Model, and Data Binding. We can pretty print JSON using the writerWithDefaultPrettyPrinter() of the ObjectMapper class, which is a factory method for constructing an ObjectWriter that will serialize objects using the default pretty printer for indentation. In this article, we will learn how to pretty print JSON using the Jackson ... Read More

Program for Area Of Square in C++

Sunidhi Bansal
Updated on 23-Sep-2019 11:53:37

2K+ Views

We are given with a side of a rectangle and our task is to print the area of the square from that side.Square is 2-D plain figure which have 4 sides and forms 4 angles of 90degree each and all the sides are of equal shape. In other words we can say that the square is a form of rectangle with equal sides.Given below is representation of a square −The Area of square is Side x SideExampleInput: 6 Output: 36 As the side is 6 so the output is 6*6=36 Input: 12 Output: 144AlgorithmSTART    Step 1-> Declare a function ... Read More

Advertisements