• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Need Help Understand this.

 
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have trouble writting classes in java and I been going through my java book looking at there examples but there comments don't really explain enough for me to understand. I find personaly this very hard to grasp with breaking it up into methods and stuff.

I don't know if there any good sites or tutorials that would help me out(if there plz tell me)

But anyways maybe someone can walk me me through what is happening here. Like bits and peices make sense but most of it does not.




 
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Michael Hubele:
Like bits and peices make sense but most of it does not.


Which parts do you feel like you do have a good grasp of, and which areas are confounding you?
 
Michael Hubele
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I understand the the stuff like private,main method, why it is final for the die all the system print lines.

Stuff I sort of understand:

-------------------------
Die die1;

Die is the variable type and die1 is the variable name.

------------------------

die1 = new Die();

This is making a new method of the die??? and calling it die1?
-----------------------

die1.roll();

This is calling the roll method?? what contains this:

public int roll()
{
faceValue = (int) (Math.random() * MAX) + 1;
return faceValue;
}
-----------------------

I don't understand
------------------------
What is the constructor and what are methods
--------------------------
public int roll()
{
faceValue = (int) (Math.random() * MAX) + 1;
return faceValue;
}


Like first I don't see how someone would know to cast this as a int. Then I don't know what is going on. To me it looks like instead of importing the random libary at the top they threw it all in the same line then for some reason they times it by MAX(6) then plus 1

then they return the faceValue;

not sure whats going on with the return type either.

-----------------------------
public void setFaceValue (int value)
{

}
I don't understand why this is empty like I know in the other file something gets set to 4 by using this method but I would have though something would still have to go in here and I am not sure why there is (int value) in brackets there.

These are some of the biggest problems I am having now but I still would apperiate everything to be commented so I know exactly what is going together and how they join together.
[ January 28, 2006: Message edited by: Michael Hubele ]
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
die1 = new Die();
Takes the previously declared variable and instantiates it. In this case there (IMO) is no good reason to decare the variable on one line, and instantiate it on another, I think it makes the code unnecesarily more verbose. A cleaner (again IMO) way of doing this would be:

but neither of us wote the code and I guess its just good to know two ways of writing a piece of code can produce identical results.

-----------------------

I don't understand
------------------------
What is the constructor and what are methods
--------------------------


There is no constructor written for the RollingDice class, and this class only has one method called main() which calls a number of methods of the Die class.


public int roll()
{
faceValue = (int) (Math.random() * MAX) + 1;
return faceValue;
}


Like first I don't see how someone would know to cast this as a int. Then I don't know what is going on. To me it looks like instead of importing the random libary at the top they threw it all in the same line then for some reason they times it by MAX(6) then plus 1

then they return the faceValue;

-----------------------------


I'm guessing from this statement that you are familiar with the java.util.Random class. What's going on here is there is a convience method in the java.lang.Math class Math.random() which returns a random double n where 0 <= n < 1 so multiplying n * 6 and then casting it to an int will give a random number x where 1 <= x <= 6.

-----------------------------
public void setFaceValue (int value)
{

}
I don't understand why this is empty like I know in the other file something gets set to 4 by using this method but I would have though something would still have to go in here and I am not sure why there is (int value) in brackets there.


I have to agree that writing a method signiture and then not doing anything with it is questionable style, but the point here is that you should not be able to set the face value of a die to anything, if the state of the face value of the dice is to change, then it must happen randomly via the roll() method.

[ January 28, 2006: Message edited by: Garrett Rowe ]
[ January 28, 2006: Message edited by: Garrett Rowe ]
 
Michael Hubele
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I the only thing I understood of what you wrote was that is that

die2.setFaceValue(4) does nothing because in the other file it has nothing in it and it does not return anything. So it does nothing at this moment at first I thought it did because when ran it it had the value of 4 for it so I thought it worked while testing it again I noticed that it was not working.


at this time I am trying to write comments in the file and once I am done I will post it up and you guys can check if what I am saying is write.

So if none of these are constructors or methods what do I call them then?
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm sorry, I should have said:
The Die class does have a constructor written into it, only the DiceRoller class doesn't have an explicit constructor written for it (it does however have a default constructor implied but that is another lesson). The RollingDice class has one method called main() as I stated earlier, the Die class has a number of methods: roll(), getFaceValue(), toString(), and the unimplemented setFaceValue().
 
Michael Hubele
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here this is what I got can someone check it over to see if it make sense


if you see ********************** that means I got a question to ask at that part

thxs.



 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

if you see ********************** that means I got a question to ask at that part


So what are your questions at these parts?

Layne
 
Michael Hubele
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is in the code they may not be in exact question format but basicly I wrote something about what I feel is going on and hoping for you all to tell me if it is right or wrong.

like:

//**************************************************************************
//since I think it goes back to the default roll() one when something is
// is not specified
//**************************************************************************

So at that part I am telling you what I think is going on and if it is wrong then correct me plz.
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay, I understand now. I just saw a lot of code with a lot of comments and my eyes went all blurry through that part of your message. I didn't understand that the comments actually contained your questions. Now that I look closer, you seem to understand much more than you claim. All of your comments in main() describe what is going on fairly accurately. Let's look at your questions:

These comments aren't entirely accurate. Since the setFaceValue() method is empty, this line of code does NOTHING at the moment. It SHOULD set the face value of the die. By looking at your Die class, this seems to mean that you can force the die to show any number you wish, unlike a physical die. So what do you think you should put inside the setFaceValue() method? (Hint: check the member variables.)

Now your next question:

Yes, you could do it that way. What is the difference? Perhaps you need to implment the setFaceValue() method (i.e. write the code) to see it.


A constructure must have the same name as the class. Also, all member variables must have an initial value. In fact, member variables have a default initial value depending on their type. Object references are set to null by default and numeric values are set to 0. Sometimes this default value is not meaningful so we will set our own intial value explicitly.


First of all () are called parentheses, { and } are braces, and [ and ] are brackets. The parentheses means that roll() is a method that is a method that takes no parameters. Sometimes we put parameters in between the parentheses like your setFaceValue() method.

The return statement makes it so you can do something like this:

or

In other words, when some other method calls the roll() method it can use the returned value any way it wants to. Without this return value, you would have to call getFaceValue() which would take an extra line of code.

Notice that the variable facevalue is declared outside of any methods. This means that each Die object has its own facevalue that is stored internally. This is the value that is later returned by the getFaceValue() method. This means that another method can roll() the die once and get its value as many times as you want afterwards. Whether or not this is useful can be debated.

I would like to make a note here that this is not a very good use of the Random class. This is because you create a new Random object each time this method is called. This is likely to not give an appearance of randomness. However, I'm not sure how to concisely explain a good way for you to fix this. So I'll move on to the next question for now:

The "int value" means that you can "send" a value to this method. This is the value that the face value should be set to. You are correct that this method does nothing at the moment. I think you need to add some code inside this method (probably a single line) to do what the method's name implies it should do.

Next question:

