473,324 Members | 2,501 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

Palindromes help!!

3
hello any guidance on this would be greatly appreciated( im very new to programming the more comments the better)

the objective is to create a class that defines a single String data member. The class should define a isPalindrome method which returns true when the String is a Palindrome and false otherwise. The class's toString method should return the String data member along with the result of the isPalindrome method is a readable format.

then you need a client application and its responsible for retrieving a word or sentence from the user, passing the input String to the class and calling the toString method. The client should allow the user to test any number of strings.

Heres what i got so far:

import java.lang.String;
import java.lang.Object;


public class Palindrome
{
private String palin;

public Palindrome()
{
palin = " ";
}

public Palindrome(String newPalin, String initPalin)
{
palin = initPalin.toUpperCase();
palin = newPalin;

}

public String getPalin()
{
return palin;
}

public void setPalin(String newPalin)
{
palin = newPalin;

}

public static boolean isPalindrome(String palin)
{
int left = 0;
int right = palin.length() -1;
while (left < right)
{
if (palin.charAt(left) != palin.charAt(right))
{
return false;
}
left++;
right--;
}
return true;
}

public String toString()
{
return isPalindrome();
}

}


Thats ^^ the standard class ( which is incomplete) and as far as my client goes this is where im confused and cant figure out how to do any of it

import java.util.Scanner;
import java.lang.String;

public class palinClient
{
public static void main(String[] args)
{
String entry;

Scanner scan = new Scanner(System.in);
Palindrome palin1 = new Palindrome();

System.out.println("Please enter your expression");
entry = scan.next();

palin1.setPalin(entry);

}
}

Ty for the Help!!
Mar 12 '07 #1
6 2530
Ganon11
3,652 Expert 2GB
So, is the test program you wrote working? Is it making the palindrome object correctly? Try printing out the object's toString() result to check. Then, work on a whole() loop that will continue making Palindrome objects until the user wants to stop.
Mar 12 '07 #2
Tmoney
3
So, is the test program you wrote working? Is it making the palindrome object correctly? Try printing out the object's toString() result to check. Then, work on a whole() loop that will continue making Palindrome objects until the user wants to stop.

Well, currently I'm having trouble compiling the program for instance here are some of my errors:



Palindrome.java:50: isPalindrome(java.lang.String) in Palindrome cannot be applied to ()
return isPalindrome();
^
1 error

thats ^ is in the Palindrome class at the bottom i dont understand why it wouldnt compile correctly
Mar 12 '07 #3
Ganon11
3,652 Expert 2GB
You have written the isPalindrome method to take a String argument, but in your .toString() method, you try to call isPalindrome without arguments.

The method should not accept an argument, but instead try to find out if palin (the instance variable) is a palindrome.
Mar 12 '07 #4
Tmoney
3
You have written the isPalindrome method to take a String argument, but in your .toString() method, you try to call isPalindrome without arguments.

The method should not accept an argument, but instead try to find out if palin (the instance variable) is a palindrome.

Okay things are starting to look good still having a little trouble getting it all finished this is what i have now

import java.lang.String;
import java.lang.Object;


public class Palindrome
{
private String palin;

public Palindrome()
{
palin = " ";
}

public Palindrome(String newPalin, String initPalin)
{
palin = initPalin.toUpperCase();
palin = newPalin;

}

public String getPalin()
{
return palin;
}

public void setPalin(String newPalin)
{
palin = newPalin;

}

public boolean isPalindrome()
{
int left = 0;
int right = palin.length() -1;
while (left < right)
{
if (palin.charAt(left) != palin.charAt(right))
{
return false;
}
left++;
right--;
}
return true;
}

public String toString()
{
return "Palindrome " + isPalindrome();
}

}


then the client class

import java.util.Scanner;
import java.lang.String;

public class palinClient
{
public static void main(String[] args)
{
String palin;
boolean isP = false;

Scanner scan;
scan = new Scanner(System.in);

System.out.println("Enter text and press [enter]:");
String input = scan.nextLine();
Palindrome text = new Palindrome(palin);

isP = text.isPalindrome();

if(isP)
System.out.println(input + " is a palindrome");
else System.out.println(input + " is not a palindrome");
}

}


