Jump to content

Where do algorithms go in the java program heiarchy


ALine

Recommended Posts

I am learning about java programs currently reading Data structure using java by duncan bell when he goes over the basic structure of a java program. In which being at the lowest level is the "record/payload", the next being "data structures" the next being "the application" and finally is the "driver"/"main class." 

My question is where do algorithms factor into this hierarchy? Would it fit in with data structures? Or would they be at the applications level due to them actually using the data structures. 

Thank you for your response.

Edited by ALine
Switched method with class
Link to comment
Share on other sites

5 hours ago, ALine said:

My question is where do algorithms factor into this hierarchy? Would it fit in with data structures? Or would they be at the applications level due to them actually using the data structures. 

I'll try a short answer that opens for further discussions, this is an interesting question. Initially I would put algorithms close together with data structures. When I first studied computer science the introduction course one data structures was immediately followed by an introduction to algorithms. I believe that was a good way to start; many algoritms are tied to, or depend on, the underlying structure of the data. I think it would be less efficient to start with algorithms and then continue with data structures.

5 hours ago, ALine said:

Or would they be at the applications level due to them actually using the data structures. 

I would not put algorithms at program level in the general case; many projects I've been involved in have relied heavily on standard libraries that someone else have developed, tested etc. The algorithm was, from our application perspective, pretty much a "black box". But in other cases the algorithm has been much more connected to the application. For instance when behavior of the algoritm is controlled or depends on the user input.  So the answer would be "it depends".

 

I see that you mention "main class". I have not read the book you refer to but it implies a standalone program. My quick reply above does not necessarily apply to large and/ore distributed applications and systems.

Edited by Ghideon
clarified about main class
Link to comment
Share on other sites

cool, thank you for the response. The book is a good read so far, very straight forward. Do you have any books that you could recommend from you time starting off?

Before I ask any more questions how do you structure your different classes in Java? by knowing this I can have some reference with how I am learning it in my own study at uni.

Also with algorithms in Java I understand that they are instructions, however I am not really sure how they interact with data structures if that makes any sense. Like when implementing an algorithm is it taking data from data structures as an input and outputting the processing step of the algorithm when applicable? Also, if you can write an program as an algorithm then what is the difference between the two? (just looked back and I think you already answered this question, just wanted to added in case.)

Ex: say you have a sorting algorithm an what to sort the entries of a linked list, do you apply the black box algorithm to the data structure or is it written into the data structure as a method of the data structure class?

Also is this all just dependent on what you want the program to do and is just slightly arbitrary/optimal selection of black boxes in different places?

p.s. apologies for poor grammer. English is my first language, I am just bad at it. :)

Edited by ALine
Link to comment
Share on other sites

Blackbox means that you have no idea how 3rd party library or class is working internally and it can be changed at any time by the creator e.g. your app connects to database. It could be MySQL, SQLite, in-memory db, or something less recognizable..

Blackbox is not an algorithm.

 

Usage of certain algorithms sometimes force programmer to use of specific solutions e.g. hash map requires that class will calculate hash code for supplied data. Lack of proper implementation of hash code method e.g. return 0 all the time, will lead to useless non optimal hash map which will work slower than plain list/array.

If you are expecting there will be very few elements in the collection there is no sense to use hash map. It will just slowdown. The amount of elements in collection dictates you what algorithm you should use to have the quickest and the most optimal access to a single element.

e.g. list/array of strings (specific data source) "begs" for being sorted alphabetically and then usage of binary search algorithm to find exact element (string) on the list.

Binary search algorithm will find element in O(log2(x)) instead of O(x). Make graph f(x) of both functions to see how much can you gain depending on value of x.

Edited by Sensei
Link to comment
Share on other sites

28 minutes ago, Sensei said:

Blackbox is not an algorithm.

Almost anything, including an algorithm, can be considered a blackbox. The defining characteristic seems to be something viewed in terms of inputs and outputs,  without knowledge of the internal workings. A disease classifier (i.e. an algorithm) would be considered a blackbox to a medical professional who doesn't think in terms of hyperplanes and decision boundaries. 

Link to comment
Share on other sites

There is no sense to use Kd-tree or octree algorithms on data which are not 3 dimensional. There is no sense to use quad tree on data which is no 2 dimensional. So the number of dimensions of data forces programmer to use or not use specific algorithms.

 

4 minutes ago, Prometheus said:

Almost anything, including an algorithm, can be considered a blackbox.

OP said:

16 hours ago, ALine said:

you apply the black box algorithm

Therefore I replied, "blackbox is not an algorithm"..

No teacher would say "let's apply the blackbox algorithm"..

Blackbox is lack of knowledge about specific  implementation by unauthorised people. 

e.g. programmer executing method Collections.sort( list ); has no idea whether it is quicksort algorithm or bubble sort algorithm.

Authors of this method/class know what is algorithm used there in the reality. 3rd party programmers don't.

Unauthorised programmer can gain knowledge about specific implementation by reverse engineering the code.

Link to comment
Share on other sites

20 hours ago, ALine said:

black box algorithm

Apologies for the confusion, I meant to say "Black Box" Algorithm implying that the algorithms workings is unknown to the programmer when implementing it. Again sorry bad at English.

4 hours ago, Sensei said:

There is no sense to use Kd-tree or octree algorithms on data which are not 3 dimensional. There is no sense to use quad tree on data which is no 2 dimensional. So the number of dimensions of data forces programmer to use or not use specific algorithms.

So what do you mean by data which is dimensional, do you mean like data with more than 1 field or are you referring to vectors in cs language?

 

Edited by ALine
Link to comment
Share on other sites

9 hours ago, ALine said:

So what do you mean by data which is dimensional, do you mean like data with more than 1 field or are you referring to vectors in cs language?

Read about spatial database:

https://en.m.wikipedia.org/wiki/Spatial_database

e.g. Google maps, computer game playfield, stars and galaxy database etc.

It is an example of data which dictates the most optimal algorithms to store and query records.

Sorry. That part was meant to be together with the rest of my reply but was split to two by Prometheus's post written in the middle of my longer reply.

Edited by Sensei
Link to comment
Share on other sites

 

On 2/14/2021 at 9:15 PM, ALine said:

Do you have any books that you could recommend from you time starting off?

Unfortunately those books have run out of fashion; the Java language has evolved a lot since I started.

On 2/14/2021 at 9:15 PM, ALine said:

Before I ask any more questions how do you structure your different classes in Java?

It's hard to give a short answer, it is very dependent on the problem the Java program will solve. In an educational context, for instance if I was involved in a course at a university*, I would likely organize the classes to mirror some "reality" to make the discussion less abstract. Example: a board game would probably have classes that represent players, the board, game state and similar.

On 2/14/2021 at 9:15 PM, ALine said:

Also with algorithms in Java I understand that they are instructions, however I am not really sure how they interact with data structures if that makes any sense. Like when implementing an algorithm is it taking data from data structures as an input and outputting the processing step of the algorithm when applicable? Also, if you can write an program as an algorithm then what is the difference between the two? (just looked back and I think you already answered this question, just wanted to added in case.)

Ex: say you have a sorting algorithm an what to sort the entries of a linked list, do you apply the black box algorithm to the data structure or is it written into the data structure as a method of the data structure class?

It might help to separate data structure and algorithm from the implementation. Data structure could be for instance an array of sorted objects. An algorithm for finding a specific object could be "binary search", since the array is sorted. A description how to perform a binary search and any discussion about the algorithm's behavior does not require a specific programming language, we could print an array on paper and perform the algoritm's steps manually. 

Now let's say we want to implement this in Java; the class will be namned SortedArray. For this discussion we may need to take into account other aspects, for instance how to insert or remove elements from the array. Since the binary search fails unless the array is sorted we could have an insert() method implemented in the SortedArray class; insert() is implemented to always add the new object in the correct location to keep the array sorted at all times**. In this case I would also implement binary search in a methon search() in the class SortedArray. Data and algorithm implementation is in the same class in this case. 

The user of the class can see from the name SortedArray that the objects are sorted but the search method does not reveal the underlaying algorithm. We can also see that the initial description we did of the data structure and the algorithm holds even if we would change to another programming language or if we would need the binary search algorithm in another programming task in Java. 

 

On 2/14/2021 at 9:15 PM, ALine said:

Also is this all just dependent on what you want the program to do and is just slightly arbitrary/optimal selection of black boxes in different places?

That sounds correct. In an enterprise project there will be maybe hundreds of classes. Even if classes belong to the same system they should not know about nor depend upon the internals of other classes unless necessary. "Black box" is not just for someone looking at the system or program from the outside but also between internal parts of the program. How to organize, select proper sub systems or components of systems (or programs etc) is a large topic with interesting ongoing research. 

 

 

*) I use this as context for this discussion since it seems suitable for OP situation. My answer does not necessarily apply to Java development in general.

**) This is a quick example for educational purposes, not necessarily useful or efficient in real life situation. Abstractions such as Java's collections framework is out of scope at this time.

Edited by Ghideon
Link to comment
Share on other sites

OOOOOHH, ok I think I understand now. You want to implement each object as its own separate thing which can be used and implemented in different areas with other classes/objects. All of which come together to form an overall software or program which gets executed in the main/driver class. And you want to maintain this separating principle because it allows for some kind of translatability between classes. Analogous to say not necessarily a function but a physical construction of something. Like building blocks kind of. Except they can be called and implemented differently.

A guess I would have is that this would prevent massive code from falling apart. So if the program breaks then it is either one of two things. The object or the computer. THATS PRETTY FUCKIN COOL!

Now I remember someone telling me that everything in java is an object, how do you get from c in which java is built, I am assuming, on top of to an object understanding. Is java just like an analogous "wrapper" which makes programming life easier for writing in c? Or is it like "passing on the torch" where everything was once written in binary and then assembly then c and then other languages?

Thank you again for your responses.

Link to comment
Share on other sites

7 hours ago, ALine said:

OOOOOHH, ok I think I understand now. You want to implement each object as its own separate thing which can be used and implemented in different areas with other classes/objects. All of which come together to form an overall software or program which gets executed in the main/driver class. And you want to maintain this separating principle because it allows for some kind of translatability between classes. Analogous to say not necessarily a function but a physical construction of something. Like building blocks kind of. Except they can be called and implemented differently.

That is correct. Object and class are building blocks in Java language.

7 hours ago, ALine said:

Is java just like an analogous "wrapper" which makes programming life easier for writing in c? Or is it like "passing on the torch" where everything was once written in binary and then assembly then c and then other languages?

Program written in C/C++ is compiled to machine code. Understood by some physical processor. It runs directly only on CPUs which understand it.

Program written in Java is compiled to special code. It is executed on virtual machine. Simulation of the real computer. This disallow program direct access to mother machine devices and resources. e.g. if you allocate memory in Java it is from JVM heap. It is problematic on Android for example. Default heap size on my smartphone is.. 128 MB per app. Programmer has to set special field in manifest largeheap=true to get 512 MB. Virtual machine interprets Java special code to the real processor instructions.

Link to comment
Share on other sites

So java code gets interpreted on the java virtual machine which then get translated to the computers processing instructions through the jvm?

But why does java have so much portability? Is the JVM easier to write than a c compiler for the processor? How does that work?

Sorry if I am skipping around in learning about computer science. Things are just brought up that I am completely blank on.

Edited by ALine
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.