Constructor

Inheritence, Super()

Page 1 of 1

3 Replies - 502 Views - Last Post: 20 April 2009 - 08:34 PM Rate Topic: -----

#1 Gjobsin  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 55
  • Joined: 24-February 09

Constructor

Post icon  Posted 20 April 2009 - 06:49 PM

In my Drink Class I want to use the Super method and set up the inherited Item classes variables that way, am i Close?

Error "cannot find symbol constructor Drink"

public class Drink extends Item{
	
	private int itemFlOz;
	private int itemCalories;
	
	public Drink(){
		
		  super();
		this.itemFlOz = 0;
		this.itemCalories = 0;
	
		}
		
	public Drink(String itemName, double itemPrice, int itemQuantity, int itemNum, int itemFlOz, int itemCalories){
			super();
			  this.itemFlOz=itemFlOz;
			  this.itemCalories=itemCalories;
		}
		
	public void setitemFlOz(int itemFlOz)
		  {
			  this.itemFlOz=itemFlOz;
			  }
	public int getitemFlOz()
		  {
			  return this.itemFlOz;
			  }
			  
	public void setitemCalories(int itemCalories)
		  {
			  this.itemFlOz=itemCalories;
			  }
	public int getitemCalories()
		  {
			  return this.itemCalories;
			  }


public class Item {
	private String itemName;
	private double itemPrice;
	 private int   itemQuantity;
	 private int	itemNum;
				
public Item() //default
		  
		  {
			  this.itemName= " ";
			  this.itemQuantity= ' ';
			  this.itemPrice= 0;
			  this.itemNum= 0;
			  
		  }
		  
public Item (String itemName, double itemPrice, int itemQuantity, int itemNum)
		  {
			  this.itemName=itemName;
			  this.itemPrice=itemPrice;
			  this.itemQuantity=itemQuantity;
			  this.itemNum=itemNum;
			  }
			  
public void setitemName(String itemName)
		  {
			  this.itemName=itemName;
			  }
public String getitemName()
		  {
			  return this.itemName;
			  }
public void setitemQuantity(int itemQuantity)
		  {
			  this.itemQuantity=itemQuantity;
			  }
public int getitemQuantity()
		  {
			  return this.itemQuantity;
			  }
public void setitemPrice(double itemPrice)
		  {
			  this.itemPrice=itemPrice;
			  }
public double getitemPrice()
		  {
			  return this.itemPrice;
			  }
public void setitemNum(int itemNum)
		  {
			  this.itemPrice=itemNum;
			  }
public int getitemNum()
		  {
			  return this.itemNum;
			  }
	}





public class Mesacafe
{

	public static void main(String[] args){
	
		Drink a = new Drink("Beer", 5.99, 1, 001, 12.5, 96);
		Item y = new Item("Chicken", 5.00, 1, 002);
		Item z = new Item("Cheetos", 2.00, 1, 003);
		
		
		System.out.println("Thank you for shopping at the Mesa Cafe!");
		
		System.out.println("Item #" + "  "+ "Item" + "   Qty" + "	Price"); 
				
		System.out.println(a.getitemNum()+"	"+(a.getitemName())+"	"+(a.getitemQuantity())+ "	" +(a.getitemPrice()));
		System.out.println(y.getitemNum()+"	"+(y.getitemName())+"	"+(y.getitemQuantity())+ "	" +(y.getitemPrice()));
		System.out.println(z.getitemNum()+"	"+(z.getitemName())+"	"+(z.getitemQuantity())+ "	" +(z.getitemPrice()));
	
		System.out.println("");
		System.out.println("Your Total is" + "	" + (a.getitemPrice()+ y.getitemPrice()+z.getitemPrice()));
	}
}

This post has been edited by Gjobsin: 20 April 2009 - 06:59 PM


Is This A Good Question/Topic? 0
  • +

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8346
  • View blog
  • Posts: 31,904
  • Joined: 06-March 08

Re: Constructor

Posted 20 April 2009 - 07:11 PM

This is your constructor of Drink

public Drink(String itemName, double itemPrice, int itemQuantity, int itemNum, int itemFlOz, int itemCalories){

and this is how you call it:

Drink a = new Drink("Beer", 5.99, 1, 001, 12.5, 96);

OK:
- String itemName: "Beers"
- doubte itemPrice: 5.99
- int itemQuantity: 1
- int itemNum: 001
- int itemFlOz: 12.5.... oups 12.5 is NOT a int <------- there is the problem
- int itemCalories: 96
Was This Post Helpful? 1

#4 Gjobsin  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 55
  • Joined: 24-February 09

Re: Constructor

Posted 20 April 2009 - 07:15 PM

wow ...thanks
Was This Post Helpful? 0
  • +
  • -

#5 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: Constructor

Posted 20 April 2009 - 08:34 PM

You may also need to pass the parameters in your super call. I noticed you have 6 params for the constructor but you only use 2 of them in the constructor. If you are calling the super classes constructor then you would need to do something like this
public Drink(String name, double price, ouble oz, int quanity){
super(name,price);
ozs = oz;
quant = quantity;}


If the superclasses constructor needs a parameter then ou need to sen it those. In this example my super constructor needs a string and an double. SO i sent it the name and the price and use the remaining for the class variables only

Hope this helps!

This post has been edited by Fuzzyness: 20 April 2009 - 08:34 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1