Create a Math class that has the following methods public in
Create a Math class that has the following methods:
public int divide(int x, int y) // returns x divided by y; returns ERROR if y is 0
public int max(int x, int y) // returns the bigger number: x or y
public int remainder(int x, int y) // returns the remainder when x is divided by y
public int triple(int x) // returns x times three
//returns true if the operation is any of \"*\", \"/\", \"%\", \"+\", or \"-\"; otherwise false:
public boolean isValidOperation(String operation) // use a switch statement
Solution
Math.java
public class Math {
public int divide(int x, int y) // returns x divided by y; returns ERROR if y is 0
{
if(y==0) return 0;
else
return x/y;
}
public int max(int x, int y) // returns the bigger number: x or y
{
if(x > y)
return x;
else
return y;
}
public int remainder(int x, int y) // returns the remainder when x is divided by y
{
return x % y;
}
public int triple(int x) // returns x times three
{
int value = 0;
for(int i =0; i<x; i++){
value = value + x;
}
return value;
}
//returns true if the operation is any of \"*\", \"/\", \"%\", \"+\", or \"-\"; otherwise false:
public boolean isValidOperation(String operation) // use a switch statement
{
boolean status = false;
switch(operation.charAt(0)){
case \'*\':
case \'/\':
case \'%\':
case \'+\':
case \'-\': status = true;break;
default: status = false;
}
return status;
}
}
