This site requires JavaScript, please enable it in your browser!
Greenfoot back
manster2008
manster2008 wrote ...

2011/6/23

Problem with arrays? String[] whiteKeys?

manster2008 manster2008

2011/6/23

#
I am currently doing chapter 5 of the Greenfoot Book, and learning about arrays currently. When I do the following code:
String[] whiteKeys;
        whiteKeys = {"a", "s", "d", "f", "g", "h", "j", "k", "l", ";"};
The following error appears: illegal start of expression But if I do this:
String[] whiteKeys = {"a", "s", "d", "f", "g", "h", "j", "k", "l", ";"};
It compiles fine. Could someone help?
F-L-Undead F-L-Undead

2011/6/23

#
You could write this:
String[] whiteKeys;
String[] Keys= {"a", "s", "d", "f", "g", "h", "j", "k", "l", ";"};  
whiteKeys =Keys;
or this:
String[] whiteKeys = new String[10];
whiteKeys[0] = "a";
whiteKeys[1] = "s";
whiteKeys[2] = "d";
whiteKeys[3] = "f";
whiteKeys[4] = "g";
whiteKeys[5] = "h";
whiteKeys[6] = "j";
whiteKeys[7] = "k";
whiteKeys[8] = "l";
whiteKeys[9] = ";";  
davmac davmac

2011/6/23

#
It's a Java peculiarity. If you declare an array variable, you can use an array initializer list, but you can't do the same if you assign to an existing array variable. What you can do if you need to assign a new array to existing variable is this:
whiteKeys = new String[] {"a", "s", "d", "f", "g", "h", "j", "k", "l", ";"};
manster2008 manster2008

2011/6/24

#
Again, I must say this: Davmac you are a genius... I am actually forced to learn vectors and arrays as I will not use them unless I know and understand how almost every piece works.
manster2008 manster2008

2011/6/24

#
Also... if anyone looks at this... I have a few questions about static variables... Are they accessible from the subclasses? Since static means the variable can be shared, can it be shared throughout all subclasses? If a change is made to this static variable from a subclass, is the same change made to the superclass? Thanks...
davmac davmac

2011/6/24

#
Yes, static variables are accessible from subclasses (unless they are declared private), and yes, the value is shared between the base class and all subclasses. (BUT, if you re-declare a variable with the same name in a subclass, it is a new variable with a separate value).
manster2008 manster2008

2011/6/24

#
Sorry for so many questions, but... Chapter 6 of the book, the Newton Simulation... I am studying Vector and Smoothmover by writing down their methods and statements and drawing representative images of them. I was just looking at abstract class Smoothmover.... could someone explain why in the setLocation(double x, double y), it has a super.setLocation() ?????? What does the super stand for, normally, and here?
davmac davmac

2011/6/24

#
The superclass. In this case, "super.setLocation()" calls the setLocation() method from the superclass. In this case, it's necessary to call Actor.setLocation() to actually move the actor. If you remove the call to super.setLocation(), you'll see that your actors don't move.
manster2008 manster2008

2011/6/24

#
I still don't truly understand the super. So, the actor Body that we actually want to move accesses its superclass SmoothMover, and moves the SmoothMover?
davmac davmac

2011/6/24

#
Hmm, maybe. The "super.setLocation(x,y)" line in SmoothMover calls the superclass of SmoothMover, which is Actor. If you have an instance of a subclass of SmoothMover, you can call setLocation() on it, that will run the method in SmoothMover; this method then deliberately calls the version of setLocation() in the Actor class (super.setLocation(...)), as well as doing some additional things. For example: If you have an actor of class Body, which is a subclass of SmoothMover, then the actor is an instance of Body, but it is also an instance of SmoothMover, and also of Actor! When you call setLocation(), then SmoothMover.setLocation() is executed (but the actor it operates on is still the Body actor); this calls Actor.setLocation(), which actually causes the actor to move.
manster2008 manster2008

2011/6/24

#
I am confused about the actual physics part of this simulation that actually makes a planet orbit around the sun. But if I may summarize the following code:
public void act() 
    {
        applyForces();
        move();
    }
    public void applyForces() {
        List<Body> bodies = getWorld().getObjects(Body.class);
        for(Body body : bodies) {
            if(body != this) {
                applyGravity(body);
            }
        }
    }
    public void applyGravity(Body other) {
        double dx = other.getExactX() - this.getExactX();
        double dy = other.getExactY() - this.getExactY();
        Vector force = new Vector(dx, dy);
        double distance = Math.sqrt((dx * dx) + (dy * dy));
        double strength = Gravity * this.mass * other.mass / (distance * distance);
        double acceleration = strength / this.mass;
        force.setLength(acceleration);
        addForce(force);
    }
The instance of Body gets all other instances of Body. And for each instance, it calculates the distance from the other to itself, and creates a new Vector, with x and y offsets of dx and dy... respectively. Then it calculates the strength it is to apply to itself due to the other instance's existence, and uses one of Newton's formulas. Then to find the acceleration of itself, it divides this strength by it's mass. Finally, it sets the Length of the Vector it had created to this acceleration and changes the Vector in SmoothMover, so that it adds this Vector force to the existing one. This is all very confusing... I am not really great in physics, as I still have to take it 2 1/2 years from now. I really don't get why this makes the planets orbit... Thanks davmac for all you quick responses. All I need to do now is study the use of Vector in the asteroids game, and then I can go on to my own game...
mjrb4 mjrb4

2011/7/4

#
On the physics side of things, essentially it's calculating the force between every pair of bodies in the world and then applying the forces to those bodies. The formula used is (GMm)/(r^2), given by this line:
double strength = Gravity * this.mass * other.mass / (distance * distance);  
...summarised as "the force between two bodies is directly proportional to the product of the masses and inversely proportional to the square of the distance between them." The other bits in there are applying the force and calculating parts of it, but that's the underlying equation in use.
manster2008 manster2008

2011/7/5

#
The thing is, I get that that is the equation used to find the force... what I am interested is in why this equation works and how it was figured out. I am one of those people who does not use a certain thing unless he is sure of how almost every piece functions. This is why I have been learning vectors (I have been doing so by drawing the methods out on paper with images to fully understand their effect).
mjrb4 mjrb4

2011/7/5

#
You're looking at Newton's law of universal gravitation if you want to do some reading around :-)
You need to login to post a reply.