File: MainActivity.java
package com.example.darwin.calculator;
import android.content.Context;
import android.content.DialogInterface;
import android.content.res.AssetManager;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.math.BigDecimal;
public class MainActivity extends ActionBarActivity implements View.OnClickListener{
private Button add, sub, mul, div, tan, cos,sin, sqrt, save, recall, clear, file_read;
private TextView res;
private EditText num1, num2;
double saved_value;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
public void init(){
add = (Button) findViewById(R.id.add);
sub = (Button) findViewById(R.id.sub);
mul = (Button) findViewById(R.id.mul);
div = (Button) findViewById(R.id.div);
sin = (Button) findViewById(R.id.sin);
cos = (Button) findViewById(R.id.cos);
tan = (Button) findViewById(R.id.tan);
sqrt = (Button) findViewById(R.id.sqrt);
save = (Button) findViewById(R.id.save);
recall = (Button) findViewById(R.id.recall);
clear = (Button) findViewById(R.id.clear);
file_read = (Button) findViewById(R.id.fileread);
res = (TextView) findViewById(R.id.res);
num1 = (EditText) findViewById(R.id.num1);
num2 = (EditText) findViewById(R.id.num2);
saved_value = 0;
add.setOnClickListener(this);
sub.setOnClickListener(this);
mul.setOnClickListener(this);
div.setOnClickListener(this);
sin.setOnClickListener(this);
cos.setOnClickListener(this);
tan.setOnClickListener(this);
sqrt.setOnClickListener(this);
save.setOnClickListener(this);
recall.setOnClickListener(this);
clear.setOnClickListener(this);
file_read.setOnClickListener(this);
}
@Override
public void onClick(View v) {
String numb1 = num1.getText().toString();
String numb2 = num2.getText().toString();
switch(v.getId()){
case R.id.add:
double add_ans = Double.parseDouble(numb1) + Double.parseDouble(numb2);
res.setText(String.valueOf(add_ans));
break;
case R.id.sub:
double sub_ans = Double.parseDouble(numb1) - Double.parseDouble(numb2);
res.setText(String.valueOf(sub_ans));
break;
case R.id.mul:
double mul_ans = Double.parseDouble(numb1) * Double.parseDouble(numb2);
res.setText(String.valueOf(mul_ans));
break;
case R.id.div:
try {
BigDecimal n1 = new BigDecimal(Double.parseDouble(numb1));
BigDecimal n2 = new BigDecimal(Double.parseDouble(numb2));
BigDecimal div_ans = n1.divide(n2,5,BigDecimal.ROUND_HALF_UP);
res.setText(String.valueOf(div_ans));
}
catch(Exception e) {
res.setText("cannot divide by 0");
}
break;
case R.id.sin:
num2.setText(" ");
double sin_ans = Math.sin(Double.parseDouble(numb1));
res.setText(String.valueOf(sin_ans));
break;
case R.id.cos:
num2.setText(" ");
double cos_ans = Math.cos(Double.parseDouble(numb1));
res.setText(String.valueOf(cos_ans));
break;
case R.id.tan:
num2.setText(" ");
double tan_ans = Math.tan(Double.parseDouble(numb1));
res.setText(String.valueOf(tan_ans));
break;
case R.id.sqrt:
num2.setText(" ");
double sq_ans = Math.sqrt(Double.parseDouble(numb1));
res.setText(String.valueOf(sq_ans));
break;
case R.id.save:
String value = res.getText().toString();
saved_value = Double.parseDouble(value);
num1.setText(" ");
num2.setText(" ");
res.setText("Result");
break;
case R.id.recall:
num1.setText(String.valueOf(saved_value));
break;
case R.id.clear:
num1.setText(" ");
num2.setText(" ");
res.setText("Result");
break;
case R.id.fileread:
try {
Context context=this;
AssetManager am =context.getAssets();
InputStream is = am.open("file.txt");
BufferedReader buf = new BufferedReader(new InputStreamReader(is));
String n1=buf.readLine();
String n2=buf.readLine();
num1.setText(n1);
num2.setText(n2);
} catch(Exception e)
{
e.printStackTrace();
}
break;
}
}
}
File: activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<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="com.example.darwin.calculator.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Enter Numbers"
android:id="@+id/textView" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="@+id/num1"
android:layout_below="@+id/textView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="@+id/num2"
android:layout_below="@+id/num1"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Result"
android:id="@+id/res"
android:layout_below="@+id/num2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="63dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ADD"
android:id="@+id/add"
android:layout_centerVertical="true"
android:layout_alignLeft="@+id/mul"
android:layout_alignStart="@+id/mul" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SUB"
android:id="@+id/sub"
android:layout_above="@+id/mul"
android:layout_alignRight="@+id/num2"
android:layout_alignEnd="@+id/num2" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="MUL"
android:id="@+id/mul"
android:layout_below="@+id/add"
android:layout_alignLeft="@+id/sin"
android:layout_alignStart="@+id/sin" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="DIV"
android:id="@+id/div"
android:layout_below="@+id/sub"
android:layout_alignRight="@+id/sub"
android:layout_alignEnd="@+id/sub" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SIN"
android:id="@+id/sin"
android:layout_below="@+id/mul"
android:layout_alignRight="@+id/textView"
android:layout_alignEnd="@+id/textView" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="COS"
android:id="@+id/cos"
android:layout_above="@+id/tan"
android:layout_alignLeft="@+id/div"
android:layout_alignStart="@+id/div" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TAN"
android:id="@+id/tan"
android:layout_below="@+id/sin"
android:layout_alignLeft="@+id/sin"
android:layout_alignStart="@+id/sin" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SQRT"
android:id="@+id/sqrt"
android:layout_below="@+id/cos"
android:layout_alignLeft="@+id/cos"
android:layout_alignStart="@+id/cos" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SAVE"
android:id="@+id/save"
android:layout_alignBottom="@+id/res"
android:layout_toRightOf="@+id/sqrt"
android:layout_toEndOf="@+id/sqrt" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RECALL"
android:id="@+id/recall"
android:layout_below="@+id/save"
android:layout_alignLeft="@+id/save"
android:layout_alignStart="@+id/save" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CLEAR"
android:id="@+id/clear"
android:layout_alignParentBottom="true"
android:layout_alignRight="@+id/recall"
android:layout_alignEnd="@+id/recall"
android:layout_toRightOf="@+id/sqrt"
android:layout_toEndOf="@+id/sqrt" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="READ FILE"
android:id="@+id/fileread"
android:layout_above="@+id/clear"
android:layout_alignRight="@+id/clear"
android:layout_alignEnd="@+id/clear"
android:layout_alignLeft="@+id/clear"
android:layout_alignStart="@+id/clear" />
</RelativeLayout>
File: file.txt
Path: /app/build/intermediates/exploded-aar/com.android.support/appcompat-v7/21.0.3/assets
4
5
package com.example.darwin.calculator;
import android.content.Context;
import android.content.DialogInterface;
import android.content.res.AssetManager;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.math.BigDecimal;
public class MainActivity extends ActionBarActivity implements View.OnClickListener{
private Button add, sub, mul, div, tan, cos,sin, sqrt, save, recall, clear, file_read;
private TextView res;
private EditText num1, num2;
double saved_value;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
public void init(){
add = (Button) findViewById(R.id.add);
sub = (Button) findViewById(R.id.sub);
mul = (Button) findViewById(R.id.mul);
div = (Button) findViewById(R.id.div);
sin = (Button) findViewById(R.id.sin);
cos = (Button) findViewById(R.id.cos);
tan = (Button) findViewById(R.id.tan);
sqrt = (Button) findViewById(R.id.sqrt);
save = (Button) findViewById(R.id.save);
recall = (Button) findViewById(R.id.recall);
clear = (Button) findViewById(R.id.clear);
file_read = (Button) findViewById(R.id.fileread);
res = (TextView) findViewById(R.id.res);
num1 = (EditText) findViewById(R.id.num1);
num2 = (EditText) findViewById(R.id.num2);
saved_value = 0;
add.setOnClickListener(this);
sub.setOnClickListener(this);
mul.setOnClickListener(this);
div.setOnClickListener(this);
sin.setOnClickListener(this);
cos.setOnClickListener(this);
tan.setOnClickListener(this);
sqrt.setOnClickListener(this);
save.setOnClickListener(this);
recall.setOnClickListener(this);
clear.setOnClickListener(this);
file_read.setOnClickListener(this);
}
@Override
public void onClick(View v) {
String numb1 = num1.getText().toString();
String numb2 = num2.getText().toString();
switch(v.getId()){
case R.id.add:
double add_ans = Double.parseDouble(numb1) + Double.parseDouble(numb2);
res.setText(String.valueOf(add_ans));
break;
case R.id.sub:
double sub_ans = Double.parseDouble(numb1) - Double.parseDouble(numb2);
res.setText(String.valueOf(sub_ans));
break;
case R.id.mul:
double mul_ans = Double.parseDouble(numb1) * Double.parseDouble(numb2);
res.setText(String.valueOf(mul_ans));
break;
case R.id.div:
try {
BigDecimal n1 = new BigDecimal(Double.parseDouble(numb1));
BigDecimal n2 = new BigDecimal(Double.parseDouble(numb2));
BigDecimal div_ans = n1.divide(n2,5,BigDecimal.ROUND_HALF_UP);
res.setText(String.valueOf(div_ans));
}
catch(Exception e) {
res.setText("cannot divide by 0");
}
break;
case R.id.sin:
num2.setText(" ");
double sin_ans = Math.sin(Double.parseDouble(numb1));
res.setText(String.valueOf(sin_ans));
break;
case R.id.cos:
num2.setText(" ");
double cos_ans = Math.cos(Double.parseDouble(numb1));
res.setText(String.valueOf(cos_ans));
break;
case R.id.tan:
num2.setText(" ");
double tan_ans = Math.tan(Double.parseDouble(numb1));
res.setText(String.valueOf(tan_ans));
break;
case R.id.sqrt:
num2.setText(" ");
double sq_ans = Math.sqrt(Double.parseDouble(numb1));
res.setText(String.valueOf(sq_ans));
break;
case R.id.save:
String value = res.getText().toString();
saved_value = Double.parseDouble(value);
num1.setText(" ");
num2.setText(" ");
res.setText("Result");
break;
case R.id.recall:
num1.setText(String.valueOf(saved_value));
break;
case R.id.clear:
num1.setText(" ");
num2.setText(" ");
res.setText("Result");
break;
case R.id.fileread:
try {
Context context=this;
AssetManager am =context.getAssets();
InputStream is = am.open("file.txt");
BufferedReader buf = new BufferedReader(new InputStreamReader(is));
String n1=buf.readLine();
String n2=buf.readLine();
num1.setText(n1);
num2.setText(n2);
} catch(Exception e)
{
e.printStackTrace();
}
break;
}
}
}
File: activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<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="com.example.darwin.calculator.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Enter Numbers"
android:id="@+id/textView" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="@+id/num1"
android:layout_below="@+id/textView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="@+id/num2"
android:layout_below="@+id/num1"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Result"
android:id="@+id/res"
android:layout_below="@+id/num2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="63dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ADD"
android:id="@+id/add"
android:layout_centerVertical="true"
android:layout_alignLeft="@+id/mul"
android:layout_alignStart="@+id/mul" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SUB"
android:id="@+id/sub"
android:layout_above="@+id/mul"
android:layout_alignRight="@+id/num2"
android:layout_alignEnd="@+id/num2" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="MUL"
android:id="@+id/mul"
android:layout_below="@+id/add"
android:layout_alignLeft="@+id/sin"
android:layout_alignStart="@+id/sin" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="DIV"
android:id="@+id/div"
android:layout_below="@+id/sub"
android:layout_alignRight="@+id/sub"
android:layout_alignEnd="@+id/sub" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SIN"
android:id="@+id/sin"
android:layout_below="@+id/mul"
android:layout_alignRight="@+id/textView"
android:layout_alignEnd="@+id/textView" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="COS"
android:id="@+id/cos"
android:layout_above="@+id/tan"
android:layout_alignLeft="@+id/div"
android:layout_alignStart="@+id/div" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TAN"
android:id="@+id/tan"
android:layout_below="@+id/sin"
android:layout_alignLeft="@+id/sin"
android:layout_alignStart="@+id/sin" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SQRT"
android:id="@+id/sqrt"
android:layout_below="@+id/cos"
android:layout_alignLeft="@+id/cos"
android:layout_alignStart="@+id/cos" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SAVE"
android:id="@+id/save"
android:layout_alignBottom="@+id/res"
android:layout_toRightOf="@+id/sqrt"
android:layout_toEndOf="@+id/sqrt" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RECALL"
android:id="@+id/recall"
android:layout_below="@+id/save"
android:layout_alignLeft="@+id/save"
android:layout_alignStart="@+id/save" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CLEAR"
android:id="@+id/clear"
android:layout_alignParentBottom="true"
android:layout_alignRight="@+id/recall"
android:layout_alignEnd="@+id/recall"
android:layout_toRightOf="@+id/sqrt"
android:layout_toEndOf="@+id/sqrt" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="READ FILE"
android:id="@+id/fileread"
android:layout_above="@+id/clear"
android:layout_alignRight="@+id/clear"
android:layout_alignEnd="@+id/clear"
android:layout_alignLeft="@+id/clear"
android:layout_alignStart="@+id/clear" />
</RelativeLayout>
File: file.txt
Path: /app/build/intermediates/exploded-aar/com.android.support/appcompat-v7/21.0.3/assets
4
5
No comments:
Post a Comment