d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Newbie Java Question
Add Reply New Topic New Poll
Member
Posts: 3,466
Joined: Jul 22 2012
Gold: 30,884.00
Jan 29 2018 11: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());



This post was edited by RYce on Jan 29 2018 11:49pm
Member
Posts: 2,754
Joined: Nov 26 2007
Gold: 1,339.81
Jan 30 2018 08:51am
Quote (RYce @ Jan 30 2018 01:48am)
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());


Code

public String getFoodData(){
return foodName + " is " + (health ? "healthy" : "not healthy");
}


or

Code
public String getFoodData(){
if (healthy){
return foodName + " is healthy";
}else{
return foodName + " is not healthy";
}
}


The first method uses the conditional operator '?', and is more advanced than the 2nd method. They both do the same thing, but the first one is cleaner and more common to use.
Member
Posts: 3,466
Joined: Jul 22 2012
Gold: 30,884.00
Jan 30 2018 06:09pm
how would I write this out with boolean method?

like I have private boolean health, but it's being unused atm.

like something:

public boolean health()
{
}
Member
Posts: 5,223
Joined: May 13 2006
Gold: 1,674.26
Jan 30 2018 11:33pm
i think the:
public Food(String thisFoodName)
should be:
public Food(String thisFoodName, boolean healthy)

so,
Food food1 = new Food("Apple");
should be
Food food1 = new Food("Apple, true");

now set your variables and go from there
Member
Posts: 1,086
Joined: Aug 28 2016
Gold: 6,190.00
Feb 1 2018 02:38am
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
Member
Posts: 3,274
Joined: Dec 26 2017
Gold: 42.00
Mar 6 2018 10:52pm
Hi
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll