Version 1.3.1
- added rounded multiplication and division with remainders.
This commit is contained in:
parent
1bfae42460
commit
a7da565992
Binary file not shown.
|
@ -2,14 +2,10 @@ package com.company;
|
||||||
|
|
||||||
|
|
||||||
public class Calc {
|
public class Calc {
|
||||||
//constructor of num variables
|
public static void print(String text) { //this is a small function to makes it easier to print out to terminal
|
||||||
int num1;
|
System.out.println(text); //similar to python output
|
||||||
int num2;
|
|
||||||
|
|
||||||
//math functions
|
|
||||||
public static void print(String text){
|
|
||||||
System.out.println(text);
|
|
||||||
}
|
}
|
||||||
|
//math functions
|
||||||
public static double add(String num1, String num2){
|
public static double add(String num1, String num2){
|
||||||
double add1 = Double.parseDouble(num1);
|
double add1 = Double.parseDouble(num1);
|
||||||
double add2 = Double.parseDouble(num2);
|
double add2 = Double.parseDouble(num2);
|
||||||
|
@ -25,6 +21,7 @@ public class Calc {
|
||||||
double mul2 = Double.parseDouble(num2);
|
double mul2 = Double.parseDouble(num2);
|
||||||
return mul1 * mul2;
|
return mul1 * mul2;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static double div(String num1, String num2){
|
public static double div(String num1, String num2){
|
||||||
double div1 = Double.parseDouble(num1);
|
double div1 = Double.parseDouble(num1);
|
||||||
double div2 = Double.parseDouble(num2);
|
double div2 = Double.parseDouble(num2);
|
||||||
|
@ -35,5 +32,27 @@ public class Calc {
|
||||||
}
|
}
|
||||||
return div1 / div2;
|
return div1 / div2;
|
||||||
}
|
}
|
||||||
|
public static String mulRound(String num1, String num2){
|
||||||
|
double mul1 = Double.parseDouble(num1);
|
||||||
|
double mul2 = Double.parseDouble(num2);
|
||||||
|
return Math.round(mul1 * mul2) + "rounded";
|
||||||
|
}
|
||||||
|
public static String divRemain(String num1, String num2){
|
||||||
|
double div1 = Double.parseDouble(num1);
|
||||||
|
double div2 = Double.parseDouble(num2);
|
||||||
|
if (div2 == 0){
|
||||||
|
print("cant divide by zero");
|
||||||
|
print("error code:");
|
||||||
|
return "-1";
|
||||||
|
}
|
||||||
|
else if (div1 == 0)
|
||||||
|
return "0";
|
||||||
|
else {
|
||||||
|
int div1I = (int) div1;
|
||||||
|
int div2I = (int) div2;
|
||||||
|
int divT = div1I / div2I;
|
||||||
|
return divT + " with remainder of: " + div1 % div2;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -9,7 +9,6 @@ public class Main {
|
||||||
String num1;
|
String num1;
|
||||||
String num2;
|
String num2;
|
||||||
while (true) {
|
while (true) {
|
||||||
|
|
||||||
Scanner in = new Scanner(System.in);
|
Scanner in = new Scanner(System.in);
|
||||||
System.out.println("Enter first number: ");
|
System.out.println("Enter first number: ");
|
||||||
num1 = in.nextLine();
|
num1 = in.nextLine();
|
||||||
|
@ -24,6 +23,8 @@ public class Main {
|
||||||
System.out.println("Inputs complete");
|
System.out.println("Inputs complete");
|
||||||
System.out.println("Please input operator");
|
System.out.println("Please input operator");
|
||||||
System.out.println("+ , - , * or x , /");
|
System.out.println("+ , - , * or x , /");
|
||||||
|
System.out.println("'for rounded multiplication: use '*r', 'Xr' or 'xr'");
|
||||||
|
System.out.println("'for remainder division: use '/R' or '/r'");
|
||||||
String op = in.nextLine();
|
String op = in.nextLine();
|
||||||
|
|
||||||
switch (op) {
|
switch (op) {
|
||||||
|
@ -43,6 +44,8 @@ public class Main {
|
||||||
total = Calc.div(num1, num2);
|
total = Calc.div(num1, num2);
|
||||||
System.out.println(total);
|
System.out.println(total);
|
||||||
}
|
}
|
||||||
|
case "*/", "xR", "xr" -> System.out.println(Calc.mulRound(num1, num2));
|
||||||
|
case "/R", "/r" -> System.out.println(Calc.divRemain(num1, num2));
|
||||||
default -> System.out.println("Unexpected value: '" + op + "' ...restarting program");
|
default -> System.out.println("Unexpected value: '" + op + "' ...restarting program");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue