"String class"

Reply

Join Date: Jul 2004
Posts: 2
Reputation: Hjays is an unknown quantity at this point 
Solved Threads: 0
Hjays Hjays is offline Offline
Newbie Poster

"String class"

 
0
  #1
Jul 30th, 2004
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
Reply With Quote Quick reply to this message  
Join Date: Jul 2004
Posts: 16
Reputation: rickste_r is an unknown quantity at this point 
Solved Threads: 0
rickste_r's Avatar
rickste_r rickste_r is offline Offline
Newbie Poster

Re: "String class"

 
0
  #2
Jul 30th, 2004
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!
Only lemmings should jump to conclusions! :rolleyes: Please rate me!
Reply With Quote Quick reply to this message  
Join Date: Jul 2004
Posts: 2
Reputation: Hjays is an unknown quantity at this point 
Solved Threads: 0
Hjays Hjays is offline Offline
Newbie Poster

Re: "String class"

 
0
  #3
Aug 4th, 2004
Hello rickste_r

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

yours,
Hjays
Reply With Quote Quick reply to this message  
Join Date: Jul 2003
Posts: 117
Reputation: Iron_Cross is an unknown quantity at this point 
Solved Threads: 2
Iron_Cross's Avatar
Iron_Cross Iron_Cross is offline Offline
Junior Poster

Re: "String class"

 
0
  #4
Aug 4th, 2004
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.
elitehackers.info
Today's Penny-Arcade!
Pain is weakness leaving the body!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC