• 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

Scrabble with creating classes

 
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a scrabble project, and the steps I have completed are as follows:

- Write a definition for a class named Tile that represents Scrabble tiles. The instance
variables should be a character named letter and an integer named value.
- Write a constructor that takes parameters named letter and value and initializes the
instance variables.
- Write a method named printTile that takes a Tile object as a parameter and prints the
instance variables in some reader-friendly format.

The last step I have is:
- Write a method named testTile in your main class, not the Tile class, that creates a
Tile object with the letter Z and the value 10, and then uses printTile to print the state
of the object.

This is my code:


My question is, how do I create a Tile object with the letter z and the value 10? Would I do it similar to line 13 but with different values?
 
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, what you have there is the declaration of the Tile class. If you want to create a new Tile object you do it like this:



And then if you want to assign values to the instance variables of that object, you'd do it like this:



However... your requirements say this:

The instance variables should be a character named letter and an integer named value.



You haven't done that; you made them doubles instead. My assumption is that "Scrabble" is referring to the popular board game Scrabble where you have plastic square tiles, each of which contains a letter (like "E") and a value used while playing the game (like 1). If you aren't familiar with that game it might help if you researched it a bit, because I find that understanding the background behind a program's requirements helps considerably when writing the program.

And if my assumption is right, then the letter and value of a tile can't change as the game progresses, so you would want to make the class's instance variables private and initialize them via parameters of the class's constructor.

 
Lexi Turgeon
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When I tried to make the instance variable for letter, I was going to make it as a string, is that a correct way of doing it?
 
Paul Clapham
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You could use a String to represent a character; that would certainly work if you made sure the value always contained exactly one character, no more and no less. But Java also has a data type which is used specifically to represent a single character so it would (I believe) be preferable to use that data type rather than String.
 
Lexi Turgeon
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:You could use a String to represent a character; that would certainly work if you made sure the value always contained exactly one character, no more and no less. But Java also has a data type which is used specifically to represent a single character so it would (I believe) be preferable to use that data type rather than String.



When I change the letter to a string:



I get this compiler error:


Line 13 is incorrect, but I am not sure how to correct it. I understand that an int cannot be converted to a String, but how would I make it possible to convert it in this code?
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The requirements state that for a Tile, the letter should be a character and the value an integer. What reason do you have for not following this instruction?
 
Lexi Turgeon
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Junilu Lacar wrote:The requirements state that for a Tile, the letter should be a character and the value an integer. What reason do you have for not following this instruction?



I thought that by changing the letter to a string I could give it a character, but I am not entirely sure how to do that.
 
Paul Clapham
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Lexi Turgeon wrote:I thought that by changing the letter to a string I could give it a character, but I am not entirely sure how to do that.



Sure you can. Do you know how to represent a character in Java?
 
Lexi Turgeon
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:

Lexi Turgeon wrote:I thought that by changing the letter to a string I could give it a character, but I am not entirely sure how to do that.



Sure you can. Do you know how to represent a character in Java?



By declaring it as a string and giving it a value?
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A quick search for how are characters represented in Java can tell you.

Also, the printTile method is supposed to take a Tile object as its parameter. Why do you keep flouting the requirements you have been so clearly given?
 
Lexi Turgeon
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Junilu Lacar wrote:A quick search for how are characters represented in Java can tell you.

Also, the printTile method is supposed to take a Tile object as its parameter. Why do you keep flouting the requirements you have been so clearly given?



Because I thought I was doing it as the requirements said, I did not do it to be wrong on purpose.
 
Bartender
Posts: 732
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But the instructions say to make the variable "letter" a character, and the variable "value" an integer. So why do you keep declaring "letter" as a double or a String, and "value" a double ?
 
Lexi Turgeon
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Fred Kleinschmidt wrote:But the instructions say to make the variable "letter" a character, and the variable "value" an integer. So why do you keep declaring "letter" as a double or a String, and "value" a double ?



Because I thought strings were letters?
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Lexi Turgeon wrote: I thought I was doing it as the requirements said, I did not do it to be wrong on purpose.


I think maybe you should take a few minutes to fully understand what you're being asked to do because it's hard for me to understand how you can interpret "take a Tile object as its parameter" to mean "takes two parameters that are not Tile objects."
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Lexi Turgeon wrote:By declaring it as a string and giving it a value?

