Sei sulla pagina 1di 8

TUGAS APLIKASI OPERASI STRING

3IA18
ANGGOTA KELOMPOK :
ARIANI KARTIKA
INTAN OKA HERDANIS
I GUSTI NGURAH PUTU RYANDHIKA
JIMMY HALIM
NURUL ANNISA
TIARA INDAH




UNIVERSITAS GUNADARMA




SOURCE PROGRAM

package com.dika.tbostring;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.AdapterView.OnItemSelectedListener;

public class MainActivity extends Activity implements OnItemSelectedListener{

EditText input1, input2;
public String ambil, ambil2, hasil;
public char arraykarakter[];
public int temp,tes,temp2;
Button button01;
TextView tv1;
ImageView iv1;
Spinner spinOps;
int awal = 2;

String[] ops={"Pilih operasi","Prefix","ProperPrefix","Postfix","ProperPostfix","Head
String","Tail String","Concatenation","Alternation","Substring","Proper Substring"};
String opselected;
double a, b, c;

private OnClickListener myClickListener = new OnClickListener()
{

public void onClick(View v) {
input1.getText().toString();
input2.getText().toString();

if (opselected=="Prefix")
{
ambilString();
tv1.setText(prefix());
}

else if (opselected=="ProperPrefix")
{
ambilString();
tv1.setText(properprefix());
}

else if (opselected=="Postfix")
{
ambilString();
tv1.setText(postfix());
}
else if (opselected=="ProperPostfix")
{
ambilString();
tv1.setText(properpostfix());
}
else if (opselected=="Head String")
{
ambilString();
tv1.setText(headstring());
}
else if (opselected=="Tail String")
{
ambilString();
tv1.setText(tailstring());
}
else if (opselected=="Concatenation")
{
ambilString();
tv1.setText(concat());
}
else if (opselected=="Alternation")
{
ambilString();
tv1.setText(alternation());
}
else if (opselected=="Substring")
{
ambilString();
tv1.setText(substring());
tv1.setText(ambilsemuakarakter());
}
else if (opselected=="Proper Substring")
{
ambilString();
tv1.setText(propersubstring());
tv1.setText(ambilsemuakarakter());
}

else {tv1.setText("Maaf, pilih pengoperasian pada menu di atas");}

//tv1 = (TextView)findViewById(R.id.TextView01);
}
};

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);


tv1 = (TextView)findViewById(R.id.TextView01);

//iv1= (ImageView)findViewById(R.id.);//try imageview



button01 = (Button)findViewById(R.id.Button01);

button01.setText("Hasil");

button01.setOnClickListener(myClickListener);



input1 = (EditText)findViewById(R.id.EditText01);

input1.setText("");

input2 = (EditText)findViewById(R.id.EditText02);

input2.setText("");



//list of operations

spinOps = (Spinner)findViewById(R.id.Spinner01);

ArrayAdapter<String> adapter = new ArrayAdapter<String>(

this, android.R.layout.simple_spinner_item, ops);

adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

spinOps.setAdapter(adapter);

spinOps.setOnItemSelectedListener(this);



}

public void onItemSelected (AdapterView<?> p,View v,int position,long id) {

opselected=ops[position];

tv1.setText("Anda memilih " + opselected);

}

public void onNothingSelected(AdapterView<?> p) {

tv1.setText("Tolong dipilih menunya");

}

public void ambilString(){
tv1.setText("");
ambil = input1.getText().toString();
ambil2 = input2.getText().toString();
temp = ambil.length();
arraykarakter = new char[ambil.length()];
arraykarakter = ambil.toCharArray();
hasil = "";
}

public String prefix(){
for(int i=0; i<ambil.length()+1; i++){
for(int j=0; j<temp; j++){
hasil = hasil + arraykarakter[j];
}
for(int j=0; j<=i; j++){
hasil = hasil + "*";
}
temp = temp - 1;
hasil = hasil + ", ";
}
return hasil;
}

public String properprefix(){
for(int i=0; i<ambil.length(); i++){
for(int j=0; j<temp-1; j++){
hasil += arraykarakter[j];
}
for(int j=0; j<=i; j++){
hasil += "*";
}
temp -= 1;
hasil += ", ";
}
return hasil;
}

public String postfix(){
tes = 0;
temp2 = 0;
for(int i=0; i<ambil.length()+1; i++){
for(int j=0; j<=i; j++){
hasil += "*";
}
for(int j=0; j<temp; j++){
hasil += arraykarakter[tes];
tes +=1;
}
temp -= 1;
hasil += ", ";
temp2+=1;
tes = temp2;
}
return hasil;
}

public String properpostfix(){
tes = 0;
temp2 = 0;
for(int i=0; i<ambil.length(); i++){
for(int j=0; j<=i; j++){
hasil += "*";
}
for(int j=0; j<temp-1; j++){
hasil += arraykarakter[tes+1];
tes +=1;
}
temp -= 1;
hasil += ", ";
temp2+=1;
tes = temp2;
}
return hasil;
}

public String headstring(){
hasil = ""+arraykarakter[0];
return hasil;
}

public String tailstring(){
for(int j=1; j<temp; j++){
hasil += arraykarakter[j];
}
return hasil;
}

public String concat(){
hasil = ambil + ambil2;
return hasil;
}

public String alternation(){
hasil = ambil +"|"+ ambil2;
return hasil;
}

public String substring(){
for(int i=0; i<ambil.length(); i++){
for(int j=0; j<temp; j++){
hasil += arraykarakter[j];
}
for(int j=0; j<=i; j++){
hasil += "*";
}
temp -= 1;
hasil += ", ";
}
temp = ambil.length();
tes = 0;
temp2 = 0;
for(int i=0; i<ambil.length(); i++){
for(int j=0; j<=i; j++){
hasil += "*";
}
for(int j=0; j<temp; j++){
hasil += arraykarakter[tes];
tes +=1;
}
temp -= 1;
hasil += ", ";
temp2+=1;
tes = temp2;
}
return hasil;
}

public String propersubstring(){
for(int i=0; i<ambil.length(); i++){
for(int j=0; j<temp-1; j++){
hasil += arraykarakter[j];
}
for(int j=0; j<=i; j++){
hasil += "*";
}
temp -= 1;
hasil += ", ";
}
temp = ambil.length();
tes = 0;
temp2 = 0;
for(int i=0; i<ambil.length(); i++){
for(int j=0; j<=i; j++){
hasil += "*";
}
for(int j=0; j<temp-1; j++){
hasil += arraykarakter[tes+1];
tes +=1;
}
temp -= 1;
hasil += ", ";
temp2+=1;
tes = temp2;
}
return hasil;
}

public String ambilsemuakarakter(){
for(int i=0; i<ambil.length(); i++){
hasil += arraykarakter[i];
hasil += ", ";
}
return hasil;
}

}

Potrebbero piacerti anche