Posts

Showing posts from January, 2025

Java Programs for freshers: Find second largest number in the array

               In this series of programming, let us create a java program to find the largest number in the array. Logic: Get the number of elements in the array. Read the array elements from the user. Find the largest value and the minimum value in the array. Create a for loop for checking all elements. Now, check the element is greater than largest value, then assign the element as largest value. Else, if the element is greater than second largest and not equal to largest, assign it as second largest. If the second largest is equal to minimum value, then print there is no second largest number. Otherwise, print the second largest number. Program implementation: Include the built in package. Create a public class with main() function. Using scanner object,read the number of elements and elements from the user at run time. Use the above logic to implement the program. Program: import java.util.Scanner; public cla...

Java Program for freshers: To reverse a String

               String is a collection of characters. String operations includes concatenation, counting the number of characters, deletion of a character and so on. let us create a reversal of string program as follows. Logic: ·        Read the input from the user. ·        Create an empty string. ·        Using a for loop, repeat the process. ·        Assign the ‘i’ value to string length-1. Until i>0 , split the character at location ‘i’. ·        Add to the empty string. ·        Finally, print the value. Program implementation: ·        Import the built in module java.util.Scanner. ·        Create a public class with main() function. ·        An object is cr...

How to implement an Address book in java?

       Address book contains a set of contact details.It can be a student details, customer details and so on. Let us create it in java. Logic: This Application has four operations as follows. ·        Add Contact ·        Remove Contact ·        View Contacts ·        Exit To add the contact details, user has to enter the person name, phone number and email id. To remove the contact details, user need to give the index value. View contacts used to display the details. Exit is used to exit the application. Steps: Three classes: ContactData, AddressBookEg, AddressBookAppEg ContactData class is used to declare the variables, get the values and set the values. AddressBookEg class is used to define the functions addContact(),getContacts() and removeContact(). AddressBookAppEg is the main class. It contains the main() function. ...

Simple ChatBot Application in java

       ChatBot is a simple AI application. Whenever user asks a question, it gives you the response. This chatbot is developed using java language. How to create a simple Chatbot application in java? First, get the input from the user. Check the input with the corresponding content and display the output. Steps to follow: ·        Include the built-in package. ·        Create a public class with a main() function. ·        Read the input from the user. ·        Call the function to get the response. ·        Convert the user input into lowercase. ·        Check the input with the keywords. ·        If the keyword is found, it gives the output. ·        Finally,when you give bye,the chatbot ends the process. ...

Random java Programs: Calculator Application

Let us create a simple Calculator project in java. It has four options. Addition, Subtraction, Multiplication and Division. Steps: Get the input from user for the type of arithmetic operation as integer(1,2,3 or 4). Get the two inputs from the user. Using switch case, choose the case according to the user input. Process the statement and print the output. Program:    import java.util.Scanner;     public class CalculatorApp {        public static void main(String[] args) {            Scanner scanner1 = new Scanner(System.in);            System.out.println("Simple Calculator Application");            System.out.println("Choose an operation:");            System.out.println("1. Addition");     ...

Java Program to find Longest Substring Without Repeating Characters

              It is a typical problem in Strings. As the name indicates this concept reads a substring that should not contain any repeated characters. To solve this problem, let us use a “Sliding Window Approach”. This approach deals with a window slide around the data. Logic: ·        A window is maintained. ·        It has two pointers to point the start and end of the window. ·        When the window slides, pointers may expand or contract. Steps: ·        Import the built in packages import java.util.HashMap and import java.util.Scanner. ·        Create a public class with a constructor and main() function. ·        Inside the constructor, initialize the pointers for start and longest. ·        At first, assign the values as ze...

Meeting Maximum Guests Problem – Java implementation

     Imagine a party. Many guests are coming. You want to calculate the maximum number of guests in the party hall at a particular time given to you. How will you do this???? First, Get the inputs. The inputs are two array of integers given below. ‘arrive[]’ – This represents the guests who enters the party hall at that time. ‘depart[]’ – This represents the guests who leaves the party hall at that moment. Note: Always check both array’s length should be same in size. Arrival entry should be entered in ‘arrive[]’ and Departure entry should be noted in ‘depart[]’. Algorithm: Get the inputs ‘arrive[]’ and ‘depart[]’; Sort the arrays. Let us declare two pointers ‘i’ and ‘j’. ‘i’ points arrive[] and ‘j’ points depart[]. Call the function finditMaxGuests() This function check the condition to proceed.     If arrive[i]<=depart[j] then, a guest came into the party hall. Increase the count of arrive[].   Otherwise, decrease the count of depart ...

Different ways of Sorting using built in functions-Java Programming

       Sorting is a process of arranging elements in particular order. It may be ascending and descending order. Here, two examples are given to sort the elements using built in functions. ·        Arrays.sort() ·        Custom comparators Java Program to sort integers using ‘Arrays.sort()’:               This program reads the integer array from user. It process it using ‘Arrays.sort()’ function. Finally, it prints the sorted integers. Program:    import java.util.Arrays;    import java.util.Collections;    import java.util.Scanner;    public class SortEg1 {        public static void main(String[] args) {           Scanner scanner = new Scanner(System.in);        ...

Java Program to create a List and perform functions using Collections

     A set of elements grouped together under a common name is called a collection. To implement this, a package “java.util” is used. This package has many static methods and operates the collections. Collections has some set of operations like reverse, search and sorting. Let us list the some of methods in the ‘Collections’ class. Java Program to create a List and perform functions using Collections: This program starts by importing the header files and has following steps. Steps: A public class is created with main() function. A list object list1 is created as integer ArrayList. To insert the values, use add() function. Print the newly created list. Now, sort the elements by sort() function using Collections. Print the sorted list. To search an element using binary search, a function binarySearch() is used. It display the key position as output. Next,the list is reversed using reverse() function. The list is printed after the reversal. To shuffle the data at r...

Java Program to implement ‘Arrays’ and its member functions.

       Array has collection of elements. ‘Arrays’ is a built in class in java package java.util. This class contains many static methods to process array operations. Some of the methods are listed below. sort() :This function sorts the array elements. parrelSort() : Suits for larger array to sort the elements parallel. equals():It compares two arrays for equality. binarySearch(): It uses binary Search method to find an element. fill() :it fills a data for set of elements. toString(): It converts the array to String asList():It converts the array to List. Stream():it is used for Stream operations. copyOf(): It copys the array elements into another array. copyOfRange(): it copys the data in the specified range and add to another array. setAll(): All the elements are set by this method. An example is given below. #Java Program to implement ‘Arrays’ and its member functions. import java.util.Arrays; import java.util.Scanner; public class...