Guest User

Untitled

a guest
Jul 16th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.43 KB | None | 0 0
  1. package com.castelanjr.calculanota;
  2.  
  3. import android.app.Activity;
  4. import android.app.AlertDialog;
  5. import android.os.Bundle;
  6. import android.view.View;
  7. import android.widget.EditText;
  8. import android.widget.Button;
  9. import java.text.DecimalFormat;
  10.  
  11. public class CalculaNota extends Activity {
  12.  
  13. EditText ed1, ed2;
  14. String aprovado;
  15.  
  16. @Override
  17. public void onCreate(Bundle savedInstanceState) {
  18. super.onCreate(savedInstanceState);
  19. setContentView(R.layout.main);
  20.  
  21. ed1 = (EditText) findViewById(R.id.ed1);
  22. ed2 = (EditText) findViewById(R.id.ed2);
  23.  
  24. Button btnCalcula = (Button) findViewById(R.id.btnCalcula);
  25.  
  26. btnCalcula.setOnClickListener(new View.OnClickListener() {
  27.  
  28. @Override
  29. public void onClick(View v) {
  30. double g1 = Double.parseDouble(ed1.getText().toString());
  31. double g2 = Double.parseDouble(ed2.getText().toString());
  32.  
  33. double nota = ((g1 + (g2 * 2))/3);
  34.  
  35. if(nota >= 6){
  36. aprovado = "Parabéns, você foi aprovado!\nMédia: ";
  37. }
  38. else{
  39. aprovado = "Você foi reprovado.\nMédia: ";
  40. }
  41.  
  42. DecimalFormat df = new DecimalFormat("0.##");
  43. String notaFormatada = df.format(nota);
  44.  
  45. AlertDialog.Builder dialog = new AlertDialog.Builder(CalculaNota.this);
  46. dialog.setTitle(R.string.resultado);
  47. dialog.setMessage(aprovado + notaFormatada);
  48. dialog.setNeutralButton(R.string.ok, null);
  49. dialog.show();
  50. ed1.setText("");
  51. ed2.setText("");
  52. }
  53. });
  54. }
  55. }
Add Comment
Please, Sign In to add comment