Professional Documents
Culture Documents
Food:
public class Food {
String name;
String[] ingredients= {"Chicken","Folur","Hot Sauce"};
double price;
float calories;
Food(){
void setIngredients(String[]ingredients) {
System.out.print("Ingredients:");
for(int i=0;i<ingredients.length;i++) {
System.out.print(ingredients[i]+" ");
}
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;
}
}
}
for(int y= 0;y<=2;y++) {
if(ingredients[y]==null) {
ingredients[y] = ingredient;
}
else {System.out.print(ingredients[y]+" ");
continue;
}
}
}
}
Start:
import java.util.*;
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: