File activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<EditText
android:id="@+id/editpas"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/editanda"
android:layout_marginTop="19dp"
android:layout_toRightOf="@+id/spas"
android:ems="10" >
<requestFocus />
</EditText>
<EditText
android:id="@+id/editanda"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editpas"
android:layout_alignParentTop="true"
android:layout_marginTop="40dp"
android:ems="10" />
<EditText
android:id="@+id/editresult"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/btnhitung"
android:layout_alignRight="@+id/btnhitung"
android:layout_below="@+id/btnhitung"
android:layout_marginTop="37dp"
android:ems="10" />
<TextView
android:id="@+id/spas"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/editpas"
android:layout_alignBottom="@+id/editpas"
android:layout_alignRight="@+id/sanda"
android:text="Nama Pasangan" />
<TextView
android:id="@+id/sanda"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/editanda"
android:layout_alignBottom="@+id/editanda"
android:layout_alignParentLeft="true"
android:text="Nama Anda" />
<TextView
android:id="@+id/sresult"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/editresult"
android:layout_alignBottom="@+id/editresult"
android:layout_alignParentLeft="true"
android:text="Result" />
<Button
android:id="@+id/btnhitung"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:onClick="hitung"
android:text="Proses" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/editresult"
android:layout_marginLeft="14dp"
android:layout_toRightOf="@+id/editresult"
android:text="%" />
</RelativeLayout>
File MainActivity.java
package com.example.android_tugas;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import java.util.ArrayList;
public class MainActivity extends Activity{
private EditText editanda,editpas,editresult;
private String sanda,spas,sresult;
private Button btnhitung;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editanda=(EditText) findViewById(R.id.editanda);
editpas = (EditText) findViewById(R.id.editpas);
btnhitung = (Button) findViewById(R.id.btnhitung);
editresult = (EditText) findViewById(R.id.editresult);
}
public void hitung(View view) {
String stranda = "";
String strpas = "";
try{
stranda = editanda.getText().toString();
strpas = editpas.getText().toString();
char[] arrCharStr = stranda.toCharArray();
char[] arrCharStr2 = strpas.toCharArray();
}catch(Exception e){
}
char[] arrCharStr = stranda.toCharArray();
char[] arrCharStr2 = strpas.toCharArray();
int sum_a=0;
int sum_pas=0;
for (char c : arrCharStr) {
String biner = "0" + Integer.toBinaryString(c);
int ASCII = Integer.parseInt(biner, 2);
char karakter = (char) ASCII;
System.out.println(biner + " :: " + ASCII + " :: " + karakter);
sum_a = sum_a + ASCII;
}
System.out.println(sum_a + "");
for (char c : arrCharStr2) {
String biner = "0" + Integer.toBinaryString(c);
int ASCII = Integer.parseInt(biner, 2);
char karakter = (char) ASCII;
System.out.println(biner + " :: " + ASCII + " :: " + karakter);
sum_pas= sum_pas + ASCII;
}
System.out.println(sum_pas + "");
int nilai_anda=sum_a%100;
int nilai_pas=sum_pas%100;
System.out.println(nilai_anda + "");
System.out.println(nilai_pas + "");
int selisih;
if (nilai_anda > nilai_pas){
selisih=nilai_anda - nilai_pas;
System.out.println(selisih + "");
}
else{
selisih=nilai_pas-nilai_anda;
System.out.println(selisih + "");
}
int hasil = 100 - selisih;
System.out.println(hasil + "% cocok");
String x= String.valueOf(hasil);
editresult.setText(x);
}
}
Tidak ada komentar:
Posting Komentar