a Dairy Product hasan instance variable containsLactose of t

a. Dairy Product has-an instance variable, containsLactose, of type boolean which indicates whether it contains lactose or not. b. Produce Product has-an instance variable, fruitorVegetable, of type String which indicates whether the product is a fruit or a vegetable. c. Write both a no-args and a constructor that requires input for name, price and amount in stock for both classes. d. Write getters and setters for all instance variables for both classes.

Solution

package snippet;

public class TestProduct {

   public static void main(String[] args) {
       // TODO Auto-generated method stub
      
       DairyProduct d1=new DairyProduct(\"Amul Butter\",25,52);
       System.out.println(\"Deatils of Dairy Product name: \"+d1.getName()+\" \ price:\"+d1.getPrice()+\" \ stock:\"+d1.getStock());
      
       ProduceProduct d2=new ProduceProduct(\"Mango\",50,50);
       System.out.println(\"Deatils of Produce Product name: \"+d2.getName()+\" \ price:\"+d2.getPrice()+\" \ stock:\"+d2.getStock());

   }

}

============================================================

package snippet;

public class DairyProduct {
   boolean containsLactose;
   String name;
   int price,stock;
  
   public DairyProduct() {
       // TODO Auto-generated constructor stub
       containsLactose=true;
       name=\"Amul\";
       price=10;
       stock=10;
   }
  
  
   public DairyProduct(String n,int p,int s) {
       // TODO Auto-generated constructor stub
      
       name=n;
       price=p;
       stock=s;
   }
  
   String getName()
   {
       return name;
   }
   int getPrice()
   {
       return price;
   }
  
   int getStock()
   {
       return stock;
   }
  
   void setName(String s)
   {
       name=s;
   }
   void setPrice(int p)
   {
       price=p;
   }
  
   void setStock(int s)
   {
       stock=s;
   }

}

====================================================

package snippet;

public class ProduceProduct {
   String fruitOrVegetable;
   String name;
   int price,stock;
   public ProduceProduct() {
       // TODO Auto-generated constructor stub
       fruitOrVegetable=\"fruit\";
       name=\"Apple\";
       price=10;
       stock=10;
   }
  
  
   public ProduceProduct(String n,int p,int s) {
       // TODO Auto-generated constructor stub
      
       name=n;
       price=p;
       stock=s;
   }
  
   String getName()
   {
       return name;
   }
   int getPrice()
   {
       return price;
   }
  
   int getStock()
   {
       return stock;
   }
  
   void setName(String s)
   {
       name=s;
   }
   void setPrice(int p)
   {
       price=p;
   }
  
   void setStock(int s)
   {
       stock=s;
   }


}

==================================================

Output:

Deatils of Dairy Product name: Amul Butter
price:25
stock:52
Deatils of Produce Product name: Mango
price:50
stock:50

 a. Dairy Product has-an instance variable, containsLactose, of type boolean which indicates whether it contains lactose or not. b. Produce Product has-an insta
 a. Dairy Product has-an instance variable, containsLactose, of type boolean which indicates whether it contains lactose or not. b. Produce Product has-an insta
 a. Dairy Product has-an instance variable, containsLactose, of type boolean which indicates whether it contains lactose or not. b. Produce Product has-an insta

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site