Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
JavaMenuApplication/optionMethods.java
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
272 lines (237 sloc)
8.97 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Scanner; | |
public class optionMethods { | |
private static String dp = "%.2f"; | |
/* Declaring the private global variable for decimal point precision and | |
initialising a default precision */ | |
double result, result2, squareroot, calculation; | |
// Declaring the global variables used in both calculations | |
public static void setdp(String decp) { | |
// Creating the method for setting decimal point precision | |
dp = decp; | |
} | |
public static String getdp() { | |
// Creating the method for returning the decimal point precision | |
return dp; | |
} | |
public void accuracy() { | |
// Method for user to choose decimal point precision of equations in the program | |
int a; | |
Boolean validRange = false; | |
// Creating Boolean variable to be used for do-while loop condition | |
Scanner input = new Scanner(System.in); | |
menu menu = new menu(); | |
try { | |
// Creating try-catch block | |
do { | |
// Creating do-while loop | |
System.out.print("Please enter an integer between 1-5 to determine the decimal point precision: "); | |
a = input.nextInt(); | |
if(a>0 && a < 6) { | |
// If statement used to validate user input against allowed range | |
System.out.println(); | |
System.out.println("You have picked a decimal point precision of: " + a); | |
String decimalplace = "%." + a + "f"; | |
setdp(decimalplace); | |
// Creating precision for application depending on user input for 'a' | |
validRange = true; | |
// Condition set for do-while loop | |
} | |
else { | |
// Adding else statement if user has not input within allowed range | |
System.out.println(); | |
System.out.println("ERROR - You need to pick a range between 1-5. Please press the enter key to try again."); | |
menu.showNextStep(); | |
validRange = false; | |
// Condition set for do-while loop | |
}} | |
while(!validRange); | |
} | |
catch (Exception e) { | |
// Error message if user does not input an integer number for a | |
System.out.println(); | |
System.out.println("ERROR - Invalid Entry. You must enter an integer number."); | |
} | |
} | |
public void equation() { | |
// method solve quadratic equation depending on user input | |
float a, b, c; | |
Scanner input = new Scanner(System.in); | |
menu menu = new menu(); | |
System.out.println("The quadratic equation is ax^2 + bx + c = 0"); | |
// Quadratic equation syntax | |
menu.showNextStep(); | |
// user input for float; a,b,c | |
System.out.println("Please now pick the integer values you would like for the equation."); | |
menu.showNextStep(); | |
try { | |
// Creating try-catch block | |
System.out.print("Integer a: "); | |
a = input.nextFloat(); | |
System.out.print("Integer b: "); | |
b = input.nextFloat(); | |
System.out.print("Integer c: "); | |
c = input.nextFloat(); | |
// Calculations to work out quadratic equation | |
calculation = (b * b) - (4 * a * c); | |
squareroot = Math.sqrt(calculation); | |
result = ((-b) + squareroot) / (2 * a); | |
result2 = ((-b) - squareroot) / (2 * a); | |
// Converting float a,b,c to int for equation printing. | |
int a2 = Math.round(a); | |
int b2 = Math.round(b); | |
int c2 = Math.round(c); | |
// printing the equation with user values, followed by result | |
System.out.println(); | |
System.out.println("Given your values: " + a2 + "x^2 + " + b2 + "x + " + c2 + " =0"); | |
menu.showNextStep(); | |
String precision = getdp(); | |
// Calling the static method to declare new variable precision based on input | |
// for dp from option 1 | |
System.out.println("x = " + String.format(precision, result)); | |
System.out.println("x = " + String.format(precision, result2)); | |
} | |
catch (Exception e) { | |
// Error message if user does not input numbers for variables a,b,c | |
System.out.println(); | |
System.out.println("ERROR - Invalid Entry. You must enter an integer number."); | |
} | |
} | |
public void linerEquation() { | |
// Method for linear equation option | |
float a, b, c; | |
Scanner input = new Scanner(System.in); | |
menu menu = new menu(); | |
System.out.println("The linear equation is ax + b = c"); | |
// Linear equation syntax | |
menu.showNextStep(); | |
// user input for float; a,b,c | |
System.out.println("Please now pick the integer values you would like for the equation."); | |
menu.showNextStep(); | |
try { | |
// Creating try-catch block | |
System.out.print("Integer a: "); | |
a = input.nextFloat(); | |
System.out.print("Integer b: "); | |
b = input.nextFloat(); | |
System.out.print("Integer c: "); | |
c = input.nextFloat(); | |
result = (c - b) / a; | |
// Converting float a,b,c to int for equation printing. | |
int a2 = Math.round(a); | |
int b2 = Math.round(b); | |
int c2 = Math.round(c); | |
// printing the equation with user values, followed by result | |
System.out.println(); | |
System.out.println("Given your values: " + a2 + "x + " + b2 + " = " + c2); | |
menu.showNextStep(); | |
String precision = getdp(); | |
/* Calling the static method to declare new variable precision based on input | |
for dp from Accuracy method */ | |
System.out.println("x = " + String.format(precision, result)); | |
} | |
catch (Exception e) { | |
// Error message if user does not input numbers for variables a,b,c | |
System.out.println(); | |
System.out.println("ERROR - Invalid Entry. You must enter an integer number."); | |
} | |
} | |
public void bubblesort() { | |
// Using the bubblesort method to sort the user input for Student ID number | |
menu menu = new menu(); | |
Scanner input = new Scanner(System.in); | |
// Scanner input for user ID number | |
String studentnumber; | |
System.out.println("Please enter your 7 digit student ID when prompted!"); | |
// using e1 so variable does not clash with catch statement | |
menu.showNextStep(); | |
try { | |
// Creating try-catch block | |
Boolean validID = false; | |
// Declaring boolean validID for the do-while loop | |
do { | |
// Creating do while loop to validate student ID number inputed by user | |
System.out.print("Enter your student ID number here; "); | |
studentnumber = input.nextLine(); | |
// User to input their Student ID number | |
int s; | |
for(s=0; s < studentnumber.length(); s++) { | |
// For loop used to create variable s for each entered digit by user | |
if (Character.isLetter(studentnumber.charAt(s))) { | |
// Creating if statement to validate whether user has inputed letters | |
validID = false; | |
// Creating condition for do-while loop to validate Student ID number length | |
System.out.println(); | |
System.out.println("Error - this is not a valid Student ID, please press the enter key to try again."); | |
menu.showNextStep(); | |
break; | |
// Break used to terminate loop if condition is true | |
} else | |
validID = true; | |
// Condition for else statement for do-while loop | |
} | |
} while (!validID); | |
// loop condition for if-else statement to validate Student ID number | |
long sNumber = Long.parseLong(studentnumber); | |
/* Converting 'studentnumber' to long 'sNumber' to validate user input | |
against try-catch block for special characters. | |
sNumber variable has no other use in the method */ | |
System.out.println(); | |
System.out.print("Your Student ID is: "); | |
// Printing inputed student ID number to the console | |
int studentID[] = new int[studentnumber.length()]; | |
// Creating array 'studentID' for numbers inputed by user | |
for (int n = 0; n < studentID.length; n++) { | |
studentID[n] = Character.getNumericValue(studentnumber.charAt(n)); | |
System.out.print(studentID[n]); | |
// for loop used to obtain individual digits from user string input | |
} | |
System.out.println(); | |
System.out.println(); | |
bubblesort.bubblesort(studentID); | |
// Calling the static bubble-sort method to be used for user input | |
System.out.println("Please press enter to bubble sort your Student ID number!"); | |
menu.showNextStep(); | |
System.out.print("Your Student ID after the Bubble Sort is; "); | |
// Printing bubble-sorted student ID number using array | |
for (int i : studentID) { | |
// Enhanced For loop used to call sorted ID numbers using index | |
System.out.print(i); | |
} | |
System.out.println(); | |
} catch (Exception e) { | |
/* Error message if user does not input integer values for Student ID, returning | |
user to main menu */ | |
System.out.println(); | |
System.out.println("ERROR - Invalid Entry. You must enter an integer number."); | |
} | |
} | |
public void seconds() { | |
// User to input int a - amount of seconds | |
Scanner input = new Scanner(System.in); | |
int a; | |
try { | |
// Creating try-catch block | |
System.out.print("Enter a number of seconds: "); | |
a = input.nextInt(); | |
System.out.println(); | |
// calculations to convert seconds into hours, minutes, seconds | |
int hours = a / 3600; | |
int remainder = a % 3600; | |
int minutes = remainder / 60; | |
int seconds = remainder % 60; | |
System.out.println(a + " Seconds = " + hours + " hours " + minutes + " minutes " + seconds + " seconds "); | |
// Printing the result | |
} catch (Exception e) { | |
// Error message if user does not input numbers for variables a,b,c | |
System.out.println(); | |
System.out.println("ERROR - Invalid Entry. You must enter an integer number."); | |
} | |
} | |
public void exit() { | |
// Menu program exit method | |
menu menu = new menu(); | |
System.out.println("Thank you for using my program today."); | |
menu.showNextStep(); | |
System.exit(0); | |
} | |
} |