Category Archives: Uncategorized

Java program for a given number n, finds a number p which is greater than or equal to n and is a power of 2.

Java program for a given number n, finds a number p which is greater than or equal to n and is a power of 2.

Constraints

1 <= n <= 10000

Sample TestCase
Input
5
Output
8
import java.util.*;
import java.math.*;
public class NextPowerOfTwo {
public static int twoPowers(int num){
    while(num>0){
        if(Math.ceil(log(num,2))==Math.floor(log(num,2))){
            break;
           }
       num++;
      }
      return num;
    }
static double log(int x, int base){
    return (Math.log(x) / Math.log(base));
    }
public static void main(String[] args) throws IOException{
 Scanner in = new Scanner(System.in);
 int output = 0;
 int input = Integer.parseInt(in.nextLine().trim());
 output = twoPowers(input);
 System.out.println(String.valueOf(output));
 }
}

Java Program for Replacing White Space Characters
Creating an App Icon with the Asset Studio
Numbers in JavaScript

Advertisements

Java Program for Replacing White Space Characters

Given a sentence with multiple spaces between words and also spaces in front and back of sentence and you need to remove these extra spaces.

Input Format

You will be taking a string as an input from stdin.

Constraints

1 <= |S| <= 10^5

Output Format

You need to print the sentence by removing spaces in front and back of sentence.

Sample TestCase 1
Input
     This is Rua    Tech
Output
This is Rua Tech

 

 

import java.io.*;
import java.util.*;
public class WhiteSpaceRemove {
public static void main(String args[] ) throws Exception {

Scanner sc =new Scanner(System.in);
String str=sc.nextLine();
// str=str.trim();
String result=delSpaces(str);
System.out.println(result);

}
public static String delSpaces(String str){
StringBuilder sb = new StringBuilder(str);
ArrayList<Integer> spaceIndexes = new ArrayList<>();

for ( int i=1; i < sb.length(); i++ ){
if ( sb.charAt(i) == ‘ ‘ && sb.charAt(i-1) == ‘ ‘){
spaceIndexes.add(i);
}
}

for (int i = 0; i < spaceIndexes.size(); i++){
sb.deleteCharAt(spaceIndexes.get(i)-i);
}
return new String(sb.toString());
}
}


Understanding PHP Generator With Example

While and Do-While loop in Java with example

Add Audio To App In Android Studio

Adding audio clip to android application is a simple task as it also add some further functionality. Here is a step by step tutorial how to add audio file and how to play music when App will start.


Adding Audio to app in Android Studio:

Step 1: Open the android studio with the project in which you want to add-on audio clip/media file.
Step 2: Create a raw folder.
Step 3: Add media file to raw folder by simply copy and paste that to raw folder.
3
Step 4: Here we added a media file “color_black.mp3” . Now open the Java File of desired activity, here we are adding audio in MainActivity.
Step 5: Further add this code

MediaPlayer mediaPlayer= MediaPlayer.create(MainActivity.this,R.raw.color_black);
        mediaPlayer.start();

Step 6: Now run the App and your music will play when App will start.

Create/Add Raw Folder In Android Studio

Raw folder in Android is used to keep mp3 or ogg audio files and also video files. The raw folder is created inside \app\src\main\res\raw. So we will simply create it inside res folder. Here is the simple way to create raw folder in android studio:


How To Create/Add Raw Folder In Android Studio

Step 1: Open App folder and select res folder

Step 2: Right click on res folder, select New> Android resource directory, then studio will open a dialog box and it will ask you to enter the name.

1

Step 3: Write “raw” and selectResource type” then click OK. Open res folder and you will find your raw folder under it.

2

 

Next Step: How To Add & Play Audio File In Raw Folder

JAVA For Android Developer– Tutorial, Examples, And Programs

Learn Android Programming – For Beginners To Pro

Android is the most popular open source operating system for mobile devices like Smartphone and Tablet. This OS is based on Linux kernel which is currently developed by Google. At Google I/O 2014, it was revealed that more than one billion per month people actively uses Android. As of July 2015, Statista revealed Google play is the largest mobile App store which has over 1.6 million Apps and billions of application gets downloaded every year. Android programming is based on Java programming language so if you have basic understanding on Java programming then it will be a fun to learn Android application development.


