|
| 1 | +//import Scanner for user input |
| 2 | +import java.util.Scanner; |
| 3 | + |
| 4 | +/** |
| 5 | +* This program calculates the volume of a sphere. |
| 6 | +* |
| 7 | +* @author Dylan Mutabazi |
| 8 | +* @version 1.0 |
| 9 | +* @since 2025-September-11 |
| 10 | +*/ |
| 11 | + |
| 12 | +final class Einstein { |
| 13 | + /** |
| 14 | + *@exception IllegalStateException |
| 15 | + *@see IllegalStateException |
| 16 | + */ |
| 17 | + private Einstein() { |
| 18 | + throw new IllegalStateException("Utility class"); |
| 19 | + } |
| 20 | + |
| 21 | + // Constant for speed of light |
| 22 | + public static final double SPEEDOFLIGHT = 2.998e8; |
| 23 | + /** |
| 24 | + * Entrypoint of the program. |
| 25 | + * @param args UNUSED. |
| 26 | + */ |
| 27 | + public static void main(final String[] args) { |
| 28 | + // Get mass from user. |
| 29 | + System.out.println("Input your mass (kg): "); |
| 30 | + Scanner massScanner = new Scanner(System.in); |
| 31 | + String massString = massScanner.nextLine(); |
| 32 | + |
| 33 | + try { |
| 34 | + // Converts mass into double. |
| 35 | + double massDouble = Double.parseDouble(massString); |
| 36 | + // If mass <= 0 prints error line. |
| 37 | + if (massDouble <= 0) { |
| 38 | + System.out.println("Mass cannot be <= 0"); |
| 39 | + } else { |
| 40 | + // Else calculates energy in joules. |
| 41 | + double energy = massDouble * Math.pow(SPEEDOFLIGHT, 2); |
| 42 | + System.out.println("the amount of energy released = " |
| 43 | + + String.format("%.3e", energy) + " Joules"); |
| 44 | + } |
| 45 | + } catch (Exception e) { |
| 46 | + // If conversion to double doesnt work prints error line. |
| 47 | + System.out.println("Invalid input, try again later."); |
| 48 | + } |
| 49 | + } |
| 50 | +} |
0 commit comments