Define a class SUPPLY in Java with the following descriptions :
- Members are : Code of int type, FoodName of type String, Sticker of type String, FoodType of type String.
- Memeber Functions : A member function GetType() to assign the following values for FoodType as per the given sticker
| Sticker | FoodType |
| Green | Vegetarian |
| Yellow | Contains Egg |
| Red | Non Vegetarian |
- A function FoodIn() to allow user to enter values for Code, FoodName, Sticker and call function GetType() to assign respective FoodType.
- A function FoodOut() to allow user to view the content of all the data members.
Solution:
import java.util.*;
class SUPPLY
{
int Code;
String FoodName;
String Sticker;
String FoodType;
public void GetType(){
if(Sticker.equals("Green"))
FoodType = "Vegetarian";
if(Sticker.equals("Yellow"))
FoodType = "Contains Egg";
if(Sticker.equals("Red"))
FoodType = "Non Vegetarian";
}
public void FoodIn(){
Scanner sc = new Scanner(System.in);
System.out.println("Enter Food Code : ");
Code=sc.nextInt();
System.out.println("Enter Food Name : ");
FoodName=sc.next();
System.out.println("Enter Sticker Color : ");
Sticker=sc.next();
GetType();
}
public void FoodOut(){
System.out.println("You Entered ");
System.out.println("Food Code : "+Code);
System.out.println("Food Name : "+FoodName);
System.out.println("Sticker Color : "+Sticker);
System.out.println("Food Type : "+FoodType);
}
}
class SUPPLYFOOD
{
public static void main(String args[])
{
SUPPLY ob=new SUPPLY();
ob.FoodIn();
ob.FoodOut();
}
}
Save the above Program with “SUPPLYFOOD.Java” then compile it with the following command :
javac SUPPLYFOOD.java //to compile the code java SUPPLYFOOD // to run the code
Output is :
Enter Food Code : 101 Enter Food Name : Rice Enter Sticker Color : Red You Entered Food Code : 101 Food Name : Rice Sticker Color : Red Food Type : Non Vegetarian