Finals Programming
Finals Programming
COM231
Link: https://replit.com/@tristannerzs/FinalsInProgramming
Code definition/explanation:
Line 1:
Line 3-4:
These is where the main program start that also initiates the scanner.
Line 7:
Line 8:
I used double in this line to store the variables for the converter.
Line 10-14:
These lines prints or display the Maine Menu for the program that displays which tool will be
executed.
Line 16:
This line is an integer named choice that will be used for the first switch statement.
Line 18-25:
These lines shows the first switch statement that also shows its case 1 statement that prompts the
menu for the converter with different units of weight to convert into.
Line 27:
This line is an integer for the next switch statement which is for the converter.
Line 29-41:
The line 29 are the switch statement for the converter. This executes the first option of the converter
which is the kilograms to pound converter. These lines also shows the solution and result of the
converted input. The else prints the error code “!!!INVALID INPUT!!!” whenever the user inputs a
choice that aren’t within the if condition. The break in the line 41 is where the case 1 ends.
Line 42-81:
These lines shows the other units of weight that are used for the converter. The switch statement for
the converter have 4 cases. The line 78 ends the converter option which is also for the back to main
menu option.
Line 83-86:
These lines are the case 2 for the choice switch. These prints the header for the table of elements
which are inside the visual separators.
Line 88-89:
These two lines are the arrays for the symbols and names of the elements I used. For project output
purposes, I used only some of the elements for examples.
Line 90-91:
These are the two arrays for the atomic numbers and atomic mass. I used int for the single digit
atomic numbers and double for the atomic mass which have decimals.
Line 93-97:
These lines are used to display the array of the table of elements that consist of symbols, names,
atomic numbers and atomic mass. This are followed by the visual separator and the break for this
case.
Line 99-104:
These are the whole case 3 for the switch for choice or the main menu. The case 3 is intended for
exiting the program. Since I used an exit statement here, break; for this case is not necessary. It is
followed by the default for the whole switch (choice) statement.
Line 106:
do {
double kilograms, pounds, grams, milligrams;
System.out.println("MAIN MENU");
System.out.println("1. Weight Converter");
System.out.println("2. Display Table of Elements");
System.out.println("3. Exit");
System.out.print("Choose input: ");
switch (choice) {
case 1:
System.out.println("1. Kilograms to Pounds");
System.out.println("2. Pounds to Kilograms");
System.out.println("3. Grams to Milligrams");
System.out.println("4. Milligrams to Grams");
System.out.println("5. Back to Main Menu");
System.out.print("Choose conversion type: ");
switch (converter) {
case 1:
System.out.print("Enter weight in kilograms: ");
kilograms = scanner.nextDouble();
if (kilograms > 0) {
pounds = kilograms * 2.20462;
System.out.println(kilograms + " kilograms is " + pounds + " pounds.");
System.out.println("Solution: " + kilograms + " kg * 2.20462 = " + pounds + "
lbs.");
} else {
System.out.println("!!!INVALID INPUT!!!");
}
break;
case 2:
System.out.print("Enter weight in pounds: ");
pounds = scanner.nextDouble();
if (pounds > 0) {
kilograms = pounds * 0.453592;
System.out.println(pounds + " pounds is " + kilograms + " kilograms.");
System.out.println("Solution: " + pounds + " lbs * 0.453592 = " + kilograms + "
kg.");
} else {
System.out.println("!!!INVALID INPUT!!!");
}
break;
case 3:
System.out.print("Enter weight in grams: ");
grams = scanner.nextDouble();
if (grams > 0) {
milligrams = grams * 1000;
System.out.println(grams + " grams is " + milligrams + " milligrams.");
System.out.println("Solution: " + grams + " g * 1000 = " + milligrams + " mg.");
} else {
System.out.println("!!!INVALID INPUT!!!");
}
break;
case 4:
System.out.print("Enter weight in milligrams: ");
milligrams = scanner.nextDouble();
if (milligrams > 0) {
grams = milligrams / 1000;
System.out.println(milligrams + " milligrams is " + grams + " grams.");
System.out.println("Solution: " + milligrams + " mg / 1000 = " + grams + " g.");
} else {
System.out.println("!!!INVALID INPUT!!!");
}
break;
default:
System.out.println("!!!INVALID INPUT!!!");
}
break;
case 2:
System.out.println("-----------------------------------------------------------");
System.out.println("| Symbol | Name | Atomic Number | Atomic Mass|");
System.out.println("-----------------------------------------------------------");
String[] symbols = { "H", "He", "Li", "Be", "B", "C", "N", "O", "F", "Ne", "Na", "Mg", "Al",
"Si", "P", "S", "Cl", "Ar" };
String[] names = { "Hydrogen", "Helium", "Lithium", "Beryllium", "Boron", "Carbon",
"Nitrogen", "Oxygen", "Fluorine", "Neon", "Sodium", "Magnesium", "Aluminum", "Silicon",
"Phosphorus", "Sulfur", "Chlorine", "Argon" };
int[] AtomicNumbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18 };
double[] AtomicMass = { 1.008, 4.0026, 6.94, 9.0122, 10.81, 12.01, 14.01, 16.00, 19.00,
20.18, 22.99, 24.31, 26.98, 28.09, 30.97, 32.07, 35.45, 39.95 };
for (int i = 0; i < symbols.length; i++) {System.out.print("| " + symbols[i] + " | " +
names[i] + " | " + AtomicNumbers[i] + " | " + AtomicMass[i] + " |\n");
}
System.out.println("-----------------------------------------------------------");
break;
case 3:
System.out.println("Program Terminated!");
System.exit(0);
default:
System.out.println("!!!INVALID INPUT!!!");
}
} while (true);
}
}