wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Head first Java; simpledotcom problems... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Head first Java; simpledotcom problems..." Watch "Head first Java; simpledotcom problems..." New topic
Author

Head first Java; simpledotcom problems...

Besjamain Greenaway
Ranch Hand

Joined: Dec 21, 2007
Posts: 45
Hello all,

while working in the HFJava book, I have to make a game. This is the testclass;




And the Class itself;



Now, I can't figure out what a couple of things mean. For example;



And where does the



stringGuess come from? Another thing I don't get is public. How does it work that anyone can reach it?

Thanks again for anyone who can hint me in the right direction!
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344


Originally posted by Besjamain Greenaway:

Now, I can't figure out what a couple of things mean. For example;




What is your doubt here?


...stringGuess come from?


It came from the following lines.



You can invoke the method of a class from another or any part of the other class provided if you have visibility or access to the class and its members.


Another thing I don't get is public. How does it work that anyone can reach it?


Means, if any members of the class or the class itself is declared with public access modifier the compiler makes a note that anyone outside the class can have access to the members and they are perfectly visible.

If not, the compiler warns you with an error. That's because you have an access violation which the compiler gets an indication through these modifiers.

Does that help?
[ December 29, 2007: Message edited by: Raghavan Muthu ]

Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36578
    
  16
Originally posted by Besjamain Greenaway:


This is a typical "mutator method," also called a set method. Its name should always start with "set." There are examples in a couple of tutorials I found this tutorial with some examples of set methods. Set methods are a component of the boilerplate code in any Java class; they allow access to your fields via methods and can have controls for the values entered.

Where does stringGuess come from? It is passed there from whichever method calls it. Raghavan Muthu has shown you how you can pass it from code elsewhere, in a different object which has a reference via a variable called "dot." He has also told you the compiler will allow access from other classes if you use the "public" keyword.
  • Call a part of your code "public"
  • Compile it
  • The compiled code has a flag saying it is "public"
  • You can copy it into another directory and try getting access to it from there.
  • If the compiler finds other code trying to get access to "public" parts, it will permit it.
  • If the compiler finds code outside this class trying to gain access to "private" code, it will refuse to compile it.
  • There is also protected access and access where you miss out the modifier, but forget about those for the time being.
  • Patrick Loz
    Greenhorn

    Joined: Dec 21, 2007
    Posts: 5
    Originally posted by Besjamain Greenaway:



    Something I don't think was addressed, the bolded you are referring to is properly termed, locs is an array of ints, and you are simply setting this array of ints(which are passed through the arguments in the set mutator) to the instance variable that is locationCells.
    [ December 30, 2007: Message edited by: Patrick Loz ]
    Leonardo Lima Ribeiro
    Greenhorn

    Joined: Dec 29, 2007
    Posts: 2
    Hey guys, im with problem too.

    I tried to do this SimpleDotCom and SimpleDotComTester and I got these problems:

    javac SimpleDotComTestDrive.java
    SimpleDotComTestDrive.java:7: cannot find symbol
    symbol : method checkYourSelf(java.lang.String)
    location: class SimpleDotCom
    String result = dot.checkYourSelf(userGuess);
    ^
    1 error

    I have 2 files, see the code below:

    SimpleDotCom.java --> I compiled it with no problems!


    And SimpleDotComTestDrive.java that returns the problem above.


    Whats the problem? I cant see.
    Patrick Loz
    Greenhorn

    Joined: Dec 21, 2007
    Posts: 5
    Leonardo, make sure to match up your method signatures - checkYourself() != checkYourSelf(). I did exact the same thing recently.

    -Patrick
    Leonardo Lima Ribeiro
    Greenhorn

    Joined: Dec 29, 2007
    Posts: 2
    Sry guys but now I find the problem.

    The problem was because I put "String result = dot.checkYourSelf.." instead of "String result = dot.checkYourself..."

    Thanks for all...
    Nice book... HeadFirst!

    Aproved!
    I start from 0 and now im programming java!
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 36578
        
      16
    It shows what tiny errors one can get all sorts of problems with, Sr Ribeiro.

    And welcome to the Ranch.
    Raghavan Muthu
    Ranch Hand

    Joined: Apr 20, 2006
    Posts: 3344

    Originally posted by Leonardo Lima Ribeiro:
    Sry guys but now I find the problem.

    The problem was because I put "String result = dot.checkYourSelf.." instead of "String result = dot.checkYourself..."

    Thanks for all...
    Nice book... HeadFirst!

    Aproved!
    I start from 0 and now im programming java!


    If i am right, the same has been conveyed already by some other ranchers
    Besjamain Greenaway
    Ranch Hand

    Joined: Dec 21, 2007
    Posts: 45
    Wow, thanks guys! It took a little long for me to reply, I was celebrating new year somwhere internetless...

    Anyways, the thing I didn't get was where the stringGuess came from, since I didn't see it defined anywhere. I did see a string userGuess, but I fail to see the relation...

    And I am correct that everything between the curly brackets ({}) is a function?

    And how does the mainclass know where to find the other class? I assume you have to put them both in the same map, right?
    [ January 07, 2008: Message edited by: Besjamain Greenaway ]
    Andre Brito
    Ranch Hand

    Joined: Dec 13, 2007
    Posts: 95

    First of all, sorry my english

    Let me try to answer your questions:

    And I am correct that everything between the curly brackets ({}) is a function?


    Yes, but it's not just a function. The code of your classes, the loops that will make more than 1 thing and the functions are between curly brackets.

    And how does the mainclass know where to find the other class? I assume you have to put them both in the same map, right?


    In this one, you caught me! I believe that both (or more) classes must be at the same directory.

    Anyways, the thing I didn't get was where the stringGuess came from, since I didn't see it defined anywhere. I did see a string userGuess, but I fail to see the relation...


    Always remember: Java passes the arguments by value. So, the String userGuess at the test class is just a value that will be sent to the method. The code



    it's the same that



    So, the parameter is copied and manipulated at the method. The stringGuess is just a instance variable that receives the value from the userGuess, defined at the test class. It'll be passed the value, and the stringGuess will get that value, so the method can manipulate it.

    I'm really sorry about my english, I have to practice a little more.

    Cumps!
    Besjamain Greenaway
    Ranch Hand

    Joined: Dec 21, 2007
    Posts: 45
    You english is perfectly understandable, and, thanks for your reply! It cleared things up definately One thing I noticed though is that everytwhere the word "String" is put in front. I.e,



    It's String galore! What I wondered though, wouldn't defining "2" (as opposed to 2) make it a string already? And wouldn't that make that the result of String result a string? And even more so, doesn't Java know by then that it is a string put through in (String stringGuess)?
    Bob Abbey
    Greenhorn

    Joined: Jul 15, 2008
    Posts: 1
    Hey I'm new here! want to help me? I'm doing the simpledotcom program in HF Java Second edition, and I ran across a problem. In the book, were supposed to declare two classes:

    public class SimpleDotComTestDrive

    and

    public class SimpleDotCom

    it says it's supposed to compile, but it in fact does not. I've since figured out that apparently only ONE class can have the public modifier, and I got it working by removing the 2nd public. Is this a new problem? Why is the book showing me that both classes can be public...
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 36578
        
      16
    Welcome to JavaRanch

    What it means is that you can't have two public (top-level) classes in the same file. You declare both classes public, but you put them in two files. The names of the files must be exactly the same as the names of the classes with a .java extension.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Head first Java; simpledotcom problems...
     
    Similar Threads
    Help compiling Program
    Getting 6 errors with the use of result(noob)
    HFJ SimpleDotCom problems
    i am not able to get out put of this program
    Test class cannot find other class