Results 1 to 15 of 15
Like Tree1Likes
  • 1 Post By PhHein

Thread: Head first #2

  1. #1
    mackenzie1235 is offline Member
    Join Date
    Dec 2013
    Posts
    12
    Rep Power
    0

    Default Head first #2

    Hi all, can you tell me why this doesn't compile?

    Java Code:
    public class Clock {
    
    	String time;
    	
    	void setTime(String t) {
    		time = t;
    	}
    	
    	String getTime() {
    		return time;
    		
    	}
    }
    
    class ClockTestDrive {
    	public static void main (String [] args) {
    		
    		Clock c = new Clock();
    		
    		c.setTime("1245");
    		String tod = c.getTime();
    		System.out.println("time: " + tod);
    		
    	}
    }
    Source: Head First Java, 2nd edition, 2005, p. 93.

  2. #2
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default Re: Head first #2

    What error are you getting?
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  3. #3
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,049
    Rep Power
    6

    Default Re: Head first #2

    Note that the code printed in books tends to have many mistakes due to rushed publishing and last minute changes; also check if there is a website for the book because usually there is corrected code there and/or a list of corrections on the book's text that were found after the book went to the publisher.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,650
    Rep Power
    5

    Default Re: Head first #2

    It compiles just fine. Are you talking about compiling or executing? They are of course, different.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  5. #5
    mackenzie1235 is offline Member
    Join Date
    Dec 2013
    Posts
    12
    Rep Power
    0

    Default Re: Head first #2

    I doesn't compile and I don't get any error....

  6. #6
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default Re: Head first #2

    Why doesn't it compile? What do you see? What command(s) do you use to compile? What makes you think it doesn't compile when you don't get any errors?
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  7. #7
    mackenzie1235 is offline Member
    Join Date
    Dec 2013
    Posts
    12
    Rep Power
    0

    Default Re: Head first #2

    Java Code:
    public class Clock {
    
    	String time;
    	
    	void setTime(String t) {
    		time = t;
    	}
    	
    	String getTime() {
    		return time;
    		
    	}
    }
    
    class ClockTestDrive {
    	public static void main (String [] args) {
    		
    		Clock c = new Clock();
    		
    		c.setTime("1245");
    		String tod = c.getTime();
    		System.out.println("time: " + tod);
    		
    	}
    }
    I put this in, but there's a pop-up that states there is an error in it, but Eclipse doesn't show anything that could be erroneous. The console remains blank (or in any case shows the previous compilation).

  8. #8
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default Re: Head first #2

    You should not use an IDE until you know how path, classpath, packaging and compiling from the command line work. IDEs hide all that stuff from you. You have to learn how to use eclipse first. Have a look at the Problems view in eclipse.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  9. #9
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,049
    Rep Power
    6

    Default Re: Head first #2

    That is 100% true, but yeah if you're following a book then you basically do what the book wants you to do...
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  10. #10
    mackenzie1235 is offline Member
    Join Date
    Dec 2013
    Posts
    12
    Rep Power
    0

    Default Re: Head first #2

    @PhHein, what do you mean? Shouldn't I use Eclipse right now?

  11. #11
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default Re: Head first #2

    It is purely my personal oppinion, but I think beginners should only use a text editor and the command line to learn the basic stuff.
    SurfMan likes this.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  12. #12
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,049
    Rep Power
    6

    Default Re: Head first #2

    That opinion has been voiced numerous times and I used to agree with it but think about it - the first thing you do is learn the language syntax by creating a public static void main in which you do all your stuff. Using an IDE which gives you instant feedback is a much better learning experience in that phase. When you start to move to slightly more advanced topics such as the classpath and using multiple classes, THEN it becomes relevant to temporarily let go of the IDE and do some experimentation on the command prompt / shell alone.

    Use the right tool for the job. I am a firm believer that using an IDE is the best tool to learn the language. But it is the worst tool to learn the details of the platform.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  13. #13
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default Re: Head first #2

    That is an acceptable approach, as long as the beginner is capable of also learning how to use the IDE. Right in this thread the OP had no idea how to use the IDE. Getting used to an new IDE can be a pain in the butt, especially when you have no idea whether you have a code or an IDE problem.
    That's why I will keep suggesting to use the command line.:)
    I think we can agree to disagree.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  14. #14
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,049
    Rep Power
    6

    Default Re: Head first #2

    You make a very valid point, but you need only a couple of very minor things to get going so the problem is not really THAT big if you have a proper guide.

    I want to learn Java: 01p. Install Eclipse and learn the language

    Which friendly folks like us might just provide ;) (shameless self promotion)
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  15. #15
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,650
    Rep Power
    5

    Default Re: Head first #2

    I tend to agree. Most of my coding is done with Eclipse. If I need to do CLI I need to think about src paths vs bin paths, package locations and all the other related environment variables which Eclipse hides from the user. And then there are the related switches for the compiler. And I am not very good at it. Using an IDE is like turning up the thermostat. It is of no value if you are camping.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. Head First Java
    By mackenzie1235 in forum New To Java
    Replies: 5
    Last Post: 01-13-2014, 12:43 PM
  2. What does LinkedList.Node head = linkedList.head(); does?
    By Jack_Tauson_Sr in forum New To Java
    Replies: 1
    Last Post: 04-19-2013, 08:32 AM
  3. HEAD first by kathy sierra
    By j2vdk in forum New To Java
    Replies: 0
    Last Post: 08-30-2008, 01:08 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •