How is it possible to set an object and method in a condition? I understand that, if the animal is over 50kg it weighs too much. But how about if an animal is hangry, need Love and feel boring return the method feelingNegative()?
I don't know how to set it. But after an animal sleeps, it is hangry. A thought would be:
Animal {
if (hangry == false && needLove == false && boring == false) {
return feelingNegative();
}
}
still don't know how to set it.
public class Animal {
private boolean needLove;
private boolean hangry;
private boolean boring;
private int kg;
public boolean sleep() {
return hangry = true;
}
public boolean watchTv() {
return needLove = true;
}
public void feelingPositive() {
System.out.println("I feel good");
}
public void feelingNeutral() {
System.out.println("Someting is missing...");
}
public void feelingNegative() {
System.out.println("I need love, food and fun!");
}
public void weight(int kg) {
if(50 < kg) {
System.out.println("You ate way too much");
}else {
System.out.println("You need to eat more");
}
}
}
The methods you are calling don't return anything (they are void). Just remove the return. And use boolean negation (!) instead of == false. Like,
if (!hangry && !needLove && !boring) {
feelingNegative();
}
The Method feelingNegative() doesn't return anything (Void). So you just have to define a method that call feelingNegative() when all the conditions are satisfied.
public void myMethod ()
{
if(!hangry && !needLove && !boring)
feelingNegative();
}
Related
So I have this simple boolean method for checking whether my game has ended. In the main method, I then have a big loop that starts with the statement while (isGameEnd() == false) {...}. This loop never seems to break, even when one, two or all of the conditions in the method becomes true. I don't get it.
public static boolean isGameEnd() {
if (lives == 0 || steps == 0 || isMazeCompleted()) { return true; }
else { return false; }
}
The use of static in the function definition is a red flag to me. If a class is defined with default field values, then those default values will be what is checked rather than the particular implementation of the class:
class Game {
int lives = 3;
int steps = 10;
public boolean isMazeCompleted() {
return false;
}
public void doStuff() {
lives--;
}
public static boolean isGameEnd() {
if (lives == 0 || steps == 0 || isMazeCompleted()) {
return true;
} else {
return false;
}
}
}
public static void main(String[] args){
Game a;
while(!a.isGameEnd()){ // check 'isGameEnd' for the static class
a.doStuff(); // This does *not* update the static class
}
}
Most Java editors will complain about the use of static functions in a non-static context, so will suggest Game.isGameEnd() instead of a.isGameEnd(), which makes it a bit more obvious to the programmer where the error is.
Basically I want to do a method that is used to make decision, which when the HitshipPercentage is less than 50 and the shots is not equal to zero, it will use the method public space fire(). I am not sure how to use the boolean in the correct way.
public Space fire()
{
System.out.println("Hi");
}
public boolean hitRateAnalysis()
{
if (HitShipPercentage < 50 && Shots!=0)
return true;
else{
return false;
}
}
Your fire() is wrong and will cause a compilation error, you have to return an instance of Space. Either change the return type of the method to void or return an instance of the Space class.
This will call the fire method correctly:
public boolean hitRateAnalysis() {
if (HitShipPercentage < 50 && Shots != 0){
fire();
return true;
}
return false;
}
Your code has a few errors in it, the fire() method is of type Space which means it's supposed to return a Space. I don't think this is what you want, my guess is fire isn't supposed to return anything in which case it should be void.
public void fire() {
System.out.println("Hi");
}
public boolean hitRateAnalysis() {
if (HitShipPercentage < 50 && Shots!=0) {
fire();
return true;
}
return false;
}
I've created this method and I'm unsure why it says there's a missing return statement. do I need to change the print to a return? (it's the method at the very bottom) I'm a bit of a Java beginner so any help will be appreciated!
public class Book {
private String title;
private String author;
private int copies;
private boolean borrowed;
public Book( String inAuthor, String inTitle, int inNumberOfCopies ) {
this.author = inAuthor;
this.title = inAuthor;
this.copies = inNumberOfCopies;
}
public void borrowed() {
borrowed = true;
}
public void rented() {
borrowed = true;
}
public void returned() {
borrowed = false;
}
public boolean isBorrowed() {
return borrowed;
}
public String getAuthor() {
return this.author;
}
public static String getTitle() {
return getTitle();
}
public int getTotalCopies() {
return this.copies;
}
public int getAvailableCopies() {
}
public void withdrawCopy() {
int found = 0;
for (Book b : Library.getListOfBooks()) {
if (b.getTitle().equals(title)) {
if (found == 0) {
found = 1;
}
if (!b.isBorrowed()) {
b.borrowed=true;
found = 2;
break;
}
if (found == 0) {
System.out.println("Sorry, this book is not in our catalog.");
} else if (found == 1) {
System.out.println("Sorry, this book is already borrowed.");
} else if (found == 2) {
System.out.println("You successfully borrowed " + title);
}
}
}
}
public String returnCopy() {
boolean found = false;
for (Book book : Library.getListOfBooks()) {
if (getTitle().equals(title) && book.isBorrowed()) {
book.returned();
found = true;
}
if (found) {
System.out.println("you successfully returned " + title);
}
}
}
}
public String returnCopy()
String after public means that this method will return a String.
Your public String returnCopy() is currently not returning anything.
If you don't want to return anything, you can use void like this:
public void returnCopy(){
// code
}
Same issue with public int getAvailableCopies(), this is supposed to return an int but you are not returning anything.
Be careful:
this method:
public static String getTitle() {
return getTitle();
}
is a recursive method without a base condition. This will cause an error and force your application to crash.
You've defined the method as returning a String but you don't return a value anywhere in the method body. Simplest fix is probably to change the return type to void...
public void returnCopy() {...
}
All the above answer are pointing to the same issue, you have defined methods that are breaking the contract about what they return..
In you code you have as well something like this:
public int getAvailableCopies() {
}
so you are telling the compiler, you have a method with the name getAvailableCopies, it takes no params and return an integer.
BUT if you don't return anything, then you are contradicting your own method, your own contract, this is an enough reason for a compiler to complain...
Conclusion:
keep in mind the information that defines the method.
in this homework i have to do a predicate method that prints a question and then waits for a question. if the user enters no, the method should return false, if the user enters yes the method should return true. I have done that ! but in this part i have problems: if the user enters another thing the program must say something like "wrong answer" and repeat the question. I can't return a string because is a boolean method and i dont know how to resolve this.
Thank you!!
import acm.program.ConsoleProgram;
public class YesNo extends ConsoleProgram{
public void run () {
String answer = readLine ("would you like instructions? ");
println (StrBoo (answer));
}
private boolean StrBoo(String answer){
if (answer.equals("yes")) {
return true;
} else if (answer.equals("no")) {
return false;
} else {
return false;
}
}
}
First StrBoo is a poor method name. I would call it getAnswer(), and use something like,
private static boolean getAnswer() {
while (true) {
String answerStr = readLine ("would you like instructions? ");
answerStr = (answerStr != null) ? answerStr.trim() : "";
if ("yes".equalsIgnoreCase(answerStr)) {
return true;
} else if ("no".equalsIgnorecase(answerStr)) {
return false;
} else {
System.out.println("Wrong answer");
}
}
return false;
}
The correct design for such kind of program is to throw an exception. Here is an example:
import acm.program.ConsoleProgram;
public class YesNo extends ConsoleProgram
{
class WrongAnswerException extends Exception
{
}
public void run ()
{
try
{
String answer = readLine("would you like instructions? ");
println(StrBoo(answer));
}
catch(WrongAnswerException e)
{
println("You have to write yes or no!")
}
}
private boolean StrBoo(String answer) throws WrongAnswerException
{
if ("yes".equals(answer))
{
return true;
}
else if ("no".equals(answer))
{
return false;
}
else
{
throw new WrongAnswerException()
}
}
}
Could anyone tell me what purpose a return statement in a Finite State Machine's state serves? For example I have this code for a soccer player's state:
public class ChaseBall extends State<FieldPlayer> {
private static ChaseBall instance = new ChaseBall();
private ChaseBall() {
}
//this is a singleton
public static ChaseBall Instance() {
return instance;
}
#Override
public void Enter(FieldPlayer player) {
player.Steering().SeekOn();
}
}
#Override
public void Execute(FieldPlayer player) {
//if the ball is within kicking range the player changes state to KickBall.
if (player.BallWithinKickingRange() && player.isReadyForNextKick()) {
player.GetFSM().ChangeState(KickBall.Instance());
return;
}
//if the player is the closest player to the ball then he should keep
//chasing it
if (player.isClosestTeamMemberToBall()) {
player.Steering().SetTarget(player.Ball().Pos());
return;
}
//if the player is not closest to the ball anymore, he should return back
//to his home region and wait for another opportunity
player.GetFSM().ChangeState(ReturnToHomeRegion.Instance());
}
#Override
public void Exit(FieldPlayer player) {
player.Steering().SeekOff();
}
}
I was wondering if someone could explain what purpose the the return keywords in the first two if statements of the Execute() method serve?
Thanks
In this case it's mainly a formatting alternative to a series of else if clauses. It is logically equivalent to
if (<condition>) {
<code>
} else if (<condition>) {
<code>
} else {
<code>
}