Posts

Showing posts from November, 2020

Lets Solve Camel Case Program In Java

 package demo; public class CamelCase { public static void main(String[] args) { String str = "saveChangesInTheEditor"; int count = 1; for (int i = 0; i < str.length(); i++) { int a = str.charAt(i); if (a >= 65 && a <= 90) { count += 1; } } System.out.println(count); } }

Check The Equality Between Two Arrays Using Java

 package demo; import java.util.HashMap; import java.util.Map; public class CheckArraysEquality { public static void main(String[] args) { int arr1[] = { 2, 1, 0, 7, 8, 9 }; int arr2[] = { 1, 0, 2, 9, 8, 7 }; System.out.println(isArraysEqual(arr1, arr2)); } private static boolean isArraysEqual(int[] arr1, int[] arr2) { Map<Integer, Integer> map = new HashMap<>(); for (int i : arr1) { if (map.containsKey(i)) { int count = map.get(i); count++; map.put(i, count); } else { map.put(i, 1); } } for (int j : arr2) { if (map.containsKey(j)) { map.remove(j); } else { return false; } } return true; } }

Don't Have A Music Player Lets Create One....

Image
For Music Player you need two libraries pygame and tkinter. You need to install both via python -m install 'library name' command. You can get all the libraries from here: https://pypi.org/project/pip/ Now lets dive into code:- Step 1 would be create a function for playing the song. Next to pause the song Then finally to resume the song And finally to stop the song So there is something called as ListBox which returns you a playlist env You just need to get the song in folder and via os iterate throught the folder Insert the song files to the playlist Now final step would be create different buttons for different functions And play around with the inbuilt methods in the mixer lib which comes from pygame Lets view the code now. from pygame import mixer from tkinter import * import os def play_song (): current_song = playlist.get(ACTIVE) print (current_song) mixer.music.load(current_song) song_status.set( "Playing" ) mixer.music.play() def pause_song...

Java Interview Questions : Series 10

Image
  200. What is CopyOnWriteArrayList? How it is different from ArrayList in Java? CopyOnWriteArrayList was introduced in Java 5 version. It is a thread-safe collection. It is similar to an ArrayList. In CopyOnWriteArrayList, all mutative operations (add, set etc.) are implemented by making a fresh copy of the underlying array. Iterator of CopyOnWriteArrayList is guaranteed to not throw ConcurrentModificationException. But Iterator also does not reflect any additions, removals that happened to list after the Iterator was created. All elements including null are permitted in CopyOnWriteArrayList. 201. How remove() method is implemented in a HashMap? Remove() method in HashMap uses logic similar to the one used in get() method. First we locate the correct bucket in HashMap for an entry. Then within that bucket we remove the element e. It is similar to removing a node from a single-linked list. If e is the first element in the bucket we set the corresponding element of Hash to e.next. E...

Java Interview Questions : Series 9

Image
 164. What are the differences between Comparable and Comparator? Main differences between Comparable and Comparator are: 1. Type: Comparable<T> is an interface in Java where T is the type of objects that this object may be compared to. 2. Comparator<T> is also an interface where T is the type of objects that may be compared by this comparator. 3. Sorting: In Comparable, we can only create one sort sequence. In Comparator we can create multiple sort sequences. 4. Method Used: Comparator<T> interface in Java has method public int compare (Object o1, Object o2) that returns a negative integer, zero, or a positive integer when the object o1 is less than, equal to, or greater than the object o2. A Comparable<T> interface has method public int compareTo(Object o) that returns a negative integer, zero, or a positive integer when this object is less than, equal to, or greater than the object o. 5. Objects for Comparison: The Comparator compares two objects given to...

UJ Snake Using Python

Image
Lets Dive Into The Code Directly:- Draw the screen area. Draw Snake And Food Give your favourite to all As you can see pygame lib is used for creating the design Random lib is for generating the area where the food would appear. If the snake touches the screen borders game ends there. If the snake touches himself game ends there. The Snake size would increase as he touches the food and the score gets updated by 1. The food disappears and appears in different location when snake touches the food. All Set!!!Play Now. Still doubtful you can get it from here:- https://github.com/joshiupendra1995/PythonWorld/blob/master/SnakeGame.py import pygame import random pygame.init() white = ( 255 , 255 , 255 ) yellow = ( 255 , 255 , 102 ) black = ( 0 , 0 , 0 ) red = ( 213 , 50 , 80 ) green = ( 0 , 255 , 0 ) blue = ( 50 , 153 , 213 ) dis_width = 600 dis_height = 400 dis = pygame.display.set_mode((dis_width , dis_height)) pygame.display.set_caption( 'UJ Snake' ) clock = pygame.time.Clock() sna...

3 Lines of Code And You Can Download Your Favourite Youtube Video.

Image
Lets Dive Into the code. Firstly you need to install pytube  using pip install pytube. You then need to import pytube library I have used sys library to take input from command line you dont need it Just replace your url with sys.argv[1] Please find the sample url https://www.youtube.com/watch?v=Jx5Vn4F0OBc&t=45s You then need to stream the first video from the object yt Finally use download method and dowload it to your desktop location Here I have given my desktop location,you can replace it with yours. Thats it walla!!! import pytube import sys # replace sys.argv[1] with your youtube link yt = pytube.YouTube(sys.argv[ 1 ]) stream = yt.streams.first() stream.download( "D:/" ) print ( "Video Downloaded" )

Java Interview Questions : Series 8

Image
  144. What is the difference between Collection and Collections Framework in Java? In Java, a Collection is an object that contains multiple elements of same type in a single unit. These multiple elements can be accessed through one Collection object. In Java Collections Framework is a library that provides common architecture for creating, updating and accessing different types of collections. In Collections framework there are common methods that are frequently used by developers for working on a Collection object. 145. What are the main benefits of Collections Framework in Java? Main benefits of Collections Framework in Java are as follows: 1. Reusability: Java Collections Framework provides common classes and utility methods than can be used with different types of collections. This promotes the reusability of the code. A developer does not have to re-invent the wheel by writing the same method again. 2. Quality: Using Java Collection Framework improves the program quality, si...