View Full Version : Simple problem, but im a moron in Java


schawks9900
03-28-03, 05:17 PM
I am in a Java Programming class and i am dumbfounded. I understand the concepts, but i cant remember the code. If anyone has any tricks to help me, or has dealt with the "Java" by Software Solutions book, and happens to have chapter 3 and 4 projects laying around, that would great, really weird, but great.

AntonK
03-28-03, 05:23 PM
Sorry...no easy ways out. You just need to program... A LOT. Java is a language just like any foriegn language. Try not to think about the code like you would equations for a math class. Think of them like you would learning another language. You couldn't learn spanish by just reading spanish. You need to speak and write spanish. Write lots of code. Even if you are just copying the code out of books, it is still familiarizing your mind with the code. Then you'll reach a point where instead of being dumbfounded you know there is a certain peice of code you want, but you can't remember it exactly. The whole "tip of the tongue" thing. Then keep programming and you'll just remember it. Simple as that.

-AntonK

Sorry..wish i could be more help...but there are not tricks.

CompiledMonkey
03-28-03, 06:14 PM
Yeah, he's right. Just keep writing code. When you go over the control structures (loops, conditionals, etc), just keep writing them over and over on your own. Same with the other concepts (arrays, methods, classes, OOP). You probably will just start OOP as your class is ending. Most intro programming classes using Java make it through about what I posted. The best advice is to just enjoy what you are doing, keep an open mind to the concepts (esp OOP), and practice.

cjard
04-01-03, 06:25 AM
<pre>
can you explaina bit more about your particular issue..

at the moment i get the impression: you can understand the concept of a Bubble Sort, i.e. "lighter" items float (via a swap) when examined.. but you cant convert that to java code, in the form (for example)

if ( object1.getWeight() < object2.getWeight() )
swap( object1, object2 )

-
this reads as: (and its pretty normal english)

if (
find object1 in the sandpit, open it up and press the "getWeiight" button, which effectively turns the object into just a simple number, in my hands.

now find object2 in the sandpit. again, press the getWeight button and watch it dissolve into just a simple number

compare the 2 numbers in your hands: ask if object1's weight is less than object2's weight)

if the comparison is true, that object1 is lighter than object2
-> go to the portion of program that swaps them over


//
if the weight of object1 is heavier, they arent swapped..

-

the hard bits in all of this are the objects, dot, gets and brackets
object1 is the name of this.. thing, floating round in your computer memory. every time you write object1, java can go find the object and wait for you to press a button on it to turn it into something else (it doesnt quite happen like that, but the concept is valid for simple explanation purposes)

objects have a lifetime, known as scope. once they go out of scope, they vanish:

Car myCar = new Car("red", "chevrolet");
//this makes a new car. ignore the technicalities. we now have a car and every time we say myCar, java goes and gets it for us

myCar will only be around for so long tho, before it vanishes. you can tell exactly when it will vanish, by looking at the girly (curly) brackets { }
{ } defines scopes.. everything between them, including other girly brackets, are part of a scope. like this:

public void makeACupOfCoffee()
{
//we have started a scope.. the scope is for the contents of the method makeACupOfCoffee. this basically tells java how far we can follow before we fall off the page of instructions called makeACupOfCoffee. and thats just it.. every method is just like a list of instructions..
boil the kettle, get the coffee..

i had to do this when learning java and it confused the bejesus outta me cos they really tried to make it too object oriented to start with

anyway, back to scopes
the makeACupOfCoffee scope will end with another girly bracket
}

everything within is to do with the method, all outside is nothing to do with makeACupOfCoffee (tho it might refer to it:
boolean dadWantsACupOfCoffee = true;
if (dadWantsACupOfCoffee){
makeACupOfCoffee()
}

again, there is a scope.. see the girly brackets on the IF {}
that defines everything to do if that condition (dadWantsACup...) is true

if(dadWantsACup...){
makeACupOfCoffee()
boolean mumWantsACupOfCoffee = askMumDoesSheWantACupOfCoffee()
makeACupOfCoffee()
}

we now do theose 3 things every time dad wants a cuppa (note, its not a correct algorithm.. deliberately, read on!

-
scopes within scopes..
back to scopes and vanishing objects

an object created within a scope will only exist for the length of that scope. in the middle of that coffee thing (the 3 step one) we created a new boolean object (actually, its a primitive, but ignore that technicality)

see the line:
boolean mumWantsA...

that creates the new boolean. when the scope finishes, and you hit that } girly bracket.. that boolean will disappear

so you cant say:

if(dadWantsA...){
...
boolean mumWatsA...
...
}

//zap!

if(mumWantsA...)

----

the mumWantsA.. boolean that we made in the middle of that scope, disappeared as soon as we hit that girly bracket denoting the end of that scope. if we had created it before:

boolean mumWantsA... = (whatever);
if(dadWantsA....){
...
mumWantsA... = (whatever)
...
}

//its still here

if(mumWantsA..){
}

----

note another difference.. in the if(dadWantsA) now, i do not create another boolean, cos ive already created it.. i jsut set it by writing [ mumWantsA... = ] not by creating it [ boolean mumWantsA... ]
the key to creation lies int he "boolean" word.. if its not there, java tries to find an object called mumWantsA. if it is there, java tries to create one called mumWantsA.
it will fail if there is already one floating arousn somewhere

---

you could write if you really wanted:

<pre>
{
{
{
boolean dadWants
{
{
boolean mumWants
}
}
}//end of scope 3
}
}
</pre>

the mumwants boolean would only be valid for scope number 5 (count the btrakcets) the dadWants would be valid through scopes 3 4 and 5, but not in 1 and 2, cos it disappears when the } of scope 3 is reached




perhaps you just dont easily remember the semantics and syntax of java.. in which case:

if ( evaulation) {
//do if true
}
else{
//do if not true
}

evaluation but come to a true or false conclusion. bear in mind that you dont need to write:
if( myBoolean == true )

because myBoolean is aleady either true or false, its suitable for use as an evaluation:

if( myBoolean )

for example:

boolean shipIsSinking = true;

if ( shipIsSinking == true )
if ( shipIsSinking )

//they both achieve the same thing, but the first one involves unnecessary calculation. i'd expect most compilers to automatically remove a (boolean == true) comparison and convert a (boolean == false) to !boolean, but its better that you get the concept understood :)
it is also a good teaching point for to name your vaiables carefully.. calling that boolean "sinkStatus" is not good, because:

if ( shipIsSinking ) is much clearer than:
if ( sinkStatus )

//i can see automatically in the first case, what the block of code under the if actually applies to. the second case, it is not immdiately apparent what the code is for.. something to do witha sinking ship,, or maybe the fullness of the sink in the galley. or maybe even if there is a sink in the galley. what about the heatsink on the cpu controlling the radar? you see...

but ask away specifics by all means :)</pre>

cjard
04-01-03, 06:52 AM
maybe i should write a book

testify
04-13-03, 02:59 AM
lol you basically put together everything I have learned in my java class so far. It took me 5 min. to read that, and 4 weeks to learn it in school.

Oh...and with your Bubble Sort...when I did that I found that there was like 15 different ways to actually go about it. Which way would you say is the easiest/most efficient?

AntonK
04-13-03, 09:50 AM
Yes...and you'll find out sooner or later that bubble sort is INCREDIBLY inefficient, and no programmer uses it. I mean come on, its O(n^2).

-AntonK

CompiledMonkey
04-13-03, 10:41 AM
Yup, O(1) owns j00!

cjard
04-13-03, 07:20 PM
Originally posted by testify
lol you basically put together everything I have learned in my java class so far. It took me 5 min. to read that, and 4 weeks to learn it in school.

Oh...and with your Bubble Sort...when I did that I found that there was like 15 different ways to actually go about it. Which way would you say is the easiest/most efficient?

