views:

878

answers:

4

I am trying to use one file to create a menu in the command window. The user selects from those menu options. They are prompted to enter a number. The number is passed to two overloaded methods which determine if the number is an integer or a float. After the calculation is done the result is printed to the screen and the menu reappears. Here is the code from my two files.

MyMathOpsTest class:

import java.util.Scanner; // import Scanner class
public class MyMathOpsTest
{
   //method to pause until a key is pressed
   public static void pause() 
   { 
       try 
   { 
       System.out.print("Press <Enter> to continue..."); 
       System.in.read(); 
   } 
       catch (Exception e)
       {
           System.err.printf("Error %s%c\n",e.getMessage(),7);
       }
}//end pause

public static void main( String args[] )
{
 //variables to capture keyboard input
 Scanner keyBd = new Scanner( System.in );
 char selection;
 //int selection;

 do{//display 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[1-6]: " );

  //get menu selection
  selection = keyBd.next().charAt(0);
  //selection = keyBd.nextInt();

  //process menu selection
  switch (selection){
   case '1':
    MyMathOpsTest.squareTheNumber();
    pause();
    break;
   case '2':
    MyMathOpsTest.cubeTheNumber();
    pause();
    break;
   case '3':    
    MyMathOpsTest.raiseTheNumber();
    pause();
    break;
   case '4':    
    MyMathOpsTest.maximumNumber();
    pause();
    break;
   case '5':    
    MyMathOpsTest.minimumNumber();
    pause();
    break;
    case '6':
    //recognize as valid selection but do nothing
    break;
   default :
    System.out.printf("%c\n",7);
    System.out.println("Invalid Selection");
  }//end switch

 }while( selection != '6');
} // end method main

public static void squareTheNumber()
{

}

public static void cubeTheNumber()
{
}

public static void raiseTheNumber()
{
}

public static void maximumNumber()
{
MyMathOps.maximum();
}

public static void minimumNumber()
{
}

} // end class MyMathOpsTest

MyMathOps class:

import java.util.Scanner;

public class MyMathOps
{
public static int square(x:Integer):Integer
{
}

public static double square(x:Double):Double
{
}

public static int cube(x:Integer):Integer
{
}

public static double cube(x:Double):Double
{
}

public static int maximum(x:Integer, y:Integer, z:Integer):Integer
{
 // create Scanner for input from command window
 Scanner input = new Scanner( System.in );
 // obtain user input
 System.out.print( "Enter three integer values separated by spaces: ");
 int numberl = input.nextInt();
 // read first integer
 int number2 = input.nextInt();
 // read second double
 int number3 = input.nextInt();
 // read third double
 // determine the maximum value
 int result = maximum( numberl, number2, number3 );
 // display maximum value
 System.out.println( "Maximum is: " + result );
} // end method maximum

public static double maximum(x:Double, y:Double, z:Double):Double
{
 // create Scanner for input from command window
 Scanner input = new Scanner( System.in );
 // obtain user input
 System.out.print( "Enter three floating-point values separated by spaces: ");
 number1 = input.nextDouble();
 // read first double double
 number2 = input.nextDouble();
 // read second double
 double number3 = input.nextDouble();
 // read third double
 // determine the maximum value
 double result = maximum( numberl, number2, number3 );
 // display maximum value
 System.out.println( "Maximum is: " + result );
} // end method maximum

public static int minimum(x:Integer, y:Integer, z:Integer):Integer
{
 // create Scanner for input from command window
 Scanner input = new Scanner( System.in );
 // obtain user input
 System.out.print( "Enter three integer values separated by spaces: ");
 int numberl = input.nextInt();
 // read first integer
 int number2 = input.nextInt();
 // read second double
 int number3 = input.nextInt();
 // read third double
 // determine the minimum value
 int result = minimum( numberl, number2, number3 );
 // display minimum value
 System.out.println( "Minimum is: " + result );
} // end method minimum

public static double minimum(x:Double, y:Double, z:Double):Double
{
// create Scanner for input from command window
 Scanner input = new Scanner( System.in );
 // obtain user input
 System.out.print( "Enter three floating-point values separated by spaces: ");
 number1 = input.nextDouble();
 // read first double double
 number2 = input.nextDouble();
 // read second double
 double number3 = input.nextDouble();
 // read third double
 // determine the minimum value
 double result = minimum( numberl, number2, number3 );
 // display minimum value
 System.out.println( "Minimum is: " + result );
} // end method minimum

} // end class MyMathOps

This code is a combination of code I type myself and example code from my text book. This will not compile for me in jGRASP. I get these errors.

MyMathOps.java:10: expected public static int square(x:Integer):Integer ^ MyMathOps.java:96: ')' expected } // end method minimum ^ 2 errors

----jGRASP wedge: exit code for process is 1. ----jGRASP: operation complete.

What am I doing wrong here? I have spent hours working on this and reading in my textbook. If I do not get this right. I will get a bad grade. I need to get a good grade in this class so I can get into a top notch Computer Science University. Thanks for your help.

In the unlikely event that my instructor or any administrator from Salt Lake Community College ever comes across this question, let me make my intentions clear. This question is posted in the greatest spirit of academic honesty. I ask this question to seek general advice and help in understanding the proper way to use the Java programming language. I in no way use the work of others and represent it as my own work. I use the answers provided here as a general aid in my understanding. I do all my own work and do not copy work provided by people answering my question.

+7  A: 

The lines like this are not valid Java syntax:

