Menampilkan Nilai Dengan Aritamatika
CODE di dalam activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="vertical"
app:layout_constraintTop_toTopOf="parent"
android:padding="10dp">
<EditText
android:id="@+id/nama"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:hint="Masukan nama" />
<EditText
android:id="@+id/tugas"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:hint="Nilai Tugas" />
<EditText
android:id="@+id/uh"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:hint="Nilai Uh" />
<EditText
android:id="@+id/uts"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:hint="Nilai UTS" />
<EditText
android:id="@+id/uas"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:hint="Nilai UAS" />
<Button
android:onClick="simpan"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/teal_700"
android:text="SIMPAN"
android:layout_margin="5dp"
android:textColor="@color/white"/>
<TextView
android:id="@+id/nnama"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="NAMA :"
android:textSize="20dp"
android:layout_marginTop="10dp"/>
<TextView
android:id="@+id/ntugas"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Nilai Tugas :"
android:textSize="20dp"
android:layout_marginTop="10dp"/>
<TextView
android:id="@+id/nUh"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Nilai Uh :"
android:textSize="20dp"
android:layout_marginTop="10dp"/>
<TextView
android:id="@+id/nUts"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Nilai Uts :"
android:textSize="20dp"
android:layout_marginTop="10dp"/>
<TextView
android:id="@+id/nUas"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Nilai Uas :"
android:textSize="20dp"
android:layout_marginTop="10dp"/>
<TextView
android:id="@+id/nAkhir"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="NA :"
android:textSize="20dp"
android:layout_marginTop="10dp"/>
<TextView
android:id="@+id/nKet"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Keterangan:"
android:textSize="20dp"
android:layout_marginTop="10dp"/>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
CODE di dalam MainAktivity.java
package com.example.nilai;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
EditText Nama,Tugas,Uh,Uts,Uas;
TextView NNAMA,NTUGAS,NUH,NUTS,NUAS,NAKHIR,NKET;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void simpan(View view) {
EditText Nama = findViewById(R.id.nama);
EditText Tugas = findViewById(R.id.tugas);
EditText Uh = findViewById(R.id.uh);
EditText Uts = findViewById(R.id.uts);
EditText Uas = findViewById(R.id.uas);
TextView NNAMA = findViewById(R.id.nnama);
TextView NTUGAS = findViewById(R.id.ntugas);
TextView NUH = findViewById(R.id.nUh);
TextView NUTS = findViewById(R.id.nUts);
TextView NUAS = findViewById(R.id.nUas);
TextView NAKHIR = findViewById(R.id.nAkhir);
TextView NKET = findViewById(R.id.nKet);
String GetNama = Nama.getText().toString();
int GetTugas = Integer.parseInt(Tugas.getText().toString());
int GetUh = Integer.parseInt(Uh.getText().toString());
int GetUts = Integer.parseInt(Uts.getText().toString());
int GetUas = Integer.parseInt(Uas.getText().toString());
double NilaiAkhir = (GetTugas * 0.1) + (GetUh * 0.2) + (GetUts * 0.3) + (GetUas * 0.4);
String Ket;
if (NilaiAkhir > 80) {
Ket = "Berhasil";
} else if (NilaiAkhir > 70) {
Ket = "Cukup";
} else if (NilaiAkhir > 65) {
Ket = "Kurang";
} else {
Ket = "Gagal";
}
NNAMA.setText("Nama : " + GetNama);
NTUGAS.setText("Nilai Tugas : " + GetTugas);
NUH.setText("Nilai Uh : " + GetUh);
NUTS.setText("Nilai Uts : " + GetUts);
NUAS.setText("Nilai Uas : " + GetUas);
NAKHIR.setText("Nilai Akhir : " + NilaiAkhir);
NKET.setText("Keterangan : " + Ket);
}
}