theres no "efficient" way to do a bubble sort.. it can be improved upon by having a rough idea for the mean weightings of the objects to sort and bubbling them further than just one place based on how relatively heavy or light they are (relative t their depth in the table.. items near the bottom that are very light, can be pressurized to the shallower depths with a good chance of success in reducing the bubblings), but at the end of the day its always going to be hella inefficient.

it is taught because to try and get your head round a shuttle sort, whenyou dont know the syntax of java etc, is nigh on impossible :) (shuttle sort is somewhat like a quicksort with performance improvements, and involves binary chopping a data set and swapping the chopped elements according to how they are weighted against the other side of the pivot (chop point)

just wait while you get to avl trees :)

is there anything youre stuck on in particular?

GMontag
04-20-03, 02:21 AM
Originally posted by AntonK
Yes...and you'll find out sooner or later that bubble sort is INCREDIBLY inefficient, and no programmer uses it. I mean come on, its O(n^2).

-AntonK

There are situations where a bubble sort is very useful. Bubble sorts are quite efficient when you know that the list you want to sort is already mostly sorted.

testify
04-20-03, 02:55 AM
Well we weren'treally taught what the "bubble" for bubble sort stood for, but we were shown how to sort through arrays and had to write the code to do it. Then we were shown how to do Insertion sort which is quite a bit more complex than bubble sort but even that wouldn't have been that tough to code. I wasn't too stuck on anything really, but I was kind of wondering what the difference is beween the many kinds of sorting methods you can take (tree sort, bubble sort, etc.)?

Yes...and you'll find out sooner or later that bubble sort is INCREDIBLY inefficient, and no programmer uses it. I mean come on, its O(n^2).

I'm sorry I don't get O(n^2). Is O a given object? We just did bubble sort on simple int arrays. *shrug*

cjard
04-20-03, 05:59 AM
bubble is jusst a pet term.. you could call it swap sort, or sink sort.. anything that describes the assessment of relative "weights" of objects and moves them accordinly, could be used.

insertion sort, i never heard of..

tree sort is another way of sorting data according to relative weighting.. like climbing a tree, and going left if your object is lighter and right if your object is heavier , than th object at the meeting point of the branches.. eventually you get a tree full of objects and you can sort them a-z and z-a by reding from left to right, or right to left.. normally used to teach recusrion

O refers to order, and specifies how BIG your problem is O(n^2) is order-n-squared. if you have N objects, you must make N squared comparisons.
a 10 long list will take up to 100 comparisons to ensure it is fully sorted, because it could be totally upside down, and every object will bubble somewhere... O describes the worst case

O(n) is very good.. only 10 comparisons needed to sort your objects

some problems are order N factorial O(n!) - this is very bad, and only a few objects result in millions of comparisons. for example, this web page http://www.durangobill.com/N_Queens.html lists the n-queens problem.. about how many combinations of ways you can put N queens on an NxN chessboard, so that no queen is attacking another..
the jump from 22 to 23 queens results in a jump from 2, to 24 quadrillion (22e15) combinations.. whereas from 21 to 22 is a paltry 2.3 quadrillion jump..

that sort of stuff is really hard

cjard
04-20-03, 06:04 AM
i forgot to mention, you can normally throw time or space at a problem to make it better, but reducing one increases the other..
tree sort is fast, but needs a lot of space. bubble sort is slow, but memory requirements never extend beyond the original list

its a bit like time/money.. there are mpeople in the world with no money, but enough time to fix their car.. there are other people who are too busy earning money to have the time to fix their car. eventually we progress from the first state, to the second.. and pay someone to fix our cars.. cos it makes way more sense than getting dirty :)
in computer science, you e;larn all the theories, write sort routines yourself, then eventually use a ShuttlSort provided by Sun Microsoystems and forget bout doing it ourselves, ever again.. after all, there are geeks out there paid to make sorting better.. we just get on and write the apps on top