This is my first time asking a question here, so I'll ask you to bear with me: I am trying to create a public boolean method, isEven(), that will check if a number is evenly divisible by two and return a value of true or false based on that. However, as this is a public method, I am unsure of how exactly to write it; this is my process thus far:
public boolean isEven()
{
if(WHAT_GOES_HERE? % 2 == 0)
return true;
else
return false;
}
I would appreciate some advice on how exactly to go about writing this method; thanks in advance!
The simplest way would be
public boolean isEven(int value){
return value % 2 == 0;
}
Using an if/else statement to return or set variables to boolean values is almost always redundant. Since you can return the condition you put in the if/else itself, the if/else is not needed.
Related
I'm trying to understand how I can use the result of my method calculateBMI() in a boolean.
Here's my method with the return value that I need
public int calculateBMI(){
return (100*100*weight)/(length*length);
}
Here's what I've tried so far but I'm a big noob to Java and I know it is wrong, can someone elaborate on how I can use the return value of the calculateBMI() in a boolean for a return value? Sorry if this is very beginner Java.
public boolean hasOverweight(){
if (calculcateBMI() >= 30) {
return true;
} else {
return false;
}
EDIT: Solved, thanks guys! Appreciated :)
What you have is perfectly fine. You get a int and compare it to a known value. A slightly shorter statement would be
public boolean hasOverweight(){
return (calculcateBMI() >= 30);
}
Both are equivalent, and have no real performance difference.
I´m not sure if i undernstood your question , but you can do this :
public boolean hasOverweight(){
return calculcateBMI() >= 30
}
Just with that validation return true o false
i'm sorry this problem is simple but keeps frustrating me. I'm really appreciate that if you could tell me the reasons.
ps,it's homework2 of cs61b.
i need to check if a year is a leapyear,but obviously the boolean method only needs to return one boolean value (true or false)
since i add another return code line, the compiler error has gone. I really don't understand why, the brackets is in the right place.
public static boolean isLeapYear(int year) {
if ((year%4==0 && year%100!=0) || year%400=0) {
return true;
}
}
You only have to return one boolean (you can't return two), but you must provide a return statement for each possible scenario, both for if-condition is true, and if-condition is false.
Your code does not define what to return when your if condition evaluates to false. Here's a shorter version:
public static boolean isLeapYear(int year){
return ((year%4==0 && year%100==0)||year%400==0);
}
Your program returns true if it is a leap year, else... nothing
public static boolean isLeapYear(int year) {
if ((year%4&&year%100!=0)||year%400==0)
return true;
else
return false;
}
Or you can return an evaluation like :
public static boolean isLeapYear(int year) {
return ((year%4&&year%100!=0)||year%400==0);
}
(which a better way to do it)
What is the difference between the following two methods:
public boolean recursionMethodOne(Node n) {
System.out.println(n.getValue());
return recursionMethodOne(n.next());
}
public void recursionMethodTwo(Node n) {
System.out.println(n.getValue());
recursionMethodTwo(n.next());
}
Which one do you use for recursion and what is the difference?
Thanks
Both your codes doesn't exits. You need to add a return for a test condition. For example:
public void recursionMethodTwo(Node n) {
if (n == null) {
// Standard way to exit a void function without executing remaing code
// note that return null; doesn't compile
return;
}
System.out.println(n.getValue());
recursionMethodTwo(n.next());
}
Returning a value or not depends on the kind of function.
For example if you need to calculate a factorial you need a result, if you need to print a list you don't.
So for your example seems that the method two is most closer to your needs.
Otherwise you need to ask yourself what is the returning boolean value of the function? If you have a nice answer to this question you can implement the code returning a value.
I am working on an ATM simulation program. Currently I'm working on methods to verify the user input. I have an arraylist with bank card objects these objects consist of 3 variables I want
to check if a given reknr and pasnr combination is equal with any reknr and pasnr combination in my arraylist with the bankcard objects. If the arraylist contains the given combination the method has to return true, otherwise it has to return false.
public static boolean reknrpasnrCheckOke(String reknr,String pasnr){
for (int i=0; i<rekpaspin.size(); i++){
if (rekpaspin.get(i).reknr.equals(reknr) && rekpaspin.get(i).pasnr.equals(pasnr))
return true;
}
return false;
}
}
It doesn't matter what the input is it always returns false, how to solve this?
Edit::
I forgot an { after the if so I changed the code to:
public static boolean reknrpasnrCheckOke(String reknr,String pasnr){
for (int i=0; i<rekpaspin.size(); i++){
if (rekpaspin.get(i).reknr.equals(reknr) && rekpaspin.get(i).pasnr.equals(pasnr)){
return true;
}
}
return false;
}
But still the same problem.
You are only checking the first item in the list. If it's not matching, then you immediately return false without checking the rest of the items.
Move the return false; after the end of the for loop.
To evaluate equality of objects by means other that memory reference, you have to override the equals() method and hashcode() method of you rekpaspin class.
Also, you should not use an ArrayList<> and search it each time. Consider using a Set or HashMap
First, as stated by #rgettman, if the first 'if' check doesn't match, you automatically return false, so it needs to arranged better.
However, in this case you probably want to use a boolean flag. It should be set to false until the case matches, in which case you would set the flag to true. Outside the loop you would return the flag.
Got probably a simple problem but where ever I google it it seems the problem
is a semicolon at the end of the if statement, the problem is eclipse giving me the syntax error asking to delete my else on the else if statement, this happens nearly all the time for me and i end up using multiple IF's.
if(saleStatus == false || offerPrice <= currentOffer)
{
System.out.print("Error, sale is not open");
return false;
}
else if(currentOffer >= reservePrice)
{
saleStatus = false;
}
Every path your function can take must return a value, if you specify that it will return something.
In this case, you have probably specified it as
access_modifier boolean function_name(params){
... // ^return type
}
So, all code paths must return a boolean.
In your code, if it takes the else... path, and exits without returning anything, that isn't permitted.
else if(currentOffer >= reservePrice)
{
saleStatus = false;
//return something here (null if you don't care)
}
//or return something here (which all code-paths hit)
If you use an IDE like Eclipse, it can warn you in advance about things like this.
There's no return statement in your else block. If a return type is declared in your method, the method would not know what to return if the code enters the else block.
Put one in it or after (*).
In the first if, you return a value, so there is no point on specifying "else" because the rest of the method is not executed.
Some developers avoid multiple return statements in functions for code quality.
I wrapped your code in a class declaration, with minimum additional declarations, and a return after the whole if-else structure, and Eclipse shows no errors. I suggest writing a similarly minimal complete program that does show the problem, and posting it.
You do not need "else if" rather than "if" for the second test, but it should be harmless.
public class Bad {
boolean saleStatus;
int offerPrice;
int currentOffer;
int reservePrice;
public boolean problem() {
if(saleStatus == false || offerPrice <= currentOffer)
{
System.out.print("Error, sale is not open");
return false;
}
else if(currentOffer >= reservePrice)
{
saleStatus = false;
}
return true;
}
}