943,912 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 4245
  • Java RSS
Jul 30th, 2004
0

"String class"

Expand Post »
write and test a class that can be used to create and object whose instance string variable can be initialised to a string constant such as:
"I fined that the harder I work the more luck I seem to have"

the class need to provide the following methods

A method to diplay an object's string.
A method that given a character, it returns the number of times that characte occurs in the string within the object.
eg: the character 'e' occurs 7 times within the sample string.


Please help me with this
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Hjays is offline Offline
2 posts
since Jul 2004
Jul 30th, 2004
0

Re: "String class"

Ok , you want something like this:

Class StringProg has the 2 methods you require:
---------------------------------------------------------------

public class StringProg
{

String s;
int count = 0;

//constructor
public StringProg(String sIn)
{
s = sIn;
}

//method to diplay string
public void displayString()
{
System.out.println ("\nYour String: " + s);
}

//method to find character
public void findChar(char letter)
{

for (int i = 0; i<s.length(); i++)
{
if(s.charAt(i) == letter)
{
count ++;
}
}
System.out.println ("\nYour character " + "\"" + letter
+ "\"" + " appeared " + count + " time(s)");
}

}
------------------------------------------------------
The second class tests the methods from StringProg:

public static void main(String [] args)
{
//the requested letter to find
char letter = 'e';

//create a StringProg object
StringProg t = new StringProg("THE STRING GOES HERE");

//display the string
t.displayString();
//search the string for a letter
t.findChar(letter);


}

---------------------------------------------------------
That should help ya. rickste_r!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
rickste_r is offline Offline
16 posts
since Jul 2004
Aug 4th, 2004
0

Re: "String class"

Hello rickste_r

Thankyou very much for the help. It was very kind of you.

yours,
Hjays
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Hjays is offline Offline
2 posts
since Jul 2004
Aug 4th, 2004
0

Re: "String class"

rickste_r,
Don't give out homework for free! They're never going to learn anything if you do it for them. There is nothing wrong with helping them, but DON'T do it for them!
And also, use the [*code] and [/code] tags around your code please (be sure to take off the *).

Thank you.
Reputation Points: 46
Solved Threads: 2
Junior Poster
Iron_Cross is offline Offline
117 posts
since Jul 2003

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: string translation
Next Thread in Java Forum Timeline: Error message when running simple Java pgm





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC