• 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
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Bear Bibeault
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh

two attributes where I entered the number 0

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am a beginner.
I have:
My question.
Everything works ok except those two attributes where I entered the number 0. I have tried  without success. Is there a way I can execute b.print(); without having to insert those 0(s)?
Thanks
 
Marshal
Posts: 72993
330
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

Does that mean you want somebody who has 0 as postcode and 0 as age? That appears strange to me. You can overload the constructor, so you have two constructors, one of which sets those two fields to some sort of default value.
 
Saloon Keeper
Posts: 13002
281
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to CodeRanch!

You mean that when you print an instance of Bediende, it doesn't print the properties postcode and leeftijd? Well, then, why don't you modify the toString() method to include those properties?

Some other things that you can improve in your code:

  • Make classes final unless you have a good reason to extend them.
  • Make fields final unless you have a good reason to reassign their values.
  • Use String.format() to easily build strings that contain variable values.
  • You don't have to call toString() explicitly when calling println(). So in your print() method, you can just call System.out.println(this).
  • Don't override methods when the implementation is going to be the same. Bediende.print() does the same thing as Persoon.print(), so you can just remove the entire method from Bediende.
  • Don't use double for money. Floating point values are inaccurate. Instead, use an integral type or BigDecimal.
  • Don't use int just because the value looks like a number, such as postcode. If you don't intend to perform arithmetic on it, use String (or preferably a stronger custom datatype).
  • Start method names with a lower case letter. The name should be a verb, not a noun. For example, SalarisVerhoging -> verhoogSalaris.
  • Format your code nicely: Indent your code properly , and remove unnecessary empty lines.
  •  
    Sheriff
    Posts: 16278
    271
    Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    If your toString() method is behaving the way you want it to, then you'd have to provide another constructor that doesn't take the last two parameters that the current constructor takes. The closest thing to optional parameters that Java provides is allowing the last parameter to be variadic. However, a variadic parameter should represent a list of related values, not two separate and unrelated values like postal code and age.

    Did you know that you can have more than one constructor for a class as long as each of their argument lists differ from the others in types or number of parameters?
     
    Campbell Ritchie
    Marshal
    Posts: 72993
    330
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    What's variadic? I can't find it in the dictionary. Do you mean variable arity arguments (varargs)? Do you think varargs would actually help?
     
    Stephan van Hulst
    Saloon Keeper
    Posts: 13002
    281
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    https://en.wikipedia.org/wiki/Variadic_function
     
    Campbell Ritchie
    Marshal
    Posts: 72993
    330
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thank you In this case it does mean the same as varargs, then.
     
    Bartender
    Posts: 732
    10
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    When a class toString() method is going to list the value of many (or all) of its attributes, including those specified in its superclasses, it is often desirable to just call super.toString() rather than repeating all of that code.

    This is especially useful if the superclass has many attributes, or if for some reason the toString() method of one of the superclasses is modified, since you then do not have to modify all of the subclass toString() methods.
    also, the toString() methods often enclose the values in brackets, so you do not end up with a bunch of periods at the end of each class output:

    Persoon:

    Bediente:
     
    Self destruct mode activated. Instructions for deactivation encoded in this tiny ad.
    Thread Boost feature
    https://coderanch.com/t/674455/Thread-Boost-feature
    reply
      Bookmark Topic Watch Topic
    • New Topic