1,076,414 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?

Posts by JamesCherrill which have been Voted Up

Disabling compile on save just means you are running the old version of your code. It's a really bad idea for you (it exists for very large programs that take long time to compile). You must fix all your compile errors before trying to run.

The Game class is a good way to go, but you are creating a whole load of Game instances, which only works because the ArrayList is static. What you should do is to create a single instance of Game in main, then pass that to all the Players as they are constructed, so every Player has a reference to the same Game instance. Then the arraylist can be / should be an instance variable, not static.

Finally, you still have the "dummy" Player that only exists so you can call addPlayer. It's horrible. Change addPlayer to be an ordinary constructor and simplify the code:eg

    main...
       Game g = new Game();
       new Player(g, "James");
       ...

  class Player..
    Player(Game g, String name) { // a constructor
       this.game = g;
       this.name = name;
       g.allPlayers.add(this);
       etc

ps: Java naming convention is that variables should start with a lower case, so g or p, not G or P

JamesCherrill
... trying to help
Moderator
8,527 posts since Apr 2008
Reputation Points: 2,583
Solved Threads: 1,456
Skill Endorsements: 30

OK, you are making a decent effort, so there are plenty of people here will wiull be happy to help.

Beware line 27 - that declares a new variable called "ascii", so it won't initialise the one you declared on line 13.
Beware also that you are creating instance variables, but trying to use them froma static method.

Don't be afraid to put your code through the compiler as soon as possible. It's the quickest possible way to find errors. The code you posted isn't complete, so it won't work properly, but with the errors fixed it should compile.

Inside your inner-most loop you will want to put together a 4-letter String by concatenating letters from your array. Each loop will contribute a different letter, so you will need different looop variables for each loop. You can concatenate Strings with the + operator, so it goes along these lines...

for (int i = .....
   for (int j = ...
      ...
      String try = ascii[i] + ascii[j] ...

(Using an array of 1-letter Strings like this is perfectly valid, but it is going to be very inefficient. But I recommend that for now you ignore efficiency and get something, anything, that works. Then you can try smarter data structures or algorithms and you will be able to measure now much faster they are. Just go one step at a time.)

JamesCherrill
... trying to help
Moderator
8,527 posts since Apr 2008
Reputation Points: 2,583
Solved Threads: 1,456
Skill Endorsements: 30

ArrayList is still broken.
Every time you call new Player() you execute the default constructor and start a new ArrayList. That's perfectly good logic considering that the ArrayList is an instance member - one ArrayList per Player, but I doubt very much that it's what you want. Presumably you want one list for the whole game, comtaining all the Players. The purist solution is to have that list as an instance member in some kind of Game class, but if there is only one game happening at a time you could simply make the list static and initialise it when you declare it. The same argument applies to the showTeam method - either it belongs in a Game class or it should be a static method in Player (or maybe there's a case for a Team class?).

Your main method is also pretty bizarre. You create an instance of Player, then keep adding Players to it. What does it mean to "add a player to another player"? Is that just an attempt to work round the non-static ArrayList?

Hint 1:
Personally I'd dump the no-args Playerconstructor, and turn addPlayer into a constructor (because that's what it does - creates a new Player with a given name)

Hint 2:

Iterator<Player> IT = this.allPlayers.iterator();
while (IT.hasNext()) {
   Player temp = IT.next();

is the long and harder and more error-prone way to code

for (Player temp : allPlayers) {
JamesCherrill
... trying to help
Moderator
8,527 posts since Apr 2008
Reputation Points: 2,583
Solved Threads: 1,456
Skill Endorsements: 30

You need 4 nested (identical) loops.
The System class has a method that gives you the current time in mSec. Call it before and after, subtract the two for elapsed time.

Have a go, see how far you get, come back here if you get stuck (post what you hace done so far)

JamesCherrill
... trying to help
Moderator
8,527 posts since Apr 2008
Reputation Points: 2,583
Solved Threads: 1,456
Skill Endorsements: 30

Here's a minimal runnable exmple that illustrates the key things you need to do - in particular a "model" that has the current values for whatever changes overe time (pahse/position of moon, brightness of sky etc), a timer-based method that updates those variables in a predictable steady way, and a paintComponent that updates the display using the latest values.

import java.awt.*;
import java.util.TimerTask;
import javax.swing.*;

public class Animation0 extends JPanel {
   // absolutely minimal example of how to structure animation in Swing.
   // One method that steps the simulation model through time
   // One method that updates the screen with the latest data

   public static void main(String[] args) {
      // create and display the animation in a JFrame
      final JFrame frame = new JFrame("Animation 0 (close window to exit)");
      Animation0 animationPanel = new Animation0();
      animationPanel.setPreferredSize(new Dimension(400, 150));
      frame.add(animationPanel);
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setVisible(true);
   }

   // this is the "model" - just a single object moving L-R at constant speed
   private int xPos = 0; // current position of animated object

   Animation0() {
      // start Timer to update the model every 30 milliseconds...
      // uses a java.util Timer rather than javax.swing Timer
      // to avoid running intensive simulation code on the swing thread
      java.util.Timer timer = new java.util.Timer();
      TimerTask timerTask = new TimerTask() {
         @Override
         public void run() {
            updateSimulation();
         }
      };
      timer.scheduleAtFixedRate(timerTask, 0, 30);
   }

   public void updateSimulation() {
      // reliably called by Timer every 30 milliseconds
      xPos += 1; // update position of object
      repaint(); // notify that screen refresh is now needed
   }

   // this is the GUI - draws the model on the screen
   @Override
   public void paintComponent(Graphics g) {
      // screen refresh - called by Swing as needed
      // Never update positions etc here because there are no
      // guarantees about when or how many times this will be called.
      super.paintComponent(g); // ensure background etc is painted
      g.fillOval(xPos, 35, 100, 100); // draw object at current position
   }

}
JamesCherrill
... trying to help
Moderator
8,527 posts since Apr 2008
Reputation Points: 2,583
Solved Threads: 1,456
Skill Endorsements: 30

The idea is to ensure that you always get a valid completely initialised object - you keep calling the setters in the builder until your object is fully specified, then, and only then, you call build() and that returns a complete valid object (or throws some kind of "incomplete or invalid" exception).
If you just create a new object then call its setters you cannot guarantee that the object will be complete and valid, or maybe there's exact subclass you need to create depending on the supplied values.

The reason the builder's setters return the builder object is so you can chain calls, as in
myObject = new Builder().setThing(value1).setOther(value2).setAnother(value3).build();

JamesCherrill
... trying to help
Moderator
8,527 posts since Apr 2008
Reputation Points: 2,583
Solved Threads: 1,456
Skill Endorsements: 30

The "proper" way to do it is probably to subclass JTable and override the method that implements the UI for the table header. There are a few discussions about that on the web. It looks pretty difficult to me.

In my opinion while you should always strive to do the best, real life constrains you, and sometimes a quick fix is the most appropriate thing to do.
In this case for example, if the exercise is all about UI then you need to do it properly, but if it's an exercise in database mananagement then spending a too much time on prettyfying the UI is probably a mistake...

JamesCherrill
... trying to help
Moderator
8,527 posts since Apr 2008
Reputation Points: 2,583
Solved Threads: 1,456
Skill Endorsements: 30

You exit the loop lines 12-18 with y = map01.length then on line 21 you have map01[y+1] so yes, index out of bounds.

JamesCherrill
... trying to help
Moderator
8,527 posts since Apr 2008
Reputation Points: 2,583
Solved Threads: 1,456
Skill Endorsements: 30

http://docs.oracle.com/javase/tutorial/java/concepts/package.html

Basically, you group related classes together into packages to help organise big systems, eg if your 99th application has a GUI, some business logic and a database interface you could group all its many classes into packages

application99.gui
application99.logic
application99.database

you source and class files the go into folders that match, eg

application99\gui\
application99\logic\
application99\database\

it's essential to get the correspondance between the package names and the directory structure, because that's how Java finds your files. Your IDE will alos organise its files the same way.

YOu will have already seen them in your import statements:
import javax.swing.JLabel;
JLabel is found in the swing package inside the javax package.

JamesCherrill
... trying to help
Moderator
8,527 posts since Apr 2008
Reputation Points: 2,583
Solved Threads: 1,456
Skill Endorsements: 30

nurib:
DaniWeb Member Rules (which you agreed to when you signed up) include:
"Do provide evidence of having done some work yourself if posting questions from school or work assignments"
http://www.daniweb.com/community/rules

As a final year student you should be capable of doing more than just whining. Post what you have done so far and someone will help you from there.

JamesCherrill
... trying to help
Moderator
8,527 posts since Apr 2008
Reputation Points: 2,583
Solved Threads: 1,456
Skill Endorsements: 30

DaniWeb Member Rules (which you agreed to when you signed up) include:
"Do provide evidence of having done some work yourself if posting questions from school or work assignments"
http://www.daniweb.com/community/rules

Post what you have done so far and someone will help you from there.
ps: This is not a hard assignment

JamesCherrill
... trying to help
Moderator
8,527 posts since Apr 2008
Reputation Points: 2,583
Solved Threads: 1,456
Skill Endorsements: 30

You seem to be asking for a tutorial that exactly describes your problem. That's not going to happen. YOu will just have to learn some Java.

DaniWeb Member Rules (which you agreed to when you signed up) include:
"Do provide evidence of having done some work yourself if posting questions from school or work assignments"
http://www.daniweb.com/community/rules

JamesCherrill
... trying to help
Moderator
8,527 posts since Apr 2008
Reputation Points: 2,583
Solved Threads: 1,456
Skill Endorsements: 30

you don't need to add the @Override annotation in order to actually override the method.

... but it is a Very Good Thing To Do because it tells the compiler to check that you have correctly overidden a superclass method, thus avoiding annoying bugs like

public String toStrimg() {
  return this.firstName + " " + this.lastName; // doesn't get executed when you expect
}
JamesCherrill
... trying to help
Moderator
8,527 posts since Apr 2008
Reputation Points: 2,583
Solved Threads: 1,456
Skill Endorsements: 30

No, that's how you call a static method. It should look like someInstanceOfNode.MinDistanceNode()

Since I have no idea what the method does, and it's undocumented, it's hard for me to answer. In terms of syntax it's an easy answer... the method is an instance method of Node and has no parameters, so there's no way you can apply it to an ArrayList.

JamesCherrill
... trying to help
Moderator
8,527 posts since Apr 2008
Reputation Points: 2,583
Solved Threads: 1,456
Skill Endorsements: 30

The method MinDistanceNode() is defined in the Node class, so to call it you need a single instance of Node, not a whole ArrayList of Nodes

JamesCherrill
... trying to help
Moderator
8,527 posts since Apr 2008
Reputation Points: 2,583
Solved Threads: 1,456
Skill Endorsements: 30

It's just a convenience thing. It means you can print anything simply like

Object something = ...         // can be absolutely anything
System.out.print(something);   // will always work because print actually calls something.toString()

and it will always work and usually tell you enough to identify what something was.

JamesCherrill
... trying to help
Moderator
8,527 posts since Apr 2008
Reputation Points: 2,583
Solved Threads: 1,456
Skill Endorsements: 30

Your folder structure needs to match the package structure, so what you just said is only true if the classes are all in the same package.

JamesCherrill
... trying to help
Moderator
8,527 posts since Apr 2008
Reputation Points: 2,583
Solved Threads: 1,456
Skill Endorsements: 30

Not sure what rules you are referring to... "A display area for a short text string or an image, or both. A label does not react to input events."
But anyway, I chose JLabel because it comes with font/text attributes that make it easy to implement some control of formatting of the headings, and sets its height appropriately, but otherwize, yes, a JPanel would be a sensible choice.

JamesCherrill
... trying to help
Moderator
8,527 posts since Apr 2008
Reputation Points: 2,583
Solved Threads: 1,456
Skill Endorsements: 30
JamesCherrill
... trying to help
Moderator
8,527 posts since Apr 2008
Reputation Points: 2,583
Solved Threads: 1,456
Skill Endorsements: 30

I recommend that you avoid java.awt.GridBagLayout

Just for balance - most of my Java work is GUI-centric, and GridBagLayout is the only layout manager I use for anything other than trivial cases. It's the only one that gives me enough control over what happens when the systen font is different or the window is resized. In my opinion it's well worth the effort to learn to use it.

JamesCherrill
... trying to help
Moderator
8,527 posts since Apr 2008
Reputation Points: 2,583
Solved Threads: 1,456
Skill Endorsements: 30
 
© 2013 DaniWeb® LLC
Page rendered in 0.3597 seconds using 2.75MB