You guys were a huge help. I was able to get it to work now. As for the if-else issue. I had tried if-else at first but it kept returning a strange error. Once I moved the creation of keyBd that was solved. As for my braces, they aren't required, and we are graded on indentation. I've become used to not doing them because it's that much more indentation that I can mess up.
Since I needed to get both Int's and Doubles out of this program, I had to change the methods to object methods so that it would return anything that was an object. (But I'm sure you knew that.
) In case you were curious, here is the modified working code below.
import java.util.Scanner;
public class myMathOpsTest
{
static Scanner keyBd;
public static Object squareTheNumber()
{
System.out.print("Enter the number:");
double number = keyBd.nextDouble();
if (number == Math.floor(number)) //this line will determine if the number is an integer or a double.
{
int result = myMathOps.squareInt((int)number);
return System.out.printf("The square of the number is: " + result + "\n");
}
else
{
double result1 = myMathOps.squareDouble(number);
return System.out.printf("The Square of the number is: " + result1 + "\n");
}
}//end method
public static Object cubeTheNumber()
{
System.out.print("Enter the number you wish to cube");
double numberCubed = keyBd.nextDouble();
if (numberCubed == Math.floor(numberCubed)) //this line will determine if the number is an integer or a double.
{
int result = myMathOps.cubeInt((int)numberCubed);
return System.out.printf("The cube of your number is: " + result + "\n");
}
else
{
double result1 = myMathOps.cubeDouble(numberCubed);
return System.out.printf("The cube of your number is %.4f \n", result1);
}
}//end method
public static Object raiseTheNumber()
{
System.out.print("Enter the number you wish to raise.(base number):");
double basenumber = keyBd.nextDouble();
System.out.print("Enter the number you are raising it too(the power):");
double powernumber = keyBd.nextDouble();
if (basenumber == Math.floor(basenumber) || powernumber == Math.floor(powernumber))
{
double result = Math.pow(basenumber,powernumber);
return System.out.printf("The result of " + (int)basenumber + " to the power of " + (int)powernumber + " is: " + (int)result + "\n");
}
else
{
double result1 = Math.pow(basenumber,powernumber);
return System.out.printf("The result of " + basenumber + " to the power of " + powernumber + " is: " + result1 + "\n");
}
}//end method
public static Object maximumNumber()
{
System.out.print("Enter the first number, start with a double if the list has them:");
double n1 = keyBd.nextDouble();
if (n1 == Math.floor(n1)) //Testing to see if first number is an integer. If it is, the rest must be integers
{
System.out.print("Enter the Second number:");
int num2 = keyBd.nextInt();
System.out.print("Enter the Third number:");
int num3 = keyBd.nextInt();
int result = myMathOps.maximumInt((int)n1,num2,num3);
return System.out.printf("The maximum is: " + result + "\n");
}
else
{
System.out.print("Enter the Second number:");
double n2 = keyBd.nextDouble();
System.out.print("Enter the Third number:");
double n3 = keyBd.nextDouble();
double result1 = myMathOps.maximumDouble(n1, n2, n3);
return System.out.printf("The maximum is: %.4f \n", result1);
}
}//end method */
public static Object minimumNumber()
{
System.out.print("Enter the first number(Do not start with an integer there are doubles):");
double numberMin1 = keyBd.nextDouble();
if (numberMin1 == Math.floor(numberMin1)) //Testing to see if first number is an integer. If it is, the rest must be integers
{
numberMin1 = Math.floor(numberMin1);
System.out.print("Enter the Second number:");
int numberIntMin2 = keyBd.nextInt();
System.out.print("Enter the Third number:");
int numberIntMin3 = keyBd.nextInt();
int result = myMathOps.minimumInt((int)numberMin1, numberIntMin2, numberIntMin3);
return System.out.printf("The minimum is: " + result + "\n");
}
else
{
System.out.print("Enter the second number:");
double numberDoubleMin2 = keyBd.nextDouble();
System.out.print("Enter the third number:");
double numberDoubleMin3 = keyBd.nextDouble();
double result1 = myMathOps.minimumDouble(numberMin1, numberDoubleMin2, numberDoubleMin3);
return System.out.printf("The minimum is %.4f \n", result1);
}
}//end method */
public static void main(String [] args)
{
keyBd = new Scanner( System.in );
char selection = ' ';
do{
//create the menu
System.out.println("Select from the menu:");
System.out.println("1. Square a Number");
System.out.println("2. Cube a Number");
System.out.println("3. Raise a Number to a Power");
System.out.println("4. Maximum of Three Numbers");
System.out.println("5. Minimum of Three Numbers");
System.out.println("6. Exit");
System.out.print ("Selection:");
//get the menu selection
selection = keyBd.next().charAt(0);
switch( selection )
{
case '1'://square the number
squareTheNumber();
break;
case '2'://Cube the number
cubeTheNumber();
break;
case '3'://Raise the number to a Power.
raiseTheNumber();
break;
case '4'://Find the maximum of three numbers.
maximumNumber();
break;
case '5'://Find the minimum of three numbers.
minimumNumber();
break;
case '6'://Exit the program.
break;
default:
System.out.println("Invalid selection!");
System.out.println("Press a key+<Enter> to continue...");
keyBd.next();
}
}while(selection != '6');
}//end main
}//end myMathOpsTest class
I only had to make one small modification to myMathOps itself. So here it is:
public class myMathOps{
public static int squareInt (int x)
{
return (x * x);
}//end square int method
public static double squareDouble (double x)
{
return (x * x);
}//end square double method
public static int cubeInt (int x)
{
return (x * x * x);
}//end cube int method
public static double cubeDouble (double x)
{
return (x * x * x);
}//end cube double method
public static int maximumInt (int x, int y, int z)
{
int maxValue = x;
if (y > maxValue)
maxValue = y;
if (z > maxValue)
maxValue = z;
if (x > maxValue)
maxValue = x;
return Math.max( x, Math.max(y, z));
}//end int maximum method
public static double maximumDouble (double x, double y, double z)
{
double maxValue = x;
if (y > maxValue)
maxValue = y;
if (z > maxValue)
maxValue = z;
if (x > maxValue)
maxValue = x;
return Math.max( x, Math.max(y, z));
}//end double maximum method
public static double minimumDouble (double x, double y, double z)
{
double minValue = x;
if (y < minValue)
minValue = y;
if (z < minValue)
minValue = z;
if (x < minValue)
minValue = x;
return Math.min( x, Math.min(y, z));
}//end double minimum method
public static int minimumInt(int x, int y, int z)
{
int minValue = x;
if (y < minValue)
minValue = y;
if (z < minValue)
minValue = z;
if (x < minValue)
minValue = x;
return Math.min( x, Math.min(y, z));
}//end int minimum method
}//End myMathOps Class
This has been really illuminating. Thanks again you two.