Search code examples
javaandroidcomputer-science

Calculate in android studio Java


I have created several EditTexts that a user can enter numbers and I would like to get and show the output in the last Edittext called math_test2.Can you guys help me with the syntax? Iam new to android studio and I dont know the syntax to make this work. I think I need to store the number input of x and y and another variable z and then set z = output. Then get the method from the regular java file I created. I know I am suppose to created an instance, but I am not sure I know how to do that correctly.I am trying to get better at using android studio. I made the MainActivity dependent of the JavaMathExample1 file located in the New module I created. Thank you!

**activity_main.xml file:**

<?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"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="166dp"
        android:layout_height="70dp"
        android:gravity="center"
        android:text="Math Test Demo"
        android:textAlignment="center"
        android:textSize="20sp"
        android:textStyle="bold"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="0.044"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.167" />


    <EditText
        android:id="@+id/calculateX"
        android:layout_width="251dp"
        android:layout_height="62dp"
        android:hint="Enter x value:"
        android:padding="15dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.071"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.316" />


    <EditText
        android:id="@+id/calculateY"
        android:layout_width="265dp"
        android:layout_height="58dp"
        android:hint="Enter y value:"
        android:padding="15dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.078"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.456" />


    <EditText
        android:id="@+id/math_test2"
        android:layout_width="324dp"
        android:layout_height="68dp"
        android:hint="Calculated Output for x and y: "
        android:padding="15dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.126"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.598" />

</androidx.constraintlayout.widget.ConstraintLayout>


**MainActivity.java file:**

package com.example.testdemo;



import com.example.javaprogram.JavaMathExample1;

public class MainActivity extends AppCompatActivity {

    EditText calculateX,calculateY,output;
    JavaMathExample1 javaMathExample1;

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

        calculateX = (EditText) findViewById(R.id.calculateX);
        calculateY = (EditText) findViewById(R.id.calculateY);
        output = (EditText) findViewById(R.id.math_test2);


        String x = calculateX.getText().toString();// Enter a number for x
        String y  = calculateY.getText().toString();// Enter a number for y
        String result = output.getText().toString();// Get output from method created in JavaMathExample1

        calculateY.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

            }
        });

        calculateY.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

            }
        });

        output.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
            
            JavaMathExample1 myObj = new JavaMathExample1();
            myObj.fullThrottle(x,y);

            }
        }); 
    }
}



**Regular Java file from "New Module android library created"**

package com.example.javaprogram

public class JavaMathExample1 {

    public void fullThrottle(double x, double y) {

        // return the maximum of two numbers
        System.out.println("Maximum number of x and y is: " + Math.max(x, y));
    }
}

Solution

  • In order to calculate x and y which are entering, I have made changes in all three classes which you have posted above.

    xml layout:

    <?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">
    
    <TextView
      android:layout_width="166dp"
      android:layout_height="70dp"
      android:gravity="center"
      android:text="Math Test Demo"
      android:textAlignment="center"
      android:textSize="20sp"
      android:textStyle="bold"
      app:layout_constraintBottom_toBottomOf="parent"
      app:layout_constraintHorizontal_bias="0.044"
      app:layout_constraintLeft_toLeftOf="parent"
      app:layout_constraintRight_toRightOf="parent"
      app:layout_constraintTop_toTopOf="parent"
      app:layout_constraintVertical_bias="0.167" />
    
    <EditText
      android:id="@+id/calculateX"
      android:layout_width="251dp"
      android:layout_height="62dp"
      android:hint="Enter x value:"
      android:padding="15dp"
      app:layout_constraintBottom_toBottomOf="parent"
      app:layout_constraintEnd_toEndOf="parent"
      app:layout_constraintHorizontal_bias="0.071"
      app:layout_constraintStart_toStartOf="parent"
      app:layout_constraintTop_toTopOf="parent"
      app:layout_constraintVertical_bias="0.316" />
    
    
    <EditText
      android:id="@+id/calculateY"
      android:layout_width="265dp"
      android:layout_height="58dp"
      android:hint="Enter y value:"
      android:padding="15dp"
      app:layout_constraintBottom_toBottomOf="parent"
      app:layout_constraintEnd_toEndOf="parent"
      app:layout_constraintHorizontal_bias="0.078"
      app:layout_constraintStart_toStartOf="parent"
      app:layout_constraintTop_toTopOf="parent"
      app:layout_constraintVertical_bias="0.456" />
    
    <EditText
      android:id="@+id/math_test2"
      android:layout_width="324dp"
      android:layout_height="68dp"
      android:hint="Calculated Output for x and y: "
      android:padding="15dp"
      app:layout_constraintBottom_toBottomOf="parent"
      app:layout_constraintEnd_toEndOf="parent"
      app:layout_constraintHorizontal_bias="0.126"
      app:layout_constraintStart_toStartOf="parent"
      app:layout_constraintTop_toTopOf="parent"
      app:layout_constraintVertical_bias="0.598" />
    
    <Button
      android:id="@+id/calculate"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Calculate"
      app:layout_constraintTop_toBottomOf="@id/math_test2"
      app:layout_constraintStart_toStartOf="parent"
      app:layout_constraintEnd_toEndOf="parent"/>
    
     </androidx.constraintlayout.widget.ConstraintLayout>
    

    MainActivity:

    public class MainActivity extends AppCompatActivity {
    
    EditText calculateX, calculateY, output;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        calculateX = findViewById(R.id.calculateX);
        calculateY = findViewById(R.id.calculateY);
        output = findViewById(R.id.math_test2);
    
        findViewById(R.id.calculate).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                JavaMathExample1 myObj = new JavaMathExample1();
                String inputX = calculateX.getText().toString();
                String inputY = calculateY.getText().toString();
    
                if (inputX.isEmpty() || inputY.isEmpty()) {
                    Toast.makeText(MainActivity.this, "Enter X or Y value", Toast.LENGTH_LONG).show();
                } else {
                    double x = Double.parseDouble(inputX);  // Enter a number for x
                    double y = Double.parseDouble(inputY); // Enter a number for y
                    double max = myObj.fullThrottle(x, y); //This will give you result
                    output.setText(String.format("%s", max));
                }
            }
        });
    }
    }
    

    JavaMathExample1:

    public class JavaMathExample1 {
    public double fullThrottle(double x, double y) {
        double max = Math.max(x, y);
        // return the maximum of two numbers
        System.out.println("Maximum number of x and y is: " + max);
        return max;
    }
    }
    

    If you still have issue, you can find full code here: https://github.com/parmeshtoyou/StackOverflow/tree/sof_23_oct_21

    Result will look like this: enter image description here