Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

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

  1. #1
    Junior Member
    Join Date
    Oct 2013
    Posts
    5
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Exclamation Help with objects, methods, and class inheritance.

    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.

    Here is my MyMathOps class. No real problems creating this.(But I'm so often wrong I'll post it anyways)
    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 minimum(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

    The problem I've encountered is in the test (MyMathOpsTest) class.

    import java.util.Scanner;
     
    public class myMathOpsTest
    {
     
       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
     
       public static double 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 method
     
       public static double 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 method
     
       public static double 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);
     
                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 method
     
       public static double 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 method
     
       public static void main(String [] args)
       {
     
       Scanner 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

    Specifically, my issue is this error:
    myMathOpsTest.java:21: error: '.class' expected
             int result = myMathOps.squareInt((int)number);
                 ^
    myMathOpsTest.java:21: error: not a statement
             int result = myMathOps.squareInt((int)number);
             ^
    myMathOpsTest.java:21: error: illegal start of expression
             int result = myMathOps.squareInt((int)number);
                        ^
    myMathOpsTest.java:21: error: ';' expected
             int result = myMathOps.squareInt((int)number);
                                   ^
    myMathOpsTest.java:26: error: '.class' expected
             double result1 = myMathOps.squareDouble(number);
                    ^
    myMathOpsTest.java:26: error: not a statement
             double result1 = myMathOps.squareDouble(number);
             ^
    myMathOpsTest.java:26: error: illegal start of expression
             double result1 = myMathOps.squareDouble(number);
                            ^
    myMathOpsTest.java:26: error: ';' expected
             double result1 = myMathOps.squareDouble(number);
                                       ^
    myMathOpsTest.java:37: error: '.class' expected
             int result = myMathOps.cubeInt((int)numberCubed);
                 ^
    myMathOpsTest.java:37: error: not a statement
             int result = myMathOps.cubeInt((int)numberCubed);
             ^
    myMathOpsTest.java:37: error: illegal start of expression
             int result = myMathOps.cubeInt((int)numberCubed);
                        ^
    myMathOpsTest.java:37: error: ';' expected
             int result = myMathOps.cubeInt((int)numberCubed);
                                   ^
    myMathOpsTest.java:42: error: '.class' expected
             double result1 = myMathOps.cubeDouble(numberCubed);
                    ^
    myMathOpsTest.java:42: error: not a statement
             double result1 = myMathOps.cubeDouble(numberCubed);
             ^
    myMathOpsTest.java:42: error: illegal start of expression
             double result1 = myMathOps.cubeDouble(numberCubed);
                            ^
    myMathOpsTest.java:42: error: ';' expected
             double result1 = myMathOps.cubeDouble(numberCubed);

    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.


  2. #2
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    610
    My Mood
    Sleepy
    Thanks
    10
    Thanked 93 Times in 86 Posts

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

    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.

  3. The Following User Says Thank You to PhHein For This Useful Post:

    Gingerbread Fetus (October 18th, 2013)

  4. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,855
    My Mood
    Mellow
    Thanks
    239
    Thanked 722 Times in 708 Posts

    Default 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:
    public class myMathOpsTest
    {
        static Scanner keyBd;
     
        public static double squareTheNumber()
        {
    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.

  5. The Following User Says Thank You to GregBrannon For This Useful Post:

    Gingerbread Fetus (October 18th, 2013)

  6. #4
    Junior Member
    Join Date
    Oct 2013
    Posts
    5
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Red face 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.

    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.

Similar Threads

  1. I need some help with inheritance methods.
    By 13078 in forum Object Oriented Programming
    Replies: 1
    Last Post: December 31st, 2012, 11:14 AM
  2. Problem with calling objects from same-class methods in main
    By thekbon in forum Object Oriented Programming
    Replies: 12
    Last Post: December 18th, 2012, 12:10 AM
  3. Java and Flickr - Help with Inheritance between Main Class and sub-class
    By thientanchuong in forum What's Wrong With My Code?
    Replies: 0
    Last Post: December 12th, 2012, 07:10 PM
  4. display methods with inheritance
    By ddotreprah in forum Object Oriented Programming
    Replies: 2
    Last Post: November 20th, 2011, 10:06 PM
  5. Replies: 7
    Last Post: July 21st, 2011, 02:29 PM