aspose file tools*
The moose likes Beginning Java and the fly likes Java Function Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Java Function" Watch "Java Function" New topic
Author

Java Function

paulo cs
Ranch Hand

Joined: Dec 30, 2008
Posts: 34
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.


Paulo Correa, Desenvolvimento de Software, (55 19)8126-8418
John de Michele
Rancher

Joined: Mar 09, 2009
Posts: 600
Paulo:

So, what's your question?

John.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61221
    
  66

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.

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
paulo cs
Ranch Hand

Joined: Dec 30, 2008
Posts: 34
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
Marshal

Joined: Mar 22, 2005
Posts: 41867
    
  63
Since the problem involves string handling, I'd start by checking whether the String or StringBuilder classes have any methods that might be useful.


Ping & DNS - my free Android networking tools app
salvin francis
Ranch Hand

Joined: Jan 12, 2009
Posts: 928

You mean that a reverse string should be equal to the initial string

hint in the sentence above,


My Website: [Salvin.in] Cool your mind:[Salvin.in/painting] My Sally:[Salvin.in/sally]
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19696
    
  20

That would be the easy but less efficient way, yes. Bonus points if you avoid using StringBuilder or creating new objects while checking


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Costi Ciudatu
Ranch Hand

Joined: Oct 24, 2006
Posts: 74
[RP] Removed full solution[/RP]
salvin francis
Ranch Hand

Joined: Jan 12, 2009
Posts: 928

someone please censor that

Costi Ciudatu we usually do not handout answers out in this manner.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19696
    
  20

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

Joined: Oct 24, 2006
Posts: 74
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

Joined: Oct 27, 2005
Posts: 19696
    
  20

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
Ranch Hand

Joined: Jan 12, 2009
Posts: 928

Costi Ciudatu wrote: ...and had my code reviewed at no cost


Now, that's positive thinking...
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11314
    
  16

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
salvin francis
Ranch Hand

Joined: Jan 12, 2009
Posts: 928

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Java Function