Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Head first Java: simpledotcom problems

 
Besjamain Greenaway
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3381
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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 ]
 
Campbell Ritchie
Sheriff
Pie
Posts: 49770
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
    Posts: 5
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 2
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 5
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Leonardo, make sure to match up your method signatures - checkYourself() != checkYourSelf(). I did exact the same thing recently.

    -Patrick
     
    Leonardo Lima Ribeiro
    Greenhorn
    Posts: 2
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Pie
    Posts: 49770
    69
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    It shows what tiny errors one can get all sorts of problems with, Sr Ribeiro.

    And welcome to the Ranch.
     
    Raghavan Muthu
    Ranch Hand
    Posts: 3381
    Mac MySQL Database Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 45
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 95
    Java Scala
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 45
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Pie
    Posts: 49770
    69
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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.
     
    Twan Janssen
    Greenhorn
    Posts: 2
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Andre Brito wrote: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!



    I know i am rather reopening an old topic. But thanks for the explanation.

    Just a couple of questions (hoping that i can get an answer ;)

    The value given at the testclass passes the value to the method. In the testcase, there is only one object of simpledotcom defined.(dot)
    In other cases (with several different objects created from the class..how would this work? Or are these values considered static for the whole class?
    Hope you can understand what i am saying here.

    And why wouldn't it be wise to create something like

    int guess=integer.parseInt(userguess)?


    Thanks in advance
     
    Junilu Lacar
    Bartender
    Posts: 7578
    52
    Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Twan Janssen wrote:
    The value given at the testclass passes the value to the method. In the testcase, there is only one object of simpledotcom defined.(dot)
    In other cases (with several different objects created from the class..how would this work? Or are these values considered static for the whole class?
    Hope you can understand what i am saying here.

    Unfortunately, it's not clear what you're asking.  I have bolded the ambiguous references you made.  What class are you referring to, the tester class or the class being tested. What values? Maybe it will be clearer if you give a concrete code example that we can look at and discuss.
     
    Campbell Ritchie
    Sheriff
    Pie
    Posts: 49770
    69
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Welcome to the Ranch
     
    Twan Janssen
    Greenhorn
    Posts: 2
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Junilu Lacar wrote:
    Twan Janssen wrote:
    The value given at the testclass passes the value to the method. In the testcase, there is only one object of simpledotcom defined.(dot)
    In other cases (with several different objects created from the class..how would this work? Or are these values considered static for the whole class?
    Hope you can understand what i am saying here.

    Unfortunately, it's not clear what you're asking.  I have bolded the ambiguous references you made.  What class are you referring to, the tester class or the class being tested. What values? Maybe it will be clearer if you give a concrete code example that we can look at and discuss.


    I'll try to be more clear:


    In the case given (from the head first java book) there are two classes






    I'll try to explain my thinking, could you tell me i am correct:

    The value of the method (not sure if use all the? terminology correct) is 2 in this case.
    The value of the method is only for one object, the dot object in the case.
    I was stuck on the part where the value of a method is stuck on a object..
    It didn't make sense in the beginning. I thought a method wouldn't hold a value  , but rather only a method how to calulate it.

    The thing that I am a bit unsure about is this:

    If i want to use the input of more players. Let's say 3 players. Then I would probably have 3 userguess strings
    userguess1
    userguess2
    userguess3

    But they would all use the same checkyourself method right? Which 'holds' the value 2
    Maybe i am going to fast and this will get clear in the future.


    The second question i had was about the method checkyourself..

    Why not use the value you wanna use (the value of userGuess) directly? A new String stringGuess is created, which is made an Integer and put in the guess variable. It seems redundant somehow.
    If i use userGuess instead of stringGuess the programs runs fine too, so i am not sure what the benefit is..







     
    Ganesh Patekar
    Ranch Hand
    Pie
    Posts: 347
    17
    Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Oracle Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
  • In main method of SimpleDotComTester following code creates an array of int with 3 elements 2, 3 and 4.

  • Below code invokes setLocationCells(int[] locs) method of SimpleDotCom. locations array is passed as parameter to that method. Now array locationCells refers to same array locations referred.

  • Here value of userGuess i.e. "2" is passed to checkYourself(String stringGuess) method of SimpleDotCom means here stringGuess is referring same String value which userGuess referred i.e. "2".
    Twan Janssen wrote://why not use (String userGuess)? does this have to be a new name? Or is this just more clear?

    Yes here at checkYourself(String stringGuess) you can give userGuess Or any name to this formal parameter variable. Not necessary to be new name.

  • Twan Janssen wrote:The value of the method (not sure if use all the? terminology correct) is 2 in this case.
  • "2" is the value passed to this method means now value of stringGuess is "2" .


  • Now we have int guess = 2 , String result = "miss", numOfHits = 0 and locationCells.length = 3 (Because we have 3 elements in that array 2, 3 and 4).
  • When loop is executed first time the value at index 0 of array locationCells is 2 which is assigned to int variable cell in for loop. Now we have cell =0.
  • if (guess == cell) means 2 == 2 returns true then control goes in If statement then assigns result = "hit" and increments numofHits by 1 so numofHits becomes 1, then break; statement gets executed which transfers control out of for loop and goes to below code.

  • where checks numOfHits == locationCells.length means 1 == 3 so returns false means   result = "kill"; will not get executed.
  • Then System.out.println(result); prints value of result i.e. "hit" and returns String result to code on line no 8 in main method of SimpleDotComTester class.


  • If i want to use the input of more players. Let's say 3 players. Then I would probably have 3 userguess strings
    userguess1
    userguess2
    userguess3

    But they would all use the same checkyourself method right? Which 'holds' the value 2
    Maybe i am going to fast and this will get clear in the future. 
  • Now in your example you have only one String i.e. userGuess = "2". If want three then either create 3 String values and assign to 3 String variables Or directly pass String values to checkYourself(String stringGuess) method. Invoke checkYourself(String stringGuess) method thrice on dot reference variable of SimpleDotComTester.


  • Why not use the value you wanna use (the value of userGuess) directly? A new String stringGuess is created, which is made an Integer and put in the guess variable. It seems redundant somehow.
    If i use userGuess instead of stringGuess the programs runs fine too, so i am not sure what the benefit is.. 
  • No you can't use value of String like this checkYourself("2"). You have to assign the value to variable then you can use that variable further in that method.
  • It is not Integer. It is converted to int a primitive type.
  •  
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic