# Program based on a physics concept

I am a beginner and I wanted to make programs that would help me while I learnt Java. I made a program to help me in my assignments. Any suggetions?

Main method

public class MirrorFormula {

public static void main(String[] args) {
Scanner keyboardInput = new Scanner(System.in);
double u =0;
double v =0;
double f =0;

System.out.println("Hello! This is a calculator based on the Mirror Formula. \n");
System.out.println("The mirror formula is a relation between the object distance, the image distance \n and the focal length.");
System.out.println("The formula is : 1/u + 1/v = 1/f.  (u=distance of object, v=distance of image, f=focal length.)");
System.out.println("It is applicable for both convex mirrors and concave mirrors.");
System.out.println();
System.out.println("This calculator can calculate :");
System.out.println();
System.out.println("1. Distance of the object from the pole of the spherical mirror.");
System.out.println("2. Distance of the image from the pole of the spherical mirror.");
System.out.println("3. Focal length");
System.out.println("[If you have 2 of the values.]");
System.out.println("________________________________________________________________________________________________________________________________");
System.out.println("What value would you like to find out? ");
System.out.println("Enter: \n 1. for Distance of object \n 2. for distance of image \n 3. for Focal length \n (Please note that you don't need to enter units. The ans. is in whatever unit you enter the values)" );

int userChoice = keyboardInput.nextInt();

System.out.println("________________________________________________________________________________________________________________________________");

switch (userChoice) {

case 1 :
System.out.println("Please enter the distance of the image: ");
v = keyboardInput.nextDouble();
System.out.println("Please enter the focal length: ");
f = keyboardInput.nextDouble();
u = findU(v, f);
break;

case 2 :
System.out.println("Please enter the distance of the object: ");
u = keyboardInput.nextDouble();
System.out.println("Please enter the focal length: ");
f = keyboardInput.nextDouble();
v = findV(u, f);
break;

case 3 :
System.out.println("Please enter the distance of the object: ");
u = keyboardInput.nextDouble();
System.out.println("Please enter the distance of the image: ");
v = keyboardInput.nextDouble();
f = findF(u, v);
break;

}
System.out.println("___________________________________________________");
System.out.println(" Distance of the object from the pole is  | " + u + "  |");
System.out.println(" Distance of the image from the pole is   | " + v + "  |");
System.out.println(" The focal length                         | " + f + "  |");
System.out.println("___________________________________________________");
}


Other methods

These calculate the ans. based on the input:

public static double findU(double v, double f) {

double oneUpon;

oneUpon = 1/f - 1/v;
return 1/oneUpon;

}

public static double findV(double u, double f){

double oneUpon;

oneUpon = 1/f - 1/u;
return 1/oneUpon;
}

public static double findF(double u, double v){

double oneUpon = 1/u + 1/v;
return 1/oneUpon;
}

}


First, the findX functions don't need to initialise the variable oneUpon inside. It's also unclear, anyway.

public static double findU(double v, double f) {

double oneUpon;

oneUpon = 1/f - 1/v;
return 1/oneUpon;

}

public static double findV(double u, double f){

double oneUpon;

oneUpon = 1/f - 1/u;
return 1/oneUpon;
}

public static double findF(double u, double v){

double oneUpon = 1/u + 1/v;
return 1/oneUpon;
}


Just return the variable directly.

I'd also like to point out that findU and findV are identical in function, just named differently.

public static double findUV(double uv, double f) {
return 1/(1/f - 1/uv);
}

public static double findF(double u, double v){
return 1/(1/u + 1/v);
}


    System.out.println("Hello! This is a calculator based on the Mirror Formula. \n");
System.out.println("The mirror formula is a relation between the object distance, the image distance \n and the focal length.");
System.out.println("The formula is : 1/u + 1/v = 1/f.  (u=distance of object, v=distance of image, f=focal length.)");
System.out.println("It is applicable for both convex mirrors and concave mirrors.");
System.out.println();
System.out.println("This calculator can calculate :");
System.out.println();
System.out.println("1. Distance of the object from the pole of the spherical mirror.");
System.out.println("2. Distance of the image from the pole of the spherical mirror.");
System.out.println("3. Focal length");
System.out.println("[If you have 2 of the values.]");


This is not how you print large blocks. Separate your strings with line separators like /n. Keep the text as a large multi-line string block, and print that out instead. It will take less maintenance and less System.out.println calls.

Feel free to take the user input as a string/letter instead of an integer, makes everything much clearer. On a related note, an exit function would be nice too.

    switch (userChoice) {

case 1 :


FunU and FindV do the same thing you should find an appropriate name for the operation it does instead. Consider changing your function findV to

return (f * u) / (u - f);


You can change findF in a similar way. More importantly functions findV and findU will fail when both parameters are equal. (Division by zero).