Java Menu
Menu for Java
import java.util.Scanner; //library for user input
import java.lang.Math; //library for random numbers
public class Menu {
// Constructor on this Object takes control of menu events and actions
public Menu() {
this.print(); // print Menu
Scanner sc = new Scanner(System.in); // using Java Scanner Object
boolean quit = false;
while (!quit) {
try { // scan for Input
int choice = sc.nextInt(); // using method from Java Scanner Object
System.out.print("" + choice + ": ");
quit = this.action(choice); // take action
} catch (Exception e) {
sc.nextLine(); // error: clear buffer
System.out.println(e + ": Not a number, try again.");
}
}
sc.close();
}
// Print the menu options to Terminal
private void print() {
//System.out.println commands below is used to present a Menu to the user.
System.out.println("-------------------------\n");
System.out.println("Choose from these choices");
System.out.println("-------------------------\n");
System.out.println("1 - Say Hello");
System.out.println("2 - Coulomb Calculator");
System.out.println("3 - Vectors Demo");
System.out.println("0 - Quit");
System.out.println("-------------------------\n");
}
// Private method to perform action and return true if action is to quit/exit
private boolean action(int selection) {
boolean quit = false;
switch (selection) { // Switch or Switch/Case is Control Flow statement and is used to evaluate the user selection
case 0:
System.out.print("Goodbye, World!");
quit = true;
break;
case 1:
System.out.print("Hello, World!");
break;
case 2:
System.out.println("Welcome to the Coulomb Calculator");
Coulomb calc = new Coulomb();
break;
case 3:
Vectors vectorObject = new Vectors();
vectorObject.demo(vectorObject);
break;
default:
//Prints error message from console
System.out.print("Unexpected choice, try again.");
}
System.out.println("");
return quit;
}
static public void main(String[] args) {
new Menu();
}
}
Menu.main(null);
import java.util.ArrayList;
public class Vectors {
public Vectors() {
}
ArrayList<Integer> vector1 = new ArrayList<Integer>();
ArrayList<Integer> vector2 = new ArrayList<Integer>();
public ArrayList default1() {
vector1.add(1);
vector1.add(2);
vector1.add(3);
return vector1;
}
public ArrayList default2() {
vector2.add(4);
vector2.add(5);
vector2.add(6);
return vector2;
}
public void printVectorContent(ArrayList<Integer> v1) {
for (int i : v1) {
System.out.println(i);
}
}
public int dot(ArrayList<Integer> v1, ArrayList<Integer> v2) {
int product = 0;
for (int i = 0; i < v1.size(); i++) {
product += v1.get(i) * v2.get(i);
}
return product;
}
public void demo(Vectors test) {
System.out.println("Vector Class Demo: ");
System.out.println("Vector 1: ");
test.printVectorContent(test.default1());
System.out.println("Vector 2: ");
test.printVectorContent(test.default2());
System.out.println("Dot Product: ");
System.out.println(test.dot(test.vector1, test.vector2));
}
public static void main(String[] args) {
Vectors test = new Vectors();
test.demo(test);
}
}
import java.util.Scanner;
import java.lang.Math;
public class Coulomb {
// Constructor with the enterValues method.
public Coulomb () {
this.enterValues();
}
// Generally, charge values can be integers, since charge is quantized.
// The distance can be a float, since it is often a decimal.
// The force is a double due to the size of k.
private double force;
private int q1;
private int q2;
private float r;
private static double k = 9 * Math.pow(10, 9);
public static void main(String[] args) {
}
private void enterValues() {
Scanner input;
// 3 while loops with try-catch statements to make sure the correct datatype is inputted.
while (true) {
input = new Scanner(System.in);
System.out.print("Enter the first charge: ");
try {
q1 = input.nextInt();
break;
} catch (Exception e) { // if not a number
System.out.println("Not an int" + e);
}
input.close();
}
System.out.println("");
while (true) {
input = new Scanner(System.in);
System.out.print("Enter the second charge: ");
try {
q2 = input.nextInt();
break;
} catch (Exception e) { // if not a number
System.out.println("Not an int" + e);
}
input.close();
}
System.out.println("");
while (true) {
input = new Scanner(System.in);
System.out.print("Enter the distance between the charges: ");
try {
r = input.nextFloat();
break;
} catch (Exception e) { // if not a number
System.out.println("Not a float" + e);
}
input.close();
}
CalculateCoulomb();
System.out.println("The force between the two charges, by Coulomb's Law, is " + force + " Coulombs");
// If the user wishes to continue, they can enter new values. If not, the program exits.
if (moreCalc()) {
enterValues();
}
else {
return;
}
}
// The actual calculator. A simple formula called Coulomb's Law is employed.
// The method itself is of type double, and outputs the force which is of double datatype.
public double CalculateCoulomb() {
force = (k * q1 * q2 / (Math.pow(r, 2)));
return force;
}
// This method checks if the user wishes to do a new calculation.
// The method is of type boolean, and outputs true if the user wants to enter new values, and false otherwise.
public boolean moreCalc() {
Scanner input;
input = new Scanner(System.in);
System.out.print("Do you wish to continue? Type Yes to continue, or anything else to exit. ");
String chosen = input.nextLine();
// String is a class. Therefore, the object of the String class, chosen, has methods.
// The .equals method is used to check if two strings are equivalent.
if (chosen.equals("Yes")) {
return true;
}
else {
return false;
}
}
}