Utility Wizard - WordPress.com

advertisement
import java.applet.Applet;
import java.lang.Math.*;
import java.util.Scanner;
import java.util.ArrayList;
/**
* @author JC Dy, Adnan Yunus, Nabeel Mamoon
* February 2-3, 2015
* HS Hacks II Hackathon - San Jose @PayPal headquarters
*/
public class UtilityWizard extends Applet
{
public void init()
{
MathCorrector mc = new MathCorrector(); // to correct rounding
Scanner kb = new Scanner(System.in);
MathCalculations calc = new MathCalculations();
System.out.println("Enter a number for your category:");
System.out.println("\t 0) END PROGRAM \n\t 1) Physics \n\t 2) Mathematics \n\t 3) Chemistry \n\t
4) Finance ");
int x = kb.nextInt();
int equation = 4;
switch(x)
{
case 0:
kb.close();
return;
case 1: equation = 1;
break;
case 2: equation = 2;
break;
case 3: equation = 3;
break;
case 4: equation = 4;
break;
}
int y;
switch(equation)
{
case 1: System.out.println("Enter a number for your equation:");
System.out.println("\t 1) Force \n\t 2) Momentum \n\t 3) Gravitational Potential Energy \n\t 4)
Hooke's Law \n\t 5) Kinetic Energy \n\t 6) Power \n\t 7) Impulse");
y = kb.nextInt();
if (y == 1){
System.out.println("Unknown: Force");
System.out.println("Enter mass (kg): ");
double mass = kb.nextDouble();
System.out.println("Enter acceleration (m/s²): ");
double acc = kb.nextDouble();
System.out.println("Force = " + calc.getForce(mass, acc) + " Newtons");
}
if (y == 2){
System.out.println("Unknown: Momentum");
System.out.println("Enter mass (kg): ");
double mass = kb.nextDouble();
System.out.println("Enter velocity (m/s): ");
double vel = kb.nextDouble();
System.out.println("Momentum = " + calc.getForce(mass, vel) + " kg*m/s");
}
if (y == 3){
System.out.println("Unknown: Gravitational Potential Energy");
System.out.println("Enter mass (kg): ");
double mass = kb.nextDouble();
System.out.println("Enter height (m): ");
double height = kb.nextDouble();
System.out.println("Gravitational Potential Energy = " +
mc.mul(calc.gravPotentialEnergy(height, mass),1) + " Joules");
}
if (y == 4){
System.out.println("Unknown: Restoring force of spring");
System.out.println("Enter spring constant (N/m): ");
double k = kb.nextDouble();
System.out.println("Enter displacement (m): ");
double d = kb.nextDouble();
System.out.println("Force of spring = " + mc.mul(calc.hookeslaw(k, d),1) + " Newtons");
}
if (y == 5){
System.out.println("Unknown: Kinetic Energy");
System.out.println("Enter mass (kg): ");
double mass = kb.nextDouble();
System.out.println("Enter velocity (m/s): ");
double vel = kb.nextDouble();
System.out.println("KE = " + mc.mul(calc.kineticenergy(mass, vel),1) + " Joules");
}
if (y == 6) {
System.out.println("Unknown: Power");
System.out.println("Enter Work (Joules): ");
double work = kb.nextDouble();
System.out.println("Enter time (s): ");
double time = kb.nextDouble();
System.out.println("Power = " + mc.mul(calc.powercalc(work, time),1) + " Watts");
}
if (y == 7) {
System.out.println("Unknown: Impulse");
System.out.println("Enter force (N): ");
double force = kb.nextDouble();
System.out.println("Enter time (s): ");
double time = kb.nextDouble();
System.out.println("Impulse = " + mc.mul(calc.Impulse(force, time),1) + " Newton-seconds");
}
break;
case 2: System.out.println("Enter a number for your equation:");
System.out.println("\t 1) Find the zeros (linear) \n\t 2) Find the zeros (quadratic) \n\t 3) Cube
Volume \n\t 4) Rectangular Prism Volume \n\t 5) Cylinder Volume");
y = kb.nextInt();
if (y == 1){
System.out.println("Format: y = Ax+B");
System.out.println("Enter A: ");
double A = kb.nextDouble();
System.out.println("Enter B: ");
double B = kb.nextDouble();
System.out.println("Equation: " + A + "x " + "+ " + B);
System.out.println("Zeros = " + mc.mul(calc.getZerosLinear(A, B), 1));
}
if (y == 2){
System.out.println("Format: y = Ax²+Bx+C");
System.out.println("Enter A: ");
double A = kb.nextDouble();
System.out.println("Enter B: ");
double B = kb.nextDouble();
System.out.println("Enter C: ");
double C = kb.nextDouble();
System.out.println("Equation: " + A + "x²" + " + " + B + "x" + " + " + C);
ArrayList<Double> numbers = calc.getZerosQuadratic(A, B, C);
if(!numbers.isEmpty())
{
System.out.println("Zeros = " + numbers.get(0) + ", " + numbers.get(1));
}
// If it gets here, the array list is empty, and there are no solutions
else{
System.out.println("No real solutions");
}
}
if (y == 3){
System.out.println("Enter units: ");
String s = kb.next();
System.out.println("Enter side of cube: ");
double sideLength = kb.nextDouble();
System.out.println("Volume of cube: " + calc.getCubeVolume(sideLength) + " " + s + "
cubed");
}
if (y == 4){
System.out.println("Enter units: ");
String s = kb.next();
System.out.println("Enter one side of rectangle: ");
double side1 = kb.nextDouble();
System.out.println("Enter another side of rectangle: ");
double side2 = kb.nextDouble();
System.out.println("Enter height of rectangle: ");
double h = kb.nextDouble();
System.out.println("Volume of rectangular prism: " + calc.rectPrismVolume(side1, side2, h) +
" " + s + " cubed");
}
if (y == 5){
System.out.println("Enter units: ");
String s = kb.next();
System.out.println("Enter height of cylinder: ");
double h = kb.nextDouble();
System.out.println("Enter radius of cylinder: ");
double r = kb.nextDouble();
System.out.println("Volume of cylinder: " + calc.cylinderVolume(r, h) + " " + s + " cubed");
}
break;
case 3: System.out.println("Enter a number for your equation:");
System.out.println("\t 1) Wavelength \n\t 2) pH \n\t 3) Einsteins energy \n\t 4) Percent yield
\n\t 5) Photon Energy Calculator \n\t 6) Pressure Calculator");
y = kb.nextInt();
if (y == 1){
System.out.println("Enter frequency (Hertz): ");
double frq = kb.nextDouble();
System.out.println("Wavelength is: " + calc.wavelength(frq) + " meters");
}
if (y == 2){
System.out.println("Enter Hydrogen concentration (moles/Liter): ");
double conc = kb.nextDouble();
System.out.println("pH is: " + calc.phcalc(conc) + " moles per liters");
}
if (y == 3){
System.out.println("Enter mass (kg): ");
double mass = kb.nextDouble();
System.out.println("The energy is " + calc.energycalc(mass) + " Joules");
}
if (y == 4){
System.out.println("Enter actual yield: ");
double ac = kb.nextDouble();
System.out.println("Enter total yield: ");
double tot = kb.nextDouble();
System.out.println("The percent yield is " + calc.percyield(ac,tot) + "%");
}
if (y == 5){
System.out.println("Enter the wavelength: ");
double length = kb.nextDouble();
System.out.println("The photon energy is " + calc.calcphotonenergy(length) + " J");
}
if (y == 6){
System.out.println("Enter Volume (m^3): ");
double volume = kb.nextDouble();
System.out.println("Enter number of Moles: ");
double moles = kb.nextDouble();
System.out.println("Enter Temperature: ");
double temp = kb.nextDouble();
System.out.println("The pressure needed is " + calc.getpressure(volume,moles, temp) + "
Pascals");
}
break;
case 4: System.out.println("Enter a number for your equation:");
System.out.println("\t 1) Simple interest \n\t 2) Compound Interest");
y = kb.nextInt();
if (y == 1){
System.out.println("Unknown: simple interest");
System.out.println("Enter principal $: ");
double prin = kb.nextDouble();
System.out.println("Enter time (years): ");
double yrs = kb.nextDouble();
System.out.println("Enter rate: ");
double rate = kb.nextDouble();
System.out.println("Interest over " + yrs + " years is " + calc.simpInterest(yrs, prin, rate));
}
if (y == 2){
System.out.println("Unknown: compound interest");
System.out.println("Enter principal $: ");
double prin = kb.nextDouble();
System.out.println("Enter time (years): ");
double yrs = kb.nextDouble();
System.out.println("Enter rate: ");
double rate = kb.nextDouble();
System.out.println("Enter number of times interest is compounded yearly: ");
double compTime = kb.nextDouble();
System.out.println("Total accumulated $ after " + yrs + " years is $" + calc.compInterest(prin,
rate, yrs, compTime));
}
break;
}
}
}
Download