arguments don't match datatype of constructor

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Freckles
    New Member
    • May 2007
    • 9

    arguments don't match datatype of constructor

    Hi! I've been trying to get my inventory program to compile, but haven't had any luck. My instructor tells me that my arguments in the Inventory3 class need to match the datatype of the constructor in my Product class. I'm sure this is something rather simple that I should be able to figure out, but I'm at my wits end trying to make this work. Does anyone have any suggestions? My code is below. Thanks so much to anyone with advice in advance!!

    Code:
    class Product
    {
    	private int itemNumber; //Number assigned to product
    	private String itemName; //Name of product
    	private int stock; //Number of units of the product in stock
    	private double price; //Price of each unit of the product
    
    	public Product () //constructor for the product class
    	{
    		itemNumber = 0;
    		itemName = "";
    		stock = 0;
    		price = 0.00;
    	} //end constructor
    
    	public Product (int itemNumber, String itemName, int stock, double price) //begin constructor for item array
    	{
    		this.itemNumber = itemNumber;
    		this.itemName = itemName;
    		this.stock = stock;
    		this.price = price;
    	}
    
    	public void setItemNumber(int itemNumber) //sets and gets the item number
    	{
    		this.itemNumber = itemNumber;
    	}
    	public int getItemNumber()
    	{
    		return itemNumber;
    	}
    
    
    	public void setItemName(String itemName) //sets and gets the item name
    	{
    		this.itemName = itemName;
    	}
    	public String getItemName()
    	{
    		return itemName;
    	}
    
    
    	public void setStock(int stock) //sets and gets the number of units in stock
    	{
    		this.stock = stock;
    	}
    	public int getStock()
    	{
    		return stock;
    	}
    
    
    	public void setPrice(double price) //sets and gets the price per unit
    	{
    		this.price = price;
    	}
    	public double getPrice()
    	{
    		return price;
    	}
    
    
    	public double value() //calculates the value of the stock by multiplying the number of units on hand by the price of each unit
    	{
    		return stock * price;
    	}
    
    	public String toString()
    	{
    		return "Item Number: "+itemNumber + "\nProduct Name: "+itemName+"\nQuantity in Stock: "+stock+"\nPrice per Unit: $"+price+"\nValue of Entire Stock: $"+value();
    	}
    
    }//end class Product
    
    class Plastic extends Product
    {
    	private String itemColor; //color of product
    
    	public Plastic () //begin constructor
    	{
    		super();
    		itemColor = "";
    	}
    
    	public Plastic ( int itemNumber, String itemName, int stock, double price, String itemColor )
    	{
    		super(itemNumber, itemName, stock, price );
    		this.itemColor = itemColor; //add new attribute
    	}
    
    	public void setItemColor(String itemColor) //sets and gets the item color
    	{
    		this.itemColor = itemColor;
    	}
    	public String getItemColor()
    	{
    		return itemColor;
    	}
    
    	public double restockFee()
    	{
    		return value() * 0.05;
    	}
    
    } //end class Plastic
    
    public class Inventory3 //begin class Inventory3
    {
    	 public static void main( String args[])
    	 {
    		Product[] item = new Product [4];
    
    		Product item1 = new Product (1003182, "Polytrope TPP", 7, 1.81, "Black" );
    		Product item2 = new Product (1003296, "Polyvin PVC", 3, 1.72, "Light Neutral" );
    		Product item3 = new Product (1013972, "Polyvin PVC-DC", 8, 1.75, "Beige" );
    		Product item4 = new Product (1019392, "Invision", 9, 1.57, "Medium Dark Pewter" );
    
    		item[0] = item1;
    		item[1] = item2;
    		item[2] = item3;
    		item[3] = item4;
    
    
    		double all = 0.00;
    
    		for (int total= 0; total < 4;total++)
    		{
    			all = all + item[total].value();
    		}
    
    		for (Product myProduct: item)
    		{
    			System.out.println(item);
    			System.out.println();
    		}
    
    		System.out.println("The Value of all items combined is: $"+all);
    	} //end method main
    } //end class Inventory3
  • blazedaces
    Contributor
    • May 2007
    • 284

    #2
    Hey, next time also post the errors you're getting when you try to compile, that would help people out.

    See in inventory3 where you're calling new Product(bla bla)

    Don't you mean new Plastic?

    See, in inventory3 you're writing this:


    "Product item1 = new Product (1003182, "Polytrope TPP", 7, 1.81, "Black" );"

    which is Product (int, string, int, double, string), but looking at your product's constructor, like your teacher said, it doesn't match, it's like this, from your code:

    "public Product (int itemNumber, String itemName, int stock, double price)"

    Where's that string I wondered... you're extending class Plastic and the constructor for class plastic is from your code:

    "public Plastic ( int itemNumber, String itemName, int stock, double price, String itemColor )"

    So I believe you wanted to type:

    Code:
    Product item1 = new Plastic (1003182, "Polytrope TPP", 7, 1.81, "Black" );
    Product item2 = new Plastic (1003296, "Polyvin PVC", 3, 1.72, "Light Neutral" );
    Product item3 = new Plastic (1013972, "Polyvin PVC-DC", 8, 1.75, "Beige" );
    Product item4 = new Plastic (1019392, "Invision", 9, 1.57, "Medium Dark Pewter" );
    Hope that helped...
    -Asaf Erlich

    Comment

    • JosAH
      Recognized Expert MVP
      • Mar 2007
      • 11453

      #3
      Originally posted by Freckles
      Hi! I've been trying to get my inventory program to compile, but haven't had any luck. My instructor tells me that my arguments in the Inventory3 class need to match the datatype of the constructor in my Product class. I'm sure this is something rather simple that I should be able to figure out, but I'm at my wits end trying to make this work. Does anyone have any suggestions? My code is below. Thanks so much to anyone with advice in advance!!

      Code:
      		Product item1 = new Product (1003182, "Polytrope TPP", 7, 1.81, "Black" );
      		Product item2 = new Product (1003296, "Polyvin PVC", 3, 1.72, "Light Neutral" );
      		Product item3 = new Product (1013972, "Polyvin PVC-DC", 8, 1.75, "Beige" );
      		Product item4 = new Product (1019392, "Invision", 9, 1.57, "Medium Dark Pewter" );
      You don't have Product constructors taking five parameters. That's what your
      instructor is trying to tell you. Carefully check your Product constructors.

      kind regards,

      Jos

      Comment

      • Freckles
        New Member
        • May 2007
        • 9

        #4
        You guys are super ... thanks - that worked! My program compiles now, but it's not giving me the correct ouput. This is what I get rather than a list of inventory items with their respective details:

        [LProduct;@42e81 6

        [LProduct;@42e81 6

        [LProduct;@42e81 6

        [LProduct;@42e81 6

        The Value of all items combined is: $45.96
        Press any key to continue . . . .

        Any suggestions? I don't even know where to begin with this one!

        Comment

        • nomad
          Recognized Expert Contributor
          • Mar 2007
          • 664

          #5
          Originally posted by Freckles
          You guys are super ... thanks - that worked! My program compiles now, but it's not giving me the correct ouput. This is what I get rather than a list of inventory items with their respective details:

          [LProduct;@42e81 6

          [LProduct;@42e81 6

          [LProduct;@42e81 6

          [LProduct;@42e81 6

          The Value of all items combined is: $45.96
          Press any key to continue . . . .

          Any suggestions? I don't even know where to begin with this one!

          yes add a boolean statement
          with a while statement
          use a scanner to prompt the user to make a choice.
          if yes then continue
          if no close the program.

          nomad

          Comment

          • Freckles
            New Member
            • May 2007
            • 9

            #6
            Originally posted by nomad
            yes add a boolean statement
            with a while statement
            use a scanner to prompt the user to make a choice.
            if yes then continue
            if no close the program.

            nomad
            I tried this and it didn't help. I'm still getting the same output except now with the option
            for the user to enter a choice. Any other suggestions?

            Comment

            • JosAH
              Recognized Expert MVP
              • Mar 2007
              • 11453

              #7
              Originally posted by Freckles
              You guys are super ... thanks - that worked! My program compiles now, but it's not giving me the correct ouput. This is what I get rather than a list of inventory items with their respective details:

              [LProduct;@42e81 6

              [LProduct;@42e81 6

              [LProduct;@42e81 6

              [LProduct;@42e81 6

              The Value of all items combined is: $45.96
              Press any key to continue . . . .

              Any suggestions? I don't even know where to begin with this one!
              You're printing the wrong object; you're printing 'item' four times; you should
              print myProduct instead. Note that the output is the same four times which
              matches the fact that you're printing the same object four times.

              kind regards,

              Jos

              Comment

              • Freckles
                New Member
                • May 2007
                • 9

                #8
                Originally posted by JosAH
                You're printing the wrong object; you're printing 'item' four times; you should
                print myProduct instead. Note that the output is the same four times which
                matches the fact that you're printing the same object four times.

                kind regards,

                Jos
                Thanks Jos. That fixed my output errors, but it's not printing the additional attribute (color) or restock fee that's in my "Plastic" class. How do I link the two up so everything prints?

                Comment

                • Ganon11
                  Recognized Expert Specialist
                  • Oct 2006
                  • 3651

                  #9
                  You will probably have to override the toString() method in Plastic. It can be very simple - call Product's .toString() to get all that information, concatenate the additional information that Plastic has, and return the resultant String.

                  Comment

                  • Freckles
                    New Member
                    • May 2007
                    • 9

                    #10
                    Originally posted by Ganon11
                    You will probably have to override the toString() method in Plastic. It can be very simple - call Product's .toString() to get all that information, concatenate the additional information that Plastic has, and return the resultant String.
                    I feel so dumb when it comes to this stuff! How do I call the toString from the Product class into the Plastic class?

                    Comment

                    • Ganon11
                      Recognized Expert Specialist
                      • Oct 2006
                      • 3651

                      #11
                      By using the super keyword:

                      [CODE=java]super.superclas sMethodHere()[/CODE]

                      In this case, it's super.toString( )

                      Comment

                      • Freckles
                        New Member
                        • May 2007
                        • 9

                        #12
                        Originally posted by Ganon11
                        By using the super keyword:

                        [CODE=java]super.superclas sMethodHere()[/CODE]

                        In this case, it's super.toString( )
                        I added this plus the additional information, but now I'm getting a new error ...

                        Code:
                        class Plastic extends Product
                        {
                        	private String itemColor; //color of product
                        
                        	public Plastic () //begin constructor
                        	{
                        		super();
                        		itemColor = "";
                        	}
                        
                        	public Plastic ( int itemNumber, String itemName, int stock, double price, String itemColor )
                        	{
                        		super.toString();
                        		return "Item Number: "+itemNumber + "\nProduct Name: "+itemName+"\nQuantity in Stock: "+stock+"\nPrice per Unit: $"+price+"\nValue of Entire Stock: $"+value+"\nProduct Color"+itemColor+"\nValue of Inventory with a restocking fee: $:"+restockFee();
                        		this.itemColor = itemColor; //add new attribute
                        	}
                        
                        	public void setItemColor(String itemColor) //sets and gets the item color
                        	{
                        		this.itemColor = itemColor;
                        	}
                        	public String getItemColor()
                        	{
                        		return itemColor;
                        	}
                        
                        	public double restockFee()
                        	{
                        		return getPrice() * 0.05;
                        	}
                        
                        } //end class Plastic
                        error:
                        java:96: cannot return a value from method whose result type is void
                        return "Item Number: "+itemNumbe r + "\nProduct Name: "+itemName+"\nQ uantity in Stock: "+stock+"\nPric e per Unit: $"+price+"\nVal ue of Entire Stock: $"+value+"\nPro duct Color"+itemColo r+"\nValue of Inventory with a restocking fee: $:"+restockFee( );

                        the ^ is below the final + before 'restockFee'

                        Comment

                        • JosAH
                          Recognized Expert MVP
                          • Mar 2007
                          • 11453

                          #13
                          You stuck that piece of code in your constructor. You're supposed to override
                          the superclass's toString() method and call it too; like this:[code=java]
                          public String toString() { return super.toString( )+" my additional info"; }[/code]
                          kind regards,

                          Jos

                          Comment

                          • Freckles
                            New Member
                            • May 2007
                            • 9

                            #14
                            Originally posted by JosAH
                            You stuck that piece of code in your constructor. You're supposed to override
                            the superclass's toString() method and call it too; like this:[code=java]
                            public String toString() { return super.toString( )+" my additional info"; }[/code]
                            kind regards,

                            Jos
                            Perfect - Thank you!! Is there anywhere that I can put in the %.2f argument to keep my restock fee to 2 decimal places? I've tried putting it in several places, but end up either having it print or with an error.

                            Comment

                            • sandyw
                              New Member
                              • Mar 2007
                              • 122

                              #15
                              Originally posted by Freckles
                              Perfect - Thank you!! Is there anywhere that I can put in the %.2f argument to keep my restock fee to 2 decimal places? I've tried putting it in several places, but end up either having it print or with an error.
                              Not sure but I think you want to add %2f to the
                              public PrintStream format(String format, Object... args)

                              nomad

                              Comment

                              Working...