• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java Function

 
paulo cs
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have the test below:

Write a Java function that receives as parameter a word and returns true if the word received as input is a palindrome, and false otherwise. Palindrome is a word that when written backwards has the
same spelling of the word written in the normal position, such as RADAR, ASA, ARARA.

 
John de Michele
Rancher
Posts: 600
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paulo:

So, what's your question?

John.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65216
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK. So where's your code and what problems are you having with it?

Also, please be sure to take the time to compose descriptive subjects for your posts; read this for more information.

A title such as "Java function" isn't all that descriptive. Please go back and change your post to add a more meaningful subject by clicking the button on your post.
 
paulo cs
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My function return TRUE or FALSE, ex:
I have a word: RADAR. If i write as opposed the word, the result is RADAR, so my function return TRUE.
I have other word: CASA. If I write as opposed the word, the result is ASAC, so my function return FALSE.

I do not know the opposed.
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since the problem involves string handling, I'd start by checking whether the String or StringBuilder classes have any methods that might be useful.
 
salvin francis
Bartender
Posts: 1308
10
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You mean that a reverse string should be equal to the initial string

hint in the sentence above,

 
Rob Spoor
Sheriff
Pie
Posts: 20661
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That would be the easy but less efficient way, yes. Bonus points if you avoid using StringBuilder or creating new objects while checking
 
Costi Ciudatu
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[RP] Removed full solution[/RP]
 
salvin francis
Bartender
Posts: 1308
10
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
someone please censor that

Costi Ciudatu we usually do not handout answers out in this manner.
 
Rob Spoor
Sheriff
Pie
Posts: 20661
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Salvin is right. With one exception (you used getBytes() where you should have used toCharArray() or even easier a combination of length() and charAt(int)) it would have worked perfectly though.
 
Costi Ciudatu
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm sorry !
I was just thinking: why tease the poor guy with maieutics when he's looking for something to paste .
Anyway, I got the idea now (and had my code reviewed at no cost ). Won't happen again.
 
Rob Spoor
Sheriff
Pie
Posts: 20661
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Costi Ciudatu wrote:I was just thinking: why tease the poor guy with maieutics when he's looking for something to paste .

Because this appears to be a homework assignment and if we hand out solutions he won't learn anything. In the end Paulo needs to be able to write code for these problems himself or he will fail his class.
 
salvin francis
Bartender
Posts: 1308
10
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Costi Ciudatu wrote: ...and had my code reviewed at no cost


Now, that's positive thinking...
 
fred rosenberger
lowercase baba
Bartender
Posts: 12196
35
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is how I would start this assignemnt:



Now, try and do the next step on your own.

I am 100% serious when I say this is how I start EVERY program I ever write. The best way to code is to write as little as possible before compiling and testing. This is the minimum code you can write to start and see it do something.

Next I'd read the problem and try and break down everything. The first thing is says is "Write a Java function..." So, see if you can write a java function. it doesn't have to take any parameters, it doesn't have to return anything, it doesn't have to DO anything...just see if you can write one and get your code to compile and run.

Once you have THAT done, try the next part...have it take a 'word' (what kind of object would you use for that???) and do nothing else but print it out inside the function...

at each and every step, write a line or two, compile, and test the hell out of it. Only when you are sure the previous step is right should you move on to adding something new.
 
salvin francis
Bartender
Posts: 1308
10
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:I am 100% serious when I say this is how I start EVERY program I ever write.

Hats off to you fred
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic