About Me

Foto saya
Depok, Jawa Barat, Indonesia
Saya Rudy, saya sedang study di Gunadarma Depok jurusan Teknik Informatika. Saya belum ahli dalam bidang IT, namun saya berusaha agar dapat menerapkan ilmu yang saya punya dan dapat bermanfaat bagi orang lain.

Popular Posts

Senin, 15 Oktober 2012

Program Android Simulasi Lempar atau Tosh Koin

Source code Main.xml

















Source code AcakKoinActivity.java
package com.AcakKoin;

import java.text.DecimalFormat;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class AcakKoinActivity extends Activity implements OnClickListener{
    EditText txtlempar;
    TextView angka, gambar, pangka, pgambar;
    int ang, gam=0;
   
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        View acak = findViewById(R.id.acak);
        acak.setOnClickListener((OnClickListener) this);
        View clear = findViewById(R.id.clear);
        clear.setOnClickListener((OnClickListener) this);
        txtlempar = (EditText) findViewById(R.id.txtlempar);
        angka = (TextView) findViewById(R.id.angka);
        gambar = (TextView) findViewById(R.id.gambar);
        pangka = (TextView) findViewById(R.id.pangka);
        pgambar = (TextView) findViewById(R.id.pgambar);
    }
    
    public void onClick(View v)
    {
        switch(v.getId())
        {
        case R.id.acak:
        	String lempar = txtlempar.getText().toString();
        	if (lempar.length() != 0) {
        	long n = Long.parseLong(lempar);
          	for (int x = 1; x <= n ; ++x){
        	      double koin = Math.random();
        	      if (koin<=0.5)
        	    	  ang=ang+1;
        	      else
        	    	  gam=gam+1;
            DecimalFormat twoDForm = new DecimalFormat("#.##");
            angka.setText(Long.toString(ang));
        	gambar.setText(Long.toString(gam));
        	double persen=(ang*100.0)/n;
        	double persenbulat = Double.valueOf(twoDForm.format(persen));
           	pangka.setText(Double.toString(persenbulat));
        	pgambar.setText(Double.toString(100-persenbulat));
        	}
        	}
        	else 
        		Toast.makeText(getBaseContext(), 
                "Isi banyaknya acak koin", 
                Toast.LENGTH_SHORT).show();
        	break;
        			
        case R.id.clear:
        	if (angka.length()>0)
        	{
	        	ang=0;
	        	gam=0;
	        	angka.setText("");
	        	gambar.setText("");
	        	pangka.setText("");
	        	pgambar.setText("");
	        	txtlempar.setText("");
	        }
        	break;
        }
    }
}

0 comments:

Posting Komentar