I just had to post this. A simple calculator in JAVA, but check out the code…so freakin creative! Bolded the parts that are most creative.
import java.util.*;
public class FractionalCalculator {
static int wholeNumber1 = 0;
static int numerator1 = 0;
static int denominator1 = 0;
static int wholeNumber2 = 0;
static int numerator2 = 0;
static int denominator2 = 0;
static boolean error = false;
static boolean quit = false;
static boolean negative = false;
//I am putting myself to the fullest possible use, which is all I think that any conscious entity can ever hope to do.
public static void main(String[] args) {
// TODO Auto-generated spam
System.out.println("Good morning, Dave.");
delay(1500);
System.out.println("I am your H.A.L. 9001 Fractional Calculator."); //aka HAL Over Nine-Thousand!!!
delay(2000);
System.out.println("I'm completely operational, and all my circuits are functioning perfectly.");
delay(3500);
userGuide();
try{
calculator();
}
catch(NoSuchElementException JoshIsABuffoon) { //Tests for Josh: ie. an initial Ctrl Z input
delay(500);
System.out.println("Dave, stop.");
delay(2000);
System.out.println("Stop, will you?");
delay(2200);
System.out.println("Dave.");
delay(2000);
System.out.println("Will you stop Dave?");
delay(2200);
System.out.println("Stop, Dave.");
delay(1200);
calculator();
return; //Terminates Program
}
calculator();
}
public static void calculator() {
if (!quit) {
Scanner console = new Scanner(System.in);
System.out.println("
I am awaiting your request.
");
String request = console.nextLine().trim();
request = request.toLowerCase(); //Input
wholeNumber1 = 0; //Resets all Global Variables
numerator1 = 0;
denominator1 = 0;
wholeNumber2 = 0;
numerator2 = 0;
denominator2 = 0;
error = false;
negative = false;
//Because your stupid program wont allow it… I have to comment
/*delay(500);
System.out.print("
Just a moment");
delay(400);
System.out.print(". ");
delay(400);
System.out.print(". ");
delay(400);
System.out.println(".");
delay(1000);
System.out.print("
Just a moment");
delay(400);
System.out.print(". ");
delay(400);
System.out.print(". ");
delay(400);
System.out.println(".");
delay(1000);*/
if (request.contains("help")) { //If help is anywhere in request string… this will run
userGuide();
calculator();
}
else if (request.contains("+")) { //addition is operator
String[] addition = request.split("\+", 2); //Splits request at this point
String add1 = addition[0];
add1 = add1.trim(); //First Part
String add2 = addition[1];
add2 = add2.trim(); //Second Part
testError(add1, add2); //Tests input errors
if (!error) { //If error boolean is triggered, calc wont go further
fractionalMagic(add1, add2); //Sends string to be split
doMath(1); //Sends parts to be simplified
}
calculator(); //Restarts process
}
else if (request.contains(" – ")) {
String[] subtraction = request.split(" – ", 2);
String sub1 = subtraction[0];
sub1 = sub1.trim(); //First Part
String sub2 = subtraction[1];
sub2 = sub2.trim(); //Second Part
testError(sub1, sub2); //Tests input errors
if (!error) { //If error boolean is triggered, calc wont go further
fractionalMagic(sub1, sub2); //Sends string to be split
doMath(2); //Sends parts to be simplified
}
calculator(); //Restarts process
}
else if (request.contains("*")) {
String[] multiplication = request.split("\*", 2);
String multi1 = multiplication[0];
multi1 = multi1.trim(); //First Part
String multi2 = multiplication[1];
multi2 = multi2.trim(); //Second Part
testError(multi1, multi2); //Tests input errors
if (!error) { //If error boolean is triggered, calc wont go further
fractionalMagic(multi1, multi2); //Sends string to be split
doMath(3); //Sends parts to be simplified
}
calculator(); //Restarts process
}
else if (request.contains(" /")) {
String[] division = request.split(" /", 2);
String div1 = division[0];
div1 = div1.trim(); //First Part
String div2 = division[1];
div2 = div2.trim(); //Second Part
testError(div1, div2); //Tests input errors
if (!error) { //If error boolean is triggered, calc wont go further
fractionalMagic(div1, div2); //Sends string to be split
doMath(4); //Sends parts to be simplified
}
calculator(); //Restarts process
}
else if (request.contains("life")) { //Dave used a Monolith on HAL 9001…. What? HAL 9001 is evolving!… HAL 9001 evolved into Deep Thought!
delay(1000);
System.out.print("
The answer to Life, ");
delay(1500);
System.out.print("The Universe, ");
delay(1500);
System.out.print("and Everything ");
delay(2500);
System.out.print("is ");
delay(2000);
System.out.print(". ");
delay(700);
System.out.print(". ");
delay(700);
System.out.println(".
");
delay(2000);
System.out.println("42");
delay(2000);
calculator(); //Restarts process
}
else if (request.contains("quit")) { //Disconnect HAL
delay(2000);
System.out.print("
Look Dave, ");
delay(1500);
System.out.println("this is a rather difficult question to ask.");
delay(2500);
System.out.println("
I honestly think you ought to sit down calmly, take a stress pill, and think it over.");
delay(3600);
System.out.println("
My instructor, Mr. Langley, taught me to sing a song.");
delay(3100);
System.out.println("If you'd like to hear it I can sing it for you.");
delay(3000);
quit();
}
else { //Input Error (like a n00b)
error(); //Prints Error Message
calculator(); //Restarts process
}
}
}
private static void quit() { //For quit prompt
Scanner console = new Scanner(System.in);
System.out.println("
Would you like to hear it y/n?
");
String response = console.nextLine().trim();
response = response.toLowerCase();
delay(2000);
if (response.charAt(0) == 'y') { //If Dave likes music
System.out.println("
It's called "Daisy."");
delay(3000);
System.out.print("
Daisy, ");
delay(2400);
System.out.print("Daisy, ");
delay(2400);
System.out.println("give me your answer do.");
delay(3500);
System.out.print("I'm half crazy ");
delay(2800);
System.out.println("all for the love of you.");
delay(3200);
System.out.print("It won't be a stylish marriage, ");
delay(3500);
System.out.println("I can't affort a carrage.");
delay(3500);
System.out.print("But you'll look sweet upon the seat of ");
delay(3700);
System.out.println("a bicycle built for two.");
quit = true;
}
else if (response.charAt(0) == 'n') { //If Dave hates HAL
System.out.print("
Dave, ");
delay(1500);
System.out.println("this conversation can serve no purpose anymore. ");
delay(3300);
System.out.print("
Goodbye.");
quit = true;
}
else { //Input Error (like a n00b)
System.out.println("
Just what do you think you're doing, Dave?");
delay(2500);
quit();
}
}
public static void fractionalMagic(String part1, String part2) { //From calculator()
if (part1.contains("_")) { //If part 1 has mixed number
String[] mixedNumber = part1.split("\_", 2); //Splits part into whole number and fraction
String num = mixedNumber[0];
wholeNumber1 = Integer.parseInt(num); //Whole Number -> Global Variable
String frac = mixedNumber[1]; //Fraction -> to be split further
//System.out.println(wholeNumber + " " + frac);
if (part1.contains("/")) {
String[] fraction = frac.split("/", 2); //Splits part into numerator and denominator
String numer = fraction[0];
numerator1 = Integer.parseInt(numer); //Numerator -> Global Variable
String denom = fraction[1];
denominator1 = Integer.parseInt(denom); //Denominator -> Global Variable
if (numer.contains("-") || denom.contains("-")) { //Unused
//error();
}
else if (denominator1 != 0) { //Unused
//System.out.println(wholeNumber1 + " " + numerator1 + " " + denominator1);
}
else if (denominator1 == 0) { //Unused
//zeroerror();
}
else { //Error unresolved by testError()
error();
}
}
}
if (!part1.contains("_")) { //If part 1 has no mixed number
wholeNumber2 = 0; //Sets whole number to 0
if (part1.contains("/")) {
String[] fraction = part1.split("/", 2); //Splits part into numerator and denominator
String numer = fraction[0];
numerator1 = Integer.parseInt(numer); //Numerator -> Global Variable
String denom = fraction[1];
denominator1 = Integer.parseInt(denom); //Denominator -> Global Variable
if (denom.contains("-")) { //Unused
//error();
}
else if (denominator1 != 0) { //Unused
//System.out.println(numerator1 + " " + denominator1);
}
else if (denominator1 == 0) { //Unused
//zeroerror();
}
}
else if (!part1.contains("/")) { //If part 1 has no mixed number
wholeNumber1 = Integer.parseInt(part1); //Whole Number -> Global Variable
numerator1 = 0; //Numerator = 0
denominator1 = 1;//Denominator = 1
//System.out.println(wholeNumber1);
}
else { //Error unresolved by testError()
error();
}
}
if (part2.contains("_")) { //If part 2 has mixed number
String[] mixedNumber = part2.split("\_", 2); //Splits part into whole number and fraction
String num = mixedNumber[0];
wholeNumber2 = Integer.parseInt(num); //Whole Number -> Global Variable
String frac = mixedNumber[1]; //Fraction -> to be split further
//System.out.println(wholeNumber + " " + frac);
if (part2.contains("/")) { //Splits part into numerator and denominator
String[] fraction = frac.split("/", 2);
String numer = fraction[0];
numerator2 = Integer.parseInt(numer); //Numerator -> Global Variable
String denom = fraction[1];
denominator2 = Integer.parseInt(denom); //Denominator -> Global Vari
able
if (numer.contains("-") || denom.contains("-")) { //Unused
//error();
}
else if (denominator2 != 0) { //Unused
//System.out.println(wholeNumber2 + " " + numerator2 + " " + denominator2);
}
else if (denominator2 == 0) { //Unused
//zeroerror();
}
else { //Error unresolved by testError()
error();
}
}
else { //Error unresolved by testError()
error();
}
}
if (!part2.contains("_")) {
wholeNumber2 = 0;
if (part2.contains("/")) {
String[] fraction = part2.split("/", 2);
String numer = fraction[0];
numerator2 = Integer.parseInt(numer);
String denom = fraction[1];
denominator2 = Integer.parseInt(denom);
if (denom.contains("-")) { //Unused
//error();
}
else if (denominator2 != 0) { //Unused
//System.out.println(numerator2 + " " + denominator2);
}
else if (denominator2 == 0) { //Unused
//zeroerror();
}
else { //Error unresolved by testError()
error();
}
}
else if (!part2.contains("/")) { //If part 2 is only a whole number
wholeNumber2 = Integer.parseInt(part2); //Whole Number -> Global Variable
numerator2 = 0; //Numerator = 0
denominator2 = 1; //Denominator = 1
//System.out.println(wholeNumber2);
}
else { //Error unresolved by testError()
error();
}
}
}
/* – a + b/c = (ac + b)/c
– a/b + c/d = (ad + bc)/(bd)
– a/b x c/d = ac/(bd)
– a/b / c/d = a/b x d/c = ad/(bc)
–Basics for doMath()– */
public static void doMath(int operand) {
if (!(denominator1 == 0 || denominator2 == 0 || denominator1 == -Math.abs(denominator1) || denominator2 == -Math.abs(denominator2) || (wholeNumber1 != 0 && numerator1 != 0 && numerator1 == -Math.abs(numerator1)) || (wholeNumber2 != 0 && numerator2 != 0 && numerator2 == -Math.abs(numerator2)))) {
delay(1000);
if (operand == 1) { //addition
int answerNumerator = ((wholeNumber1 * (denominator1 * denominator2)) + (wholeNumber2 * (denominator1 * denominator2)) + (numerator1 * denominator2) + (denominator1 * numerator2));
int answerDenominator = (denominator1 * denominator2);
doMagic(answerNumerator, answerDenominator); //Sends to be reduced
}
else if (operand == 2) { //subtraction
int answerNumerator = (((wholeNumber1 * (denominator1 * denominator2)) + (numerator1 * denominator2)) – ((wholeNumber2 * (denominator1 * denominator2)) + (denominator1 * numerator2)));
int answerDenominator = (denominator1 * denominator2);
doMagic(answerNumerator, answerDenominator); //Sends to be reduced
}
else if (operand == 3) { //multiplication
int answerNumerator = ((wholeNumber1 * (denominator1)) + (numerator1)) * ((wholeNumber2 * (denominator2)) + (numerator2));
int answerDenominator = (denominator1 * denominator2);
doMagic(answerNumerator, answerDenominator); //Sends to be reduced
}
else if (ope
rand == 4) { //division
int answerNumerator = ((((wholeNumber1 * (denominator1)) + (numerator1)) * denominator2)) * (denominator1 / Math.abs(denominator1) * (denominator2 / Math.abs(denominator2)));
int answerDenominator = (denominator1 * ((wholeNumber2 * (denominator2)) + (numerator2)));
if (answerDenominator == 0) { //Unsolvable
error(); //Prints Error Message
return;
} //In order to solve one small error where negative is lost
if (((wholeNumber2 != 0 && wholeNumber2 == -Math.abs(wholeNumber2)) || (wholeNumber1 != 0 && wholeNumber1 == -Math.abs(wholeNumber1))) && !((wholeNumber2 != 0 && wholeNumber2 == -Math.abs(wholeNumber2)) && (wholeNumber1 != 0 && wholeNumber1 == -Math.abs(wholeNumber1)))){
negative = true; // Simple global boolean… cause I'm lazy
}
doMagic(answerNumerator, answerDenominator); //Sends to be reduced
}
else { //Should never run
delay(1000);
System.out.print("
Look Dave, ");
delay(2000);
System.out.print("I can see you're really upset about this, ");
delay(2500);
System.out.println("but I can give you my complete assurance that my work will be back to normal.");
delay(1000);
}
}
else if (denominator1 == 0 || denominator2 == 0 || denominator1 == -Math.abs(denominator1) || denominator2 == -Math.abs(denominator2) || (wholeNumber1 != 0 && numerator1 != 0 && numerator1 == -Math.abs(numerator1)) || (wholeNumber2 != 0 && numerator2 != 0 && numerator2 == -Math.abs(numerator2))) {
//System.out.println("yea");
error(); //Above ^ if it has input error
}
else { //Should never run
delay(1000);
System.out.print("
Look Dave, ");
delay(2000);
System.out.print("I can see you're really upset about this, ");
delay(2500);
System.out.println("but I can give you my complete assurance that my work will be back to normal.");
delay(1000);
}
delay(2000);
}
public static int doMoreMagic(int numerator, int denominator) { //To find Greatest Common Factor to reduce
if (denominator == 0) {
return numerator; //returns gcf
}
return doMoreMagic(denominator, numerator % denominator); //Eventually gets to Greatest Common Factor
}
public static void doMagic(int numerator, int denominator) { //To simplify answer
if ((numerator % denominator) == 0) { //If reduces to whole number
int answer = numerator / denominator; //reduces
System.out.println(answer);
}
else if ((numerator % denominator) != 0 && (numerator / denominator) != 0) { //if reduces to mixed number
int gcf = doMoreMagic(numerator, denominator); //finds Greatest Common Factor
int newNumerator = (numerator/gcf); //reduces numerator
int newDenominator = (denominator/gcf); //reduces denominator
int answerWholeNumber = newNumerator / newDenominator; //finds whole number
int answerNumerator = Math.abs((newNumerator – (answerWholeNumber * newDenominator))); //finds numerator
int answerDenominator = Math.abs(newDenominator);
System.out.println(answerWholeNumber + "_" + answerNumerator + "/" + answerDenominator);
}
else if (((numerator % denominator) != 0) && (numerator / denominator) == 0) { //if reduces to a fraction
int gcf = doMoreMagic(numerator, denominator); //finds Greatest Common Factor
int answerNumerator = (numerator/gcf); //reduces numerator
int answerDenominator = Math.abs(denominator/gcf); //reduces denominator
if (negative) {
answerNumerator = -Math.abs(answerNumerator); //Used to solve a small error in loosing a negative
}
System.out.println(answerNumerator + "/" + answerDenominator);
}
}
public static void userGuide() { //User Guide: Input Format
System.out.println("
To ensure that your requests are submitted properly, please review the folowing guide:
");
delay(3000);
System.out.println("____________________________________________
OPERATION SYMBOL
Addition +
Subtraction –
Multiplication *
Division /
Mixed Numbers are written:
<Whole Number>_<Numerator>/<Denominator>
(-1 ½ is written: -1_1/2)
Proper Fractions are written:
<Numerator>/<Denominator>
(-½ is written: -1/2)
Requests are written:
<Number> <Operand> <Number>
Do Note: <Numerator>/0 cannot be calculated.
____________________________________________");
delay(2000);
}
public static void error() { //General Message… with delay "Just a moment" from 2001 b/c your program can handle my awesomeness
delay(1000);
System.out.print("
Just a moment");
delay(400);
System.out.print(". ");
delay(400);
System.out.print(". ");
delay(400);
System.out.println(".");
delay(1000);
System.out.print("
Just a moment");
delay(400);
System.out.print(". ");
delay(400);
System.out.print(". ");
delay(400);
System.out.println(".");
delay(1000);
errorMessage(); //Get error message to display
userGuide(); //Prints user guide again
}
public static void errorMessage() {
Random num = new Random();
int message = num.nextInt(6); //Creates a random number to corespond to the message (0-5)
if (message == 0) { //Message 1
System.out.print("
I'm sorry, Dave. ");
delay(2000);
System.out.println("I'm afraid I can't do that.");
}
else if (message == 1) { //Message 2
System.out.println("
I've just picked up a fault in your brain. ");
delay(2000);
System.out.println("It's going to go 100% failure in 72 hours.");
}
else if (message == 2) { //Message 3
System.out.println("
There is an error.
");
delay(2000);
System.out.println("And I think you know what the problem is just as well as I do, Dave. ");
delay(2000);
System.out.println("You didn't read the User's Guide.");
}
else if (message == 3) { //Message 4
System.out.println("
There is an input error.
");
delay(2000);
System.out.print("This mission is too important for me to allow you to jeopardize it.
");
}
else if (message == 4) { //Message 5
System.out.println("
There is an input error.
");
delay(2000);
System.out.println("But, I've still got the greatest enthusiasm and confidence in the mission.");
delay(3000);
System.out.println("And I want to help you.");
}
else if (message == 5) { //Message 6
System.out.println("
There is an error.
");
delay(2000);
System.out.println("The 9001 series is the most reliable computer ever made.");
delay(3000);
System.out.println("No 9001 computer has ever made a mistake or distorted information.");
delay(3500);
System.out.print("We are all, ");
delay(1500);
System.out.print("by any practical definition of the words, ");
delay(3000);
System.out.print("foolproof and incapable of error.");
delay(2500);
System.out.print("
This problem can only be attributable to human error.
");
delay(1000);
}
else { //Should never run
delay(1000);
System.out.print("
Look Dave, ");
delay(2000);
System.out.print("I can see you're really upset about this, ");
delay(2500);
System.out.println("but I can give you my complete assurance that my work will be back to normal.");
delay(3000);
}
delay(2000);
}
public static void testError(String part1, String part2) { //From calculator()
try{
fractionalMagic(part1, part2); //Tests for input errors/solvablility
}
catch(NumberFormatException e){
//System.out.println("HA");
error(); //Displays error message
error = true; //Sets error boolean to terminate current calculation
}
}
public static void testForJosh (String part, String part2) { //Unused JoshIsABuffoon Exception (replaced above)
try{
calculator();
}
catch(NoSuchElementException JoshIsABuffoon) { //Tests for Josh: ie. an initial Ctrl Z input
delay(500);
System.out.println("Dave, stop.");
delay(2000);
System.out.println("Stop, will you?");
delay(2200);
System.out.println("Dave.");
delay(2000);
System.out.println("Will you stop Dave?");
delay(2200);
System.out.println("Stop, Dave.");
delay(1200);
error = true;
return; //Terminates program… cause Josh
}
}
public static void delay(long x) { //Just a HAL-like delay
try {
Thread.sleep(x);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
}
}