Skip to main content

Android Studio Membuat Kalkulator Sederhana

Pada kesempatan kali ini saya akan memberikan tutorial cara membuat kalkulator sederhana yang dapat melakukan perhitungan tambah, kurang, kali dan bagi, seperti tampak pada gambar di bawah ini,

Pertama buatlah project baru dengan nama KalkulatorSederhana, atau nama lain sesuai keinginan Anda.

Buka file activity_main.xml , kemudian rubahlah isinya sebagai berikut:


    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/aaa"
    android:orientation="vertical"
    android:padding="15dp"
    tools:context="com.example.hp.kalkulatorsederhana.MainActivity">


            android:id="@+id/KalkulatorSederhana"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Kalkulator Sederhana\n"
        android:textAlignment="center"
        android:textColor="@android:color/background_light"
        android:textSize="24sp" />

            android:id="@+id/Angka1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Angka Pertama"
        android:textColor="@android:color/background_light" />

            android:id="@+id/ed_angka1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/Angka1"
        android:ems="10"
        android:inputType="number|numberDecimal"
        android:textColor="@android:color/background_light" />

            android:id="@+id/Angka2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/ed_angka1"
        android:text="Angka Kedua"
        android:textColor="@android:color/background_light" />

            android:id="@+id/ed_angka2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/Angka2"
        android:ems="10"
        android:inputType="number|numberDecimal"
        android:textColor="@android:color/background_light" />

            android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/btn_tambah"
        android:text="Hasil"
        android:textColor="@android:color/background_light" />

            android:id="@+id/ed_hasil"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView3"
        android:ems="10"
        android:inputType="number|numberDecimal"
        android:textColor="@android:color/background_light" />

   




Buka file MainActivity.java , kemudian rubahlah isinya sebagai berikut:

package com.example.hp.kalkulatorsederhana;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {

    private EditText edtAngka1;
    private EditText edtAngka2;
    private EditText edtHasil;
    private Button btnTambah, btnKurang, btnKali, btnBagi;
    double angka1, angka2, hasil;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        edtAngka1 = (EditText) findViewById(R.id.ed_angka1);
        edtAngka2 = (EditText) findViewById(R.id.ed_angka2);
        edtHasil = (EditText) findViewById(R.id.ed_hasil);
        btnTambah = (Button) findViewById(R.id.btn_tambah);
        btnKurang = (Button) findViewById(R.id.btn_kurang);
        btnKali = (Button) findViewById(R.id.btn_kali);
        btnBagi = (Button) findViewById(R.id.btn_bagi);

        btnTambah.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                hitungTambah();
            }
        });

        btnKurang.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                hitungKurang();
            }
        });

        btnKali.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                hitungKali();
            }
        });

        btnBagi.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                hitungBagi();
            }
        });

    }

    private void hitungTambah(){
        angka1 = Double.parseDouble(edtAngka1.getText().toString());
        angka2 = Double.parseDouble(edtAngka2.getText().toString());
        hasil = angka1+angka2;
        edtHasil.setText(hasil+"");
    }

    private void hitungKurang(){
        angka1 = Double.parseDouble(edtAngka1.getText().toString());
        angka2 = Double.parseDouble(edtAngka2.getText().toString());
        hasil = angka1-angka2;
        edtHasil.setText(hasil+"");
    }

    private void hitungKali(){
        angka1 = Double.parseDouble(edtAngka1.getText().toString());
        angka2 = Double.parseDouble(edtAngka2.getText().toString());
        hasil = angka1*angka2;
        edtHasil.setText(hasil+"");
    }

    private void hitungBagi(){
        angka1 = Double.parseDouble(edtAngka1.getText().toString());
        angka2 = Double.parseDouble(edtAngka2.getText().toString());
        hasil = angka1/angka2;
        edtHasil.setText(hasil+"");
    }
}

Terakhir jalankan program tersebut, kemudian lihat hasilnya.

Comments

Popular posts from this blog

Program PHP sederhana untuk menghitung simulasi kredit

Membuat Form <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Untitled Document</title> <style type="text/css"> <!-- body,td,th { font-family: Geneva, Arial, Helvetica, sans-serif; font-size: 12px; color: #333333; } --> </style></head> <?php $harga=$_GET['harga']; $dp=$harga*0.2; ?> <body> <H2>FORM SIMULASI KREDIT </H2> <br /> <form id="form1" name="form1" method="post" action="simulasi_proses1.php"> <table width="580" border="0" cellspacing="2" cellpadding="2"> <tr> <td>Harga Kendaraan </td>

Android studio contoh penerapan looping

Berikut adalah contoh penerapan looping pada android studio, program akan melakukan looping sebanyak 10 kali dan mencetak angka dari 1 sampai 11 . Buka dan edit file activity_main.xml seperti berikut: activity_main.xml xml version= "1.0" encoding= "utf-8" ?> < RelativeLayout 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= "daniyusuf.com.contohlooping.MainActivity" > < TextView android :id= "@+id/txt_looping" android :layout_width= "match_parent" android :layout_height= "wrap_content" android :text= "Hello World!" app :layout_constraintBottom_toBottomOf= "parent"

COMPONENT-LEVEL DESIGN

Component-level design, atau juga dikenal dengan procedural design, baru ada setelah data, arsitektur dan rancangan antarmuka dibuat terlebih dahulu. Component-level design tujuannya adalah untuk menterjemahkan model design ke bentuk software yang akan dibuat. Namun dikarenakan abstraksi model design yang sudah ada relatif tinggi sedangkan abstraksi tingkat program operasionalnya rendah, maka proses penterjemahannya ini menjadi sebuah tantangan tersendiri. Menurut Edsgar Dijkstra, dalam perlkuliahannya mengatakan [DIJ72]: “Software ini berbeda dibanding dengan produk lain, dimana aturannya adalah semakin tinggi kualitas akan berdampak pada harga. Orang yang benar-benar ingin memperoleh software yang dapat diandalkan akan percaya bahwa mereka harus bisa menemukan suatu alat/cara untuk menghindari memulai suatu sistem dengan bug, dan hasilnya adalah, proses programming menjadi lebih murah . . . programmer efektif . . . tidak boleh menghabiskan waktunya untuk memperbaiki debugg—mereka se