/* Lesley Hogan * July 15th 2003 * MathVariables.java * Some common-ish math functions * Homework from: * http://teachertech.rice.edu/Participants/marilynt/Lessons/Exeter/Programming/mathlab.html */ package net.bunnie.dos; import java.text.DecimalFormat; import net.bunnie.*; public class MathVariables { public static void main (String[] args) { // temperature variables double degreesF = 0; double degreesC = 0; String degreesCString; String continues = ""; // km to school variables double averageSpeed = 0; double minutesToSchool = 0; // e=mc^2 variables double mass; double energy; double bulbs; // sleep variables int day; int month; int year; int tDay; int tMonth; int tYear; int alive; int sHours; double sDays; // general variables KeyboardReader r = new KeyboardReader(); DecimalFormat dec = new DecimalFormat("#.##"); boolean loop = false; boolean fullLoop = true; int switchInt = 0; // array of options String[] optionList = new String[6]; String optionString = ""; int options = optionList.length - 1; optionList[0] = "0: View this menu"; optionList[1] = "1: Convert a temperature from F to C"; optionList[2] = "2: Calculate the time it will take for you to get to school"; optionList[3] = "3: Calculate energy using E=MC^2"; optionList[4] = "4: Calculate how long you have slept in your life"; optionList[options] = "" + options + ": Press " + options + " to escape"; int i = 0; do { optionString += optionList[i]; optionString += "\n"; i++; } while (i < optionList.length); // welcome message System.out.print("-----------------------------------\n"); System.out.print(" M A T H V A R I A B L E S \n"); System.out.print(" L . H O G A N \n"); System.out.print(" _______ \n"); System.out.print(" | | | | \n"); System.out.print(" | | | | \n"); System.out.print(" |____| | | \n"); System.out.print(" | | | | \n"); System.out.print(" | | | \n"); System.out.print(" | | ___|___ + \n\n"); System.out.print("-----------------------------------\n\n\n"); // print out list of options and let user choose // what they want to do in the program do { switch(switchInt) { case 0: // show menu System.out.print("What would you like to do today?\n"); System.out.print(optionString + "\n"); switchInt = r.readInt(); break; case 1: // temperature conversion, in loop for ease of repeat do { System.out.print("Enter the temperature in Farenheit: "); degreesF = r.readDouble(); degreesC = (5.0 / 9.0) * (degreesF - 32.0); degreesCString = dec.format(degreesC); System.out.print("The temperature in Celsius is " + degreesCString + " degrees.\n\n"); System.out.print("Would you like to convert another number? Y/N\n"); continues = r.readLine().toLowerCase(); if ((continues.equals("y")) || (continues.equals("yes"))) { loop = true; } else { loop = false; } System.out.print('\n'); } while (loop == true); switchInt = 0; break; case 2: // time to get to school System.out.print("Enter your average speed, in km per hour: "); averageSpeed = r.readDouble(); minutesToSchool = 60 * (32.0 / averageSpeed); System.out.print("The trip to school will take " + dec.format(minutesToSchool) + " minutes.\n\n"); switchInt = 0; break; case 3: // e = mc^2 System.out.print("Enter the mass in kg: "); mass = r.readDouble(); energy = mass * Math.pow((3.0 * Math.pow(10,8)), 2); System.out.print("This mass could produce " + energy + " Joules of energy.\n"); bulbs = energy / 360000.0; System.out.print("It could power " + bulbs + " 100-watt light bulbs for an hour.\n\n"); switchInt = 0; break; case 4: // how long have you slept System.out.print("Enter your birthdate (using number for months):\n"); System.out.print("Month: "); month = r.readInt(); System.out.print("Day: "); day = r.readInt(); System.out.print("Year: "); year = r.readInt(); System.out.print("\n\nEnter today's date (using numbers for months):\n"); System.out.print("Month: "); tMonth = r.readInt(); System.out.print("Day: "); tDay = r.readInt(); System.out.print("Year: "); tYear = r.readInt(); alive = ((tYear - year) * 365) + ((tMonth - month) * 30) + (tDay - day); sHours = alive * 8; sDays = sHours / 24.0; System.out.print("\n\nYou have been alive for " + alive + " days.\n"); System.out.print("You have slept " + dec.format(sHours) + " hours.\n"); System.out.print("You have slept " + dec.format(sDays) + " days.\n\n"); switchInt = 0; break; default: // exit program fullLoop = false; break; } } while (fullLoop == true); } }