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 tell me how to fix warning msg in my code of C ++? I...
Can someone tell me how to fix warning msg in my code of C ++? I got run-time error for this question please help me asap! Errors are: In function 'void bfs(int, int)': warning: comparison between signed and unsigned integer expressions [-Wsign-compare] for(int j = 0; j < adj[pppp].size(); j++){ ^ In function 'int main()': warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result] scanf("%d %d %d %d %d", &a, &q, &c, &N, &m); ^...
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 take a look and tell me what I have wrong here. VS 2019 .net...
Can someone take a look and tell me what I have wrong here. VS 2019 .net framework console app c## using System; using System.Collections.Generic; using System.Linq; using System.Runtime.CompilerServices; using System.Text; using System.Threading.Tasks; namespace dropbox10 { class MainClass { public static void Main(string[] args) { //create a list of employees List<Employee> allEmployees = new List<Employee>(); //create two full time employees FullTimeEmployee fe1 = new FullTimeEmployee("111", "Alice", 67888.00m); FullTimeEmployee fe2 = new FullTimeEmployee("222", "Bob", 67555.00m); //create two part time employees PartTimeEmployee pe1...
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 you please tell me why my code isn't working? It won't calculate the values I...
Can you please tell me why my code isn't working? It won't calculate the values I have using my input file. /******************************************************************************* AUTHOR SECTION ENGR 200.07 DATE: 10/23/2020 PROGRAM: ******************************************************************************** PROGRAM DESCRIPTION This program takes a pre-made .txt file’s input values, and calculates the kinetic energy wind farms produce from moving air into electrical energy. Using 3 different formulas this program calculates the available power in the wind, the maximum available power that can be produced, and the amount of...
Can you please tell me if this code needs to be fixed, if it does can...
Can you please tell me if this code needs to be fixed, if it does can you please post the fixed code below please and thank you /** * Models a Whole Life Policy. * * @author Tina Comston * @version Fall 2019 */ public class WholeLifePolicy { // instance variables    private double faceValue; // your code here - code the remaining instance field // constants /** * surrender rate. */ public static final double SURRENDER_RATE = .65; /**...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT