Mainactivity Java

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8

Name :amad Hussain

Fa18-bse-016

MainActivity.java :
import android.view.Menu;
import android.view.MenuItem;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import java.text.DecimalFormat;
import android.graphics.Typeface;

public boolean containsBracket(String expression) {


return expression.contains("(");
}

public ArrayList<String> split(String equation) {


String expression = "";
ArrayList<String> splitedEquation = new ArrayList();
boolean isInsideBracket = false;

for (int i = 0; i < equation.length(); i++) {


String chr = Character.toString(equation.charAt(i));

if ((chr.equals("+") || chr.equals("-")) && i != 0 && !isInsideBracket) {


splitedEquation.add(expression);
expression = chr;
} else {
expression +=chr;
}
if (chr.equals("(")) {
isInsideBracket = true;
} else if (chr.equals(")")) {
isInsideBracket = false;
}
}
splitedEquation.add(expression);

return splitedEquation;
}

public static int getCoefficient(String variable){


String coefficient = "";
if(variable.length() == 1) return 1;
else if(variable.length() == 2 && variable.charAt(0) == '-') return -1;

for(int i = 0; i < variable.length(); i++){


if(Character.isDigit(variable.charAt(i)))coefficient+=variable.charAt(i);
}
if(variable.charAt(0) == '-')return Integer.parseInt("-" + coefficient);
return Integer.parseInt(coefficient);
}

strings.xml:
<resources>
<string name="app_name">LinearEquation </string>
<string name="equation_hint">Enter values of a b and c </string>
<string name="accepted_values">123 </string>
</resources>

Main.xml:

android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent"
tools:context=".MainActivity">

<EditText
android:id="@+id/equation"
android:layout_marginTop="20dp"
android:layout_height="52dp"
android:layout_width="fill_parent"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:background="@drawable/rectangular_input"
android:hint="@string/equation_hint"

android:textSize="17sp"

android:digits="@string/accepted_values"
android:inputType="text"
android:paddingLeft="10dp" />

<LinearLayout
android:id="@+id/wrapper"
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
</LinearLayout>

<TextView
android:id="@+id/error"
android:gravity="center"
android:textSize="20dp"
android:textColor="#000"
android:textStyle="bold"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
/>
</LinearLayout>

RootResultActivity.java:

public void displaySteps(String solution, String step, String content) {


int textColor = Integer.parseInt("000000", 16)+0xFF000000;

LinearLayout wrapper = findViewById(R.id.wrapper);


LinearLayout linearLayout = new LinearLayout(this);
TextView stepTextView = new TextView(this);
TextView stepContentTextView = new TextView(this);
TextView solutionTextView = new TextView(this);

LinearLayout.LayoutParams linearLayouParams = new LinearLayout


.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
LinearLayout.LayoutParams textParams = new LinearLayout
.LayoutParams( ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);

LinearLayout.LayoutParams solutionTextParams = new LinearLayout


.LayoutParams( ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
solutionTextParams.setMargins(40, 18, 0, 0);
linearLayouParams.setMargins(40, 20, 0, 0);

stepTextView.setLayoutParams(textParams);
stepTextView.setText(step);

stepContentTextView.setLayoutParams(textParams);
stepContentTextView.setText(content);
stepContentTextView.setTextSize(17);
stepContentTextView.setTextColor(textColor);

stepTextView.setLayoutParams(textParams);
stepTextView.setText(step);
stepTextView.setTextSize(17);
stepTextView.setTextColor(textColor);
stepTextView.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));

solutionTextView.setLayoutParams(solutionTextParams);
solutionTextView.setText(solution);
solutionTextView.setTextSize(17);
solutionTextView.setTypeface(Typeface.defaultFromStyle(Typeface.ITALIC));
solutionTextView.setTextColor(textColor);

linearLayout.setLayoutParams(linearLayouParams);
linearLayout.setOrientation(LinearLayout.HORIZONTAL);
linearLayout.addView(stepTextView);
linearLayout.addView(stepContentTextView);
wrapper.addView(linearLayout);
wrapper.addView(solutionTextView);

Activity_Root_Result.xml

