Having trouble with Java constructor

Discussion in 'Scripting & Programming' started by Pheonicks56, Jan 30, 2009.

  1. Pheonicks56

    Pheonicks56 Kilobyte Poster

    364
    6
    49
    Is there anyone who can help me with a program I am writing for a class?

    I'm using NetBeans IDE to build my program and it's giving me an error saying that I'm missing a constructor for a class when I create a new object, the thing is I have the constructor written, I don't know what I'm doing wrong. Can anyone take a look at this and show me where I might be going wrong.

    public class DVD
    {
    private String dvdTitle; // Title of DVD
    private Double dvdNumber; // DVD's number
    private Double dvdUnits; // Number of units in stock
    private Double dvdPrice; // Price per unit

    // Constructor Initializes DVD

    public DVD( String title, Double number, Double units, Double price )
    {
    dvdTitle = title; // Initializes dvdTitle
    dvdNumber = number; // Initializes dvdNumber
    dvdUnits = units; // Initializes dvdUnits
    dvdPrice = price; // Initializes dvdPrice
    } // End DVD constructor

    Then this is where I create the new object in the main class and am getting an error:

    public static void main(String[] args)
    {
    // Create DVD object myDVD
    DVD myDVD = new DVD( "Something we'll work on" );
     
    Certifications: BSIT, AAIT, A+
    WIP: Network+
  2. dmarsh
    Honorary Member 500 Likes Award

    dmarsh Petabyte Poster

    4,305
    503
    259
    Method signatures must match the parameter list for the method in java, there are no default params.

    Your constructor call has one parameter and the declared constructor has three !

    Parameter lists must match in types and order too as well as number.

    Other related things of note :-

    In Java you get a default no arg constructor by default, if you declare a constructor you lose the default constructor.

    You do not get parameter defaults in java, instead you can define multiple constructors and chain them.

    I'd prefer double to Double in general in java. Autoboxing in JDK 1.5 allows promotion from double to Double for collections and suchlike, in general the primitive types are more efficient and map more closely to native types (Explanation).

    Since you can't really have a fractional number of DVD's I'd make dvdNumber and dvdUnits an int. (Half a DVD is classed as junk, not rental stock !)

    Putting 'dvd' in front of all your class members/properties is kinda wasted reading/typing effort as you are already in a DVD class.
    The Sun Java coding standard is to use 'this.member', personally I prefer the C++ notation of prefixing all members with 'm_' ie 'm_units' to denote class members as distinct from local variables.

    If you want to get really pernickety you can use final on your parameters to the constructor but in small methods this buys you little extra safety for harder to read code.

    Large Monetary values are best handled with fixed point math(see BigDecimal), not floating point but since this is a DVD rental system and not an accounting system double should suffice. Float (about 7 decimal digits of precision) would be fine for a unit price in most (non hyper-inflation) currencies.

    I'd ditch netbeans and get Eclipse its much better.

    Write a few JUnit tests if you get stuck on stuff, always good to have tests anyway.
     
  3. soundian

    soundian Gigabyte Poster

    1,460
    71
    107
    ^What he said.
    Although personally I found it worthwhile to use a simple text editor for a while. I felt it made me concentrate on the basics of the language, not the basics of the IDE, and for small projects it's not much extra work.
     
    Certifications: A+, N+,MCDST,MCTS(680), MCP(270, 271, 272), ITILv3F, CCENT
    WIP: Knuckling down at my new job
  4. Pheonicks56

    Pheonicks56 Kilobyte Poster

    364
    6
    49
    Thank you so much for your help and quick response! I'm still getting the error, I am not sure I am doing it right, If I email you the full files could you give them a quick look over and see if I'm just over thinking it? What I tried is I put:

    public static void main(String[] args)
    {
    // Create DVD object myDVD
    DVD myDVD = new DVD( String title, double number, double units, double price );

    I did however fix all the other issues, thank you for pointing them out, I'm only on my fifth week of class, four more left and I feel like my brain is going to explode.
     
    Certifications: BSIT, AAIT, A+
    WIP: Network+
  5. dmarsh
    Honorary Member 500 Likes Award

    dmarsh Petabyte Poster

    4,305
    503
    259
    You do not need to declare the type when passing a parameter, only when declaring an interface.

    Call the constructor like this :-

    // Create DVD object myDVD
    DVD myDVD = new DVD(title, number, units, price);


    Where title, number, units and price have been defined as variables of the correct types.

    Your first example would have been correct for a single string argument constructor.

    DVD myDVD = new DVD( "Something we'll work on" ); // Construct with a single string literal assuming constructor exists...
     
  6. Pheonicks56

    Pheonicks56 Kilobyte Poster

    364
    6
    49
    Thanks to dmarsh and soundian, I have a fully functioning DVD inventory program!!!
     
    Certifications: BSIT, AAIT, A+
    WIP: Network+

Share This Page

Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.