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

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!

Edited 3 Years Ago by Dani: Formatting fixed

Hello rickste_r

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

yours,
Hjays

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 tags around your code please

Thank you.

Edited 3 Years Ago by happygeek: fixed formatting

This article has been dead for over six months. Start a new discussion instead.