0% found this document useful (0 votes)
36 views4 pages

Oop Lab 4

This document contains code for a Food class with methods to set name, ingredients, price, and calories of a food item. It also contains methods to remove or add ingredients to the ingredients array. The main method creates Food objects, gets user input for name, price, and calories, and calls methods to set the attributes, remove an ingredient, and add an ingredient.

Uploaded by

Dipto Paul
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views4 pages

Oop Lab 4

This document contains code for a Food class with methods to set name, ingredients, price, and calories of a food item. It also contains methods to remove or add ingredients to the ingredients array. The main method creates Food objects, gets user input for name, price, and calories, and calls methods to set the attributes, remove an ingredient, and add an ingredient.

Uploaded by

Dipto Paul
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

OOP Lab 4

Pranabesh Kumar Paul


Id: 21-45760-3

Food:
public class Food {
String name;
String[] ingredients= {"Chicken","Folur","Hot Sauce"};

double price;
float calories;
Food(){

public void setName(String name) {


System.out.println("Name:"+name);

void setIngredients(String[]ingredients) {
System.out.print("Ingredients:");

for(int i=0;i<ingredients.length;i++) {
System.out.print(ingredients[i]+" ");

void setPrice(double price) {


System.out.println("Price:"+price);

}
void setCalories(float calories) {
System.out.println("Calories:"+calories);
}
void removeIngredient(String ingredient) {

for(int x = 0;x<=2;x++) {
if(ingredients[x]==ingredient) {
ingredients[x] = null;

}
else {
System.out.print(ingredients[x]+" ");
continue;

}
}
}

public void addIngredient(String ingredient) {

for(int y= 0;y<=2;y++) {
if(ingredients[y]==null) {
ingredients[y] = ingredient;

}
else {System.out.print(ingredients[y]+" ");

continue;
}
}
}

void showDetails(String name,double price,float calories) {

}
Start:
import java.util.*;

public class Start {


public static void main(String args[]) {
String[] ingredients= {"Butter","Ice","Vanilla"};
Food x=new Food();
Food y=new Food();
System.out.print("Name,price,calories:\n");
Scanner Name = new Scanner(System.in);
String name=Name.nextLine();
Scanner Price = new Scanner(System.in);
double price=Price.nextDouble();
Scanner Calories = new Scanner(System.in);
float calories=Calories.nextFloat();
System.out.println();

x.setName(name);
x.setCalories(calories);
y.setPrice(price);
y.setIngredients(ingredients);
System.out.print("\nAfter remove:");
x.removeIngredient("Butter");
System.out.print("\nAfter add:");
y.addIngredient("Butter");

}
Result:

You might also like