This is similar to the return statement in the roll() method. I hope I explained clearly what is going on. If not, I think you should read the section in your textbook about methods. It should make more sense than it did the first time. (You DID read it already didn't you?)

It is common to have a method called toString() in any class. In fact, if you don't supply one, a default one already exists, but I won't get into that. The purpose is usually to provide information when you need to debug a program. As you said, you don't know where this is used. It's hiding inside of code like

When you use the + operator in this way, it calls the toString() method on the objects referred to by the die1 and die2 references.

As I said before, I think you need to review the section in your textbook about methods. Now that you have some experience with them, it should make some sense.

I hope this helps answer your questions. Please come back with more.

Layne
 
Michael Hubele
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok the stuff is sort of making a bit more sense but I guess I just need to keep working at it till I understand it.

Anyways I been working on another question but something are not working and somethings don't seem to work how they should.

Same deal like last time I have all my questions/thoughts in my code I have not commented everything thing time just the stuff I am having problems in.

here is what I needed to do:


Write a class Name that stores a person's first, middle, and last names and provides the following methods:
�public Name(String first, String middle, String last)�constructor. The name should be stored in the case given; don't convert to all upper or lower case.
�public String getFirst()�returns the first name
�public String getMiddle()�returns the middle name
�public String getLast()�returns the last name
�public String firstMiddleLast()�returns a string containing the person's full name in order, e.g., "Mary Jane Smith".
�public String lastFirstMiddle()�returns a string containing the person's full name with the last name first followed by a comma, e.g., "Smith, Mary Jane".
�public boolean equals(Name otherName)�returns true if this name is the same as otherName. Comparisons should not be case sensitive. (Hint: There is a String method equalsIgnoreCase that is just like the String method equals except it does not consider case in doing its comparison.)
�public String initials()�returns the person's initials (a 3-character string). The initials should be all in upper case, regardless of what case the name was entered in. (Hint: Instead of using charAt, use the substring method of String to get a string containing only the first letter�then you can upcase this one-letter string. See Figure 3.1 in the text for a description of the substring method.)
�public int length()�returns the total number of characters in the full name, not including spaces.

2.Now write a program TestNames.java that prompts for and reads in two names from the user (you'll need first, middle, and last for each), creates a Name object for each, and uses the methods of the Name class to do the following:
a.For each name, print
�first-middle-last version
�last-first-middle version
�initials
�length
b.Tell whether or not the names are the same.


Here is my code





[ January 29, 2006: Message edited by: Michael Hubele ]
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First of its not necessary to store references to anything other than the first, middle, and last names since everything else can be derived from these.
i.e.:

Can be shorthened to :

As far as your equals method first look at the method signiture that your requirements specify:
public boolean equals(Name otherName)
Don't change that. Your'e on the right track just think about what you are trying to compare:

As for the initials, read the API documentation, or your textbook's notes on the substring() method of String class and see if you can figure this one out.

Finally for the length think about what you want to do in real words, and then turn that into java syntax. i.e. (again in pseudocode):

I say you're on the right track, just needs a few more tweaks
[ January 29, 2006: Message edited by: Garrett Rowe ]
 
Michael Hubele
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



Can be shorthened to :

As far as your equals method first look at the method signiture that your requirements specify:
public boolean equals(Name otherName)
Don't change that. Your'e on the right track just think about what you are trying to compare:

As for the initials, read the API documentation, or your textbook's notes on the substring() method of String class and see if you can figure this one out.

Finally for the length think about what you want to do in real words, and then turn that into java syntax. i.e. (again in pseudocode):

I say you're on the right track, just needs a few more tweaks

[ January 29, 2006: Message edited by: Garrett Rowe ][/qb]<hr></blockquote>





Don't understand can u explain in more detail.

with the equals I still am not sure I been playing around with it but still nothing

this is what I have at this time.



but still not working.

I have not looked at substring one yet.

[ January 31, 2006: Message edited by: Michael Hubele ]
[ January 31, 2006: Message edited by: Michael Hubele ]
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your requirements say you need a method that looks like

This is not the same as the equal() method you are working on. In fact, I don't see anything in the project description that says you need an equal(String, String) method. Do you see the difference?

Once you understand that much, you need to define what it means for two Name objects to be "equal". It looks like you are representing a Name internally with three separate Strings (first, middle, and last names). How will you use these to determine if two Names are "equal"?

Layne
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Don't understand can u explain in more detail.


No problem. What I mean is if your class already holds references to the first, middle and last names; and you (will) have methods to detemine their first, middle, and last itnitital, their names in another order etc. then you dont need to store references to these values. I just reread that and it stlii sounds confusing to me so let me state it another way. In your code you have this method.

which should work fine. What I'm saying is that you really dont need a class level variable to reflect this. If you ever want a String containing the names in this order all you have to do is call this method. Therefore this method can be changed to:

or even use no variable at all

All I'm saying here is that it is a good idea in geeral to limit all variables to as small a scope as possible.


with the equals I still am not sure I been playing around with it but still nothing

this is what I have at this time.



From the specifications you gave, the signiture of the equals method should look like

Hope this helps a little.

Garrett
 
Michael Hubele
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thxs I figured it out. Thxs all for all ur help.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic