A few java questions

  • (2 Pages)
  • +
  • 1
  • 2

27 Replies - 2313 Views - Last Post: 26 September 2012 - 02:59 PM Rate Topic: -----

#1 Ytry   User is offline

  • D.I.C Head

Reputation: 16
  • View blog
  • Posts: 120
  • Joined: 25-July 12

A few java questions

Posted 25 September 2012 - 05:57 AM

Like a lot of you in this forum know I have been learning java over the past year. While I wait for my birthday so I can get a java book, I have been trying to fill in the gaps of things I don't know / don't understand as well as I would like to. I am not asking anyone here to teach me, though if you do that is even better, but rather point me in the right direction with a place to learn. The first thing I have don't really understand is the toString method in java. The second is recursion, and the last is GUI in general. I know all three of these are very broad topics, and that I have not been very specific with what I don't understand. This is because I know so little that I could not even explain what I don't understand. I would appreciate just pointing me in the right direction of some good tutorials, or giving some examples.

Is This A Good Question/Topic? 0
  • +

Replies To: A few java questions

#2 GregBrannon   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2250
  • View blog
  • Posts: 5,340
  • Joined: 10-September 10

Re: A few java questions

Posted 25 September 2012 - 06:22 AM

Not sure which book you decided on, but don't forget to review the "Best Beginner's Book" thread in the Advanced Forum.
Was This Post Helpful? 0
  • +
  • -

#3 farrell2k   User is offline

  • D.I.C Lover
  • member icon

Reputation: 874
  • View blog
  • Posts: 2,706
  • Joined: 29-July 11

Re: A few java questions

Posted 25 September 2012 - 06:34 AM

Recursion is a somewhat advanced topic that I think should not be approached by beginners. Swing GUIs are also an intermediate topic that beginners should avoid until they have a better understanding of the language itself. If you really want to play with GUIs while learning Java, I recommend Netbeans and its GUI builder. You only worry about dragging and dropping and double clicking components to make them work. When you feel more comfortable with the language, then you can learn to code them by hand.

I think the best way to learn Java is via the web. The Head First Java book is a popular recommendation, however.
Was This Post Helpful? 0
  • +
  • -

#4 Ytry   User is offline

  • D.I.C Head

Reputation: 16
  • View blog
  • Posts: 120
  • Joined: 25-July 12

Re: A few java questions

Posted 25 September 2012 - 06:43 AM

The book I previously decided on is http://www.amazon.co...l/dp/0132575663 which was recommended in a previous thread I made. Though it is pretty expensive, and I cannot afford it so I am waiting for my birthday to get it(My birthday is November 1st). I just made this thread to get some recommendations of what to do until then. Also I didn't know netbeans had a GUI builder I will definitely look into that thanks for letting me know.
Was This Post Helpful? 0
  • +
  • -

#5 jon.kiparsky   User is online

  • Beginner
  • member icon


Reputation: 11482
  • View blog
  • Posts: 19,546
  • Joined: 19-March 11

Re: A few java questions

Posted 25 September 2012 - 06:57 AM

The toString method is inherited from Object, which is the parent class of all Java objects. This means that all Java objects have a toString method, which defaults to returning a String consisting of the name of the object's class and a unique identifier (the object's address in memory is what is actually used, but that's not important).
This method is what is called if an object is presented as the argument to a print method. For example, if you have a Dog class and the toString() method is just
public String toString(){
  return this.name;
}


then
Dog mutt = new Dog("Fido"); // assume the constructor sets the name
System.out.println(mutt);



would produce "Fido" as output.

If you write a class, it's worth overriding this method with something that produces a String which represents this particular object.


Recursion is not that difficult a concept to understand, but it can be hard to swallow. Simply put, a recursive method is a method M that calls another instance of the method M to complete its work.
It's often said to "call itself" but this is not quite a useful way to conceive of the thing.

The easiest way to understand it is to play with it. Here's a recursive method that adds two numbers together. Write a main method to drive the add method, and test it out and convince yourself that it works for some numbers and not for others (where does it fail? why?) See if you can understand how it works, and ask a few questions about it.
If you think you understand it, write a method that multiplies two numbers, on the same principle.


public class RecursiveAdder {
  public int add(int x, int y)
  {
    if (y==0) return x;
    else return add(x++, y--);
  }
}

This post has been edited by jon.kiparsky: 25 September 2012 - 07:32 AM
Reason for edit:: forgot a semi - too much python!

Was This Post Helpful? 1
  • +
  • -

#6 Ytry   User is offline

  • D.I.C Head

Reputation: 16
  • View blog
  • Posts: 120
  • Joined: 25-July 12

Re: A few java questions

Posted 25 September 2012 - 07:22 AM

GregBrannon has been generous enough to provide me with plenty of reading material for a while, but after reading for about an hour I find that I actually want to code, and not read. Is it bad for me to stray away from reading, and do some coding myself, or should I continue on with the book?
Was This Post Helpful? 0
  • +
  • -

#7 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12568
  • View blog
  • Posts: 45,690
  • Joined: 27-December 08

Re: A few java questions

Posted 25 September 2012 - 07:24 AM

*
POPULAR

No. You learn coding by coding. Practice what you read. Play around a little. You don't need us to hold your hand through learning. In this industry, you'll have to learn to tinker and learn by experimentation. You might as well start now with some of the basics. :)
Was This Post Helpful? 5
  • +
  • -

#8 jon.kiparsky   User is online

  • Beginner
  • member icon


Reputation: 11482
  • View blog
  • Posts: 19,546
  • Joined: 19-March 11

Re: A few java questions

Posted 25 September 2012 - 07:31 AM