public boolean isValidEquation(){


String sign = ""; int ovar = 0;
String num = "";
TextView textView = findViewById(R.id.error);

if(equation.length() == 0){
textView.setText("You did not input anything!");
return false;
}

int numOfEqualitySign = 0;
for(int j = 0; j < equation.length(); j++){
if (equation.charAt(j) == '=') {
numOfEqualitySign +=1;
numOfEqualitySign +=1;
}

if(equation.charAt(equation.length()-1) == '='){
textView.setText("Invalid equation! You did not input anything
return false;

for(int k = 0; k < equation.length(); k++) {


if((equation.charAt(0) == '+' && equation.charAt(1) == '=') || (equation.charAt(0) == '-' &&
equation.charAt(1) == '=')){
System.out.println("Invalid equation! It is wrong to have only " + equation.charAt(0) + " in the
left hand side of the equation");
return true;
}
if (equation.charAt(k) == '-' || equation.charAt(k) == '+'){
sign = "" + equation.charAt(k);
if(k == equation.length() -1){
System.out.println("Invalid equation! You can not end an equation with " + sign);
return true;
}
else if(equation.charAt(k+1) == '+' || equation.charAt(k+1) == '-'){

textView.setText("Invalid equation! Two or more signs(e.g +, -) can not be together ");


return true;
}
}
if(Character.isLetter(equation.charAt(k))){
if(k != equation.length() -1){

if(Character.isLetter(equation.charAt(k+1))){
textView.setText("Invalid equation! Two or more variables can not be together. ");
return false;
}
}
}
if(k != equation.length() -1){
if(Character.isLetter(equation.charAt(k)) && Character.isDigit(equation.charAt(k+1)))
{
textView.setText("Invalid equation! You are expected to input either \"+\" or \"-\" sign after
" + equation.charAt(k) + " but you input " + equation.charAt(k+1) + " which is a number");
return false;
}
}
if(equation.charAt(k) == '(' || equation.charAt(k) == ')'){
sign = "" + equation.charAt(k);
if(k != equation.length() -1){
if(equation.charAt(k+1) == '(' || equation.charAt(k+1) == ')'){
textView.setText("Invalid equation! You cant have two or more " + sign + " together");
return false;
}
}
}
}

for(int m = 0; m < equation.length(); m++) {


if(equation.charAt(m) == '(' )ovar += 1;
else if(equation.charAt(m) == ')')ovar -= 1;
if(ovar > 1){
textView.setText("Invalid equation! You are expected to put a close bracket \")\" after " +
equation.charAt(m-1) + " not an open bracket \"(\" ");
return false;
}
else if (ovar < 0){
textView.setText("Invalid equation! You are expected to put a open bracket \"(\" after " +
equation.charAt(m-1) + " not a close bracket \") \" ");
return false;
}

}
if(ovar == 1){
textView.setText("Invalid equation! You didnt close a bracket you opened.");
return false;
}
String hold = "";

for(int i = 0; i < equation.length(); i++){


if(equation.charAt(i) == '('){
hold = "";
num.
int j = i-1;

int k = i+1;
if(j>= 0){
while(Character.isDigit(equation.charAt(j)) || Character.isLetter(equation.charAt(j))){
num = num + equation.charAt(j);
j -=1;
if (j < 0) break;
}
}

while (equation.charAt(k) != ')'){


hold = hold + equation.charAt(k);
k +=1;
}
for(int q = 0; q < num.length(); q++){
if(Character.isLetter(num.charAt(q))){
textView.setText("Invalid equation! You are not allowed to open a bracket with a
variable");
return false;
}
}
for(int q = 0; q < hold.length(); q++){
if(hold.charAt(q) == '='){
textView.setText("Invalid equation! It does not make sense to have an equality sign inside a
bracket");
return false;
}
if(hold.length() == 1 ){
if(hold.charAt(q) == '+' || hold.charAt(q) == '-'){
// TextView textView = findViewById(R.id.);
textView.setText("Invalid equation! It is wrong to have only " + hold.charAt(q) + " in a
bracket");
return false;
}
}
}
num = "";
}
}
return true;
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy