Refactored all solutions

This commit is contained in:
rshaghoulian
2017-04-05 18:42:35 -05:00
parent 5f4c2f4a78
commit decc8e791e
135 changed files with 848 additions and 849 deletions
@@ -16,10 +16,10 @@ import java.security.Permission;
class Calculate {
static Scanner scan = new Scanner(System.in);
static Output output = new Output();
static Calculate output = new Calculate();
public static Calculate do_calc() {
return new Calculate();
return output;
}
public int get_int_val() throws IOException, NumberFormatException {
@@ -54,13 +54,64 @@ class Calculate {
return Math.PI * (r * r) * h;
}
static void display (double value) {
System.out.println(value);
}
}
class Output {
void display(double value) {
System.out.format("%.3f%n", value);
}
}
public class Solution {
public static void main(String[] args) {
DoNotTerminate.forbidExit();
try {
Calculate cal = new Calculate();
int T = cal.get_int_val();
while (T--> 0) {
double volume = 0.0;
int ch = cal.get_int_val();
if (ch == 1) {
int a = cal.get_int_val();
volume = Calculate.do_calc().get_volume(a);
} else if (ch == 2) {
int l = cal.get_int_val();
int b = cal.get_int_val();
int h = cal.get_int_val();
volume = Calculate.do_calc().get_volume(l, b, h);
} else if (ch == 3) {
double r = cal.get_double_val();
volume = Calculate.do_calc().get_volume(r);
} else if (ch == 4) {
double r = cal.get_double_val();
double h = cal.get_double_val();
volume = Calculate.do_calc().get_volume(r, h);
}
cal.output.display(volume);
}
} catch (NumberFormatException e) {
System.out.print(e);
} catch (IOException e) {
e.printStackTrace();
} catch (DoNotTerminate.ExitTrappedException e) {
System.out.println("Unsuccessful Termination!!");
}
} // end of main
} //end of Solution
/**
*This class prevents the user form using System.exit(0)
* from terminating the program abnormally.
*
*/
class DoNotTerminate {
public static class ExitTrappedException extends SecurityException {}
public static void forbidExit() {
final SecurityManager securityManager = new SecurityManager() {
@Override
public void checkPermission(Permission permission) {
if (permission.getName().contains("exitVM")) {
throw new ExitTrappedException();
}
}
};
System.setSecurityManager(securityManager);
}
} //end of Do_Not_Terminate