Monday, 29 March 2021

Constructors Part 4 | Static Constructors vs Non Static Constructor |

Static Constructors Vs Non-Static Constructors :

If a constructor is explicitly declared by using a static modifier we call that constructor as static constructor whereas rest of other are non-static constructor only.

Constructors are responsible for initializing fields/variables of a class, static fields are initialized by static constructors and non-static fields are initialized by non-static constructors.

Static constructors are implicitly called whereas non-static constructors must be explicitly called.

Static constructors executes immediately once the execution of a class starts and more over it's the first block of code to run under a class whereas non-static constructors executes only after creating the instance of class as well as each and every time the instance of class is created.

In the life cycle of a class static constructors executes one and only one time whereas non-static constructors executes for zero times if no instances are created and "n" times if "n" instances are created.

 Non-static constructors can be parameterized but static constructors can't have any parameters because  static constructors are implicitly called and more over it's the first block of code to run under class.

Non-static constructors can be overloaded where as static constructors can't be overloaded.

 // C# Program to demonstrate
// how to declare the static
// constructor and non-static
// constructor
using System;
 
class ABC{
   
// Static variable
static int s;
 
// Non-static variable
int ns;
 
// Declaration of
// static constructor
static ABC()
{
    Console.WriteLine("It is static constructor");
}
 
// Declaration of
// non-static constructor
public ABC()
{
    Console.WriteLine("It is non-static constructor");
}
 
// Main Method
static void Main(string[] args)
{
 
    // Static constructor will call implicitly
    // as soon as the class start to execute
    // the first block of code to execute
    // will be static constructor
 
    // Calling non-static constructor
    ABC obj1 = new ABC();
}
}

Output :

It is static constructor
It is non-static constructor

Every class contains an implicit constructor if not defined explicitly and those implicit constructors are defined based on the following criteria :

⟶ Every class except a static class contains an implicit non-static constructor if not defined with an explicit constructors .

⟶ Static constructors are implicitly defined only if that class contains any static fields or else that constructor will not be present at all.

Thursday, 25 March 2021

Constructors in C#.NET Part 3 | Why Constructors are Needed in our class | Clcoding

Every class requires a constructor to be present init if we want to create the instance of that class.

Every class contains an implicit constructor if not defined explicitly and with the help of that implicit constructor instance of class can be created.

What is the need of defining a constructor explicitly again ?

Implicit constructor of a class will initialize variables of a class with the same value even if we create multiple instance of that class.


If we define constructor explicitly with parameters then we get a change of initializing the fields or variables of the class with a new value every time we are going to create instance of that class.

When ever we define a class identify whether if the class variables requires some values to execute and if they are required then define a constructor explicitly and pass values thru that constructor , so that every time the instance of the class is created we get a chance of passing new values.

Note : Generally every class requires some values for execution and the values that is required for a class to execute are always sent to that class by using the constructor only.


Tuesday, 23 March 2021

Types of Constructors in C#.NET Part 2 | C#.NET Tutorial | Clcoding

Type of Constructors 

1. Default or Parameter Less Constructor

2. Parameterized Constructor

3. Copy Constructor

4. Static Constructor

Default or Parameter Less Constructor : 

 If a constructor method doesn't take any parameters then we call that as default or parameter less. these constructors can be defined by a programmer explicitly or else will be default implicitly provided there is no explicit constructor under the class. 

class Test

{

public Test ()     //Implicit Constructor

    {

  }

}


Parameterized Constructor :

If a constructor method is defined with out any parameters we call that as parameterized constructor and these constructor can be defined by the programmers only but never can be defined implicitly.


Copy Constructor :

If we want to create multiple instances with the same values then we use these copy constructors, in a copy constructors the  constructors takes the same class as a parameters to it.

Static Constructor :

If a constructor is explicitly declared by using static modifier we call that as static constructor. All the constructors we have defined till now are non-static or instance constructors.

Class Test