But now my biggest problem is that it reports every word as being a Palindrome, whether its right or wrong
Mar 13 '07 #5
Ganon11
3,652 Expert 2GB
Was that palindrome solution given to you, or did you have to write it yourself? It looks correct, but I usually use a different type of function to check palindromes.

Also, when posting your code, please be sure to use the [code] tags to make it easier to read. Just select the coded text and hit the # symbol in the editing box when making your reply.
Mar 13 '07 #6
r035198x
13,262 8TB
Okay things are starting to look good still having a little trouble getting it all finished this is what i have now

import java.lang.String;
import java.lang.Object;


public class Palindrome
{
private String palin;

public Palindrome()
{
palin = " ";
}

public Palindrome(String newPalin, String initPalin)
{
palin = initPalin.toUpperCase();
palin = newPalin;

}

public String getPalin()
{
return palin;
}

public void setPalin(String newPalin)
{
palin = newPalin;

}

public boolean isPalindrome()
{
int left = 0;
int right = palin.length() -1;
while (left < right)
{
if (palin.charAt(left) != palin.charAt(right))
{
return false;
}
left++;
right--;
}
return true;
}

public String toString()
{
return "Palindrome " + isPalindrome();
}

}


then the client class

import java.util.Scanner;
import java.lang.String;

public class palinClient
{
public static void main(String[] args)
{
String palin;
boolean isP = false;

Scanner scan;
scan = new Scanner(System.in);

System.out.println("Enter text and press [enter]:");
String input = scan.nextLine();
Palindrome text = new Palindrome(palin);

isP = text.isPalindrome();

if(isP)
System.out.println(input + " is a palindrome");
else System.out.println(input + " is not a palindrome");
}

}


But now my biggest problem is that it reports every word as being a Palindrome, whether its right or wrong
1.)Use code tags when posting code
2.)You don't have to import classes in the java.lang package
3.)You have two constructors in your Palindrome class :

Expand|Select|Wrap|Line Numbers
  1.  public Palindrome() 
  2.     {
  3.         palin = " ";
  4.     }
  5.  
takes no arguments and

Expand|Select|Wrap|Line Numbers
  1.  public Palindrome(String newPalin, String initPalin) 
  2.     {
  3.         palin = initPalin.toUpperCase();
  4.         palin = newPalin;
  5.  
  6.     }
  7.  
takes two arguments but in your test class you have the line
Expand|Select|Wrap|Line Numbers
  1.  Palindrome text = new Palindrome(palin); 
which call a one argument constructor. This of course will not compile so show us the code that you are using that is compiling and give the incorrect results.
Mar 13 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

21
by: Dave | last post by:
After following Microsofts admonition to reformat my system before doing a final compilation of my app I got many warnings/errors upon compiling an rtf file created in word. I used the Help...
4
by: Lorin Leone | last post by:
Can anyone help me modify the program so that it recognizes strings like "Anna" as palindromes. To make the program "case-insensitive." using the built-in C++ function "toupper". and so that it...
6
by: wukexin | last post by:
Help me, good men. I find mang books that introduce bit "mang header files",they talk too bit,in fact it is my too fool, I don't learn it, I have do a test program, but I have no correct doing...
3
by: Colin J. Williams | last post by:
Python advertises some basic service: C:\Python24>python Python 2.4.1 (#65, Mar 30 2005, 09:13:57) on win32 Type "help", "copyright", "credits" or "license" for more information. >>> With...
1
by: IsmaZ | last post by:
hi!!! i just started studyin programming so i m having small problems.... can any1 tell me how 2 write a program in vc++ to identify palindromes...i cant get it to check digits other than ffirst...
4
by: elsa | last post by:
hi everybody..i need to define two functions that test for word Palindromes.. 1- void bool isPalindromeUsingIteration(string ) that takes a string as a paremeter and tests if it is a Palindrome...
4
by: kryptor | last post by:
Hi there, Just need some help with this small program. Using recursion I'm trying to find out if a word (in this case a test word) is a palindrome or not. If it is it should return a 1 to...
0
by: 249740 | last post by:
A palindrome is a number/string that reads the same backwards and forward. For example: BOB, 3113 etc. Write a program that reads in an input value or string and outputs the number of possible...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.