Thursday, May 18, 2017

[MOBILE PROGRAMMING] Menentukan Nilai Akhir

Berikut adalah potongan kode dalam bentuk XML:
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
<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" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="Menentukan Nilai Akhir"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="19dp"
        android:text="Nilai Absensi" />

    <EditText
        android:id="@+id/edTugas"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/edKuis"
        android:layout_alignRight="@+id/edKuis"
        android:layout_below="@+id/edKuis"
        android:ems="10"
        android:inputType="numberDecimal" />

    <EditText
        android:id="@+id/edUTS"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/edTugas"
        android:layout_alignRight="@+id/edTugas"
        android:layout_below="@+id/edTugas"
        android:ems="10"
        android:inputType="numberDecimal" />

    <EditText
        android:id="@+id/edUAS"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/edUTS"
        android:layout_alignRight="@+id/edUTS"
        android:layout_below="@+id/edUTS"
        android:ems="10"
        android:inputType="numberDecimal" />

    <Button
        android:id="@+id/btnProses"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView4"
        android:layout_below="@+id/textView6"
        android:layout_marginTop="21dp"
        android:onClick="proses"
        android:text="Proses" />

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/edUAS"
        android:layout_marginLeft="18dp"
        android:layout_toRightOf="@+id/textView2" >
    </LinearLayout>

    <EditText
        android:id="@+id/edAbsen"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/textView3"
        android:layout_alignLeft="@+id/linearLayout1"
        android:ems="10"
        android:inputType="numberDecimal" >

        <requestFocus />
    </EditText>

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView2"
        android:layout_below="@+id/textView2"
        android:layout_marginTop="18dp"
        android:text="Nilai Kuis" />

    <TextView
        android:id="@+id/textView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/edTugas"
        android:layout_alignLeft="@+id/textView5"
        android:text="Nilai Tugas" />

    <TextView
        android:id="@+id/textView5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/edUAS"
        android:layout_alignRight="@+id/textView3"
        android:text="Nilai UTS" />

    <TextView
        android:id="@+id/textView6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/linearLayout1"
        android:layout_alignLeft="@+id/btnProses"
        android:text="Nilai UAS" />

    <TextView
        android:id="@+id/textView7"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/btnProses"
        android:layout_below="@+id/btnProses"
        android:layout_marginTop="24dp"
        android:text="Nilai Akhir" />

    <TextView
        android:id="@+id/textView8"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView7"
        android:layout_below="@+id/textView7"
        android:layout_marginTop="18dp"
        android:text="Nilai Huruf" />

    <LinearLayout
        android:id="@+id/linearLayout2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView8"
        android:layout_toLeftOf="@+id/linearLayout1" >
    </LinearLayout>

    <TextView
        android:id="@+id/txtNA"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/textView8"
        android:layout_alignLeft="@+id/linearLayout1" />

    <TextView
        android:id="@+id/txtHuruf"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/textView8"
        android:layout_alignLeft="@+id/txtNA" />

    <Button
        android:id="@+id/btnKeluar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/btnProses"
        android:layout_toRightOf="@+id/linearLayout2"
        android:onClick="keluar"
        android:text="Keluar" />

    <EditText
        android:id="@+id/edKuis"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/edAbsen"
        android:layout_alignRight="@+id/edAbsen"
        android:layout_below="@+id/edAbsen"
        android:ems="10"
        android:inputType="numberDecimal" />

</RelativeLayout>

Berikut ini adalah kode menggunakan code Java:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package com.example.nilaiakhir01;


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

public class MainActivity extends Activity {

 EditText absen,tugas,kuis,uts,uas;
 TextView NA,NH;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        absen=(EditText)findViewById(R.id.edAbsen);
        tugas=(EditText)findViewById(R.id.edTugas);
        kuis=(EditText)findViewById(R.id.edKuis);
        uts=(EditText)findViewById(R.id.edUTS);
        uas=(EditText)findViewById(R.id.edUAS);
        
        NA=(TextView)findViewById(R.id.txtNA);
        NH=(TextView)findViewById(R.id.txtHuruf);
    }


    public void proses(View v){
     if (absen.length() == 0){
      Toast.makeText(getApplication(),"Komponen Nilai Absen tidak boleh kosong",Toast.LENGTH_SHORT).show();
     }
     else if (tugas.length() == 0){
      Toast.makeText(getApplication(),"Komponen Nilai Tugas tidak boleh kosong",Toast.LENGTH_SHORT).show();
     }
     else if (kuis.length() == 0){
      Toast.makeText(getApplication(),"Komponen Nilai Kuis tidak boleh kosong",Toast.LENGTH_SHORT).show();
     }
     else if (uts.length() == 0){
      Toast.makeText(getApplication(),"Komponen Nilai UTS tidak boleh kosong",Toast.LENGTH_SHORT).show();
     }
     else if (uas.length() == 0){
      Toast.makeText(getApplication(),"Komponen Nilai UAS tidak boleh kosong",Toast.LENGTH_SHORT).show();
     }
     else{
      double n1=Double.parseDouble(absen.getText().toString());
         double n2=Double.parseDouble(tugas.getText().toString());
         double n3=Double.parseDouble(kuis.getText().toString());
         double n4=Double.parseDouble(uts.getText().toString());
         double n5=Double.parseDouble(uas.getText().toString());
         
         double nilai = nilaiAkhir(n1, n2, n3, n4, n5);
         
         NA.setText(String.valueOf(nilai).toString());
         
         char nilaiHuruf='-';
         if (nilai>=80){
          nilaiHuruf='A';
         }
         else if (nilai>=70){
          nilaiHuruf='B';
         }
         else if (nilai>=50){
          nilaiHuruf='C';
         }
         else if (nilai>=40){
          nilaiHuruf='D';
         }
         else if (nilai>=0){
          nilaiHuruf='E';
         }
         
         NH.setText(String.valueOf(nilaiHuruf).toString());
     }
     
     
    }
    
    public double nilaiAkhir(double ABSEN,double TUGAS, double KUIS, double UTS, double UAS ){
     return (0.1*ABSEN+0.1*TUGAS+0.1*KUIS+0.3*UTS+0.4*UAS);
    }
    
    public void keluar(View v){
     finish();
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    
}

Hasil potongan kode di atas adalah sebagai berikut:

No comments:

Post a Comment