Bu makalede Android Java ile kullanıcının girdiği üç yazılının ortalamasını hesaplayan kodları oluşturarak açıklayacağız.
1. XML Tasarımı
activity_main.xml
dosyasını aşağıdaki gibi düzenleyin. Bu tasarımda üç adet EditText
, bir buton ve bir sonuç göstermek için bir TextView
kullanacağız.
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 | <?xml version="1.0" encoding="utf-8"?> <LinearLayout 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:id="@+id/main" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity"> <!-- Birinci Not --> <EditText android:id="@+id/et_not1" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Birinci Notu Girin" android:inputType="numberDecimal" /> <!-- İkinci Not --> <EditText android:id="@+id/et_not2" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="İkinci Notu Girin" android:inputType="numberDecimal" /> <!-- Üçüncü Not --> <EditText android:id="@+id/et_not3" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Üçüncü Notu Girin" android:inputType="numberDecimal" /> <!-- Hesaplama Butonu --> <Button android:id="@+id/btn_hesapla" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Ortalama Hesapla" /> <!-- Sonuç --> <TextView android:id="@+id/tv_ortalama" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="16dp" android:text="Ortalama: " android:textSize="18sp" /> </LinearLayout> |

Açıklama:
EditText
: Kullanıcıdan notları almak için.Button
: Ortalama hesaplama işlemini başlatmak için.TextView
: Ortalama sonucunu göstermek için.
2. Java Kodları
MainActivity.java
dosyasını aşağıdaki gibi düzenleyin.
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 | package com.example.yaziliortalama; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import androidx.activity.EdgeToEdge; import androidx.appcompat.app.AppCompatActivity; import androidx.core.graphics.Insets; import androidx.core.view.ViewCompat; import androidx.core.view.WindowInsetsCompat; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); EdgeToEdge.enable(this); setContentView(R.layout.activity_main); ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> { Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars()); v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom); return insets; }); EditText etNot1 = findViewById(R.id.et_not1); EditText etNot2 = findViewById(R.id.et_not2); EditText etNot3 = findViewById(R.id.et_not3); Button btnHesapla = findViewById(R.id.btn_hesapla); TextView tvOrtalama = findViewById(R.id.tv_ortalama); btnHesapla.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { double not1 = Double.parseDouble(etNot1.getText().toString()); double not2 = Double.parseDouble(etNot2.getText().toString()); double not3 = Double.parseDouble(etNot3.getText().toString()); double ortalama = (not1 + not2 + not3) / 3; tvOrtalama.setText("Ortalama: " + ortalama); } }); } } |
Açıklama:
findViewById
: XML’de tanımlanan bileşenleri Java kodlarına bağlar.EditText.getText().toString()
: Kullanıcıdan alınan metin verilerini alır.Double.parseDouble()
: Metni ondalıklı sayıya çevirir.- Ortalama Hesaplama: Üç notun toplamını alır ve 3’e böler.
String.format("%.2f", average)
: Ortalama sonucunu virgülden sonra iki basamak gösterecek şekilde formatlar.setText
: SonucuTextView
üzerinde gösterir.
Çalışma Prensibi
- Kullanıcı üç farklı notu
EditText
alanlarına girer. - Ortalama Hesapla butonuna tıklanır.
- Kodlar metin kutusundaki değerleri alır, ondalık sayıya çevirir ve ortalama hesaplar.
- Ortalama sonucu
TextView
üzerinde gösterilir.
Sonuç:
