Question

In: Computer Science

Can someone look into my code and tell me what do you think: Thats Palindrome; //class...

Can someone look into my code and tell me what do you think: Thats Palindrome;

//class name Palindrome

public class Palindrome {

   public static void palindromeChecker(String... str) {
       // takes string one by one
       for (String s : str) {
           // creates a stringbuilder for s
           StringBuilder sb = new StringBuilder(s);
           // reverses the sb
           sb.reverse();
           // checks if both are equal
           if (s.equals(sb.toString())) {
               System.out.println(s + " is Palindrome");
           } else {
               System.out.println(s + " is not a Palindrome");
           }

       }
      
   }
}

//Testing Palindrome
public class TestPalindrome {

   public static void main(String[] args) {

       Palindrome pal = new Palindrome();
       //Checking palindrome
       pal.palindromeChecker(" ", "a", "aa", "bb", "aba", "bab", "bob", "ab", "ba", "bba", "abb");

   }
}

Solutions

Expert Solution

The given code is correct according to logic. Only one thing is not standard is that inside main function there is no need to created object of Palindrome class.

Please find the updated code below:


class Palindrome {

public static void palindromeChecker(String... str) {
// takes string one by one
for (String s : str) {
// creates a stringbuilder for s
StringBuilder sb = new StringBuilder(s);
// reverses the sb
sb.reverse();
// checks if both are equal
if (s.equals(sb.toString())) {
System.out.println(s + " is Palindrome");
} else {
System.out.println(s + " is not a Palindrome");
}

}
}
}

//Testing Palindrome
public class TestPalindrome {
public static void main(String[] args) {
   /*here no need to create object of palindrome checker. Function can be called directly
   as it is static function*/
Palindrome.palindromeChecker(" ", "a", "aa", "bb", "aba", "bab", "bob", "ab", "ba", "bba", "abb");
}
}

output:


Related Solutions

Can someone please tell me if these calculations are correct! I'm reviewing my notes, and my...
Can someone please tell me if these calculations are correct! I'm reviewing my notes, and my professor said to always multiply the lipids by 3 and then divide by 7 to get the total amount of cals of lipids per day... I'm not completely sure why you do that? Can someone explain. Why don't you just stop at 700 cals for lipids? 1. Calculate the number of calories and grams protein for the following TPN solution: D50W in 500cc 10%...
I was wondering is someone could tell me why my code isn't compiling - Java ------------------------------------------------------------------------------------------------------------...
I was wondering is someone could tell me why my code isn't compiling - Java ------------------------------------------------------------------------------------------------------------ class Robot{ int serialNumber; boolean flies,autonomous,teleoperated; public void setCapabilities(int serialNumber, boolean flies, boolean autonomous, boolean teleoperated){ this.serialNumber = serialNumber; this.flies = flies; this.autonomous = autonomous; this.teleoperated = teleoperated; } public int getSerialNumber(){ return this.serialNumber; } public boolean canFly(){ return this.flies; } public boolean isAutonomous(){ return this.autonomous; } public boolean isTeleoperated(){ return this.teleoperated; } public String getCapabilities(){ StringBuilder str = new StringBuilder(); if(this.flies){str.append("canFly");str.append(" ");} if(this.autonomous){str.append("autonomous");str.append("...
In your own words, can you tell me What do you think are the most significant...
In your own words, can you tell me What do you think are the most significant factors responsible for changes in the meaning of race today?
Can someone tell me what is wrong with this code? Python pong game using graphics.py When...
Can someone tell me what is wrong with this code? Python pong game using graphics.py When the ball moves the paddles don't move and when the paddles move the ball doesn't move. from graphics import * import time, random def racket1_up(racket1):    racket1.move(0,20)        def racket1_down(racket1):    racket1.move(0,-20)   def racket2_up(racket2):    racket2.move(0,20)   def racket2_down(racket2):    racket2.move(0,-20)   def bounceInBox(shape, dx, dy, xLow, xHigh, yLow, yHigh):     delay = .005     for i in range(600):         shape.move(dx, dy)         center = shape.getCenter()         x = center.getX()         y = center.getY()         if x...
Hi there Can someone tell me. If someone file a case against you and you dont...
Hi there Can someone tell me. If someone file a case against you and you dont have money to fight against it. Then what person should do in that situation.? If someone file a sexual harassment case?
Okay, can someone please tell me what I am doing wrong?? I will show the code...
Okay, can someone please tell me what I am doing wrong?? I will show the code I submitted for the assignment. However, according to my instructor I did it incorrectly but I am not understanding why. I will show the instructor's comment after providing my original code for the assignment. Thank you in advance. * * * * * HourlyTest Class * * * * * import java.util.Scanner; public class HourlyTest {    public static void main(String[] args)     {        ...
can someone finish and check my code on main. cpp? Its not working for me even...
can someone finish and check my code on main. cpp? Its not working for me even though im sure my code make sense is it possible to output each function to show they work. this is supposed to be a vector class library made from allocated memory i have included templated functions in the class file to help create the rest of the functions. Thank you so much note: i did not include main.cpp because it  was empty- im hoping someone...
can someone tell me why I'm getting the error code on Eclipse IDE: Error: Main method...
can someone tell me why I'm getting the error code on Eclipse IDE: Error: Main method is not static in class StaticInitializationBlock, please define the main method as:    public static void main(String[] args) This is what I'm working on class A { static int i; static { System.out.println(1); i = 100; } } public class StaticInitializationBlock { static { System.out.println(2); } public static void main(String[] args) { System.out.println(3); System.out.println(A.i); } }
Can you tell me what is wrong with this code ? def update_char_view(phrase: str, current_view: str,...
Can you tell me what is wrong with this code ? def update_char_view(phrase: str, current_view: str, index: int, guess: str)->str: if guess in phrase: current_view = current_view.replace(current_view[index], guess) else: current_view = current_view    return current_view update_char_view("animal", "a^imal" , 1, "n") 'animal' update_char_view("animal", "a^m^l", 3, "a") 'aamal'
Can someone tell me the complications and safety for hemorrhagic stroke? this is for a pathophysiology...
Can someone tell me the complications and safety for hemorrhagic stroke? this is for a pathophysiology class
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT