• 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

Defining a data type

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:
 
Marshal
Posts: 79240
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:)
reply
    Bookmark Topic Watch Topic
  • New Topic