Results 1 to 9 of 9
  1. #1
    ianyappy is offline Member
    Join Date
    Dec 2010
    Posts
    13
    Rep Power
    0

    Default Passing final parameter from a class

    How would I pass a final parameter from a class into a method? Example:

    Java Code:
    class Vehicle{
         private direction = 0;
         public static final int TURN_LEFT = -90;
         public static final int TURN_RIGHT = 90;
         
         public void turn (final int TURN_LEFT){     // This is probably wrong... How to fix this?
            direction = direction + TURN_LEFT;
         }
    }

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    How would I pass a final parameter from a class into a method?

    I'm not completely sure what you are trying to do. Perhaps you mean something like this:

    Java Code:
    class Vehicle {
         private int direction = 0;
         public static final int TURN_LEFT = -90;
         public static final int TURN_RIGHT = 90;
         
         public void turn (int amount) {
            //direction = direction + amount;
            // better as
            direction += amount;
         }
            // the following code would probably be in another method
            // in another class
        public static void main(String[] args) {
            Vehicle test = new Vehicle();
            System.out.println("test created, direction=" + test.direction);
    
            test.turn(TURN_LEFT);
            System.out.println("test turned, direction=" + test.direction);
    
            test.turn(TURN_RIGHT);
            test.turn(TURN_RIGHT);
            test.turn(TURN_RIGHT);
            System.out.println("finally, direction=" + test.direction);
        }
    }

    Note that what the method ends up with is the value of the parameter. SO it doesn't really matter whether a variable used as a parameter is final or not - the method uses the value it is given in the same way.

    ----------------------

    If you are not trying to do this (use final variables as parameters), say.

  3. #3
    ianyappy is offline Member
    Join Date
    Dec 2010
    Posts
    13
    Rep Power
    0

    Default

    This was kind of an exercise on overloading. I need to create two methods, one where turn accepts any float value and another where turn accepts either of the two final parameters TURN_LEFT or TURN_RIGHT declared in the class Vehicle. I suppose this seems a little weird.. but that was the question.

  4. #4
    ianyappy is offline Member
    Join Date
    Dec 2010
    Posts
    13
    Rep Power
    0

    Default

    To be specific, the question is exactly as such:

    Add two turn methods to Vehicle: one that takes a number of degrees to turn and one that takes either of the constants Vehicle.TURN_LEFT or Vehicle.TURN_RIGHT.

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    Since the methods are overloaded you will need a different type for TURN_LEFT and TURN_RIGHT. That is, these things can't be ints because if they were the compiler would have no way of telling which turn() method to use.

    Java provides enums for just this sort of situation. Unlike the "static final whatevers" that we used to use an enum is a type and, so, can be used in overloaded methods (they have other advantages too). They are described in the EnumTypes section of Oracle's Tutorial and the 1.5 Guide.

    Consider something like:

    Java Code:
    import java.awt.Color;
    
    class Vehicle{
         private Color color = Color.BLACK;
    
         public enum StandardColor {
             RED(Color.RED), BLUE(Color.BLUE);
             Color color;
             StandardColor(Color c) {
                 color = c;
             }
         }
    
         public void paint(Color c) {
             System.out.println("Nonstandard!");
             color = c;
         }
    
         public void paint(StandardColor c) {
             System.out.println("Standard!");
             color = c.color;
         }
    
            // the following code would probably be in another method
            // in another class
        public static void main(String[] args) {
            Vehicle test = new Vehicle();
            System.out.println("test created, color=" + test.color);
    
            test.paint(Color.GREEN);
            System.out.println("test painted=" + test.color);
    
            test.paint(StandardColor.BLUE);
            System.out.println("test painted=" + test.color);
    
        }
    }

    You could make Vehicle.TURN_XXX some other type (like a String) and overload the methods that way. But it seems to me that enums would be neater.

  6. #6
    ianyappy is offline Member
    Join Date
    Dec 2010
    Posts
    13
    Rep Power
    0

    Default

    Thanks pbrockway2, followed your advice and did something like this

    Java Code:
    // In Vehicle.java
    public class Vehicle{
     private float direction = 0;
    
     public enum FixedAngle{
        TURN_LEFT(-90.0),TURN_RIGHT(90.0);
        private double turnAngle;
        FixedAngle(double turnAngle){
          this.turnAngle = turnAngle;
        }
        double value(){
           return turnAngle;
        }
      }
          
      // My overloaded turn methods
      public void turn(float turnAngle){
        direction = direction + turnAngle;
      }
    	
      public void turn(FixedAngle turnAngle){
         double z = turnAngle.value();
      }
         
           ...
    }
    
    // In TurnVehicle.java
    public class TurnVehicle{
    
      public static void main(String[] args){
        Vehicle Z = new Vehicle(90,90,"lame_driver");
        System.out.println("Initial direction: " + Z.getDirection());
        Z.turn(-100);
        System.out.println("Turning -100: " + Z.getDirection());
        Z.turn(Vehicle.FixedAngle.TURN_LEFT);
        System.out.println("Turning Left: " + Z.getDirection());			
         Z.turn(Vehicle.FixedAngle.TURN_RIGHT);
        System.out.println("Turning Right: " + Z.getDirection());			
       }
    }
    Is there no other way of writing in such that I don't have to include the .FixedAngle?

    As a side question, in the enum I could only use doubles but not floats, got an error message from the compiler (javac) as such (the arrows are misplaced, should be pointing at each '('):

    Vehicle.java:14: cannot find symbol
    symbol : constructor FixedAngle(double)
    location: class Vehicle.FixedAngle
    TURN_LEFT(-90.0),TURN_RIGHT(90.0);
    ^
    Vehicle.java:14: cannot find symbol
    symbol : constructor FixedAngle(double)
    location: class Vehicle.FixedAngle
    TURN_LEFT(-90.0),TURN_RIGHT(90.0);
    ^
    2 errors
    Last edited by ianyappy; 12-02-2010 at 11:06 AM.

  7. #7
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    If can use floats, but the constructor and the values of the enums must agree:

    Java Code:
    public enum FixedAngle{
        TURN_LEFT(-90.0f),TURN_RIGHT(90.0f);
        private float turnAngle;
        FixedAngle(float turnAngle){
          this.turnAngle = turnAngle;
          // etc

    Floats are relatively little used in Java. (Also you were using ints at the start of the thread.)

    Is there no other way of writing in such that I don't have to include the .FixedAngle?

    If your classes are in packages you can use an "import static" and refer to the fixed angles in other classes as TURN_XXX. Or you could have a final static variable variable in your Vehicle class:

    Java Code:
    public class Vehicle{
        private float direction = 0;
    
        public enum FixedAngle{
            TURN_LEFT(-90.0),TURN_RIGHT(90.0);
            private double turnAngle;
            FixedAngle(double turnAngle){
                this.turnAngle = turnAngle;
            }
            double value(){
                return turnAngle;
            }
        }
        public static final FixedAngle TURN_LEFT = FixedAngle.TURN_LEFT;
        // ...

    The fixed angle TURN_LEFT can now be used the way you were trying to use an int at the start of the thread but now it is a different type and, so, can be used for overloading.

    --------------------

    Check you get the right answers in main(). Your turn(FixedAngle) method doesn't seem to be changing the value of direction.

  8. #8
    ianyappy is offline Member
    Join Date
    Dec 2010
    Posts
    13
    Rep Power
    0

    Lightbulb

    Ah, ok understood that all. Thanks for the help!

  9. #9
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

Similar Threads

  1. Switch Statement/Parameter Passing
    By spmooney@hotmail.co.uk in forum NetBeans
    Replies: 1
    Last Post: 01-06-2010, 02:50 PM
  2. passing parameter to a thread
    By adammyth in forum Threads and Synchronization
    Replies: 1
    Last Post: 01-02-2010, 08:58 PM
  3. passing a parameter
    By aarthi2learn in forum AWT / Swing
    Replies: 4
    Last Post: 12-22-2008, 06:46 AM
  4. Passing short value as parameter
    By javanewbie83 in forum New To Java
    Replies: 16
    Last Post: 07-16-2008, 06:27 AM
  5. passing an enum type as a parameter ??!
    By SCS17 in forum New To Java
    Replies: 11
    Last Post: 07-13-2008, 02:44 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •