Results 1 to 7 of 7
Thread: Passing arguments noob question
- 04-01-2014, 03:12 AM #1
Member
- Join Date
- Apr 2014
- Posts
- 3
- Rep Power
- 0
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(); }
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; }
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.
- 04-01-2014, 03:25 AM #2
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.
- 04-01-2014, 10:44 AM #3
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 27
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 **
- 04-01-2014, 08:52 PM #4
Member
- Join Date
- Apr 2014
- Posts
- 3
- Rep Power
- 0
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) { } }
Thanks!
BDF
- 04-01-2014, 09:01 PM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
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,
JosBuild a wall around Donald Trump; I'll pay for it.
- 04-01-2014, 09:23 PM #6
Member
- Join Date
- Apr 2014
- Posts
- 3
- Rep Power
- 0
Re: Passing arguments noob question
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
- 04-01-2014, 09:40 PM #7
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
Similar Threads
-
Passing arguments from java to python
By wavefunction in forum Advanced JavaReplies: 4Last Post: 07-10-2011, 05:36 PM -
passing arguments to other methods
By popeyito18 in forum New To JavaReplies: 2Last Post: 07-03-2011, 12:01 AM -
Passing arguments to jar file with a pipe
By clover in forum New To JavaReplies: 7Last Post: 06-27-2011, 05:36 PM -
Passing array arguments into main
By JohnDas in forum New To JavaReplies: 12Last Post: 11-10-2010, 04:00 PM -
passing arguments
By mac in forum New To JavaReplies: 3Last Post: 04-08-2010, 12:30 AM
Bookmarks