{

static Test ()    //Static constructor defined explicitly

   {

   }

public Test ()   // Implicit default constructor

  {

  }

}


If a class contains any static the only implicit static constructors will be present or else we need to define them explicitly whereas non-static constructors will be implicitly defined in every class (except static class) provided we did not define them explicitly.

Static constructor are responsible in initializing static variables and these constructors are never called explicitly they are implicitly called and more over these constructor are first to execute under any class.

Static constructors can't be parameterized so overloading static constructors is not possible. 

 

Monday, 22 March 2021

Constructors in C#.NET Part 1 | C#.NET Tutorial | Clcoding

It's a special method present under a class responsible for initializing the variable of that class.

The name of a constructor method is exactly the same name of the class in which it was present and more over it's a non-value returning method.

Each and every class requires this constructor if we want to create the instance of that class.

class Test

{

int i ;

Test obj = new Test();  // Valid

⟶ It's the responsibility of a programmer to define a constructor under his class and if he fails to do so, on behalf of the programmer an implicit constructor gets defined in that class by the compiler.

 class Test

{

int i ; string s; bool b;

public Test ()

{

i = 0;  // Initializing the variables

s = null;

b = false;

}

}  

Implicitly defined constructors are parameter less and these constructor are also known as default constructors.

Implicitly defined co0nstructor are public.

We can also defined a constructor under the class and if we define it we can call it as explicit constructor and explicit constructor can be parameter less or parameterized also.

[ < modifiers > ] <Name> ( [ < parameter list > ] )

-Stmts

}

Defining : Implicit or Explicit

Calling : Explicit

Wednesday, 10 February 2021

Calculator in android studio with Source Code | Calculator in Android studio | Clcoding

 Activity Main :
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 
 xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000"
tools:context=".MainActivity">


<Button
android:id="@+id/button1"
android:layout_width="80dp"
android:layout_height="80dp"
android:background="@drawable/circle_button"
android:text="1"
android:textColor="#ffff"
android:textSize="28sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.043"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.817" />

<Button
android:id="@+id/button2"
android:layout_width="80dp"
android:layout_height="80dp"
android:background="@drawable/circle_button"
android:text="2"
android:textColor="#ffff"
android:textSize="28sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.352"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.817" />

<Button
android:id="@+id/button3"
android:layout_width="80dp"
android:layout_height="80dp"
android:background="@drawable/circle_button"
android:text="3"
android:textColor="#ffff"
android:textSize="28sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.672"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.817" />

<Button
android:id="@+id/button4"
android:layout_width="80dp"
android:layout_height="80dp"
android:background="@drawable/circle_button"
android:text="4"
android:textColor="#ffff"
android:textSize="28sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.043"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.677" />

<Button
android:id="@+id/button5"
android:layout_width="80dp"
android:layout_height="80dp"
android:background="@drawable/circle_button"
android:text="5"
android:textColor="#ffff"
android:textSize="28sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.352"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.677" />

<Button
android:id="@+id/button6"
android:layout_width="80dp"
android:layout_height="80dp"
android:background="@drawable/circle_button"
android:text="6"
android:textColor="#ffff"
android:textSize="28sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.672"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.677" />

<Button
android:id="@+id/button7"
android:layout_width="80dp"
android:layout_height="80dp"
android:background="@drawable/circle_button"
android:text="7"
android:textColor="#ffff"
android:textSize="28sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.043"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.541" />

<Button
android:id="@+id/button8"
android:layout_width="80dp"
android:layout_height="80dp"
android:background="@drawable/circle_button"
android:text="8"
android:textColor="#ffff"
android:textSize="28sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.352"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.541" />

<Button
android:id="@+id/button9"
android:layout_width="80dp"
android:layout_height="80dp"
android:background="@drawable/circle_button"
android:text="9"
android:textColor="#ffff"
android:textSize="28sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.672"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.541" />

