• Post Reply Bookmark Topic Watch Topic
  • New Topic

Defining a data type  RSS feed

 
Derek Szpik
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I want to define a data type called Value. Value can only hold an integer in the range 0 to 65535 (inclusive), and must be able to be used in place of an int throughout my program. Do I just need to create a class like:
public int class Value{
...
}

and if so, what would I put inside to make everything work as I want?
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
and must be able to be used in place of an int throughout my program.


Short answer... you cant. int' s in Java are a primitive type and therefore can't be subclassed, you can only create a subtype of a non-final, non-primitive type (hence you also can't subclass java.lang.Integer since it is final). Therefore you can't substitute any custom types for int.

Perhaps you could forgo int's altogether and only allow your types as parameters and return types in your problem domain. Or you could provide an easy way to convert from an int to a Value:



Or you could get really creative and subtype Number:

 
Derek Szpik
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hmm..that could pose a problem...

see, I need to define the following interface:


and like I said above, a Value is a data type defined in the project outline as an integer between 0 and 65535 inclusive. Any other ideas on how to do this now that you know what it's for?
 
Moojid Hamid
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Derek Szpik wrote:hmm..that could pose a problem...


How is that a problem? Since your interface is using Value not int, once you implement Value you can forget about int altogether.
 
Derek Szpik
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well, that's my question, how do I create the data type of value? I don't know how to use the suggestions that Garrett made, so if anybody could provide me with a short section of code that creates value, that would be great.
 
Moojid Hamid
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Derek Szpik wrote:well, that's my question, how do I create the data type of value?


Well in that case you will have to pick up a book on Java and read through the section that describes the concept of 'Class' and 'Object'.
Not to be rude but there are some rules of the forum including: Not A Code Mill,DoYourOwnHomework , and ShowSomeEffort .

When you have some specific questions everyone would be happy to help you lean.
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could always use char. Although you might think that char can be used for characters only, it is in fact a numeric type and compatible with int, and it has just the right range.

The only downside I see is that you will have to cast the char to int before printing:
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Prime wrote:You could always use char.
That was my first thought too. You can restrict the range if you accept an int as a constructor parameter
 
Derek Szpik
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
okay, thank you. I'll try a small program using chars ^.^

If that doesn't work, I figure I can always use plain int's, and just do a small check like


That'll work, but it may cost me a mark or two (out of ~100) so I wanted to see if there was anything else. Thanks to all
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!