Prerequisite For Android Programming:

Before you begin Android programming below are the prerequisite needed:

JAVA:

Android programming is mostly done in JAVA language. So before you begin Android programming you must be comfortable with JAVA and OOPS concepts. If you are new to Android we recommend you to check out our JAVA for Android developer section where we have shared in-depth tutorial on JAVA.

XML:

Once you are done with JAVA the next thing you need is basic knowledge of XML for designing UI of Android App. Even though Android Studio has drag and drop for designing UI but still you will need to learn XML if you want your UI is highly user interactive. You can check out Design Android User Interface section to learn it.


Android Programming Tutorials:

Below are the list of Android programming topics. Follow the link to read full tutorial:

Topics (Follow the link to read tutorial) Description
Activity Lifecycle Learn about Android Activity lifecycle like created, started, resumed, paused, stopped or destroyed.
Intent Learn how Intent is used for communicating between the components of an Application and from one app to another
Shared Preference Learn how primitive data is saved on Android device based on key-value pair
JSON Parsing JSON stands for JavaScript Object Notation. It is structured, light weight, human readable and easy to parse. It’s a best alternative to XML when our android app needs to interchange data from server.
Toast & Custom Toast In Android, Toast is used to display information for a period of time. It contains a message to be displayed quickly and disappears after specified period of time.
Internal Storage In this tutorial we are going to learn about internal storage of data/files in Android App using example or you can say the primary memory of your phone.
External Storage In this tutorial we gonna study about storage of data/files in android external storage or you can say the secondary memory/SD card of your phone.
Sqlite SQLite is a Structure query base database, open source, light weight, no network access and standalone database. Android has built in SQLite database implementation.
AsyncTask In Android, AsyncTask (Asynchronous Task) allows us to run the instruction in the background and then synchronize again with our main thread.
Splash Screen Splash Screen is most commonly the first startup screen which appears when App is opened.
Volley Volley is a HTTP library developed by Google and was first introduced during Google I/O 2013. This library is used to transmit data over the network.
Retrofit Retrofit is a simple network library that used for network transactions. By using this library we can seamlessly capture JSON response from web service/web API.

 Android Material Design Tutorials With Examples

Android UI, Layout & Material Design Tutorial, With Examples

Creating an Android Virtual Device (AVD) in Android Studio

Android Material Design Tutorials With Examples

Capture

Material Design is a design language created for Google Android’s new OS which was announced in Summer 2014.  Since then it has become popular in designing and developing Android Apps. As per Google this language is based on paper and ink.

Android provides the following elements for you to build material design apps:

  • A new theme
  • New widgets for complex views
  • New APIs for custom shadows and animations

Here our in-depth tutorial will teach how to design beautiful and user friendly Application using Android Material Design. All our tutorial will have practical example and step by step explanation of each topic.


Prequisites For Leaning Android Material Design:

JAVA For Android Developer:

JAVA is a programming language which is most commonly used in Android App Development. Before you start learning Material Design you will need to learn Object Oriented Java. Check out our JAVA for Android Developer section to learn it.

Android UI Basics:

You will also need to be familiar with Android UI basics.

Android Studio:

Android Studio is the official IDE (integrated development environment) for developing Android Apps by Google. It is available for free download on Windows, Mac OS X and Linux.

Check out our Android Studio guide to get started with it.


Android Material Design Tutorials:

Below are the list of Material design topics. Follow the link to read full tutorial:

 
Topics (Follow the link to read tutorial) Description
TextInputLayout / Floating Labels in EditText TextInputLayout is a new element introduced in Design Support library to display the floating label in EditText.
TabLayout TabLayout provides horizontal layout to display tabs on the screen. We can display more screens in a single screen using tabs.
NavigationDrawer Navigation drawer is a side menu that helps us to organise the navigation inside our app. It is a uniform way to access different pages and information inside our app.
PercentRelativeLayout PercentRelativeLayout in Android is a subclass of RelativeLayout that supports percentage based margin and dimensions for Views(Button, TextView or any other view).
Toolbar In Android Toolbar is similar to an ActionBar(now called as App Bars). It is a Viewgroup that can be placed at anywhere in the Layout. We can easily replace an ActionBar with Toolbar.
PercentFrameLayout In Android PercentFrameLayout is a subclass of FrameLayout that supports percentage based margin and dimensions for Views(Button, TextView or any other view).
RecyclerView As ListView The RecyclerView is more flexible, powerful and a major enhancement over ListView.
RecyclerView As GridView The RecyclerView is more flexible, powerful and a major enhancement over GridView.
RecyclerView As StaggeredGrid It is used to show the items in staggered Grid (varying size)
CardView In Android, CardView is another main element that can represent the information in a card manner with a drop shadow called elevation and corner radius which looks consistent across the platform.
Pull To Refresh / SwipeRefreshLayout In Android App Pull To Refresh aka SwipeRefreshLayout is used whenever we need to refresh the content’s of a view via a vertical swipe gesture.
ViewPager ViewPager in Android is a class that allows the user to flip left and right through pages of data. This class provides the functionality to flip pages in app.
Animation The Animations Framework allows us to create visually attractive animations and transitions in our apps.

Creating an Android Virtual Device (AVD) in Android Studio

An Android Virtual Device (AVD) is a configuration that defines the characteristics of an Android phone, tablet, Android Wear, or Android TV device that you want to simulate in the Android Emulator. Before debugging and testing android application, you should create an android virtual device ( AVD ) to start a simulator. You can specify special configurations for your avd, and then start it for debugging and executing applications.

To create a new AVD, do one of the following:

  • Select Tools > Android > AVD Manager.
  • Click AVD Manager AVD Manager icon in the toolbar.

Both will Open the AVD Manager shown in figure below:

1

OR

2

Android Virtual Device Manager will be opened. After that Click on Create Virtual Device.

3

Now choose the Category, phone size and choose the pixels according to your requirement. After this click on Next button.

4

After that choose the SDK Version and Click on Next button. If you have various SDK Versions like Kitkat, Lolipop ,Marshmallow,Nougat and Oreo etc in your SDK then you can select one of them. Here we choose Nougat SDK Version. Click on the next.

5

After that Enter the AVD Name in Android Virtual Device and Click on Finish button. Here you can do customization to AVD which you are creating as per your requirement.

Click Finish and new AVD is created. To start it, click the green triangle button in AVD list Actions column. To edit it’s settings, click the green pencil button.

6

AVD will start in Emulator. Now you can run your App in this AVD.

emulator2

I hope you find this blog post very helpful while creating AVD in Android Studio. Let me know in a comment if you have any questions regarding Android Studio. I will reply you ASAP.

 

 

 

 

Add a new activity to an existing project in Android Studio

Step 1: Firstly, right-click the app folder and select New > Activity  and choose your Activity as per requirement. Here I choose Login Activity as shown in figure below.

1

Step 2: After that Customize the Activity in Android Studio. Enter the “Activity Name”, “Layout Name”, “Title” ,”Hierarchical Parent”, “Package name”,”Source Language” and “Target Source Set ” in the Text box and Click on Finish button.

Note: Default configuration works fine.

All steps are shown in figure in below:

2

To configure Hierarchical parent click on right three doted  button. Here I choose MainActivity but you can choose as your requirement.

3

If you want to write program in kotlin click Source Language and set to kotlin. Default source language  is Java in android Studio 3.

4

5

To set Target Source Set click on target source set It will show three option.By default it set to main in android studio 3.

6

Step 3: After that your new Activity in Layout will be created. Your XML Code is in Text and your Design Output is in Design. See below figure.

7

 

Close, Save and Open Recent project in Android Studio

Open Project in Android Studio

PHP7 Arithmetic Operators

PHP Arithmetic Operators