No.
I am getting the feeling of going round in circles. Your instructions were to record the letter as a character. Paul C pointed out that you are not so doing because you had a String. He asks whether you know how to record a character, so you asked whether to use a String.
How much teaching have you had from your online course? Have you been taught about different data types? How much of it have you actually done? Have you missed lots of the course?
Remind yourself of the primitive data types in the Javaâ„¢ Tutorials.


[edit]Add quote to beginning of post.
 
Lexi Turgeon
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Lexi Turgeon wrote:By declaring it as a string and giving it a value?

No.
I am getting the feeling of going round in circles. Your instructions were to record the letter as a character. Paul C pointed out that you are not so doing because you had a String. He asks whether you know how to record a character, so you asked whether to use a String.
How much teaching have you had from your online course? Have you been taught about different data types? How much of it have you actually done? Have you missed lots of the course?
Remind yourself of the primitive data types in the Javaâ„¢ Tutorials.


[edit]Add quote to beginning of post.



I was thinking I would be able to put it as a string so that I could give it a value of a letter.
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Lexi, please follow the links that have been provided to you for more information that will help you do the right thing per the requirements. You don't need to keep defending your mistakes, just learn from them and move forward. We're not being critical to be mean, we're trying to help you recognize what you need to improve.

We all recognize that programming doesn't come easily to everyone but you also need to understand that it can be frustrating to folks who try to give you tips which seem to be ignored. Again, if you follow the links to the tutorials, you'll find information about how characters are represented by their own primitive type in Java, and it's *not* String.
 
Lexi Turgeon
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Junilu Lacar wrote:Lexi, please follow the links that have been provided to you for more information that will help you do the right thing per the requirements. You don't need to keep defending your mistakes, just learn from them and move forward. We're not being critical to be mean, we're trying to help you recognize what you need to improve.

We all recognize that programming doesn't come easily to everyone but you also need to understand that it can be frustrating to folks who try to give you tips which seem to be ignored. Again, if you follow the links to the tutorials, you'll find information about how characters are represented by their own primitive type in Java, and it's *not* String.



I did check it out, and did not find it too useful. Sorry.
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Lexi Turgeon wrote:

Junilu Lacar wrote:... follow the links to the tutorials, you'll find information about how characters are represented by their own primitive type in Java, and it's *not* String.



I did check it out, and did not find it too useful. Sorry.



Well, you'll have to look harder. It's there but we won't do your work for you.
 
Lexi Turgeon
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Junilu Lacar wrote:

Lexi Turgeon wrote:

Junilu Lacar wrote:... follow the links to the tutorials, you'll find information about how characters are represented by their own primitive type in Java, and it's *not* String.



I did check it out, and did not find it too useful. Sorry.



Well, you'll have to look harder. It's there but we won't do your work for you.



So after reading, I get that I want a character, but I do not know how to use it in my code?
 
Paul Clapham
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The next page in the tutorial stream is here: http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html

It has an explicit example of how to declare a variable of character type and how to use a character literal as a value to be assigned to that variable. So why don't you see if you can find that example, for a start?
 
Lexi Turgeon
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote: So why don't you see if you can find that example, for a start?



I found that example and updated my code to that:
 
Paul Clapham
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay, that's a step forward. Now back to your requirements:

The instance variables should be a character named letter and an integer named value.



Your code still doesn't match that part of the requirements, does it? What is the next step you would have to take to fix that?
 
Lexi Turgeon
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:Your code still doesn't match that part of the requirements, does it? What is the next step you would have to take to fix that?



I now have a Character named letter, and an integer named value, correct?
 
Paul Clapham
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yup, that's right. Now let's take another sentence from the requirements:

Write a method named printTile that takes a Tile object as a parameter and prints the instance variables in some reader-friendly format.



Now you already wrote a method named printTile but you didn't make it have a Tile object as a parameter. So let's see if you can fix that.
 
Lexi Turgeon
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:Now you already wrote a method named printTile but you didn't make it have a Tile object as a parameter. So let's see if you can fix that.



How do I make it have a Tile object as a parameter? What I had before was incorrect, and I can't format it like this, right?
 
