Found 34488 Articles for Programming

Basic calculator program using Java

karthikeya Boyini
Updated on 25-Jun-2020 11:21:45

17K+ Views

A basic calculator is able to add, subtract, multiply or divide two numbers. This is done using a switch case. A program that demonstrates this is given as follows −Exampleimport java.util.Scanner; public class Calculator {    public static void main(String[] args) {       double num1;       double num2;       double ans;       char op;       Scanner reader = new Scanner(System.in);       System.out.print("Enter two numbers: ");       num1 = reader.nextDouble();       num2 = reader.nextDouble();       System.out.print("Enter an operator (+, -, *, /): "); ... Read More

Java program to find common elements in three sorted arrays

Samual Sam
Updated on 25-Jun-2020 07:16:12

958 Views

The common elements in three sorted arrays are the elements that occur in all three of them. An example of this is given as follows −Array1 = 1 3 5 7 9 Array2 = 2 3 6 7 9 Array3 = 1 2 3 4 5 6 7 8 9 Common elements = 3 7 9A program that demonstrates this is given as follows −Examplepublic class Example { public static void main(String args[]) { int arr1[] = {1, 4, 25, 55, 78, 99}; int arr2[] = {2, 3, 4, 34, 55, 68, 75, 78, 100}; int arr3[] = {4, 55, ... Read More

Android AsyncTask example and explanation

Chandu yadav
Updated on 25-Jun-2020 09:50:51

12K+ Views

Android AsyncTask going to do background operation on background thread and update on main thread. In android we cant directly touch background thread to main thread in android development. asynctask help us to make communication between background thread to main thread.Methods of AsyncTaskonPreExecute() − Before doing background operation we should show something on screen like progressbar or any animation to user. we can directly comminicate background operation using on doInBackground() but for the best practice, we should call all asyncTask methods .doInBackground(Params) − In this method we have to do background operation on background thread. Operations in this method should ... Read More

What is difference between gravity and layout_gravity on Android?

Ankith Reddy
Updated on 26-Jun-2020 05:48:05

1K+ Views

Android supports both gravity and layout_gravity. Gravity adjusts view position. Using gravity we can do alignment of view as shown below.In the above code Textview going to set in middle of parent layout.Properties of GravityCenter − it going to put view in center of parent layout.Right − it going to put view in right of parent layout.Left − it going to put view in left of parent layout.End − it going to put view in end position of parent layout.Start − it going to put view in start position of parent layout.Top − It going to put view in Top ... Read More

Java program to count occurrences of a word in string

Samual Sam
Updated on 31-May-2024 16:34:38

11K+ Views

The number of times a word occurs in a string denotes its occurrence count. An example of this is given as follows −String = An apple is red in colour. Word = red The word red occurs 1 time in the above string.A program that demonstrates this is given as follows.Example Live Demopublic class Example { public static void main(String args[]) { String string = "Spring is beautiful but so is winter"; String word = "is"; String temp[] = string.split(" "); int count = 0; for (int i = 0; i < temp.length; i++) { if (word.equals(temp[i])) count++; } System.out.println("The string ... Read More

How to get the dimensions of a view in Android?

Arjun Thakur
Updated on 25-Jun-2020 09:55:03

5K+ Views

There are so many cases, we should create dynamic view rather than creating view in XML. In that scenario, we should need to get the dimensions of a view. So here is a simple solution to get the dimensions of a view in android.To get the height of any view use the following codeint width = view.getMeasuredHeight();To get the width of any view use the following codeint height = view.getMeasuredWidth();Before get the width and height, we should assign default measure for a view as shown belowview.measure(0, 0);In the above code view is like either textview ,editText, button ..etc. Here is ... Read More

Java program to extract ‘k’ bits from a given position

karthikeya Boyini
Updated on 25-Jun-2020 10:04:23

402 Views

Extraction of k bits from the given position in a number involves converting the number into its binary representation. An example of this is given as follows −Number = 20 Binary representation = 10100 k = 3 Position = 2 The bits extracted are 010 which represent 2.A program that demonstrates this is given as follows.Example Live Demopublic class Example {    public static void main (String[] args) {       int number = 20, k = 3, pos = 2;       int exNum = ((1 > (pos - 1));       System.out.println("Extract " + k + ... Read More

How to display HTML in TextView in Android?

George John
Updated on 26-Jun-2020 05:51:31

6K+ Views

In Some situations, we should need to show HTML as text in android. Here is the simple solution to show HTML in TextView in android.Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.Step 2 − Add the following code to res/layout/activity_main.xml.     Step 3 − Add the following code to src/MainActivity.javapackage com.example.andy.myapplication; import android.os.Bundle; import android.support.v4.text.HtmlCompat; import android.support.v7.app.AppCompatActivity; import android.widget.TextView; public class MainActivity extends AppCompatActivity {    String htmlText = "What is Android?" + "Android is an open source and Linux-based ... Read More

Java program to check if two given matrices are identical

Samual Sam
Updated on 26-Jun-2020 05:52:55

1K+ Views

Two matrices are identical if their number of rows and columns are equal and the corresponding elements are also equal. An example of this is given as follows.Matrix A = 1 2 3 4 5 6 7 8 9 Matrix B = 1 2 3 4 5 6 7 8 9 The matrices A and B are identicalA program that checks if two matrices are identical is given as follows.Example Live Demopublic class Example {    public static void main (String[] args) {       int A[][] = { {7, 9, 2}, {3, 8, 6}, {1, 4, 2} };   ... Read More

How to close or hide the virtual keyboard on Android?

Chandu yadav
Updated on 25-Jun-2020 10:17:36

4K+ Views

In Android there are some situations, we should close android default keyboard forcefully. For that this example is help for you.Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.Step 2 − Add the following code to res/layout/activity_main.xml.             Step 3Add the following code to src/MainActivity.javaimport android.app.ProgressDialog; import android.os.Build; import android.os.Bundle; import android.os.Handler; import android.support.annotation.RequiresApi; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.view.inputmethod.InputMethodManager; import android.widget.Button; import android.widget.EditText; import android.widget.ProgressBar; public class MainActivity extends AppCompatActivity implements View.OnClickListener {    Handler mHandler;   ... Read More

Advertisements