Skip to main content

Posts

Showing posts from March, 2020

WRITE A PROGRAM FOR BUBBLE SORT IN JAVA

public class MyBubbleSort { // logic to sort the elements public static void bubble_srt(int array[]) { int n = array.length; int k; for (int m = n; m >= 0; m--) { for (int i = 0; i < n - 1; i++) { k = i + 1; if (array[i] > array[k]) { swapNumbers(i, k, array); } } printNumbers(array); } } private static void swapNumbers(int i, int j, int[] array) { int temp; temp = array[i]; array[i] = array[j]; array[j] = temp; } private static void printNumbers(int[] input) { for (int i = 0; i < input.length; i++) { System.out.print(input[i] + ", "); } System.out.println("\n"); } public static void main(String[] args) { int[] input = { 4, 2, 9, 6, 23, 12,34, 0, 1 }; bubble_srt(input); } }

PROGRAM TO PUT HTML LINKS AROUND URLS STRINGS in Java

import java.util.*; import java.io.*; public class URLify { public static char[] URLify(char[] chars, int len) { int spaces = countSpaces(chars, len); int end = len - 1 + spaces *2; for (int i = len - 1; i >= 0; i-) {if (chars[i] == ' ') { chars[end - 2] ='%'; chars[end - 1] ='2'; chars[end] = '0'; end -= 3; }else { chars[end] =chars[i]; end--; } }return chars; } static int countSpaces(char[] chars, int len) {int count = 0; for (int i = 0; i < len;i++) if (chars[i] == ' ') count++; return count; } public static void main(String[] args) throws IOException { char[] chars = "Mr John Smith".toCharArray(); System.out.println(URLify(chars, 13)); } }

PROGRAM TO CHECK UNIQUE NUMBER IN JAVA

Photo by  ThisIsEngineering  from  Pexels import java.util.*; import java.io.*; public class IsUnique { public static boolean isUniqueUsingHash(String word) {char[] chars = word.toCharArray(); Set<Character> set = new HashSet<Character>(); for (char c : chars) if (set.contains(c)) return false; else set.add(c); return true; } public static boolean isUniqueUsingSort(String word) {char[] chars = word.toCharArray(); if (chars.length <= 1) return true; Arrays.sort(chars); char temp = chars[0]; for (int i = 1; i < chars.length; i++) {if (chars[i] == temp) return false; temp = chars[i]; } return true; } public static void main(String[] args) throws IOException { System.out.println(isUniqueUsingHash("? "Unique" : "Not Unique"); System.out.println(isUniqueUsingSort("? "Unique" : "Not Unique"); } }

What is the difference between a Set and a Map in Java?

Main differences between a Set and a Map in Java are: Duplicate Elements: A Set does not allow inserting duplicate elements. A Map does not allow using duplicate keys, but it allows inserting duplicate values for unique keys. Null values: A Set allows inserting maximum one null value. In a Map we can have single null key at most and any number of null values. Ordering: A Set does not maintain any order of elements. Some of sub-classes of a Set can sort the elements in an order like LinkedHashSet. A Map does not maintain any order of its elements. Some of its sub-classes like TreeMap store elements of the map in ascending order of keys.

What are the main uses of Command design pattern in JAVA?

Command design pattern is a behavioral design pattern. We use it to encapsulate all the information required to trigger an event. Some of the main uses of Command pattern are: 1. Graphic User Interface (GUI): In GUI and menu items, we use command pattern. By clicking a button we can read the current information of GUI and take an action. 2. Macro Recording: If each of user action is implemented as a separate Command, we can record all the user actions in a Macro as a series of Commands. We can use this series to implement the “Playback” feature. In this way, Macro can keep on doing same set of actions with each replay. 3. Multi-step Undo: When each step is recorded as a Command, we can use it to implement Undo feature in which each step can by undo. It is used in text editors like MS-Word. 4. Networking: We can also send a complete Command over the network to a remote machine where all the actions encapsulated within a Command are executed. 5. Progress Bar: We can implement an i...

What is the difference between an ArrayList and a LinkedList data structure?

Main differences between ArrayList and LinkedList data structures  are: Data Structure: An ArrayList is an indexed based dynamic array. A LinkedList is a Doubly Linked List data structure. Insertion: It is easier to insert new elements in a LinkedList, since there is no need to resize an array. Insertion in ArrayList is O(n), since it may require resizing of array and copying its contents to new array. Remove elements: LinkedList has better performance in removal of elements than ArrayList. Memory Usage: LinkedList uses more memory than ArrayList, since it has to maintain links for next and previous nodes as well. Access: LinkedList is slower in accessing an element, since we have to traverse the list one by one to access the right location.