The PHP arithmetic operators are used with numeric values to perform common
arithmetical operations, such as addition, subtraction, multiplication etc.
+   Addition     $x + $y     Sum of $x and $y
–   Subtraction     $x – $y     Difference of $x and $y
*   Multiplication  $x * $y     Product of $x and $y
/   Division     $x / $y     Quotient of $x and $y
%   Modulus      $x % $y     Remainder of $x divided by $y
**  Exponentiation  $x ** $y    Result of raising $x to the $y’th power

PHP Increment / Decrement Operators

The PHP increment operators are used to increment a variable’s value.
The PHP decrement operators are used to decrement a variable’s value.
++$x    Pre-increment   Increments $x by one, then returns $x
$x++    Post-increment  Returns $x, then increments $x by one
–$x    Pre-decrement   Decrements $x by one, then returns $x
$x–    Post-decrement  Returns $x, then decrements $x by one
<?php

$x=20;

$y=15;

//addition

$add=$x+$y;

echo $add.'
'; //subtraction $sub=$x-$y; echo $sub.'
'; //multiplication $mult=$x*$y; echo $mult.'
'; //division $div=$x/$y; echo $div.'
'; //modulus $mod=$x%$y; echo $mod.'
'; //Exponentiation $exp=$x**$y; echo $exp.'
'; $num=20; $num2=10; //Pre-increment echo ++$num.'
'; //post-increment echo $num++.'
'; echo $num.'
'; //Pre-decrement echo --$num2.'
'; //Post-decrement echo $num2--.'
'; echo $num2.'
'; ?>

Download File

PHP 7 Comparison Operator

PHP Comparison Operators
The PHP comparison operators are used to compare two values (number or string):
==       Equal    $num1 == $num2    Returns true if $num1 is equal to $num2
===    Identical    $num1 === $num2   Returns true if $num1 is equal to $num2, and they are of the same type
!=       Not equal    $num1 != $num2    Returns true if $num1 is not equal to $num2
<>       Not equal    $num1 <> $num2    Returns true if $num1 is not equal to $num2
!==     Not identical    $num1 !== $num2   Returns true if $num1 is not equal to $num2, or they are not of the    same type
>         Greater than     $num1 > $num2     Returns true if $num1 is greater than $num2
<        Less than    $num1 < $num2     Returns true if $num1 is less than $num2
>=     Greater than or equal to $num1 >= $num2   Returns true if $num2 is greater than or equal to $num2
<=     Less than or equal to    $num1<= $num2    Returns true if $num2 is less than or equal to $num2
<?php

$num1=10;

$num2=7;

$numStr='10';

//always true

if(true){

echo'I am always true'.'<br>';

}

//always false

if(false){

echo'I am true'.'<br>';

}else{

echo'I am always false'.'<br>';

}

//return true if $num1 is equal to $numStr

if($num1==$numStr){

echo'we are same'.'<br>';

}

//return true if $num1 is identical to $numStr and same type

if($num1===$numStr){

echo'we are identical'.'<br>';

}else{

echo 'We are not identical'.'<br>';

}

//return true if $num1 is not equal to $num2

if($num1!=$num2){

echo'we are not same'.'<br>';

}

// same as !=

if($num1<>$num2){

echo'we are not same'.'<br>';

}

if($num1!==$numStr){

echo'we are not identical'.'<br>';

}

//return true if $num1 is greater than $num2

if($num1>$num2){

echo$num1.' is greater than '.$num2.'<br>';

}else{

echo$num2.' is greater than '.$num1.'<br>';

}

//return true if $num1 is less than $num2

if($num1<$num2){

echo$num1.' is less than '.$num2.'<br>';

}else{

echo$num2.' is less than '.$num1.'<br>';

}

//return true if $num1 is greater than or equal to $num2

if($num1>=$num2){

echo$num1.' is greater than or equal to'.$num2.'<br>';

}else{

echo$num2.' is greater than or equal to'.$num1.'<br>';

}

//return true if $num1 is less than or equal to $num2

if($num1<$num2){

echo$num1.' is less than or equal to '.$num2.'<br>';

}else{

echo$num2.' is less than or equal to '.$num1.'<br>';

}

?>

Download file