Advertisement
Guest User

Untitled

a guest
Jul 30th, 2016
1,402
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.79 KB | None | 0 0
  1. package com.suhu_jr;
  2.  
  3. import android.os.Bundle;
  4.  
  5. import android.app.Activity;
  6.  
  7. import android.app.AlertDialog;
  8.  
  9. import android.content.DialogInterface;
  10.  
  11. import android.view.Menu;
  12.  
  13. import android.view.View;
  14.  
  15. import android.widget.ArrayAdapter;
  16.  
  17. import android.widget.EditText;
  18.  
  19. import android.widget.Spinner;
  20.  
  21. import android.widget.Toast;
  22.  
  23.  
  24.  
  25. public class MainActivity extends Activity {
  26.  
  27.  
  28.  
  29. @Override
  30.  
  31. public boolean onCreateOptionsMenu(Menu menu) {
  32.  
  33. // Inflate the menu; this adds items to the action bar if it is present.
  34.  
  35. getMenuInflater().inflate(R.menu.main, menu);
  36.  
  37. return true;
  38.  
  39. }
  40.  
  41. //Jika Tekan Tombol back
  42.  
  43. public void onBackPressed() {
  44.  
  45. exit();//Pergi ke method exit
  46.  
  47. }
  48.  
  49. private void exit() {
  50.  
  51. AlertDialog.Builder builder = new AlertDialog.Builder(this);
  52.  
  53. builder.setMessage("Are You Sure Want to Exit?")
  54.  
  55. .setCancelable(false)//tidak bisa tekan tombol back
  56.  
  57. //jika pilih yess
  58.  
  59. .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
  60.  
  61. public void onClick(DialogInterface dialog, int id) {
  62.  
  63. finish();
  64.  
  65. }
  66.  
  67. })
  68.  
  69. //jika pilih no
  70.  
  71. .setNegativeButton("No", new DialogInterface.OnClickListener() {
  72.  
  73. public void onClick(DialogInterface dialog, int id) {
  74.  
  75. dialog.cancel();
  76.  
  77. }
  78.  
  79. }).show();
  80.  
  81.  
  82.  
  83. }
  84.  
  85. private Spinner sp;
  86.  
  87. private EditText edt_awal, edt_C, edt_R, edt_F, edt_K;
  88.  
  89. private String[] list={"C","R","F","K"};
  90.  
  91. Double awal, celcius, reamur, fahrenheit, kelvin;
  92.  
  93. ArrayAdapter<String> adapter;
  94.  
  95. @Override
  96.  
  97. protected void onCreate(Bundle savedInstanceState) {
  98.  
  99. super.onCreate(savedInstanceState);
  100.  
  101. setContentView(R.layout.activity_main);
  102.  
  103. sp=(Spinner) findViewById(R.id.spinner1);
  104.  
  105. edt_awal=(EditText) findViewById(R.id.editText1);
  106.  
  107. edt_C=(EditText) findViewById(R.id.editText2);
  108.  
  109. edt_R=(EditText) findViewById(R.id.editText3);
  110.  
  111. edt_F=(EditText) findViewById(R.id.editText4);
  112.  
  113. edt_K=(EditText) findViewById(R.id.editText5);
  114.  
  115.  
  116.  
  117. adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, list);
  118.  
  119. adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
  120.  
  121. sp.setAdapter(adapter);
  122.  
  123. }
  124.  
  125.  
  126.  
  127. public void Konversi(View v){
  128.  
  129. String satuan = String.valueOf(sp.getSelectedItemPosition());
  130.  
  131. if(edt_awal.getText().toString().equals("")){
  132.  
  133. Toast.makeText(getBaseContext(), "Masukkan suhu awal, default suhu awal = 0 ", Toast.LENGTH_LONG).show();
  134.  
  135. awal = 0.0;
  136.  
  137. }else{
  138.  
  139. awal = Double.parseDouble(edt_awal.getText().toString());
  140.  
  141. }
  142.  
  143. if(satuan.equals("0")){
  144.  
  145. celcius = awal;
  146.  
  147. reamur = 0.8 * awal;
  148.  
  149. fahrenheit = (1.8 * awal) + 32;
  150.  
  151. kelvin = awal + 273;
  152.  
  153.  
  154.  
  155. edt_C.setText(String.valueOf(celcius));
  156.  
  157. edt_R.setText(String.valueOf(reamur));
  158.  
  159. edt_F.setText(String.valueOf(fahrenheit));
  160.  
  161. edt_K.setText(String.valueOf(kelvin));
  162.  
  163. }else if(satuan.equals("1")){
  164.  
  165. celcius = 1.25 * awal;
  166.  
  167. reamur = awal;
  168.  
  169. fahrenheit = (2.25 * awal) + 32;
  170.  
  171. kelvin = celcius + 273;
  172.  
  173.  
  174.  
  175. edt_C.setText(String.valueOf(celcius));
  176.  
  177. edt_R.setText(String.valueOf(reamur));
  178.  
  179. edt_F.setText(String.valueOf(fahrenheit));
  180.  
  181. edt_K.setText(String.valueOf(kelvin));
  182.  
  183. }else if(satuan.equals("2")){
  184.  
  185. celcius = 0.55555 *(awal - 32);
  186.  
  187. reamur = 0.44444 * (awal-32);
  188.  
  189. fahrenheit = awal;
  190.  
  191. kelvin = celcius + 273;
  192.  
  193.  
  194.  
  195. edt_C.setText(String.valueOf(celcius));
  196.  
  197. edt_R.setText(String.valueOf(reamur));
  198.  
  199. edt_F.setText(String.valueOf(fahrenheit));
  200.  
  201. edt_K.setText(String.valueOf(kelvin));
  202.  
  203. }else if(satuan.equals("3")){
  204.  
  205. celcius = awal-273;
  206.  
  207. reamur = 0.8 * (awal-273);
  208.  
  209. fahrenheit = (1.8 * (awal-273)) + 32;
  210.  
  211. kelvin = awal;
  212.  
  213.  
  214.  
  215. edt_C.setText(String.valueOf(celcius));
  216.  
  217. edt_R.setText(String.valueOf(reamur));
  218.  
  219. edt_F.setText(String.valueOf(fahrenheit));
  220.  
  221. edt_K.setText(String.valueOf(kelvin));
  222.  
  223. }
  224.  
  225. }
  226.  
  227. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement