Last active
May 11, 2018 17:22
-
-
Save edenizk/9fa6247548d379b75235552c300a29c3 to your computer and use it in GitHub Desktop.
finds max value user input and generates random number between user inputs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.deniz.app9; | |
import android.support.v7.app.AppCompatActivity; | |
import android.os.Bundle; | |
import android.view.View; | |
import android.widget.Button; | |
import android.widget.EditText; | |
import android.widget.ImageButton; | |
import android.widget.TextView; | |
import android.widget.ViewSwitcher; | |
import java.security.SecureRandom; | |
public class MainActivity extends AppCompatActivity { | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
final EditText edtNum1 = (EditText) findViewById(R.id.edtNum1); | |
final EditText edtNum2 = (EditText) findViewById(R.id.edtNum2); | |
final EditText edtNum3 = (EditText) findViewById(R.id.edtNum3); | |
final EditText edtmax =(EditText) findViewById(R.id.edtmax); | |
final EditText edtmin =(EditText) findViewById(R.id.edtmin); | |
final TextView txtResult = (TextView) findViewById(R.id.txtResult); | |
final TextView txtrandom = (TextView) findViewById(R.id.txtrandom); | |
final TextView txtold = (TextView) findViewById(R.id.txtold); | |
final ImageButton btndelete = (ImageButton) findViewById(R.id.btndelete); | |
Button btnMax = (Button) findViewById(R.id.btnMax); | |
Button btnRandom = (Button)findViewById(R.id.btnRandom); | |
btnMax.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View view) { | |
double num1 = Double.parseDouble(edtNum1.getText().toString()); | |
double num2 = Double.parseDouble(edtNum2.getText().toString()); | |
double num3 = Double.parseDouble(edtNum3.getText().toString()); | |
double result = getMax(num1,num2,num3); | |
txtResult.setText(result +""); | |
} | |
}); | |
btnRandom.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View view) { | |
int max = 10; | |
int min = 0; | |
if(!edtmax.getText().toString().isEmpty()){ | |
max = Integer.parseInt(edtmax.getText().toString()); | |
} | |
else | |
edtmax.setText("10"); | |
if(!edtmin.getText().toString().isEmpty()){ | |
min = Integer.parseInt(edtmin.getText().toString()); | |
} | |
else | |
edtmin.setText("0"); | |
if(min>max){ | |
int tmp = max; | |
max = min; | |
min = tmp; | |
} | |
SecureRandom securerandomnum = new SecureRandom(); | |
String oldnum = txtrandom.getText().toString(); | |
txtold.setText(oldnum); | |
int randomnum = min + securerandomnum.nextInt((max-min+1)); | |
txtrandom.setText(randomnum + ""); | |
btndelete.setVisibility(View.VISIBLE); | |
} | |
}); | |
btndelete.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View view) { | |
((EditText) findViewById(R.id.edtmax)).getText().clear(); | |
((EditText) findViewById(R.id.edtmin)).getText().clear(); | |
txtrandom.setText("Secure Random Numbers"); | |
txtResult.setText("Result:"); | |
btndelete.setVisibility(View.GONE); | |
} | |
}); | |
} | |
public double getMax(double num1,double num2,double num3){ | |
double result=num1; | |
if(result < num2) | |
result = num2; | |
if(result < num3) | |
result = num3; | |
return result; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment