I've been struggling with this assignment for hours now. I think it's important that I summarize the assignment before asking for help.

The first task was to create the class MyMathOps, to handle the cube of integers or doubles, the square of integers or doubles, and to handle finding the max and min of a group of integers or a group of doubles.
Writing this class was rather easy, I don't think I have any problems with that part of the assignment.

The next step was to create a MyMathOpsTest class that would utilize the MyMathOps class through a menu. Writing the menu was no problem, what has confused me is the following two steps.

The following methods should be called by the selection in the menu: squareTheNumber(), cubeTheNumber(), raiseTheNumber(), maximumNumber(), and minimumNumber()

These methods should ask the user for input, then pass that input to the appropriate Math or MyMathOps method and print the result.

With that in mind. Let me now explain what I've done and the error I'm getting.

importjava.util.Scanner;publicclass myMathOpsTest
{publicstaticdouble 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);System.out.printf("The square of the number is: "+ result);if(number !=Math.floor(number))double result1 = myMathOps.squareDouble(number);System.out.printf("The Square of the number is: "+ result1);}//end methodpublicstaticdouble 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);System.out.printf("The cube of your number is %.4f" , result);if(numberCubed !=Math.floor(numberCubed))double result1 = myMathOps.cubeDouble(numberCubed);System.out.printf("The cube of your number is "+ result1);}//end methodpublicstaticdouble 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();double result =Math.pow(basenumber,powernumber);System.out.println("The result of "+ basenumber +" to the power of "+ powernumber +" is: "+ result);}//end methodpublicstaticdouble 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 integersSystem.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);System.out.printf("The maximum is: %.4f", result);if(n1 !=Math.floor(n1))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);}//end methodpublicstaticdouble minimumNumber(){System.out.print("Enter the first number(Do not start with an integer if it's a list of doubles):");double numberMin1 = keyBd.nextDouble();int numberIntMin1 =0;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);System.out.printf("The minimum is %.4f", result);if(numberMin1 !=Math.floor(numberMin1))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);
ystem.out.printf("The minimum is %.4f", result1);}//end methodpublicstaticvoid main(String[] args){
Scanner keyBd =new Scanner(System.in);char selection =' ';do{//create the menuSystem.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 ){case1://square the number
squareTheNumber();break;case2://Cube the number
cubeTheNumber();break;case3://Raise the number to a Power.
raiseTheNumber();break;case4://Find the maximum of three numbers.
maximumNumber();break;case5://Find the minimum of three numbers.
minimumNumber();break;case6://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've no idea why this error is happening. Or honestly if I'm even on the right track to solve this assignment. I figured that what I did is what he meant by asking me to create and then call these methods.

October 18th, 2013, 08:04 AM

PhHein

Re: Help with objects, methods, and class inheritance.

Code :

public static double 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);
System.out.printf("The square of the number is: " + result);
if (number != Math.floor(number))
double result1 = myMathOps.squareDouble(number);
System.out.printf("The Square of the number is: " + result1);
}//end method

is that supposed to be an if else construct? Where are the braces? get rid of all methods, but one. Clean up and compile. If that works, implement the next method and compile.

October 18th, 2013, 08:05 AM

GregBrannon

Re: Help with objects, methods, and class inheritance.

You have a number of errors for which the error messages you're receiving aren't the most helpful. I'm getting the same ones and trying to make sense of why those specific messages are being given, but sometimes it's a mystery. The compiler is confused by your code and doing the best it can to explain its confusion.

Most of your problems are related to scope or visibility of your variables. For example, you declare Scanner keyBd in the main() method and then try to use it throughout the other methods. That won't work, because keyBd will be local to main() and only visible inside main(). Declare keyBd as a class variable:

and then initialize it in main() as you are now but remove the 'Scanner' from that line.

You have repeats of this same problem: you declare variables inside if clauses and then try to use them outside. Declaring them outside the if clause.

Then you have several methods that are supposed to return values but return nothing. Either change them to void methods or return the apporpriate values from them.

Fixing the problems I've highlighted will whittle down your errors, but there will probably still be some. Come back with upated code and the resulting shorter list.

October 18th, 2013, 10:02 AM

Gingerbread Fetus

Re: Help with objects, methods, and class inheritance.

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.

Code Java:

importjava.util.Scanner;publicclass myMathOpsTest
{static Scanner keyBd;publicstaticObject 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);returnSystem.out.printf("The square of the number is: "+ result +"\n");}else{double result1 = myMathOps.squareDouble(number);returnSystem.out.printf("The Square of the number is: "+ result1 +"\n");}}//end methodpublicstaticObject 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);returnSystem.out.printf("The cube of your number is: "+ result +"\n");}else{double result1 = myMathOps.cubeDouble(numberCubed);returnSystem.out.printf("The cube of your number is %.4f \n", result1);}}//end methodpublicstaticObject 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);returnSystem.out.printf("The result of "+(int)basenumber +" to the power of "+(int)powernumber +" is: "+(int)result +"\n");}else{double result1 =Math.pow(basenumber,powernumber);returnSystem.out.printf("The result of "+ basenumber +" to the power of "+ powernumber +" is: "+ result1 +"\n");}}//end methodpublicstaticObject 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);returnSystem.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);returnSystem.out.printf("The maximum is: %.4f \n", result1);}}//end method */publicstaticObject 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);returnSystem.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);returnSystem.out.printf("The minimum is %.4f \n", result1);}}//end method */publicstaticvoid main(String[] args){
keyBd =new Scanner(System.in);char selection =' ';do{//create the menuSystem.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: