JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

area and perimeter are ideal candidates for abstract methods in the (abstract) superclass with concrete implementations in each subclass - ie every Shape can calculate and return its area, but exactly how that happens is different in each subclass.
Sides are a bit more interesting... the two sides of a rectangle are not the same things as two sides of a triangle. Yes, they are all examples of a length, but they mean different things, and you can't use them in the same ways. Personally I would not try to share them, I'd have length and width in Rectangle vs side1,side2,side3 in Triangle

pritaeas commented: Good choice for the exercise. +14
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

To be fair, the Java 7 source code for String begins

public final class String
    implements java.io.Serializable, Comparable<String>, CharSequence {
    /** The value is used for character storage. */
    private final char value[];
    ...

so you could reasonably say that a String is an array of chars wrapped in a class that provides many useful methods to work with the chars (but none that can modify them). Hence the source code for toCharArray simply returns a copy of the original char[] so as to protect it from modification

public char[] toCharArray() {
    // Cannot use Arrays.copyOf because of class initialization order issues
    char result[] = new char[value.length];
    System.arraycopy(value, 0, result, 0, value.length);
    return result;
}
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Re stultusk's post: I agree absolutely except for the anonymous inner class listener. I would always try to use that rather than implementing the listener in the main class because it scales to any number of listeners and avoids the "do everything in one listener" multiple-if nightmare. (And with Java 8 the anonymous inner class pattern will come down to a single line of simple code)
Re Schol-R-LEA's, you can have a switch on a String (java 7 onwards)

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

For PCs there's only one Java to consider - it's the one from Oracle (formerly Sun) in Windows/Mac/Linux 32/64 bit versions. You'll need the "SE" version as opposed to the "EE" (Enterprise Edition) edition, and the full JDK (Java Development Kit) rather than just the JRE (Java Runtime Environment).

NetBeans is a Integrated Development Environment that provides a huge set of tools to help develop Java, but it's not, in itself, a version of Java - you install it after installing Java. As a Java beginner you may find that NetBeans adds its own steep learning curve to Java's intrinsic learning curve, and doing both at once is really hard. You would probably be better off with just the standard compuiler that comes in the JDK, and a programmer's editor.

The import statement is part of the language, and every Java supports it. If your import is failing it's because the package you are trying to import hasn't been installed correctly.

Mr.M commented: Thank you for your help, I've downloaded and installed the se JDK how will I start working with it or I have to link or my NetBeans to it directory path or? +1
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

It's not painting because you don't call its paint method. (You create a new Player on line 18, but you never do anything with it).

From the code fragment you have posted it's hard to see what value the Player class is bringing to this program. The map has the position of the player, so why not just paint a green rectangle in Level::paint?

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

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 4,733 Most Valuable Poster Team Colleague Featured Poster

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 4,733 Most Valuable Poster Team Colleague Featured Poster

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 4,733 Most Valuable Poster Team Colleague Featured Poster

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 4,733 Most Valuable Poster Team Colleague Featured Poster

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 …
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

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 4,733 Most Valuable Poster Team Colleague Featured Poster

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 4,733 Most Valuable Poster Team Colleague Featured Poster

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 4,733 Most Valuable Poster Team Colleague Featured Poster

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 4,733 Most Valuable Poster Team Colleague Featured Poster

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 4,733 Most Valuable Poster Team Colleague Featured Poster

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 4,733 Most Valuable Poster Team Colleague Featured Poster

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 4,733 Most Valuable Poster Team Colleague Featured Poster

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 4,733 Most Valuable Poster Team Colleague Featured Poster

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 4,733 Most Valuable Poster Team Colleague Featured Poster

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 4,733 Most Valuable Poster Team Colleague Featured Poster

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 4,733 Most Valuable Poster Team Colleague Featured Poster

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 4,733 Most Valuable Poster Team Colleague Featured Poster

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 4,733 Most Valuable Poster Team Colleague Featured Poster
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

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 4,733 Most Valuable Poster Team Colleague Featured Poster

Yes yes! YES. That's exactly the "Observable" pattern that I recommended earlier. It's how the Java API handles re-painting windows when required, responding to keystrokes or mouse events, everything. Java maintains its own event queue, but it dispatches the events by calling registered "listeners" ("callbacks") for each type of event. It's a way that any experienced Java programmer would do this.

You define an interface (one or mode method signatures) for passing network events. Your GUI implements that interface, and registers itself as a listener on thr network handler. When a network event happens, the network handler calls all the registered listeners, passing the relevant data.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Hello gauravbit70, welcome to DaniWeb.
Don't be put off by Stultuske, he's just having a bit of fun. Your contributions are very welcome here.
Before posting please do check the dates on threads, and please don't just post solutions with no explanations or comments - we want people to be able to learn from what they read here.
J

ddanbe commented: I like nice people. +14
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Onca again: Your zip file needs to be read and written directly as bytes without any kind of conversion or interpretation. Using Strings will always involve some kind of conversion or interpretation, and will corrupt your file contents. Get rid of the Strings completely. Read/write the zip file contents into/from an array of bytes directly.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

OK, thanks for explaining that.
In that case I would say the logic looks reasonable once you fix the names and capitalisation.

kay19 commented: Alright thanks for help :) +1
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

