Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Static Members exercise help

 
Greenhorn
Posts: 19
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello, I have a few static members exercises due and I wasn't sure if I am doing this correctly. Here is the question:

Create a class named Shirt with data members for collar size and sleeve length.  Include 2 constructors (default and one with both data members) and appropriate set and get methods.  Create a static variable to hold the material type and set it to “cotton”. [Shirt.java].  

Create a TestShirt class with a main method to test the Shirt class.  Create 5 objects of the class. Three will use the default constructor and then the set methods and user input to set the values. The other two will use the overloaded constructors and values that you provide when you instantiate the objects. Output all information for each object.



Here is my code:


Second class:




My questions are:

1. I'm not sure what I should be putting in the default constructor- is it values you give it or just blank?
2. How can I use user input from and set method from the default constructor for the first 3 values? Super confused about that.
3. How can I use overloaded constructors when I instantiate the object for the last two?

Thank you.
 
Sheriff
Posts: 17152
298
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
1. You’re using the term “default constructor” incorrectly. You don’t have a default constructor. Any no-arguments constructor you write is by definition NOT a default constructor.

2. A default constructor is provided by the compiler when a class has no explicit constructors. That is, it is impossible to write a default constructor because only the compiler can create it. That all happens behind the scenes.

 
Junilu Lacar
Sheriff
Posts: 17152
298
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
This class has a default no-arguments constructor:

This class has an explicit no-arguments constructor:
 
Sheriff
Posts: 7113
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

Mona Smith wrote:
My questions are:

1. I'm not sure what I should be putting in the default constructor- is it values you give it or just blank?
2. How can I use user input from and set method from the default constructor for the first 3 values? Super confused about that.
3. How can I use overloaded constructors when I instantiate the object for the last two?


1) It can be either.  If there are reasonable default values, you can use them in a no-arg constructor.  But here I don't think there are reasonable defaults, so I would leave them at zero.

2) You can set fields from a constructor or a setter.  I'm not sure what you are confused about.

3) How you call the constructor will determine which of the overloaded constructors gets used.  If you write new Shirt(); it will use the no-arg constructor.  If you write new Shirt(param1, param2); it will use the two-arg constructor.
 
Marshal
Posts: 76479
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Junilu Lacar wrote:1. You’re using the term “default constructor” incorrectly. . . .

Probably not her; I think she is quoting from an assignment which includes that mistake.
 
Campbell Ritchie
Marshal
Posts: 76479
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Knute Snortum wrote:. . . I don't think there are reasonable defaults, so I would leave them at zero. . . .

My opinion would be it is incorrect to provide a constructor at all if there aren't any default values that will put the object into a consistent state.
 
Let nothing stop you! Not even this tiny ad:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic