Quote (RYce @ Jan 29 2018 10:48pm)
New to java, trying out some basic stuff. Let say I'm suppose to determine if food is healthy or not and when I compile and run the program, it would return a string message "Apple is healthy" or "Apple is not healthy".
public class Food
{
private String foodName;
private boolean health; //Stuck at this part, I know boolean is like true and false, how would I set it like if true = healthy or if false = not healthy?
public Food(String thisFoodName)
{
foodName = thisFoodName;
}
public String getFoodData()
{
return(foodName+" is "+ health);
public class Main
{
public static void main(String[] args)
{
Food food1 = new Food("Apple");
Food food2 = new Food("Hotdog");
System.out.println(food1.getFoodData());
System.out.println(food2.getFoodData());
Hey, the reason why your boolean is empty is because you haven't set it as anything. When you do Food food1 = new Food("Apple");
You are making an Object called food of Type Food Class.
In your Constructor(the part where you type ```
public Food(String thisFoodName)
{
foodName = thisFoodName;
}
```
you forgot to mention whether healthy is true or false, i.e. healthy = true; or type healthy = false; (You could change your constructor to take a String thisFoodName and a boolean isHealthy and then set foodName = thisFoodName and healthy = isHealthy;
Next in your getfood data the post above will work