Please help with my data structures and algorithms class

Yes, it’s homework, but I really just need a direction to be going.

Nowhere did the class say “data structures and algorithms class IN JAVA” but that’s apparently what it is. I haven’t taken any classes in Java. It was not a required course so I took Visual Basic as my programming class! So I am really stuck now. Here is my problem:

To the HighArray class in the highArray.Java program listing (2.3) add a method getMax() that returns the value of the highest key in the array, or -1 if the array is empty. Add some code in main() to exercise this method. You can assume all keys are positive numbers.

My question is, I am not stupid, but I don’t even know where to begin. Should I study some Java on my own time? I am getting some books from the library, will that help? How do I answer these questions? I just need to pass this course, this is my final semester, I do not need to ace it but I can’t fail it or I won’t graduate. Please help in any way possible.

Here is the Array:

By “key” do you mean value? Or index? “Key” makes no sense with respect to arrays.

That is exactly the problem, copied and pasted, from the book. :frowning:

(On preview: I agree with the comment about using the word key. Key is a terrible term for this, and has a very specific meaning in a data structures context.)

Well, I’m not a Java guy…but basically, let me try to walk you through the concepts.

You see the section where it says


public Boolean find(long searchKey)
{	 // find specified value
int j;
...


That’s a method. You need to add a new method, called getMax().

Notice that find takes a long as a parameter, and returns a boolean. Your getMax() won’t need to take any parameters - hence the () instead of the (long …). And it should return the same data type that the array holds. In this case:


class HighArray
{
private long [] a;	 // ref to array a

it looks like you should return a long.

Now…what does getMax() do? It needs to find the largest value in the array.
The classic way to do this is to define a local variable, initially set to 0. Walk through the entire array, similar to the way find does. At every step, check if the current element is larger than your local variable. If so, reset the local to match the value of the current element. Once you’re done going through the array, your local variable will have the value of the highest element. You can return this value, using the return statement.

Still with me? Ok! You also need to check if the array is empty…this should be done before you walk through the array. If it’s empty, as per the assignment, you return -1.

(You can optimize this a bit, but I wanted to keep it all broken out and seperate for the purpose of this post.)

Last part…adding code to exercise it. Well, calling the function is pretty straight forward. It will be similar to how you call find. You can create a variable of type long, and assign it the return value from getMax. Then use the println function to give some pretty output. You’ll want to call this after the insert calls…or maybe do it more than once, after a few of the inserts and then at the end, to show the change.
Call it once before any inserts, to prove the empty case works.

From the code it seems like they’re calling the “key” the value stored in each element of the array.

OP, if you examine and understand how the “find” method in the code you posted works it’ll become clear what you need to do for your problem to implement your method.

Do I have any hope of learning this stuff on my own? I understand some of what you say from my programming experience…but I am not a programmer and some of it is not familiar to me. I could try getting a Java for Dummies book and see if that will get my through the semester. I also thought about tutors! I have no idea how to find a good one though.

I will try to solve this and post my answer probably tomorrow…maybe if you don’t mind you could look at it?

Anyone willing to tutor me? For this I would even be willing to pay! The course is only 8 weeks…

I’ll be happy to look at your work, if a real Java person doesn’t come by. I’m a C programmer, but I know just enough Java to be dangerous. :slight_smile:
You probably don’t want me as a tutor, though.

Do you have any hope of learning on your own? Depends…how hard did you think your visual basic class was?

Do you know how to compiler and run the code? That’s your first step. If you can do that, then you can start trying stuff. For instance, you can copy the find() function…rename it to myFind, maybe. Call it the same way from the main program. Then change it to return a long instead of a boolean, and see what happens. You’ll have to change the “return true” to something like “return 1”. If you can do that, then you can return other things, too…like the index that was found in the array.
Feel free to ask more questions. I’m trying to just give some advice, and keep it high level, because trial and error is your friend. Let me know which parts you don’t understand, and I can try to explain better. For instance, do you understand what an array is? That’s kind of fundamental to the whole exercise. If you don’t have that part yet, then we need to start there before we can try to solve the assignment.

You really don’t need to know Java for this assignment. You just need to look at existing code (especially the find method), copy/paste it in, rename the new copy getMax() that returns a **long **instead of a Boolean, then change the code to return the maximum value or -1 as per the assignment. The syntax you can glean from other code in the sample.

Just curious - if you’re not a programmer, why take the data structures and algorithms class?

Required for my degree in Information Systems. :frowning: I’d never touch it with a ten foot pole if I didn’t have to.

I just have to stop freaking out and look at it calmly. :slight_smile:

Thank you all for your help.

ETA: I got an A in VB and I loved it.

One of the Gospels of Programming was the notion that, once you learn one language – and in particular, once you learn the basic concepts of programming – you should be able to learn any other language relatively quickly. That is, this was a popular idea 40-some years ago when I began to learn programming.

If you did well in VB and loved it, you should have a good chance of picking up any other programming language. Well, sort-of.

Once OOP programming came along, it sort of broke that. OOP programming seems different enough from older “procedural” programming (like VB, sort-of) that it takes a new and different way to thinking to learn it. Many of us old programming farts seem to have a hard time with it. And Java was designed, from the ground up, with the very deliberate intention of taking Object Oriented Programming to the maximal extremes.

Screw Java and all high-level languages anyway :wink:

I learned FORTRAN in high school, and for my very first college-level programming class in 1969, I took a class in programming techniques and Data Structures. The entire class was done in ASSEMBLY LANGUAGE! That’s the language anybody should learn, if you actually want to know how programming really works behind-the-scenes.

High level languages cover up too much of the detail for you. That’s great, for the purposes of, you know, actually getting anything done. But to learn how It Really Works, you need to see it in assembly language. High-level language students seem to learn only the vaguest notions of how “pointers” really work (Java is full of them, but they are rather hidden); how linked lists and trees and queues and FIFO’s and LIFO’s and stacks really wok; and how recursion really works.

We had to learn to build our own stack, in a day when machines didn’t come with built-in stacks, and how to write recursive subroutines, on a machine that didn’t have the hardware support for it that’s typically taken for granted these days. One actually had to learn how to do it.

As to the question about additional helpful resources. When I start playing with a new programming language I usually buy a book. Nowdays there are tons on online resources but I find having a book nearby helpful. Programming is a lot about pattern matching, finding and adapting similar patterns to get the functionality that you need. Others have hinted at that above. The find() method is a pattern you can copy and adapt. Books are convenient places to find examples and patterns to copy and adapt.

You probably know more Java that I do, but I’ve fiddled with so many languages from COBOL and Assembler360 to VB and almost everything in between

I’ll take this as a challenge

You have to consider the boundary conditions and exceptions -
Does the code work with zero elements in the array?
One element?
Return -1 if no elements - but can an element be -1? -20 could be the largest?

Normally you’d set the search element to the lowest value possible (-9999999?), and then every compare will find something equal or larger.
What I’ve done is:
return -1 if no elements.
Set search value to first element a[0] (since at this point there is at least one)
from the second element (a[1]) to the last a[nElems-1] compare with previously found max.
Take the larger.
Return that.

Of course I am clueless to the subtleties of JAVA syntax so this may not be the correct code. Espcially semicolons

I thank you all for your help. Sadly, my library is closed all weekend, so i will muddle through as best I can through this weekend’s assignment with your help and then on Tuesday head over there and pick up a bunch of books to help me along.

VB made so much sense to me and I really enjoyed the course. But people teach themselves Java all the time! And I am not dumb…just not that great at programming.

BTW, here is my answer to that question. Thoughts?

I put it right before this section:

That works. See, you don’t have to “know Java” to do this.

Now add the line to main to display the result of the call to getMax. Look in the display method to see how to display a value.

Yay! I need a cookie now. Or a hug. Feel exhausted from worrying. :slight_smile:

Since all elements in the array are assumed (according to the OP) to be positive numbers, you can do it this way too

public long getMax() // get maximum value
{
long lngMax = -1;
for (int j = 0; j < nElems; j++)
if (lngMax < a[j])
lngMax = a[j];
return lngMax;
}

Agreed…I didn’t run it through a compiler, but it looks right to me. Good job on the method!

Now show us the code where you call it from main…THEN you get the cookie!

A couple of things I’d suggest:

Code formatting matters. Particularly indentation - it makes a big difference to the readbility of the code, even for yourself. I’d suggest using the


 tag to help with this when you post code.

As someone else who started out in VB and moved to Java and C#, I found a few differences that made a crucial difference. The usage of braces and semicolons is one of the more important visual differences that took some getting used to.

I'd make the following minor changes:



public long getMax() // get maximum value
{
if (nElems == 0)
{
return -1;
}

long lngMax = a[0];

for (int j = 1; j < nElems; j++)
{
	if (lngMax < a[j])
	{
		lngMax = a[j];
	}
}
return lngMax;

}
// --------------------------------------------------------------------------------------




The changes I've made are mostly cosmetic, but they will help you make sure that you're including the right parts of the code within the loop. Helps avoid painful debugging later on, and personally I think the usage of braces (even for just one line) makes things a lot clearer.

I find it hard to believe that there were no prerequisites listed anywhere in the course description for this class.

And they should have known better even back then. There are different styles of language, with vastly different approaches to writing software, and … well, you’re a programmer, so compare Prolog to APL to Cobol. Do you really think someone conversant in one will be much use in any of the other two without a lot of re-orientation and starting mostly from scratch?

Java is less OO than some OO languages, although I agree it takes it too far. VB is pretty OO as well, in fact, especially VB.Net (which is different enough from Original VB that old-style VB programmers call VB.Net “Visual Fred”: They think the name should change as much as the language has.)

Besides, OO is 99.9% procedural at heart. In fact, it allows the worst excesses of procedural programming (global state, side-effect-heavy programming) to flourish within the confines of an object, so a veritable army of low-skilled programmers can work on a project without immediately causing it to collapse.

How It Really Works is the untyped lambda calculus. Machines are a distraction and procedural programming is a virus that infects otherwise promising young programmers and corrupts and perverts their precious bodily fluids. I use the paradigm, mind you, but I deny it my essence, Mandrake. Only though side-effect-free absolutely composable functions can our Purity Of Essence be restored. That’s why I only drink Haskell and pure grain alcohol… where was I? Oh, yes: Side effects are the enemy of comprehensibility and parallelism. Even if you sneer at the plebs who can’t keep track of 50 state variables across a 10,000 line program, your state-mutating masterpiece is going to be limited to a single core on a single machine without the Main Effort required to add locking primitives, suffer deadlocks, and re-write everything to add locking primitives correctly this time.