<Button
android:id="@+id/button0"
android:layout_width="185dp"
android:layout_height="80dp"
android:background="@drawable/new_but"
android:paddingRight="95sp"
android:text="0"
android:textColor="#ffff"
android:textSize="28sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.061"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.957" />

<Button
android:id="@+id/button_equal"
android:layout_width="80dp"
android:layout_height="80dp"
android:background="@drawable/arithmatic_button"
android:text="="
android:textColor="#ffff"
android:textSize="28sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.99"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.958" />

<Button
android:id="@+id/button_multi"
android:layout_width="80dp"
android:layout_height="80dp"
android:background="@drawable/arithmatic_button"
android:text="×"
android:textColor="#ffff"
android:textSize="28sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.99"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.541" />

<Button
android:id="@+id/button_divide"
android:layout_width="80dp"
android:layout_height="80dp"
android:background="@drawable/arithmatic_button"
android:text="/"
android:textColor="#ffff"
android:textSize="28sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.987"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.406" />

<Button
android:id="@+id/button_add"
android:layout_width="80dp"
android:layout_height="80dp"
android:background="@drawable/arithmatic_button"
android:text="+"
android:textColor="#ffff"
android:textSize="28sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.99"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.817" />

<Button
android:id="@+id/button_sub"
android:layout_width="80dp"
android:layout_height="80dp"
android:background="@drawable/arithmatic_button"
android:text="-"
android:textColor="#ffff"
android:textSize="28sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.99"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.677" />

<Button
android:id="@+id/button_clear"
android:layout_width="80dp"
android:layout_height="80dp"
android:background="@drawable/button"
android:text="AC"
android:textColor="#060606"
android:textSize="28sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.043"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.406" />

<Button
android:id="@+id/button_para1"
android:layout_width="80dp"
android:layout_height="80dp"
android:background="@drawable/button"
android:text="%"
android:textColor="#060606"
android:textSize="28sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.672"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.406" />

<Button
android:id="@+id/button_para2"
android:layout_width="80dp"
android:layout_height="80dp"
android:background="@drawable/button"
android:text="+/-"
android:textColor="#060606"
android:textSize="28sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.349"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.406" />

<Button
android:id="@+id/button_dot"
android:layout_width="80dp"
android:layout_height="80dp"
android:background="@drawable/circle_button"
android:text="."
android:textColor="#ffff"
android:textSize="28sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.671"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.958" />

<TextView
android:id="@+id/input"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@android:color/background_light"
android:textSize="68sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.909"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.252" />

<TextView
android:id="@+id/output"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@android:color/background_light"
android:textSize="36sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.909"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.051" />

</androidx.constraintlayout.widget.ConstraintLayout>
 
Main Activity :


import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.TypedValue;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
private Button b1;
private Button b2;
private Button b3;
private Button b4;
private Button b5;
private Button b6;
private Button b7;
private Button b8;
private Button b9;
private Button b0;
private Button b_equal;
private Button b_multi;
private Button b_divide;
private Button b_add;
private Button b_sub;
private Button b_clear;
private Button b_dot;
private Button b_para1;
private Button b_para2;
private TextView t1;
private TextView t2;
private final char ADDITION = '+';
private final char SUBTRACTION = '-';
private final char MULTIPLICATION = '*';
private final char DIVISION = '/';
private final char EQU = '=';
private final char EXTRA = '@';
private final char MODULUS = '%';
private char ACTION;
private double val1 = Double.NaN;
private double val2;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewSetup();

b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ifErrorOnOutput();
exceedLength();
t1.setText(t1.getText().toString() + "1");
}
});

b2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ifErrorOnOutput();
exceedLength();
t1.setText(t1.getText().toString() + "2");
}
});

b3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ifErrorOnOutput();
exceedLength();
t1.setText(t1.getText().toString() + "3");
}
});

b4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ifErrorOnOutput();
exceedLength();
t1.setText(t1.getText().toString() + "4");
}
});

