Results 1 to 7 of 7
  1. #1
    BDF
    BDF is offline Member
    Join Date
    Apr 2014
    Posts
    3
    Rep Power
    0

    Default Passing arguments noob question

    Ok I'm a FNG with programming but I understand the basic concepts of java. A friend of mine wrote a program for me that simulates a fire control computer for naval gunnery. While it mostly complete he left it for me to finish hence my desire to learn java. So I've added a few features but I'm struggling with the more advanced stuff. What I'm currently trying to do is add a simple countdown timer to indicate when a salvo will land and when I create a method in one class with an argument, I can't implement it in another class because of the argument. Ex:

    So in class Mark8 I have the following method:

    Java Code:
    public void drawSplashTime(Salvo s){
            g().drawString(("Splash Time is " + s.splashTime), 50, 50 );
            onDrawComplete();
     
                }
    The argument being past into it comes from this method:

    Java Code:
    public Salvo createSalvo(int numShots) {
            Salvo s = new Salvo();
            s.shooter = ship;
            s.target = ship.target;
            s.firingPos = new Vec2(ship.position);
     
            s.numShotsFired = numShots;
            s.firedTime = SimApp.getTime();
     
     
            double roughRange = generatedRange()  ;
            double roughTOF = ship.ballistics.rangeToTOF.eval(roughRange);
            Vec2 enemyTravel = ghost.getVelocity().scale(roughTOF);
     
            s.aimPos = ghost.position.add(enemyTravel);
     
            s.targetPos = new Vec2(s.target.position);
     
            s.rangeOnSights = s.firingPos.distanceTo(s.aimPos) - 560 + rangeSpottingCorrection() + app.us.firstSalvoMpiError();   //Todo Change this
     
            //s.deflectionOnSights = deflectionSpottingCorrection() ;
     
            double TOF = ship.ballistics.rangeToTOF.eval(s.rangeOnSights);
     
            s.splashTime = s.firedTime + TOF;
     
            System.out.println("Splash time is " + Math.round((float)s.splashTime));
     
            s.aimBearing = s.firingPos.bearingTo(s.aimPos) + deflectionSpottingCorrection();
     
            return s;
     
        }
    And it's being called in class SimApp here:

    Java Code:
     void paintWorld(Salvo s) {
            map.draw();
            us.mk8.draw();
            us.mk8.timeToSplash(s);
     
     
        }

    When I do this, it screws things up down the line. I understand the basic concepts of class and methods but the more advanced stuff is still quite a challenge. I can post the relevant classes later if it helps.


    Many thanks!
    Last edited by BDF; 04-01-2014 at 03:33 AM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default Re: Passing arguments noob question

    Please edit your post and wrap your code with code tags:
    [code]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,185
    Rep Power
    20

    Default Re: Passing arguments noob question

    "When I do this, it screws things up down the line."

    Compilation errors?
    Runtime stack traces?
    Simply doesn't do what you want it to?

    There's a lot that could be going wrong, but essentially saying "it doesn't work" gives us no clues whatsoever.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  4. #4
    BDF
    BDF is offline Member
    Join Date
    Apr 2014
    Posts
    3
    Rep Power
    0

    Default Re: Passing arguments noob question

    Yeah my bad. Basically I pass the same parameter (Salvo s) to SimApp.paintWorld then to Simapp.run. This doesn't work and it gives me an error code as follows:

    java: SimApp is not abstract and does not override abstract method run() in java.lang.Runnable

    Here is class SimApp:

    Java Code:
    public class SimApp implements Runnable, InputListener {
    
        Thread thread;
        World world;
        WorldGUI map;
        JFrame mapFrame;
    
        Ship us;
        Ship them;
    
        JFrame mk8Frame;
    
    
        // allow real time to be modified to run faster (> 1) or slower (< 1)
        double timeMult = 1.0;
        double time;
        long lastMillis = -1000;
    
        static SimApp simApp; // singleton
    
    
        public static void main(String args[]) {
            SimApp app = new SimApp();
    
            app.init(null);
            app.start();
        }
    
    
        private Input shift;
        private Input fireInput;
        private Input ownSpeedUpInput;
        private Input ownSpeedDownInput;
        private Input ownHeadingLeftInput;
        private Input ownHeadingRightInput;
    
        private double ownThrottleAggregateInput;
        private double ownHeadingAggregateInput;
    
        public SimApp() {
            assert simApp == null; // we want just one of these!
            simApp = this;
    
            shift = new Input(this, KeyEvent.VK_SHIFT);
            fireInput = new Input(this, KeyEvent.VK_F, Input.TYPE_DOWN);
    
            ownSpeedUpInput = new Input(this, KeyEvent.VK_I);
            ownSpeedDownInput = new Input(this, KeyEvent.VK_K);
            ownHeadingLeftInput = new Input(this, KeyEvent.VK_J);
            ownHeadingRightInput = new Input(this, KeyEvent.VK_L);
    
            lastMillis = System.currentTimeMillis();
    
    
            world = new World();
    
                // our ship
            us = world.addShip(new Ship(ImageCache.get("iowa.png")));
            us.giveGuns(Ballistics.US_16_50, 9);
            
    
            // target ship
            them = world.addShip(new Ship(ImageCache.get("yamato.png")));
    
    
            map = new WorldGUI(this);
            map.setSize(1024, 819); // TODO: figure out how to do this properly
            mapFrame = new JFrame("Map");
            mapFrame.add("Center", map);
            mapFrame.pack();
            mapFrame.addWindowListener(new WindowAdapter() {
                public void windowClosing(WindowEvent e) {
                    System.exit(0);
                }
            });
            mapFrame.addKeyListener(map);
    
            us.mk8 = new Mark8(this, us);
            
            mk8Frame = new JFrame("Ford Mark 8 Rangekeeper");
            mk8Frame.add("Center", us.mk8);
            mk8Frame.pack();
            mk8Frame.addWindowListener(new WindowAdapter() {
                public void windowClosing(WindowEvent e) {
                    System.exit(0);
                }
            });
            mk8Frame.addKeyListener(us.mk8);
    
        }
    
    
        public void onInput(Input i, boolean b) {
            if (i == fireInput) {
                us.fire();
            } else
            if (i == ownSpeedUpInput) {
                adjustOwnSpeed(b ? 1 : -1);
            } else
            if (i == ownSpeedDownInput) {
                adjustOwnSpeed(b ? -1 : 1);
            } else
            if (i == ownHeadingLeftInput) {
                adjustOwnHeading(b ? -1 : 1);
            } else
            if (i == ownHeadingRightInput) {
                adjustOwnHeading(b ? 1 : -1);
            }
        }
    
        private void adjustOwnSpeed(double d) {
            ownThrottleAggregateInput += d;
        }
        private void adjustOwnHeading(double d) {
            ownHeadingAggregateInput += d;
        }
    
    
        boolean shift() {
            return shift.active;
        }
    
    
        static double getTime() {
            return simApp.time;
        }
    
        void test(){
            System.out.println("Ship's position is " + them.position);
        }
    
    
        
        public void init(Object nextTurn) {
    
            double range;
            double bearing;
    
            double ourSpeed;
            double ourHeading;
    
            double theirSpeed;
            double theirHeading;
            double theirDesiredHeading;
            double theirDesiredSpeed;
    
            
    
    
    
            // Initial settings
    
            ourSpeed = Units.knots(28);
            ourHeading = 280;
            theirHeading = 220;
            bearing = 335;
            range = Units.yards(38000);
            theirSpeed = Units.knots(28);
            theirDesiredSpeed = Units.knots(28);
            theirDesiredHeading = 220;
    
    
            //
    
            them.queueTurn(1560, 270);
            them.queueTurn(1680, 220);
            them.queueTurn(2160, 270);
            them.queueTurn(2640, 220);
    
    
    
            us.firstSalvoMpiError();
            us.setInitialSpeedAndHeading(ourSpeed, ourHeading);
    
            them.position = us.position.add(Vec2.directionAndMagnitude(bearing, range));
            them.setInitialSpeedAndHeading(theirSpeed, theirHeading);
            them.setDesiredHeading(theirDesiredHeading);
            them.setDesiredSpeed((float) theirDesiredSpeed);
    
    
    
    
            map.init();
            us.mk8.init();
    
            us.setTarget(them);
    
            mapFrame.setVisible(true);
            mk8Frame.setVisible(true);
        }
    
    
    
    
        void paintWorld(Salvo s) {
            map.draw();
            us.mk8.draw();
            us.mk8.timeToSplash(s);
            }
    
    
        public synchronized void start() {
            
            thread = new Thread(this);
            thread.setName("SimApp.thread");
            
    
            thread.setPriority(Thread.MIN_PRIORITY);
            thread.start();
                    
        }
    
    
        public void stop() {
            thread = null;
        }
    
        private void uiTick(double deltaT) {
            double factor = 1;
            if (ownThrottleAggregateInput != 0) {
                if (shift())
                    factor = 8;
                us.adjustThrottle(Units.knots((float)factor) * ownThrottleAggregateInput * deltaT);
            }
            if (ownHeadingAggregateInput != 0) {
                factor = shift() ? 10 : 2;
                us.adjustDesiredHeading(factor * ownHeadingAggregateInput * deltaT);
            }
        }
    
        public void run(Salvo s) {
            while (thread == Thread.currentThread()) {
    
                long t = System.currentTimeMillis();
    
                if (lastMillis > 0) {
    
                    double deltaT = timeMult * ((t - lastMillis) / 1000.0);
    
                    // advance time
                    time += deltaT;
    
                    
                    world.simTick(deltaT);
    
                    uiTick(deltaT);
    
                    paintWorld(s);
                }
    
                lastMillis = t;
    
                try {
                    Thread.sleep(10);
                } catch (InterruptedException e) {
                    break;
                }
    
    
            }
            thread = null;
        }
    
        static final int KEY_SYNC_RANGE = KeyEvent.VK_R;
        static final int KEY_SYNC_BEARING = KeyEvent.VK_B;
    
    
    
        public void keyPressed(KeyEvent e) {
            Input.keyDown(e.getKeyCode());
    
        }
    
    
        public void keyReleased(KeyEvent e) {
            Input.keyUp(e.getKeyCode());
        }
    
        public void keyTyped(KeyEvent e) {
    
        }
    
    
    }
    I realize that I may be biting off more than I can chew at this point but practical examples is easiest for me to learn from.

    Thanks!
    BDF

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,732
    Blog Entries
    7
    Rep Power
    21

    Default Re: Passing arguments noob question

    Read what the compiler had to say:you 'claim' that your class implements the Runnable interface; that interface 'wants' the public void run() method to be implemented, but your class doesn't implement it, nor is it an abstract class ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    BDF
    BDF is offline Member
    Join Date
    Apr 2014
    Posts
    3
    Rep Power
    0

    Default Re: Passing arguments noob question

    Quote Originally Posted by JosAH View Post
    Read what the compiler had to say:you 'claim' that your class implements the Runnable interface; that interface 'wants' the public void run() method to be implemented, but your class doesn't implement it, nor is it an abstract class ...

    kind regards,

    Jos
    Maybe I'm not following you but in the class declaration is implements Runnable. If I delete the method call (us.mk8.timeToSplash) in paintWorld and the associated parameters the program runs fine. Its when I add those parameters to support that method from Mark8.timeToSplash it gives me that error.

    BDF

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,732
    Blog Entries
    7
    Rep Power
    21

    Default Re: Passing arguments noob question

    I don't see a public void run() method definition anywhere in your code, that's why your compiler is complaining (a run() method that takes parameters doesn't count).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Passing arguments from java to python
    By wavefunction in forum Advanced Java
    Replies: 4
    Last Post: 07-10-2011, 05:36 PM
  2. passing arguments to other methods
    By popeyito18 in forum New To Java
    Replies: 2
    Last Post: 07-03-2011, 12:01 AM
  3. Passing arguments to jar file with a pipe
    By clover in forum New To Java
    Replies: 7
    Last Post: 06-27-2011, 05:36 PM
  4. Passing array arguments into main
    By JohnDas in forum New To Java
    Replies: 12
    Last Post: 11-10-2010, 04:00 PM
  5. passing arguments
    By mac in forum New To Java
    Replies: 3
    Last Post: 04-08-2010, 12:30 AM

Posting Permissions

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