Paul Clapham
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, you can't. But look at what you had before:



There you had two parameters, the first being a char and the second being an int. Now look at your latest attempt and identify how your parameter declaration there differs from the parameter declarations in your first try.
 
Lexi Turgeon
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:There you had two parameters, the first being a char and the second being an int. Now look at your latest attempt and identify how your parameter declaration there differs from the parameter declarations in your first try.



Would I put char or int in front of the Tile?
 
Paul Clapham
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Lexi, I think you are getting too far ahead in the code you are trying to write. You have missed out on some very basic Java concepts and without those, there is no way you can succeed at assignments like the one you have there. I would suggest you go to your instructor and ask for extra help in making up the concepts you've missed so far. Really, you're not making any progresss by asking people here to help you out.
 
Lexi Turgeon
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My instructor offers no help, neither does my class. I asked two days ago and go no replies from anyone.
 
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Write a method named printTile that takes a Tile object as a parameter...


So two things here.  First is that Tile is a type, just like String is a type.  

The second thing is that a method parameter has two parts: a type and a variable name.  The variable name can be anything you choose.

So with those two things in mind, how would you complete this code snippet?
 
Lexi Turgeon
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
With those two things in mind:
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Lexi Turgeon wrote:the steps I have completed are as follows:

- Write a definition for a class named Tile that represents Scrabble tiles. The instance
variables should be a character named letter and an integer named value.
- Write a constructor that takes parameters named letter and value and initializes the
instance variables.
- Write a method named printTile that takes a Tile object as a parameter and prints the
instance variables in some reader-friendly format.



I hate to add more bad news... since this topic has already mentioned that you did step two *and* step three incorrectly.

Unfortunately, you also did step one incorrectly too. Considering that you are only learning Java, and likely don't know what is an "inner class" is yet; I highly doubt that your instructor wanted you to implement the class as an inner class.

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

Henry Wong wrote:
I hate to add more bad news... since this topic has already mentioned that you did step two *and* step three incorrectly.

Unfortunately, you also did step one incorrectly too. Considering that you are only learning Java, and likely don't know what is an "inner class" is yet; I highly doubt that your instructor wanted you to implement the class as an inner class.

Henry



So, the whole thing is wrong?
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Lexi Turgeon wrote:With those two things in mind:



Yes, but do you understand why?  Or are you simply guessing based on the very detailed hints provided by Knute?

Henry
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Lexi Turgeon wrote:
So, the whole thing is wrong?



Well, it is not a complete loss, as the code you provided can be fixed... but ... yeah, in terms of those three bullet points, all three will need some fixing.

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

Henry Wong wrote:

Lexi Turgeon wrote:With those two things in mind:



Yes, but do you understand why?  Or are you simply guessing based on the very detailed hints provided by Knute?

Henry



Yes, I understand that like a String, I need to use tile because it is a type. And I understand that I needed to give tile a variable name, so I used "random".
 
Marshal
Posts: 8856
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Lexi Turgeon wrote:Yes, I understand that like a String, I need to use tile because it is a type.

Correct. Just one thing, Java is case sensitive, so it isn't a tile, but Tile (T capital).

Now where this Tile type come from you may wonder. Remember you defined a class Tile?

So, that means whenever you'll pass a Tile object as a parameter to a method, you'll pass to method a box of information which will hold a letter and value.
The problem you may come across is that your Tile object will always hold letter 'z' and value 10.

Your instructions at the very beginning specifies that those should be configurable. That means you should be able to create a Tile object with any character (in simple words without modifying Tile class code).

Could you improve your constructor of Tile class so it would take two parameters and set them appropriately?
As an example how to add parameters part look to your method you have created. That is very similar.

I'm refering to this part of instructions:

- Write a constructor that takes parameters named letter and value and initializes the
instance variables.

 
Lexi Turgeon
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Would I take the number and letter I had previously put in so that there is no set value to them?

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

Lexi Turgeon wrote:Would I take the number and letter I had previously put in so that there is no set value to them?


Nope, try again. That code you wrote doesn't do anything. Maybe try researching 10 minutes on what a parameter means in a constructor and look for examples of simple constructors.
 
Please enjoy this holographic presentation of our apocalyptic dilemma right after this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic