How it works?
- Input prelim, midterm, prefi and final grades. (The input type will be set to number. It means character is not allowed.)
- Click the button Calculate. (The calculation will be done using this formula avg=((p*.2)+(m*.2)+(pf*.2)+(f*.4))
- The calculated output displayed using toast.
- TextView (Prelim, Midterm, Prefinal nd Final)
- EditText (Prelim, Midterm, Prefinal nd Final) for input
- Button (Calculate)
- Toast (Output)
- Accepted values 65 to 100
- Empty field is not accepted
Hint: declare double variables for d_pre, d_mid, d_prefi, d_final, ave
if (pre.getText().toString().trim().equals("")){
Display_Message("Prelim");
}else if (mid.getText().toString().trim().equals("")){
Display_Message("Midterm");
}else if (prefi.getText().toString().trim().equals("")){
Display_Message("Prefi");
}else if (fin.getText().toString().trim().equals("")){
Display_Message("Final");
}else {
//Code above validation for empty fields
//Declare also global double variables for p_pre, d_mid, d_prefi and d_final
d_pre=Double.parseDouble(pre.getText().toString());
d_mid=Double.parseDouble(mid.getText().toString());
d_prefi=Double.parseDouble(prefi.getText().toString());
d_final=Double.parseDouble(fin.getText().toString());
//validation for the inputted value.
}if((d_pre < 65) || (d_pre > 100)) {
Display_Message("accepted value 65 - 100");}
else if((d_mid < 65) || (d_mid > 100)) {
Display_Message("accepted value 65 - 100");}
else if((d_prefi < 65) || (d_prefi > 100)) {
Display_Message("accepted value 65 - 100");}
else if((d_final < 65) || (d_final > 100)) {
Display_Message("accepted value 65 - 100");}
else {
ave = (d_pre * .2) + d_mid * .2) + (d_prefi * .2) + (d_final * .4);
Display_Output();
//Code above validation for input value (65 - 100) and computation for GWA
}
public void Display_Output() {
Toast toast = Toast.makeText(this, "Your average is :" + ave, Toast.LENGTH_LONG);
toast.show();
}
public void Display_Message(String p) {
Toast.makeText(this, "Invalid input" + p ".", Toast.LENGTH_LONG).show();
}
//Code above user define methods
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/s_prelim"
android:textSize="20dp"
android:gravity="center"/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="20dp"
android:gravity="center"
android:inputType="number"
android:id="@+id/etPrelim" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/s_midterm"
android:textSize="20dp"
android:gravity="center"/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="20dp"
android:gravity="center"
android:inputType="number"
android:id="@+id/etMidterm"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/s_prefi"
android:textSize="20dp"
android:gravity="center"/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="20dp"
android:gravity="center"
android:inputType="number"
android:id="@+id/etPrefi"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/s_final"
android:textSize="20dp"
android:gravity="center"/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="20dp"
android:gravity="center"
android:inputType="number"
android:id="@+id/etFinal" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="20dp"
android:gravity="center"
android:text="@string/s_cal"
android:id="@+id/btnCal" />
</LinearLayout>
</ScrollView>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="s_prelim">Prelim</string>
<string name="s_midterm">Midterm</string>
<string name="s_prefi">Prefi</string>
<string name="s_final">Final</string>
<string name="s_cal">Calculate</string>
<string name="app_name">Activity2Android</string>
</resources>
Programming Language: Java + XML
Programming IDE: Eclipse