if and else statement - java

I'm trying to implement a toString method, and the output of the toString depends on the boolean variables. Below is my class and main.
public class Cell {
public int addSpaces;
boolean isEmpty;
boolean isChute;
boolean isLadder;
public Cell() {
addSpaces = 10; //I initialized addSpaces to 10 for testing purpose
}
public boolean isChute() { //first boolean method
if (addSpaces == -10) {
return true;
} else {
return false;
}
}
public boolean isLadder() {//second boolean method
if (addSpaces == 10) {
return true;
} else {
return false;
}
}
public boolean isEmpty() { //third boolean method
if (addSpaces == 0) {
return true;
} else {
return false;
}
}
public String toString() {
String print;
if (isChute = true) //if isChute is true return true.
{
print = "C10"; // toString output = "C10"
} else if (isLadder = true) // if isLadder is true return true
{
print = "L10"; // toString output == "L10"
} else {
print = "---"; // else toString print output = "---"
}
return print;
}
public static void main(String[] arg) {
Cell s = new Cell();
System.out.println(s.addSpaces);
System.out.println(s);
}
}
Regardless of the input state of toString, I basically get the same output "C10".
Can someone tell me what I did wrong?
I'm new to this website so I appreciate any feedback for future reference. Thank you.

You've fallen into one of the languages "gotchas"
This...
if(isChute = true) //if isChute is true return true.
print = "C10"; // toString output = "C10"
else if (isLadder = true) // if isLadder is true return true
print = "L10"; // toString output == "L10"
else
print = "---"
is actually assigning true to isChute. You should be using == not =
Updated
A better approach would be...
if(isChute) //if isChute is true return true.
print = "C10"; // toString output = "C10"
else if (isLadder) // if isLadder is true return true
print = "L10"; // toString output == "L10"
else
print = "---"
If there are only two states that the object can be (either a chute or ladder), you could simply use
if(isChute) //if isChute is true return true.
print = "C10"; // toString output = "C10"
else print = "L10"; // toString output == "L10"
If it can have more then 2 states then I would use an enum type instead.

isChute is assigned to true. So "C10" is being returned all the time by toString().
Change it
if(isChute){
...
}else if(isLadder){
...
}else{
..
}

Related

How do I check if a method returns a true or false?

So I have this code
public static boolean isVowel (char c) {
if (c=='a'||c=='e'||c=='i'||c=='o'||c=='u')
{
return true;
}
else
{
return false;
}
}
and this code in two seperate methods
if (isVowel == true()) //I know this is wrong but how could I make it work?
{
//run command
}
else
{
//run command
}
how could I make if (isVowel()) test if if isVowel true?
public static boolean isVowel (char c) {
// concise code here
return (c=='a'|| c=='e'|| c=='i'|| c=='o'|| c=='u');
}
// your fix here
if (isVowel(aCharVariable)) {
// your code here
} else {
// your code here
}
Concise and simple.
I don't know if I got the question right but here is what I think meets your requirements:
if (isVowel(/* put a char here */) == true) {
// Do stuff
} else {
// Do other stuff
}
In this case (because isVowel() is of type boolean you can also do this which is more elegant:
if (isVowel(/* put a char here */)) {
// Do stuff...
This is possible because the if statement checks for conditions which is nothing else than a boolean state (true or false).
In Java, an if statement checks whether its operand is true or false. Operands can only be of type boolean (and to a certain extent the boxed Boolean variant).
boolean b = true;
if (b) {
System.out.println("b was true");
}
Instead of assigning the static value/the literal true to the variable, you can also assign the result of a method call:
boolean b = isVowel('a');
if (b) {
System.out.println("a is a vowel");
}
Now, you do not necessarily need the variable, you can inline it and use the result of the method call directly:
if (isVowel('e')) {
System.out.println("e is a vowel too");
}
Note that some operators, such as ==, !=, <, return boolean values as well:
boolean greater = 5 > 3;
boolean equal = null == null;
boolean different = new Object() == new Object();
if (greater) {
System.out.println("5 is greater than 3");
}
if (equal) {
System.out.println("null equals null");
}
if (different) {
System.out.println("Two object instances have different idententity");
}
Of course, you do not need variables here and can put the comparison expression directly into the if:
if (5 > 3) {
System.out.println("5 is greater than 3");
}
if (null == null) {
System.out.println("null equals null");
}
if (new Object() == new Object()) {
System.out.println("Two object instances have different idententity");
}
or even:
if ((5 < 3) == false) {
System.out.println("The (logical) statement '5 is less than 3' is false. Therefore, the result of the boolean comparison is true and this code is executed");
}
Another way to write it that gets rid of all the ors.
private static final Set<Character> VOWELS = ImmutableSet.of('a','e','i','o','u');
public boolean isVowel(char c) {
return VOWELS.contains(c);
}
if(isVowel('a')) {
//do stuff
}

My code is printing only false. What can be wrong?

I am getting output as false, everytime.
My aim is to print true, if String t is present in the same order in String s.
For example:
String s = "gagbgcgd";
String t = "abcd";
Expected output:
true
String s = "gagcgdgb";
String t = "abcd";
Expected output:
false
Here is the code.
public class StringCompare {
public static boolean stringCompare(String t,String s) {
if (t.length() == 0) {
return true;
}
if (s.length() == 0) {
return false;
}
if (t.charAt(0) == s.charAt(0)) {
stringCompare(t.substring(1), s.substring(1));
}
else {
stringCompare(t, s.substring(1));
}
return false;
}
public static void main(String[] args) {
String s = "acaoadaianaga";
String t = "coding";
System.out.println(stringCompare(t,s));
}
}
When you recurse, you don't return the result of the recursion. Change
if(t.charAt(0)==s.charAt(0)){
stringCompare(t.substring(1), s.substring(1));
}
else{
stringCompare(t, s.substring(1));
}
to something like
if(t.charAt(0)==s.charAt(0)){
return stringCompare(t.substring(1), s.substring(1));
}
else{
return stringCompare(t, s.substring(1));
}
The main problem of your code is in the first execution of the recursion always return false no matter what the return value of remaining execution in the recursion.
You should change your code to something like:
if(t.charAt(0)==s.charAt(0)){
return stringCompare(t.substring(1), s.substring(1));
}
else{
return stringCompare(t,s.substring(1));
}
and remove the last return false; statement.
This is because the outer recursive calls you always returns false
except
if(t.length()==0){return true;}
look at Elliott Frisch's answer.
You should use .contains. Example:
boolean istrue = t.contains(s);

Compare values of return type Boolean and Integer

I have a scenario where an object receiving from UI passes a Boolean value and this is stored in database as an Integer like TRUE=1 and FALSE=0. Now when the flag is changed for ex to FALSE i need to compare its integer value if its 0 then do nothing if 1 then change to 0 and update. The one way is below, but still there maybe a better way to do this.
class AClient {
static Boolean x;
}
class BServer {
static Integer y;
}
public class Demo {
public static void main(String[] args) {
AClient.x = Boolean.TRUE;
BServer.y = 0;
System.out.println(storedValues());
}
private static Boolean storedValues() {
if (AClient.x) {
if (BServer.y.equals(new Integer(1))) {
return true;
} else {
return false;
}
} else {
if (BServer.y.equals(new Integer(1))) {
return false;
} else {
return true;
}
}
}
}
Output: false
Your storedValues method can be reduced to:
return AClient.x.equals(BServer.y.equals(1));
If x and y don't need to ever be null, I would replace them with primitives instead of the wrapper class, then storedValues could look like this:
return BServer.y == 1 == AClient.x;
You could also change the return type of storedValues to boolean.
There's a shorter way to write it:
Instead of:
if (BServer.y.equals(new Integer(1))) {
return true;
} else {
return false;
}
write :
return (BServer.y == 1) ? true : false;

Ignore a letter in isPalindrome() method - Java

I am wondering how the following method could be modified to ignore a certain letter and have it as a wildcard when checking if a string is a palindrome...
example: "wows", in this case the method should return false but
"pat" , the 't' can be a wildcard (considered as a p) and therefore it returns true
"job" , again the b can be a wildcard and considered as a j, therefore method returns true.
this is what i have so far, i have a seperate method ignoring special characters and spaces so that does not need to be considered in this post.
private static boolean checkPalindrome2(String word) {
if(word.length() < 2) {
return true;
}
char first = word.charAt(0);
char last = word.charAt(word.length()-1);
if(first != last) {
return false;
}
else {
return checkPalindrome2(word.substring(1,word.length()-1));
}
}
this is my test class,
public class testPalindromes {
public static void main(String[] args) {
//if (Palindromes.isPalindrome("a") == true) {
// System.out.println("true");
//} else {
// System.out.println("false");
//}
// block above is the same as this
// isPalindrome already returns true or false,
// and true and false can be printed as strings
System.out.println(isPalindrome("a"));
if (Palindromes.isPalindrome("cat") == true) {
System.out.println("true");
} else {
System.out.println("false");
}
if (Palindromes.isPalindrome("w o w") == true) {
System.out.println("true");
} else {
System.out.println("false");
}
if (Palindromes.isPalindrome(" a ") == true) {
System.out.println("true");
} else {
System.out.println("false");
}
if (Palindromes.isPalindrome("mom!") == true) {
System.out.println("true");
if (Palindromes.isPalindrome2("cat")==true){
System.out.print("true");
} else {
System.out.println("false");
}
}
}
}
isPalindromes2 is a method calling the checkPalindrome2 method above, the last case in my test class (word cat) should return true because the t will be the wildcard letter (wildcard again as stated above, replaced with c making cat, cac which is a palindrome)
thanks in advance for all help / input!!!!
ps, i purposely implemented a recursive method.
Just add extra conditions to your base case:
// both first and last have to NOT be the special character
// and first has to not equal last for this to return false
if(first != special && last != special && first != last)
return false;
else
return checkPalindrome(word.substring(1,word.length()-1));

Why is this not returning true?

public void check() {
if (particle < 0) {
if (point[3].equals(point[3]) == true) {
check = true;
}
check = false;
}
}
Shouldn't point[3] be equal to itself? making it true?
Maybe you mean to say else check = false?
public void check(){
if(particle < 0){
if(point[3].equals(point[3]) == true){
check = true;
}else{
check = false;
}
}
//here it is true
}
or simply:
public void check(){
if(particle < 0){
check = point[3].equals(point[3]);
}
//here it is true
}
You must return after check = true; from the function, or use else. Else it will fall down from the if and return false always
if (...) {
check = true;
}
else {
check = false;
}
public void check(){
if(particle < 0){
if(point[3].equals(point[3]) == true){
check = true;
}else{
check = false;
}
}
}
Try this:
public boolean check() {
if (particle < 0) {
return point[3].equals(point[3]);
} else {
return false;
}
}
what about particle?
by convention point ought to be equal to itself, but you could always implement it otherwise.
but of course, the other reply is correct, this function will always end with check=false

Categories