... but I did notice you have a

9  for (int i = 0; Tokens.hasMoreTokens(); i++)

loop, but in that loop you read two tokens..

10     if (Tokens.nextToken().contains(KeyWord[k]))
17     else if (Tokens.nextToken().contains(DataType[l]))

so if there are an odd number of tokens the second nextToken will fail, probably the cause of your NoSuchElementException

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Please post the exact complete text of yhour errors

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Java references are what are sometimes called "opaque pointers" - yes they are references to where to find objects, but they cannot be manipulated (screwed up) by the user. A reference can be null (in which case it will be trapped safely), but I'm not aware of any way to create an "invalid" reference (I'm sure ~s.o.s~ will correct that!)

Rubberman is clearly a serious highly expert developer who knows exactly what he is doing, and surely his code is just as secure as any Java. But IMHO letting the average programmer loose on C pointers and malloc is like giving a box of matches to a child.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

You haven't posted all the relevant code, but using String and getBytes(Charset...) is the wrong way to go when you need to read and write raw bytes from/to a stream. Using any kind of String or Charset will result in bytes being interpreted and replaced according to their character equivalences in whatever character set. You need to be reading and writing uninterpreted bytes, eg by using InputStream and read(byte[] b) and OutputStream and write(byte[] b)

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

It has no pointers. Pointer errors and related memory managment problems are a major source of security problems in C and C++

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Yes, you already said that.
last chance: What exactly is your problem/question? We have no idea what you already know/do not know. Do you need help defining a method, writing an action listener, creating a Virus instance? Did you write the code you posted?

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

chars are unsigned 16 bit numbers, so values 0-255 are no problem. Use them just like any other integer type.
But is that really what you need? The fact that you create your byte[] with hex values seems to suggest that the numeric equivalent isn't what it's about. When you put 0xC8 into a byte, it really does hold the value 0xC8.
Maybe giving a bit more background will held you get the most appropriate answer.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Nobody has the time to read all that and try to understand what it is you need. Please ask specific questions, backup up by details of what you have already done youself to try to answer them.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

There are lots of people here who will freely give their time to help you become the best Java programmer you can be. There's nobody here who is interested in doing your homework for you.

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, with your specific question(s) and someone will help you from there.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Almost. The syntax allows for a list of expressions in each part of the for, so for(i=0,ii=result.length;i<ii;i++) is OK. See Java Language Spec section 14.14
It's probably written that way to avoid re-evaluating result.length on each pass of the loop in a (mistaken?) attempt to optimise execution speed.
But your code has a 1 instead of an i, resulting in 1=0, which is invalid syntax.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Nothing you can do will ever turn a String into an int. Integer.parseInt(s) returns a new int value that you have to assign to a variable.
ps: Please do run your code thorugh the compiler if possible before posting it, otherwize people may spend a lot of time manually finding an error that the compiler would have diagnosed instamtly.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

It's a good idea to use an @Override annotation whenever overriding an inherited method or implementing an abstract one, eg

@Override
public void setRent (boolean rented){
  this.rented = rented;
}

This will immediatly generate a compiler error telling you exactly what you did wrong (and will be helpful for anyone reading the code).

peter_budo commented: +1 for Override +15
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

client-server-client really isn't hard at all. You already have the code for receiving messages from a client and sending them to a client, so the forwarding code is a very small and easy addition.
On the other hand, configuring machines that are probably behind a NAT router and one or more firewalls to receive incoming connections is a total minefield of problems.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

I'd like to answer that but, without understanding your overall design, I can't.
If the player just moves from square to square (like PacMan) then you just need to test the square in the direction that you are trying to move, eg

 if (moveUpRequested) 
    if (map[player.x][player.y-1] != 0) // can't move upwards
 // ditto for left, right, down

Youu would use intersect if yuo have the kind of game where things can move at any speed in any direction.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

I don't think "touching" is what the intersects method tests. AFAIK the two rectangles must overlap for intersects to return true.
ps This still looks crazy. The player and wall Rectangles will only overlap if a cell in map1 is both 1 and 2. :~

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

I don't understand.
After you have populated rect just System.out.println(rect); so you can see and check the values

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Why not just print the Rectangles' coordinates to System.out to see if they are ok?

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Sounds like you expect A to connect directly to B? That implies that B has a server running an open server port that's accessible from the internet.
Normally an app like this uses the server to route messages from one user to another, so no user needs to know anything about the other's connection, and the clients do not need to operate in server mode.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Now you know what needs to be done, you may want to look at the Arrays.copyOf method, which implements just what we were talking about.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

If the player is in one tile, and the wall is in another tile, and the Rectanges correspond to whole tiles, and one tile cannot be both 1 (player) and 2 (wall), then I don't see how any of there rectangles will ever intersect. Touch, yes, but overlap, no. Maybe you should print out the Rectanges to see if they ever have overlapping coordinates.
And just out of curiosity, how can a collision depend on where the camera happens to be?