Java program to count the occurrence of each character in a string using Hashmap

AmitDiwan
Updated on 21-Jun-2024 12:41:28

9K+ Views

To count the occurrence of each character in a string using Hashmap, the Java code is as follows −Example Live Demoimport java.io.*; import java.util.*; public class Demo{    static void count_characters(String input_str){       HashMap my_map = new HashMap();       char[] str_array = input_str.toCharArray();       for (char c : str_array){          if (my_map.containsKey(c)){             my_map.put(c, my_map.get(c) + 1);          }else{             my_map.put(c, 1);          }       }       for (Map.Entry entry : my_map.entrySet()){ ... Read More

Java Program to Implement Multiple Inheritance

AmitDiwan
Updated on 21-Jun-2024 12:33:10

14K+ Views

In this article, we will understand how to implement multiple inheritance. Java does not support multiple inheritance. This means that a class cannot extend more than one class, but we can still achieve the result using the keyword 'extends'.Algorithm to Implement Multiple InheritanceStep 1 – START Step 2 – Declare three classes namely Server, connection and my_test Step 3 – Relate the classes with each other using 'extends' keyword Step-4 – Call the objects of each class from a main function. Step 5 – STOPExample 1class Server{    void my_frontend(){       System.out.println("Connection to frontend established successfully");}    } ... Read More

JavaScript program to find Area and Perimeter of Rectangle

AmitDiwan
Updated on 21-Jun-2024 12:19:35

5K+ Views

We are writing a JavaScript program to calculate the area and perimeter of a rectangle. The program will prompt the user to input the width and length of the rectangle, and then we will use these values to calculate the area and perimeter. We will be continuously using these formulas: area = width * length, and perimeter = 2 * (width + length) to find the desired measurements. Approach The approach to find the Area and Perimeter of a rectangle in JavaScript can be done as follows − Define the length and width of the rectangle using variables. Calculate ... Read More

Java public static void main(String[] args)

Shriansh Kumar
Updated on 21-Jun-2024 11:45:02

5K+ Views

The java programs start execution when JVM calls the main() method. Java application begin from this method. Without main method, a java file will compile successfully because at compile time, compiler doesn’t check for main method but at run time JVM checks whether the main() method is available or not. Therefore, we will get an exception at run time. In this article, we will understand why we follow the convention “public static void main(String[] args).” Syntax public class class_name { // This line must be written as it is public static void main(String[] args) ... Read More

Java program to generate a calculator using the switch case

Chandu yadav
Updated on 21-Jun-2024 11:32:02

9K+ Views

The following program accepts two integer variables, takes an operator regarding the operation. According to the selected operator, the program performs the respective operation and print the result.Exampleimport java.util.Scanner; public class ab39_CalculatorUsingSwitch {    public static void main(String args[]) {       Scanner sc = new Scanner(System.in);       System.out.println("Enter value of 1st number ::");       int a = sc.nextInt();       System.out.println("Enter value of 2nd number ::");       int b = sc.nextInt();       System.out.println("Select operation");       System.out.println("Addition-a: Subtraction-s: Multiplication-m: Division-d: ");       char ch ... Read More

Java Program to locate a character in a string

Samual Sam
Updated on 21-Jun-2024 11:27:10

13K+ Views

To locate a character in a string, use the indexOf() method.Let’s say the following is our string.String str = "testdemo";Find a character ‘d’ in a string and get the index.int index = str.indexOf( 'd');Example Live Demopublic class Demo {    public static void main(String []args) {       String str = "testdemo";       System.out.println("String: "+str);       int index = str.indexOf( 'd' );       System.out.printf("'d' is at index %d, index);    } }OutputString: testdemo 'd' is at index 4Let us see another example. The method returns -1, if the character isn’t found −Example Live Demopublic class ... Read More

Java program to find the 2nd smallest number in an array

Ankith Reddy
Updated on 21-Jun-2024 11:21:08

9K+ Views

To find the 2nd smallest element of the given array, first of all, sort the array.Sorting an arrayCompare the first two elements of the arrayIf the first element is greater than the second swap them.Then, compare 2nd and 3rd elements if the second element is greater than the 3rd swap them.Repeat this till the end of the array.After sorting an array print the 2nd element of the array.ExampleLive Demopublic class SmallestNumberInAnArray {    public static void main(String args[]){       int temp, size;       int array[] = {10, 20, 25, 63, 96, 57};       size = array.length;       for(int i = 0; i

Java program to convert a Set to an array

Lakshmi Srinivas
Updated on 21-Jun-2024 11:15:54

13K+ Views

The Set object provides a method known as toArray(). This method accepts an empty array as argument, converts the current Set to an array and places in the given array. To convert a Set object to an array − Create a Set object. Add elements to it. Create an empty array with size of the created Set. Convert the Set to an array using the toArray() method, bypassing the above-created array as an argument to it. Print the contents of the array.ExampleLive Demoimport java.util.HashSet; import java.util.Set; public class SetToArray {    public static void main(String args[]){       Set set = new HashSet();   ... Read More

Explain the evaluation of expressions of stacks in C language

Bhanu Priya
Updated on 20-Jun-2024 22:17:25

18K+ Views

Stack is a linear data structure, where data is inserted and removed only at one end.AlgorithmsGiven below is an algorithm for Push ( ) −Check for stack overflow.if (top = = n-1) printf("stack over flow");Otherwise, insert an element into the stack.top ++ a[top] = itemGiven below is an algorithm for Pop ( ) −Check for stack underflow.if ( top = = -1) printf( "stack under flow");Otherwise, delete an element from the stack.item = a[top] top --Given below is an algorithm for Display ( ) −if (top == -1) printf ("stack is empty");Otherwise, follow the below mentioned algorithm.for (i=0; i='0' && ch

Explain insertion of elements in linked list using C language

Bhanu Priya
Updated on 20-Jun-2024 22:12:24

3K+ Views

Linked lists use dynamic memory allocation i.e. they grow and shrink accordingly. They are defined as a collection of nodes. Here, nodes have two parts, which are data and link. The representation of data, link and linked lists is given below −Operations on linked listsThere are three types of operations on linked lists in C language, which are as follows −InsertionDeletionTraversingInsertionConsider an example, wherein we insert node 5 in between node 2 and node 3.Now, insert node 5 at the beginning.Insert node 5 at the end.Insert node 5 at the end.Note:We cannot insert node 5 before node 2 as the ... Read More

Advertisements