public static int square(x:Integer):Integer
public static int maximum(x:Integer, y:Integer, z:Integer):Integer
...

This looks like a UML or pseudo-code syntax. "x:Integer" is "language-agnostic" notation that means that x is an Integer type (which maps to an int or Integer object in Java). The ":Integer" at the end means that the method returns an Integer type, which you are doing correctly already.

Try changing all your method declarations to look like this:

public static int square(int x) // Or "Integer x" if you want to use the Integer class, rather than the primitive int
public static int maximum(int x, int y, int z)
....
Andy White
"int" yes, "Integer" no. You should always prefer a primitive type over a boxed primitive (wrapper) type unless you have good reason to do otherwise.
Alan Moore
+1  A: 

Also, at the end of the first method pause(), you need another curly brace:

public static void pause() 
{ 
    try 
    { 
      System.out.print("Press <Enter> to continue..."); 
      System.in.read(); 
    } 
    catch (Exception e)
    {
      System.err.printf("Error %s%c\n",e.getMessage(),7);
    }
}<-- this one is missing in yours

Hope this helps!

Peter Nore
Looks like he has that one, but it is not indented properly. It looks like he is closing the class there, but that actually closes the pause() method. However, he is missing a curly brace to end his class.
Kevin Crowell
+2  A: 

I am guessing that you are used to Pascal (or a derivative).

public static int square(x:Integer):Integer

in Java that is

public static int square(int x)

Also since the code is inside of "MyMathOpsTest" you do not need to prefix the method calls with "MyMathOpsTest.".

Also, why call it "MyMathOps" instead of "MathOperationsTest"? Of course it is yours - it doesn't be long to me or anyone else! Pick names that have meaning, try to avoid shorthands like "Ops", unless it is common for the field you are working in (URL is a good one, "Ops" isn't).

And now for the generl programming advice for a beginner:

  • write a single line of code
  • get that line of code to compile
  • once that line of code compiles work on the next one
  • get the next line of code to compile
  • keep doing that until the program is done.

There is no point in making the same mistakes over and over again - all you get good at is making mistakes, and that isn't much fun.

So to get you started...

Step 1:

public class MathOperations
{
    public static int maximum(final int x, final int y, final int z)
    {
    }
}

(compile the above code)

Step 2:

public class MathOperations
{
    public static int maximum(final int x, final int y, final int z)
    {
        final Scanner input;
    }
}

(compile the above code)

Step 3:

public class MathOperations
{
    public static int maximum(final int x, final int y, final int z)
    {
        final Scanner input;

        intput = new Scanner(System.in);
    }
}

(compile the above code)

and then keep going one line at a time. Once you get the hang of it you can do more than one line, but at the start, doing it one line at a time will show you immediately when you make a mistake. Make sure that you fix ALL of the mistakes before you move on to the next line.

TofuBeer
The names of the classes are requirements of the assignment. Because this assignment is for an introductory course a lot of things that would normally be left up to me to decide are specified by the assignment description.
Patrick Cassell
ah... too bad people actually teach the "My" stuff.
TofuBeer
A: 

I don't know what the point of the exercise is - the math ops, the overloading, or the menu. But I'd recommend that you start over with these as your basis. At least they compile and run:

public class MyMathOps
{
   public static int square(int x)
   {
       return x*x;
   }

   public static double square(double x)
   {
       return x*x;
   }

   public static int cube(int x)
   {
       return x*x*x;
   }

   public static double cube(double x)
   {
       return x*x*x;
   }

   public static int maximum(Integer... values)
   {
       Integer maxValue = Integer.MIN_VALUE;

       for (Integer value : values)
       {
           if (value.compareTo(maxValue) > 0)
           {
               maxValue = value;
           }
       }

       return maxValue;
   }

   public static double maximum(Double... values)
   {
       Double maxValue = Double.MIN_VALUE;

       for (Double value : values)
       {
           if (value.compareTo(maxValue) > 0)
           {
               maxValue = value;
           }
       }

       return maxValue;
   }

   public static int minimum(Integer... values)
   {
       Integer minValue = Integer.MAX_VALUE;

       for (Integer value : values)
       {
           if (value.compareTo(minValue) < 0)
           {
               minValue = value;
           }
       }

       return minValue;
   }

   public static double minimum(Double... values)
   {
       Double minValue = Double.MIN_VALUE;

       for (Double value : values)
       {
           if (value.compareTo(minValue) < 0)
           {
               minValue = value;
           }
       }

       return minValue;
   }

}

and the test class (simplified):

public class MyMathOpsTest
{
    public static void main(String args[])
    {
        Integer [] intValues = { 1, 2, 3, };
        Double [] doubleValues = { 11.0, 14.0, -6.0 };

        for (Integer value : intValues)
        {
            System.out.println("value  : " + value);
            System.out.println("squared: " + MyMathOps.square(value));
            System.out.println("cubed  : " + MyMathOps.cube(value));
            System.out.println("min    : " + MyMathOps.minimum(intValues));
            System.out.println("max    : " + MyMathOps.maximum(intValues));
        }

        for (Double value : doubleValues)
        {
            System.out.println("value  : " + value);
            System.out.println("squared: " + MyMathOps.square(value));
            System.out.println("cubed  : " + MyMathOps.cube(value));
            System.out.println("min    : " + MyMathOps.minimum(doubleValues));
            System.out.println("max    : " + MyMathOps.maximum(doubleValues));
        }
    }
}

When this is done running, you'll know that your methods are correct. Spare yourself the difficulties of reading in values on the first try.

duffymo