b5.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ifErrorOnOutput();
exceedLength();
t1.setText(t1.getText().toString() + "5");
}
});

b6.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ifErrorOnOutput();
exceedLength();
t1.setText(t1.getText().toString() + "6");
}
});

b7.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ifErrorOnOutput();
exceedLength();
t1.setText(t1.getText().toString() + "7");
}
});

b8.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ifErrorOnOutput();
exceedLength();
t1.setText(t1.getText().toString() + "8");
}
});

b9.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ifErrorOnOutput();
exceedLength();
t1.setText(t1.getText().toString() + "9");
}
});

b0.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ifErrorOnOutput();
exceedLength();
t1.setText(t1.getText().toString() + "0");
}
});

b_dot.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
exceedLength();
t1.setText(t1.getText().toString() + ".");
}
});

b_para1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (t1.getText().length() > 0) {
ACTION = MODULUS;
operation();
if (!ifReallyDecimal()) {
t2.setText(val1 + "%");
} else {
t2.setText((int) val1 + "%");
}
t1.setText(null);
} else {
t2.setText("Error");
}
}
});

b_add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (t1.getText().length() > 0) {
ACTION = ADDITION;
operation();
if (!ifReallyDecimal()) {
t2.setText(val1 + "+");
} else {
t2.setText((int) val1 + "+");
}
t1.setText(null);
} else {
t2.setText("Error");
}
}
});

b_sub.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (t1.getText().length() > 0) {
ACTION = SUBTRACTION;
operation();
if (t1.getText().length() > 0)
if (!ifReallyDecimal()) {
t2.setText(val1 + "-");
} else {
t2.setText((int) val1 + "-");
}
t1.setText(null);
} else {
t2.setText("Error");
}
}
});

b_multi.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (t1.getText().length() > 0) {
ACTION = MULTIPLICATION;
operation();
if (!ifReallyDecimal()) {
t2.setText(val1 + "×");
} else {
t2.setText((int) val1 + "×");
}
t1.setText(null);
} else {
t2.setText("Error");
}
}
});

b_divide.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (t1.getText().length() > 0) {
ACTION = DIVISION;
operation();
if (ifReallyDecimal()) {
t2.setText((int) val1 + "/");
} else {
t2.setText(val1 + "/");
}
t1.setText(null);
} else {
t2.setText("Error");
}
}
});

b_para2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (!t2.getText().toString().isEmpty() || !t1.getText().toString().isEmpty()) {
val1 = Double.parseDouble(t1.getText().toString());
ACTION = EXTRA;
t2.setText("-" + t1.getText().toString());
t1.setText("");
} else {
t2.setText("Error");
}
}
});

b_equal.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (t1.getText().length() > 0) {
operation();
ACTION = EQU;
if (!ifReallyDecimal()) {
t2.setText(/*t2.getText().toString() + String.valueOf(val2) + "=" + */String.valueOf(val1));
} else {
t2.setText(/*t2.getText().toString() + String.valueOf(val2) + "=" + */String.valueOf((int) val1));
}
t1.setText(null);
} else {
t2.setText("Error");
}
}
});

b_clear.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (t1.getText().length() > 0) {
CharSequence name = t1.getText().toString();
t1.setText(name.subSequence(0, name.length() - 1));
} else {
val1 = Double.NaN;
val2 = Double.NaN;
t1.setText("");
t2.setText("");
}
}
});

// Empty text views on long click.
b_clear.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
val1 = Double.NaN;
val2 = Double.NaN;
t1.setText("");
t2.setText("");
return true;
}
});
}

