Simple Math Stuff

From D3xt3r01.tk
Jump to navigationJump to search

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="tk.d3xt3r01.medie"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="9"
        android:targetSdkVersion="19" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:windowSoftInputMode="stateVisible" 
            android:name="tk.d3xt3r01.medie.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="calculeaza">Calculeaza</string>
    <string name="teza">Teza</string>
    <string name="note">Note</string>
    <string name="app_name">Medie</string>
    <string name="reset">Reset</string>
    <string name="suma" />
    <string name="mediaaritmetica" />
    <string name="mediaponderata" />
</resources>

main.xml

<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" >

    <Button
        android:id="@+id/Button01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/calc"
        android:layout_alignBottom="@+id/calc"
        android:layout_alignRight="@+id/input_teza"
        android:layout_toRightOf="@+id/calc"
        android:onClick="reset"
        android:text="@string/reset" />

    <Button
        android:id="@+id/calc"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/input_teza"
        android:layout_toRightOf="@+id/teza"
        android:onClick="calculeaza"
        android:text="@string/calculeaza" />

    <EditText
        android:id="@+id/input_note"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_toRightOf="@+id/teza"
        android:digits="1234567890 "
        android:ems="10"
        android:inputType="number|text"
        android:singleLine="true" >
        <requestFocus />
    </EditText>

    <TextView
        android:id="@+id/note"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/input_note"
        android:layout_alignBottom="@+id/input_note"
        android:layout_alignParentLeft="true"
        android:text="@string/note"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <EditText
        android:id="@+id/input_teza"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/input_note"
        android:layout_below="@+id/input_note"
        android:digits="1234567890 "
        android:ems="10"
        android:inputType="number|text"
        android:maxLength="2"
        android:singleLine="true" />

    <TextView
        android:id="@+id/teza"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/input_teza"
        android:layout_alignBottom="@+id/input_teza"
        android:layout_alignLeft="@+id/note"
        android:text="@string/teza"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/suma"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/teza"
        android:layout_below="@+id/calc"
        android:text="@string/suma"
        android:textAppearance="?android:attr/textAppearanceSmall" />

    <TextView
        android:id="@+id/mediaaritmetica"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/suma"
        android:layout_below="@+id/suma"
        android:text="@string/mediaaritmetica"
        android:textAppearance="?android:attr/textAppearanceSmall" />

    <TextView
        android:id="@+id/mediaponderata"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/mediaaritmetica"
        android:layout_below="@+id/mediaaritmetica"
        android:text="@string/mediaponderata"
        android:textAppearance="?android:attr/textAppearanceSmall" />
    
</RelativeLayout>

main.java

package tk.d3xt3r01.medie;

import java.math.RoundingMode;
import java.text.DecimalFormat;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;
import android.app.Activity;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

    }
    public void reset(View view){
        TextView input_note = (TextView) findViewById(R.id.input_note);
        input_note.setText("");
        TextView input_teza = (TextView) findViewById(R.id.input_teza);
        input_teza.setText("");
        TextView mediaaritmetica = (TextView) findViewById(R.id.mediaaritmetica);
        mediaaritmetica.setText("");
        TextView mediaponderata = (TextView) findViewById(R.id.mediaponderata);
        mediaponderata.setText("");
        TextView suma = (TextView) findViewById(R.id.suma);
        suma.setText("");
        input_note.requestFocus();
    }
    public String implode(String glue, String[] strArray){
        String ret = "";
        for(int i=0;i<strArray.length;i++)
        {
            ret += (i == strArray.length - 1) ? strArray[i] : strArray[i] + glue;
        }
        return ret;
    }
    public void calculeaza(View view){
        TextView input_note = (TextView) findViewById(R.id.input_note);
        TextView input_teza = (TextView) findViewById(R.id.input_teza);
        TextView media_ponderata = (TextView) findViewById(R.id.mediaponderata);
        TextView media_aritmetica = (TextView) findViewById(R.id.mediaaritmetica);
        TextView text_suma = (TextView) findViewById(R.id.suma);
        DecimalFormat df = new DecimalFormat("##.##");
        df.setRoundingMode(RoundingMode.DOWN);
        double sum = 0.0;
        double num_note = 0.0;
        double m = 0.0;
        double mp = 0.0;
        double teza = 0.0;
        String[] note;
        try{
            note = input_note.getText().toString().split("\\s+");
            num_note = note.length;
            double num = 0;
            for(int i=0;i<num_note;i++){
                num = Integer.parseInt(note[i]);
                sum = sum + num;
            }
            Log.i("calculeaza", "sum: "+sum);
            Log.i("calculeaza", "num_note: "+num_note);
            m = sum/num_note;
            m = Double.parseDouble(df.format(m).toString());
            Log.i("calculeaza", "m: "+m);
            mp = m*3.0;
            Log.i("calculeaza", "mp = m*3: "+df.format(mp).toString());
        }catch(Exception e){
            media_ponderata.setText("N-am note ...");
            return;
        }
        try{
            teza = Double.parseDouble(input_teza.getText().toString());
            mp = mp + teza;
            Log.i("calculeaza", "mp+teza: "+df.format(mp).toString());
            mp = mp/4.0;
            Log.i("calculeaza", "mp/4: "+df.format(mp).toString());
        }catch(Exception e){
            media_ponderata.setText("N-am teza ...");
            return;
        }
        Double mpf = (((sum/num_note)*3.0)+teza)/4.0;
        text_suma.setText((int)num_note+" note: "+implode("+", note)+"="+(int)sum);
        media_aritmetica.setText("Media aritmetica: "+(int)sum+"/"+(int)num_note+"="+df.format(m));
        media_ponderata.setText("(("+df.format(m)+"*3)+"+Integer.parseInt(input_teza.getText().toString())+")/4="+df.format(mp)+" (sau full: "+df.format(mpf)+")");
    }
}