Skip to content

Commit ebc03b2

Browse files
committed
Handle errors: division by 0, tan, logarithms, square root
1 parent b6c058a commit ebc03b2

File tree

3 files changed

+62
-11
lines changed

3 files changed

+62
-11
lines changed

app/src/main/java/com/example/kalkulator/activities/AdvancedCalculatorActivity.java

Lines changed: 55 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import android.os.Vibrator;
66
import android.view.View;
77
import android.widget.Button;
8+
import android.widget.Toast;
89
import androidx.appcompat.app.AppCompatActivity;
910
import com.example.kalkulator.R;
1011
import com.example.kalkulator.listeners.DigitOnClickListener;
@@ -128,7 +129,11 @@ public void percentOnClick(View v)
128129

129130
String formattedOutput = DECIMAL_FORMAT.format(result).replace('.', ',');
130131

131-
if (!isOutputTooLong(formattedOutput))
132+
if (formattedOutput.equals(INFINITY_SYMBOL))
133+
{
134+
Toast.makeText(this, "Cannot divide by 0", Toast.LENGTH_SHORT).show();
135+
}
136+
else if (!isOutputTooLong(formattedOutput))
132137
{
133138
prevValueTextView.setText("");
134139
operationTextView.setText("");
@@ -147,9 +152,16 @@ private void logOnClick(View view)
147152

148153
Double currValue = Double.parseDouble(valueText.replace(',', '.'));
149154

150-
currValue = Math.log10(currValue);
155+
if (currValue > 0.0D)
156+
{
157+
currValue = Math.log10(currValue);
151158

152-
valueTextView.setText(DECIMAL_FORMAT.format(currValue).replace('.', ','));
159+
valueTextView.setText(DECIMAL_FORMAT.format(currValue).replace('.', ','));
160+
}
161+
else
162+
{
163+
Toast.makeText(this, "The number must not be negative", Toast.LENGTH_SHORT).show();
164+
}
153165

154166
makeStandardVibration();
155167
}
@@ -160,9 +172,16 @@ private void sqrtOnClick(View view)
160172

161173
Double currValue = Double.parseDouble(valueText.replace(',', '.'));
162174

163-
currValue = Math.sqrt(currValue);
175+
if (currValue >= 0.0D)
176+
{
177+
currValue = Math.sqrt(currValue);
164178

165-
valueTextView.setText(DECIMAL_FORMAT.format(currValue).replace('.', ','));
179+
valueTextView.setText(DECIMAL_FORMAT.format(currValue).replace('.', ','));
180+
}
181+
else
182+
{
183+
Toast.makeText(this, "The number must not be negative", Toast.LENGTH_SHORT).show();
184+
}
166185

167186
makeStandardVibration();
168187
}
@@ -173,9 +192,16 @@ private void lnOnClick(View view)
173192

174193
Double currValue = Double.parseDouble(valueText.replace(',', '.'));
175194

176-
currValue = Math.log(currValue);
195+
if (currValue > 0.0D)
196+
{
197+
currValue = Math.log(currValue);
177198

178-
valueTextView.setText(DECIMAL_FORMAT.format(currValue).replace('.', ','));
199+
valueTextView.setText(DECIMAL_FORMAT.format(currValue).replace('.', ','));
200+
}
201+
else
202+
{
203+
Toast.makeText(this, "The number must not be negative", Toast.LENGTH_SHORT).show();
204+
}
179205

180206
makeStandardVibration();
181207
}
@@ -211,10 +237,25 @@ private void tanOnClick(View view)
211237
String valueText = valueTextView.getText().toString();
212238

213239
Double currValue = Double.parseDouble(valueText.replace(',', '.'));
240+
double delta = 1E-3;
214241

215-
currValue = Math.tan(currValue);
242+
// find k
243+
int k = (int) Math.round((currValue - Math.PI / 2) / Math.PI);
216244

217-
valueTextView.setText(DECIMAL_FORMAT.format(currValue).replace('.', ','));
245+
// calculate the difference
246+
double diff = Math.abs(currValue - (Math.PI / 2 + k * Math.PI));
247+
248+
// check if the input is very close to PI/2 + kPI
249+
if (diff < delta)
250+
{
251+
Toast.makeText(this, "The input is very close or equal to PI/2 + kPI, so the result does not exist", Toast.LENGTH_LONG).show();
252+
}
253+
else
254+
{
255+
currValue = Math.tan(currValue);
256+
257+
valueTextView.setText(DECIMAL_FORMAT.format(currValue).replace('.', ','));
258+
}
218259

219260
makeStandardVibration();
220261
}
@@ -292,7 +333,11 @@ public void equalsOnClick(View v)
292333

293334
String formattedOutput = DECIMAL_FORMAT.format(result).replace('.', ',');
294335

295-
if (!isOutputTooLong(formattedOutput))
336+
if (formattedOutput.equals(INFINITY_SYMBOL))
337+
{
338+
Toast.makeText(this, "Cannot divide by 0", Toast.LENGTH_SHORT).show();
339+
}
340+
else if (!isOutputTooLong(formattedOutput))
296341
{
297342
prevValueTextView.setText("");
298343
operationTextView.setText("");

app/src/main/java/com/example/kalkulator/activities/SimpleCalculatorActivity.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import android.os.Vibrator;
66
import android.view.View;
77
import android.widget.Button;
8+
import android.widget.Toast;
89
import androidx.appcompat.app.AppCompatActivity;
910
import com.example.kalkulator.R;
1011
import com.example.kalkulator.listeners.DigitOnClickListener;
@@ -148,7 +149,11 @@ public void equalsOnClick(View v)
148149

149150
String formattedOutput = DECIMAL_FORMAT.format(result).replace('.', ',');
150151

151-
if (!isOutputTooLong(formattedOutput))
152+
if (formattedOutput.equals(INFINITY_SYMBOL))
153+
{
154+
Toast.makeText(this, "Cannot divide by 0", Toast.LENGTH_SHORT).show();
155+
}
156+
else if (!isOutputTooLong(formattedOutput))
152157
{
153158
prevValueTextView.setText("");
154159
operationTextView.setText("");

app/src/main/java/com/example/kalkulator/utils/CalculatorHandler.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public class CalculatorHandler
1515
public static final char CHAR_MULTIPLY = '⨯';
1616
public static final char CHAR_DIVIDE = '÷';
1717
public static final char CHAR_POWER = '^';
18+
public static final String INFINITY_SYMBOL = "\u221E"; // infinity symbol (dividing by zero)
1819
public static final int VALUE_TEXT_VIEW_MAX_SIZE = 10;
1920
public static final int OUTPUT_MAX_SIZE = 20;
2021
public static final DecimalFormat DECIMAL_FORMAT = new DecimalFormat("#.##########");

0 commit comments

Comments
 (0)