can s o explain me some java basics like vector constructor
i'm still doing the same excercise,
let me first explain what i know ... (=very little)
i'll have an exam for my minor-class 'java' in january for which i have to do an excercice which i'll have to defend next week. i don't follow any classes cause university is to far away from my place.
i have never learned any java,
but i can understand a lot of it bcs learned 'actionscript' for the animationprogram 'flash'
a few years ago. so i have no difficulties understanding things like 'switch, case, for, while, etc.)
i'm also used to learn things myself as an autodidact.
but because i'm lacking time and i seem not to be able to understand the general thing,
maybe someone could help me?
what is a vector excately ? how can i visually grasp it? (i think an array is like a matrix with colums and lines to be filled, with a fixed number of line and colums)
i can't understand constructors either, i've read about it in different books and webpages but
it remains vague.
here i try to describe a little what i think to have understood, is this right? (in fact only the day before yesterday i approached the whole java-thing as lineair and had never heard of a vector, so what i need is to understand the way to begin, how do i start? cause i can find lots of information everywhere but i struggle more with 'how do i start transforming my excercise into java?')
a program starts at the 'main'-class that has some kind of scenario of how things will be going.
it 'calls' other classes through it's methods, these methods can also return a value and can have parameters. these classes also have a 'blueprint' for creating objects.
so every action that needs to be done must have variables and objects to be created in the object-machine '=class', the action must be anounced in main and described in a method (in the class that is concerned) ?
If it's anything like C++, a constructor would be a method that is called when the object is created. It's used for initializing variables etc.
And to be more correct, a class doesn't contain a blueprint for the objects, the class is the blueprint (generally).
And to put it simply, a vector is a collection of usually 2 or 3 variables, that can be used to represent position or direction.
In computer games, there's usually a vector to represent where an object is located. There might also be a vector to tell what speed the object is traveling at.
A 2 dimensional vector is usually displayed this way: (2, 3) or (x, y).
A 3 dimensional vector can be displayed like this: (2, 3, 4) or (x, y, z).
To figure out where the object will be located next frame, you add the velocity vector onto the position vector like this for example:
(2, 3, 4) + (0, 0, 1) = (2, 3, 5)
A positive z value usually means towards the sky in computer games.
However, I'm afraid I do not know of any premade class for vectors in Java, but I suspect there is.
Hi lemon, sounds like you've got the basic idea, but could use some clarification on a few points.
First off, Java, like C and C++, begins execution in the main method of whatever class you tell it to execute, so it sounds like you've got that part worked out.
Drakilor is correct about the constructor--it's a special method that gets called whenever you create an instance of an object using the "new" keyword, and it is commonly used to initialize instance variables of that object (instance variables are variables that are unique to a particular instance of an object--if you have two instances of the same object, the values of their instance variables can be different).
A Vector is a specific type of object in Java, and to further complicate the matter it doesn't have much in common with the scientific or mathematical concept of a vector that Drakilor described. It's like an array, but with more methods (for example, you can easily determine whether it contains a reference to a given object). You can find more information about the Vector class here: Java 5 Vector reference
Hope that helps!
I think i 've got a problem with the notation...
looking for the notation of a vector i found this one:
...
Vector vec = new Vector();
vec.add(obj1);
vec.add(obj2);
...
vec.insert(3, obj3);
vec.del(2);
if (vec.getCount() > 5)
...
but in the examples i've got from school i find these (from a student, who has specified '//creation of the vector' near it):
Entite [] tab_Entites;
tab_Entites = new Entite[nb_Entites_Max];
And these ... (written by the professor himself, who is talking about vectors but maybe i misunderstood where he did that?, but i can't find any vector like the one in the example above in what he has written) , i thought this was the notation for an array (with this square brackets, and the 100)
in Main of the class Membre :
Membre tableauMembres [] = new Membre [100] ;
tableauMembres [nombreMembres] = new Membre () ;
Anyway in the excercise i have to do, he explicitely asks to create a vector.
And here i've got some square brackets too that bother me in one of the methods in class Membre (Lire is a class that reads the input asked for in the println):
void emprunter (){
System.out.println ("Entrez le titre emprunte");
emprunts [nombreEmprunts] = Lire.S;
nombreEmprunts ++;
}
thanks , your help is like a miracle ...
Uh oh... it looks like your professor is being inconsistent with the vocabulary, because you are absolutely correct--the square bracket notation the way they are using it means that they are creating an array, not a Java Vector. So it sounds like your professor may actually be using the word "vector" instead of "array," and I would e-mail your professor, explain your confusion, and ask which way you should do it. At this point, that's the only way to really be sure.
Good luck!
Your instructor may be mixing up stuff. That is mixing up math vectors, with Java arrays.
Vectors are like arrays, and conceptually you can think of them the same way. But in terms of implementation, the points in memory where everything is differs.
With an array, everything is located conveniently right next to each other. In a Vector they're scattered about seemingly randomly, but it keeps track of where it all is, so for all intents and purposes you can visualize them the same way.
The Java Vector exists, because frequently one needs to store a big list of things, and basic arrays aren't necessarily the best solution.
An array can't be resized really. You need to make a new array, copy the data, and assign your variable to that one. Which for all intents and purposes you can consider the same array. (I'm trying to keep the technical stuff to a minimum here because that might just confuse you more. If you don't get all this don't worry, it probably doesn't matter at this point. You can just leave it that, "arrays aren't always the right solution")
So again, a Vector is basically like an array, with extra functionality. There are a couple more things you can do with them. They take up a bit more memory for the number of items you have, but again, the big thing is that they're easier to resize.
Generally you should use an ArrayList rather than unless you're working with threads, which being that you're just starting with I'm going to assume you aren't. I'm not quite sure exactly how this all works, but I believe that what happened is that the Vector class existed before the "Collections" frame work. It probably hasn't been brought up yet, but believe me, those Collections classes are very useful.
(and for visualization purposes, you can assume that it's exactly like a Vector. They aren't terribly different at all)
Anyways, what happened is that Vector's got retrofitted so that they're part of the 'collections' later. The ArrayList was something that is a smidge newer (both have been around quite some time I think), and is for the most part more suitable except in thread issues.
(And to answer your question, the simple version of threads is that they are what let you do multiple tasks at once with a single CPU. You aren't really, but there's the illusion you are.)
could you give an exemple of a vector that contains name, song and kb and this for each new mp 3 for exemple ?(declaration and initialisation)
and how i should name them, fill them, and acces by a method ?
i've looked all the exemples i've got and had started very wrong ,but now i think it is better except for the vector.
and how i should name them, fill them, and acces by a method ?
i've looked all the exemples i've got and had started very wrong ,but now i think it is better except for the vector.
It sounds like you're actually dealing with two separate things here: 1) The creation and initialization of a new MP3 Object (which is where you would set the artistname, songtitle, and size) and 2) The storage of that MP3 Object in a Vector. (If I misunderstood, disregard the rest of this post and, if you wouldn't mind, provide some of the code that you've already got (probably in a PM would be best) )
Do you already have a .class or .java file for an MP3 Object? If not, you'll probably need to create your own MP3 Object in a separate .java file from the one that your main method is in. It's a pretty straightforward object, with three private instance variables (e.g. artistName, songTitle, and size) and six methods: one each to set the instance variable values, and one each to retrieve the instance variable values. The six methods aren't strictly necessary (you could just make the variables public and access them directly) but they are considered good coding practice since they give you the ability to have more control over the variables, so it is a good habit to start developing now. If you're not quite at that level yet, though don't worry. Start with just the instance variables and work your way up from there (With just the instance variables, it should be possible to do this part in less than 12 lines of code)
Once you've got that created, then in your main method you first instantiate a new Vector. After that, instantiate a new MP3 Object (with the keyword 'new' and the name of the class from which your MP3 Object is created), set the instance variables to the appropriate values, and add it to the Vector with the Vector's add() method. (With only one song, it should be possible to do this in less than 15 lines. With more songs, it should be possible to do this with 10 lines + ( 5 * number of songs ) )
Hope that helps!