private void viewSetup() {
b1 = findViewById(R.id.button1);
b2 = findViewById(R.id.button2);
b3 = findViewById(R.id.button3);
b4 = findViewById(R.id.button4);
b5 = findViewById(R.id.button5);
b6 = findViewById(R.id.button6);
b7 = findViewById(R.id.button7);
b8 = findViewById(R.id.button8);
b9 = findViewById(R.id.button9);
b0 = findViewById(R.id.button0);
b_equal = findViewById(R.id.button_equal);
b_multi = findViewById(R.id.button_multi);
b_divide = findViewById(R.id.button_divide);
b_add = findViewById(R.id.button_add);
b_sub = findViewById(R.id.button_sub);
b_clear = findViewById(R.id.button_clear);
b_dot = findViewById(R.id.button_dot);
b_para1 = findViewById(R.id.button_para1);
b_para2 = findViewById(R.id.button_para2);
t1 = findViewById(R.id.input);
t2 = findViewById(R.id.output);
}

private void operation() {
if (!Double.isNaN(val1)) {
if (t2.getText().toString().charAt(0) == '-') {
val1 = (-1) * val1;
}
val2 = Double.parseDouble(t1.getText().toString());

switch (ACTION) {
case ADDITION:
val1 = val1 + val2;
break;
case SUBTRACTION:
val1 = val1 - val2;
break;
case MULTIPLICATION:
val1 = val1 * val2;
break;
case DIVISION:
val1 = val1 / val2;
break;
case EXTRA:
val1 = (-1) * val1;
break;
case MODULUS:
val1 = val1 % val2;
break;
case EQU:
break;
}
} else {
val1 = Double.parseDouble(t1.getText().toString());
}
}

// Remove error message that is already written there.
private void ifErrorOnOutput() {
if (t2.getText().toString().equals("Error")) {
t2.setText("");
}
}

// Whether value if a double or not
private boolean ifReallyDecimal() {
return val1 == (int) val1;
}

private void noOperation() {
String inputExpression = t2.getText().toString();
if (!inputExpression.isEmpty() && !inputExpression.equals("Error")) {
if (inputExpression.contains("-")) {
inputExpression = inputExpression.replace("-", "");
t2.setText("");
val1 = Double.parseDouble(inputExpression);
}
if (inputExpression.contains("+")) {
inputExpression = inputExpression.replace("+", "");
t2.setText("");
val1 = Double.parseDouble(inputExpression);
}
if (inputExpression.contains("/")) {
inputExpression = inputExpression.replace("/", "");
t2.setText("");
val1 = Double.parseDouble(inputExpression);
}
if (inputExpression.contains("%")) {
inputExpression = inputExpression.replace("%", "");
t2.setText("");
val1 = Double.parseDouble(inputExpression);
}
if (inputExpression.contains("×")) {
inputExpression = inputExpression.replace("×", "");
t2.setText("");
val1 = Double.parseDouble(inputExpression);
}
}
}

// Make text small if too many digits.
private void exceedLength() {
if (t1.getText().toString().length() > 10) {
t1.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20);
}
}

Popular Posts

Categories

100 Python Programs for Beginner (49) AI (34) Android (24) AngularJS (1) Assembly Language (2) aws (17) Azure (7) BI (10) book (4) Books (173) C (77) C# (12) C++ (82) Course (67) Coursera (226) Cybersecurity (24) data management (11) Data Science (128) Data Strucures (8) Deep Learning (20) Django (14) Downloads (3) edx (2) Engineering (14) Excel (13) Factorial (1) Finance (6) flask (3) flutter (1) FPL (17) Google (34) Hadoop (3) HTML&CSS (47) IBM (25) IoT (1) IS (25) Java (93) Leet Code (4) Machine Learning (59) Meta (22) MICHIGAN (5) microsoft (4) Nvidia (3) Pandas (4) PHP (20) Projects (29) Python (929) Python Coding Challenge (351) Python Quiz (21) Python Tips (2) Questions (2) R (70) React (6) Scripting (1) security (3) Selenium Webdriver (3) Software (17) SQL (42) UX Research (1) web application (8) Web development (2) web scraping (2)

Followers

Person climbing a staircase. Learn Data Science from Scratch: online program with 21 courses