I agree with Mac - you should absolutely write code, in addition to reading about the language.
Whatever you read, you should try it out against the compiler - type in the example code, and make it work, and figure out how it works by changing pieces of it. "What happens if I plug this wire in over here? Oh, bright flash and smoke comes out. Why's that? Oh, let me try it this way"
You can't break the compiler, and "halt and catch fire" is only an expression - it's actually very difficult to get a modern computer to execute an HCF instruction. So go for it.

If you get an idea for something you want to make, give it a try. Again, you can't break anything. The only thing I suggest is that you try to make good mistakes, and make the most of each mistake. Try to make new mistakes, not the same ones over and over.
Was This Post Helpful? 3
  • +
  • -

#9 cfoley   User is offline

  • Cabbage
  • member icon

Reputation: 2391
  • View blog
  • Posts: 5,021
  • Joined: 11-December 07

Re: A few java questions

Posted 25 September 2012 - 07:32 AM

Is that the book I recommended? Don't bankrupt yourself. See if you can find a cheaper second hand one on eBay or google shopping. A slightly older edition might offer a compromise on price as long as it is not too ancient. Maybe your library (local or school) would consider getting it in for you. I have had success getting new books in my university library through requesting them from my department.
Was This Post Helpful? 2
  • +
  • -

#10 jon.kiparsky   User is online

  • Beginner
  • member icon


Reputation: 11482
  • View blog
  • Posts: 19,546
  • Joined: 19-March 11

Re: A few java questions

Posted 25 September 2012 - 07:43 AM

This is a two revisions back, substantially cheaper:

http://www.amazon.co.../dp/0132222205/

It targets Java 1.6, which is fine. The changes between 1.6 and 1.7 are not that significant, and you can pick up on them once you've mastered 1.6.

You'll be tempted to add other books to the list - try to keep focused on one at a time. It's very easy to get backed up, with a pile of books to read staring at you.
Was This Post Helpful? 2
  • +
  • -

#11 Ytry   User is offline

  • D.I.C Head

Reputation: 16
  • View blog
  • Posts: 120
  • Joined: 25-July 12

Re: A few java questions

Posted 25 September 2012 - 07:59 AM

Thank you for recommending a cheaper version. I was really in a bind over the price tag, but again I am willing to do what I have to in order to learn. a $12 book that teaches me the same things as a $120 book is really quite a good deal XD
Was This Post Helpful? 0
  • +
  • -

#12 jon.kiparsky   User is online

  • Beginner
  • member icon


Reputation: 11482
  • View blog
  • Posts: 19,546
  • Joined: 19-March 11

Re: A few java questions

Posted 25 September 2012 - 08:17 AM

Textbooks are usually grotesquely overpriced, and this one seems to be no exception. They're sold to college students who are spending tens of thousands on tuition, so a few hundred seems like no big deal - this is a good deal for the authors and publishers.
If this one is good, it's worth going through it in detail. Algorithmic thinking is not easy to get the hang of. (how's that recursive exercise treating you? :) )

Some other useful references:

The Sun Java Tutorials. Oracle has their branding on these now, but they were developed by Sun. Whatever the name on them, they're a very good trip through the fundamentals of the language.

The Java API. Complete reference for all of the classes of the Java standard libraries. This is where you'll do most of your reference. If you want a reason Java took off the way it did, this reference material is a good one.

The Java Language Spec. Probably heavier material than you want to get into now, but this defines the behavior of the Java language. If it doesn't conform to the JLS, it's not Java. It'll be pretty mind-breaking for you at this point, but if you want to challenge yourself, try to read through for example the section on conversions or names. It's heavy stuff, but there's a kind of poetry there.
Was This Post Helpful? 2
  • +
  • -

#13 Ytry   User is offline

  • D.I.C Head

Reputation: 16
  • View blog
  • Posts: 120
  • Joined: 25-July 12

Re: A few java questions

Posted 25 September 2012 - 08:45 AM

Wow, there is not a lack of reading material presented to me here, which I truly am thankful for. Now I just gotta take the time to read it XD. As far as the recursive exercise goes, I don't really understand what I am supposed to do could you go into more detail please?
Was This Post Helpful? 0
  • +
  • -

#14 jon.kiparsky   User is online

  • Beginner
  • member icon


Reputation: 11482
  • View blog
  • Posts: 19,546
  • Joined: 19-March 11

Re: A few java questions

Posted 25 September 2012 - 09:03 AM

Quote

As far as the recursive exercise goes, I don't really understand what I am supposed to do could you go into more detail please?


Well, try to understand it, first. I assert that this adds two numbers. Do you believe me? Do you have any qualms about that?

One way to try to understand it is to try to follow it in your head. What happens when I call

RecursiveAdder ra = new RecursiveAdder();
int n = ra.add(5,3);


If you need to, use a pencil and paper to keep track. Hint: Remember that a method call is evaluated as an expression: the value of
foo(5);


is simply the return value of the method foo, when 5 is the argument. If "foo" is a method to test whether an integer is even, this value would be "false". If foo squares its argument, the value of that expression is 25. So if you want to know what
add(3,5);

means, you only need to go figure out what the return value would be, and substitute that in.
Was This Post Helpful? 1
  • +
  • -

#15 Ytry   User is offline

  • D.I.C Head

Reputation: 16
  • View blog
  • Posts: 120
  • Joined: 25-July 12

Re: A few java questions

Posted 25 September 2012 - 09:25 AM

Okay I may be totally wrong here, but I am going to try to explain what I think is happening here.
RecursiveAdder ra = new RecursiveAdder();
int n = ra.add(5,3);


When I look at this snippet of code, I think that n passes the values of 5 and 3 into your RecursiveAdder, which you say adds the two numbers together. So I am going to go out on a limb here and assume that this method adds the numbers 5 and 3 together. Assuming you pass in 5 and 3 like you have done here wouldn't the return value be 8?
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2