Java Forum / General / January 2006
algorithms and pointers
vertigo - 03 Jan 2006 13:46 GMT Hello
Can java be used as language for algorithms learning at schools ? If there is no pointers how can i construct two direction list (and add delete elements to it) ? Or how can i create trees and add/delete/find appriopriate elements ?
Is there any way which would allow me to do that like in C, by hand ? (educational purposes) ?
Thanx Michal
Nagesh - 03 Jan 2006 13:49 GMT There are many classes in java.util.*. You just see class names u will be able to guess which class solves ur requirement.
-----Nagesh
Alan Krueger - 03 Jan 2006 13:51 GMT > Can java be used as language for algorithms learning at schools ? > If there is no pointers how can i construct two direction list (and add > delete elements to it) ? Or how can i create trees and add/delete/find > appriopriate elements ? You were misinformed. Java references are essentially pointers.
Jonathan Bartlett - 03 Jan 2006 15:21 GMT > If there is no pointers The idea that there are no pointers in Java is false. Java has references. The difference between references and pointers is that references are sanely managed by the programming language, while pointers are not.
For example, in C, this will both compile and execute:
void *a; int b; double *c;
b = 3; a = &b; c = a; printf("%f", *c);
Here is similar code in Java
Object a; Integer b; Double c;
b = new Integer(3); a = b; c = (Double) b; //This will throw an exception System.out.println(c.toString());
In addition, there is no way to delete an object -- that is handled by the garbage collector.
Finally, there is no pointer dereferencing. Everything (except builtin types) are references, so they are all accessed in the same manner -- indirectly.
References are simply sanely-language-managed pointers.
Jon ---- Learn to program using Linux assembly language http://www.cafeshops.com/bartlettpublish.8640017
Eric Sosman - 03 Jan 2006 16:29 GMT vertigo wrote On 01/03/06 08:46,:
> Hello > [quoted text clipped - 5 lines] > Is there any way which would allow me to do that like in C, by hand ? > (educational purposes) ? Linked lists, trees, hash tables, tries, and all the rest can be built entirely without "pointers." A "link," in the abstract, is just a datum that allows you to find the linked-to item. A number can be a link -- think of array indices. A String can be a link -- consider the "soft link" in a Unix file system. The famous XOR trick encodes two links into a single datum, allowing bidirectional linkage with just one (fragile) "pointer."
Java has something called a "reference," which seems a natural way of representing links.
 Signature Eric.Sosman@sun.com
Sanjay - 03 Jan 2006 17:01 GMT Michal,
I wouldnt really advocate Java for the beginners who are interested in learning linked list, trees...
Dont mix algorithm with programming language. Its different, algorithms dont need a programming language to write, you can make use of pseudo language also ;). I feel Java's purpose is beyond these things, Honestly in today's world who would want to keep writing lists, trees, and repeat all the basic things to finally end up with an application that could help make business better directly or indirectly.
I dont mean that you shouldnt or cannot do things which YOU are trying. I think its more like schools we learn step by step. These are my personal thoughts only O:)
Hope I made some sense.
Thanks Sanjay
TechBookReport - 03 Jan 2006 17:06 GMT > Michal, > [quoted text clipped - 17 lines] > Thanks > Sanjay Not sure I agree. Learning by doing seems more effective than purely theoretical explorations. And using Java to do the exploring of algorithms can be very enlightening.
There are several books available that look at algorithms and data structures explicitly using Java (see http://www.techbookreport.com/tbr0056.html for example).
Pan
 Signature TechBookReport Java book reviews http://www.techbookreport.com/JavaIndex.html
Luc The Perverse - 03 Jan 2006 20:24 GMT > Michal, > [quoted text clipped - 8 lines] > and repeat all the basic things to finally end up with an application > that could help make business better directly or indirectly. WHOA! I disagree to an extent which the English language is not able to adequately convey.
Algorithms are the foundation of logic - and abstract thinking. Book algorithms challenge students to think in a way that allows them to represent real world situations inside the computer. Abstraction is good in the hands of an educated and knowledgable person. In the hands of an amateur it can give quick gratification, accompanied by a false sense of power. If the student lacks the fundamental knowledge of how things link together in the computer then all programs will simply seem magical, and thus is born the cut and paste programmer who never understands a thing, but simplely "messes" with code until it works. Then you get .. . well . . .the quality of software that is very common today; I call it shitware.
Programming serves a double purpose - of course its innate use is obvious - getting the computer to do what you want it to. However, learning to program is an invaluable resource for learning more about the world and about yourself. But don't misunderstand, I'm not trying to say it is the only way.
-- LTP
:) Roedy Green - 03 Jan 2006 17:07 GMT >Can java be used as language for algorithms learning at schools ? >If there is no pointers how can i construct two direction list (and add [quoted text clipped - 3 lines] >Is there any way which would allow me to do that like in C, by hand ? >(educational purposes) ? All the java source code including the compiler and class library is there for you to examine. Have a look in src.zip to get started.
If you want to see a linked list implementation, look at Java's LinkedList or mine I wrote before Java had one.
http://mindprod.com/jgloss/products2.html#LIST
 Signature Canadian Mind Products, Roedy Green. http://mindprod.com Java custom programming, consulting and coaching.
Roedy Green - 03 Jan 2006 17:08 GMT >If there is no pointers how can i construct two direction list (and add >delete elements to it) ? In Java you have references which are safe pointers. They point to beginnings of objects not fields in the objects. Other than that, you can use them just the way you do in C for list processing.
 Signature Canadian Mind Products, Roedy Green. http://mindprod.com Java custom programming, consulting and coaching.
zero - 03 Jan 2006 18:23 GMT > Hello > > Can java be used as language for algorithms learning at schools ? I would think an algorithms class is something where you would expect the students to already know the basis of at least one programming language. If all of your students know the same language, use that. If they all know different languages, but all OO, then Java is a good choice. If they don't know any programming languages at all, your class will necessarily be theoretical and at a higher abstraction level. In that case you can just use pseudo-code.
> If there is no pointers how can i construct two direction list (and add > delete elements to it) ? Or how can i create trees and add/delete/find > appriopriate elements ? > > Is there any way which would allow me to do that like in C, by hand ? > (educational purposes) ? Like some others have said, Java references are a lot like C/C++ pointers. You can't create a reference to a primitive like you can in C, but for linked lists you don't need that anyway. A linked list can be as simple as:
class MyList { MyNode firstNode;
public void addToFront(Object data) { MyNode n = new MyNode(data); n.nextNode = firstNode; firstNode = n; }
// ... }
The variables n, data, nextNode and firstNode are all references, which you can compare to object pointers in C++.
Searching, deleting, inserting, ... all should be obvious as well.
 Signature Beware the False Authority Syndrome
Dimitri Maziuk - 04 Jan 2006 06:04 GMT vertigo sez:
> Hello > > Can java be used as language for algorithms learning at schools ? Yes, but I would strongly recommend using Pascal. Java wasn't created as a teaching language, Pascal was.
> If there is no pointers how can i construct two direction list (and add > delete elements to it) ? Or how can i create trees and add/delete/find > appriopriate elements ? Java "object references" are restricted pointers. That's one of the things that makes Java a bad teaching language: there is no clear distinction between "value", "reference", and "pointer".
> Is there any way which would allow me to do that like in C, by hand ? > (educational purposes) ? As long as your lists/trees contain objects, you can. You can't do that with native types -- and then you'll probably have to explain to your students why (another minus of Java as a teaching language).
Dima
 Signature Riding roughshod over some little used trifle like the English language is not a big deal to an important technology innovator like Microsoft. They did just that by naming a major project dot-Net (".Net"). Before that, a period followed by a capital letter was used to mark a sentence boundary. --T. Gottfried, RISKS 21.91
zero - 04 Jan 2006 13:24 GMT > vertigo sez: >> [quoted text clipped - 4 lines] > Yes, but I would strongly recommend using Pascal. Java wasn't > created as a teaching language, Pascal was. ... making it completely useless for the real world, and way too pedantic to allow students to learn anything.
If you are to learn anything you don't want someone (in this case pascal, in other cases an IDE or sometimes a teacher) looking over your shoulder and pointing out mistakes before you even make them.
 Signature Beware the False Authority Syndrome
Dimitri Maziuk - 04 Jan 2006 17:15 GMT zero sez:
>> vertigo sez: >>> [quoted text clipped - 6 lines] > > ... making it completely useless for the real world, In the real world where Object Pascal, Kylix, Delphi, etc. do not exist, the sky is bright pink, and the twin sunet of our binary sun over the South Methane Ocean looks magnificent...
... and way too pedantic
> to allow students to learn anything. ... and "anything" does not include algorithms and data structures, among the other things.
> If you are to learn anything you don't want someone (in this case pascal, > in other cases an IDE or sometimes a teacher) looking over your shoulder > and pointing out mistakes before you even make them. You mean, if you want to learn how to make mistakes without realising they are mistakes?
Dima
 Signature Backwards compatibility is either a pun or an oxymoron. -- PGN
Roedy Green - 04 Jan 2006 15:34 GMT >Java "object references" are restricted pointers. That's one of the >things that makes Java a bad teaching language: there is no clear >distinction between "value", "reference", and "pointer". They are equally clear to me in both languages. Java simplifies the matter by not having a distinction between a heap object and a statically allocated object. The fewer fussy fine points, the easier the language is for beginners. You also want a language with ferocious compile time checking for beginners to help them get their code in good shape before the much more daunting process of run-time debugging. That is where Java particularly shines.
I have been coding for 43 years, so it is not fair to compare my struggles learning Java with someone coming onboard without past baggage. That question should really be answered objectively, just what can students of Java/Pascal/other contenders accomplish after a year? More subjective is how is what they have learned likely to be applicable to them in future, and how much will just get in the way?
Java teaches students that is not necessary to reinvent every wheel. You can usually find what you want, or write an adapter to it.
 Signature Canadian Mind Products, Roedy Green. http://mindprod.com Java custom programming, consulting and coaching.
Dimitri Maziuk - 04 Jan 2006 17:25 GMT Roedy Green sez: ... More subjective is how is what they have learned likely to be
> applicable to them in future, and how much will just get in the way? > > Java teaches students that is not necessary to reinvent every wheel. > You can usually find what you want, or write an adapter to it. He didn't ask for a language with 18 wheels, he asked for one to teach algorithms in.
I don't know what they taught you so it "gets in the way", I was taught that languages are tools, each designed to fill a specific need, and that "right tool for the job" principle plays an important role in choosing an implementation language for a project. AFAIK Pascal is the one created to teach algorithms in, and his project is "teach algorithms and data structures".
Dima
 Signature Backwards compatibility is either a pun or an oxymoron. -- PGN
Luc The Perverse - 05 Jan 2006 01:13 GMT > Roedy Green sez: > ... More subjective is how is what they have learned likely to be [quoted text clipped - 14 lines] > > Dima Programming, like most topics is not a completely sterile environment. Although many students consider the point of a class to get it over with, with a passing grade, it can at least be hoped that the students will take a proactive interest in the topic and continue to use it on their own times and with their own projects.
Therefore, the ease of learning and "fun" factor associated with not only learning and using a language should be considered when choosing a language to teach.
He didn't ask if Java was the best and only language in the whole world for teaching algorithms. He asked if it was even possible to teach algorithms under Java.
Who knows - maybe Pascal is a better language for teaching algorithms in; it's certainly not a better language to get a job with.
 Signature LTP
:)
Free MagazinesGet these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...
|
|
|